╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
631 if (sc.x > 0) { 632 sc.x -= 1; 633 self.diffScroll().clampToContent(); 634 } else try self.focusList(root_focus); 635 }, 636 .arrow_right => { 637 sc.x += 1; 638 self.diffScroll().clampToContent(); 639 }, 640 .arrow_up => try self.moveDiff(root_focus, -1), 641 .arrow_down => try self.moveDiff(root_focus, 1), 642 .page_up => try self.pageDiff(root_focus, -10), 643 .page_down => try self.pageDiff(root_focus, 10), 644 .home => try self.jumpDiff(root_focus, false), 645 .end => try self.jumpDiff(root_focus, true), 646 // Enter / a click on a "previous"/"next" row follow its "a:" link in 647 // the host (it reloads the adjacent window), so they don't reach here. 648 .mouse => |mouse| switch (mouse.action) { 649 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 650 else => {}, 651 }, 652 else => {}, 653 } 654 } 655 656 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 657 // line count unless there is a new hunk visible (in which case it is a 658 // hunk count). on the web `delta` is always a hunk count. 659 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) !void { 660 const inner = self.diffInner(); 661 const keys = inner.children.keys(); 662 if (keys.len == 0) return; 663 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 664 const target = @as(isize, @intCast(cur)) + delta; 665 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 666 667 if (self.session.is_terminal) { 668 if (in_range and self.hunkVisible(@intCast(target))) { 669 root_focus.setFocus(keys[@intCast(target)]); 670 return; 671 } 672 673 const sc = self.diffScroll(); 674 sc.y += delta * 5; // magnify because it's a line count 675 self.diffScroll().clampToContent(); 676 if (in_range and self.hunkVisible(@intCast(target))) { 677 root_focus.setFocus(keys[@intCast(target)]); 678 } 679 } else { 680 if (in_range) { 681 root_focus.setFocus(keys[@intCast(target)]); 682 } 683 } 684 } 685 686 // page a fixed number of lines, then focus the leading visible hunk 687 // (bottom-most when paging down, top-most when paging up). in the 688 // terminal, `delta` is a line count, and on the web it's a hunk count. 689 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 690 if (self.session.is_terminal) { 691 const sc = self.diffScroll(); 692 sc.y += delta * 5; // magnify because it's a line count 693 self.diffScroll().clampToContent(); 694 try self.focusVisible(root_focus, delta > 0); 695 } else { 696 const inner = self.diffInner(); 697 const keys = inner.children.keys(); 698 if (keys.len == 0) return; 699 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 700 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 701 root_focus.setFocus(keys[target]); 702 } 703 } 704 705 // jump to the first or last hunk. on the web the browser scrolls to the 706 // focused hunk; on the terminal pin the scroll to the top/bottom too. 707 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 708 const inner = self.diffInner(); 709 const keys = inner.children.keys(); 710 if (keys.len == 0) return; 711 if (self.session.is_terminal) { 712 const sc = self.diffScroll(); 713 sc.y = if (to_end) std.math.maxInt(isize) else 0; 714 self.diffScroll().clampToContent(); 715 } 716 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 717 } 718 719 // whether hunk `index` is at least partly within the diff viewport, per the 720 // last build's layout rects (content space) and the current scroll offset. 721 fn hunkVisible(self: *View, index: usize) bool { 722 const inner = self.diffInner(); 723 const sc = self.diffScroll(); 724 const vp = sc.grid orelse return false; 725 const r = inner.children.values()[index].rect orelse return false; 726 const top = sc.y; 727 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 728 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 729 } 730 731 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 732 // the bottom-most visible (for downward motion), else the top-most. 733 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 734 const inner = self.diffInner(); 735 const sc = self.diffScroll(); 736 const vp = sc.grid orelse return; 737 const top = sc.y; 738 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 739 var chosen: ?usize = null; 740 for (inner.children.keys(), inner.children.values()) |id, *child| { 741 const r = child.rect orelse continue; // content-space layout rect 742 const r_top = r.y; 743 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 744 if (r_bot <= top or r_top >= bottom) continue; // not visible 745 chosen = id; 746 if (!prefer_last) break; // first visible 747 } 748 if (chosen) |id| root_focus.setFocus(id); 749 } 750 751 // enter the diff pane. the host arrives here on right-arrow or Enter from the 752 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 753 // case where the pane isn't laid out yet (it gets selected, then focused after 754 // the next build, landing on its remembered hunk). 755 fn focusDiff(self: *View, root_focus: *Focus) !void { 756 if (self.diffInner().children.count() == 0) return; 757 root_focus.setFocus(self.diffOuter().getFocus().id); 758 } 759 760 // return to the list. 761 fn focusList(self: *View, root_focus: *Focus) !void { 762 root_focus.setFocus(self.listScroll().getFocus().id); 763 } 764 765 pub fn clearGrid(self: *View) void { 766 self.box.clearGrid(); 767 } 768 769 pub fn getGrid(self: View) ?Grid { 770 return self.box.getGrid(); 771 } 772 773 pub fn getFocus(self: *View) *Focus { 774 return self.box.getFocus(); 775 } 776 777 // for the parent's "scroll up at the top jumps to the header" check. in the 778 // diff pane that means the first hunk is focused and the diff can't scroll up 779 // any further; otherwise up-arrow stays in the pane (scrolling or moving 780 // between hunks). when the list holds focus, report its selection directly. 781 pub fn getSelectedIndex(self: *View) ?usize { 782 if (self.diffActive()) { 783 const inner = self.diffInner(); 784 const cid = inner.getFocus().child_id orelse return 1; 785 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 786 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 787 } 788 const lb = self.listBox(); 789 const cid = lb.getFocus().child_id orelse return null; 790 return lb.children.getIndex(cid); 791 } 792 }; 793 794 // the "a:" navigation link for the commits page walking from commit `oid` within 795 // `identity`, showing the selected commit's first `after` hunks (0 = default). 796 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, after: usize) ![]const u8 { 797 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, after) orelse return error.RouteTooLong; 798 const url = try route.urlAlloc(page_arena); 799 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 800 } 801 802 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 803 // directory, within `identity` ("owner/name"). 804 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 805 const route = ui.RoutablePage.repoFilesRoute(identity, .object, oid, "", 0) orelse return error.RouteTooLong; 806 const url = try route.urlAlloc(page_arena); 807 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 808 } 809 810 // the in-page "ai:" anchor for selecting `oid` in `identity`'s commit list. it 811 // points at the same route as commitsLink but the "ai:" prefix keeps wasm clicks 812 // in-page (crossPageLink ignores it); the href is only followed with js off. 813 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 814 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, 0) orelse return error.RouteTooLong; 815 const url = try route.urlAlloc(page_arena); 816 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 817 } 818