╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
726 } else { 727 const inner = self.diffInner(); 728 const keys = inner.children.keys(); 729 if (keys.len == 0) return; 730 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 731 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 732 root_focus.setFocus(keys[target]); 733 } 734 } 735 736 // jump to the first or last hunk. on the web the browser scrolls to the 737 // focused hunk; on the terminal pin the scroll to the top/bottom too. 738 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 739 const inner = self.diffInner(); 740 const keys = inner.children.keys(); 741 if (keys.len == 0) return; 742 if (self.session.is_terminal) { 743 const sc = self.diffScroll(); 744 sc.y = if (to_end) std.math.maxInt(isize) else 0; 745 self.clampDiffScroll(); 746 } 747 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 748 } 749 750 // whether hunk `index` is at least partly within the diff viewport, per the 751 // last build's layout rects (content space) and the current scroll offset. 752 fn hunkVisible(self: *View, index: usize) bool { 753 const inner = self.diffInner(); 754 const sc = self.diffScroll(); 755 const vp = sc.grid orelse return false; 756 const r = inner.children.values()[index].rect orelse return false; 757 const top = sc.y; 758 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 759 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 760 } 761 762 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 763 // the bottom-most visible (for downward motion), else the top-most. 764 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 765 const inner = self.diffInner(); 766 const sc = self.diffScroll(); 767 const vp = sc.grid orelse return; 768 const top = sc.y; 769 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 770 var chosen: ?usize = null; 771 for (inner.children.keys(), inner.children.values()) |id, *child| { 772 const r = child.rect orelse continue; // content-space layout rect 773 const r_top = r.y; 774 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 775 if (r_bot <= top or r_top >= bottom) continue; // not visible 776 chosen = id; 777 if (!prefer_last) break; // first visible 778 } 779 if (chosen) |id| root_focus.setFocus(id); 780 } 781 782 // keep the diff scroll within its content, using the last build's grids. the 783 // scroll bar's reserved column/row isn't part of the content viewport, so 784 // exclude it (as scrollToRect does) or the last column/row stays unreachable. 785 fn clampDiffScroll(self: *View) void { 786 const sc = self.diffScroll(); 787 const vp = sc.grid orelse return; 788 const content = sc.child.box.grid orelse return; 789 const view_w = vp.size.width - sc.bar_w; 790 const view_h = vp.size.height - sc.bar_h; 791 const max_y: isize = if (content.size.height > view_h) @intCast(content.size.height - view_h) else 0; 792 const max_x: isize = if (content.size.width > view_w) @intCast(content.size.width - view_w) else 0; 793 sc.y = std.math.clamp(sc.y, 0, max_y); 794 sc.x = std.math.clamp(sc.x, 0, max_x); 795 } 796 797 // enter the diff pane. the host arrives here on right-arrow or Enter from the 798 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 799 // case where the pane isn't laid out yet (it gets selected, then focused after 800 // the next build, landing on its remembered hunk). 801 fn focusDiff(self: *View, root_focus: *Focus) !void { 802 if (self.diffInner().children.count() == 0) return; 803 root_focus.setFocus(self.diffOuter().getFocus().id); 804 } 805 806 // return to the list. 807 fn focusList(self: *View, root_focus: *Focus) !void { 808 root_focus.setFocus(self.listScroll().getFocus().id); 809 } 810 811 fn moveSelection(self: *View, root_focus: *Focus, delta: isize) !void { 812 const lb = self.listBox(); 813 const keys = lb.children.keys(); 814 if (keys.len == 0) return; 815 const cur_id = lb.getFocus().child_id orelse return; 816 const cur: isize = @intCast(lb.children.getIndex(cur_id) orelse return); 817 const last: isize = @intCast(keys.len - 1); 818 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 819 if (next == @as(usize, @intCast(cur))) return; 820 root_focus.setFocus(keys[next]); 821 if (lb.children.values()[next].rect) |rect| self.listScroll().scrollToRect(rect); 822 } 823 824 pub fn clearGrid(self: *View) void { 825 self.box.clearGrid(); 826 } 827 828 pub fn getGrid(self: View) ?Grid { 829 return self.box.getGrid(); 830 } 831 832 pub fn getFocus(self: *View) *Focus { 833 return self.box.getFocus(); 834 } 835 836 // for the parent's "scroll up at the top jumps to the header" check. in the 837 // diff pane that means the first hunk is focused and the diff can't scroll up 838 // any further; otherwise up-arrow stays in the pane (scrolling or moving 839 // between hunks). when the list holds focus, report its selection directly. 840 pub fn getSelectedIndex(self: *View) ?usize { 841 if (self.diffActive()) { 842 const inner = self.diffInner(); 843 const cid = inner.getFocus().child_id orelse return 1; 844 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 845 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 846 } 847 const lb = self.listBox(); 848 const cid = lb.getFocus().child_id orelse return null; 849 return lb.children.getIndex(cid); 850 } 851 }; 852 853 // the "a:" navigation link for the commits page walking from commit `oid` within 854 // `identity`, showing the selected commit's first `after` hunks (0 = default). 855 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, after: usize) ![]const u8 { 856 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, after) orelse return error.RouteTooLong; 857 const url = try route.urlAlloc(page_arena); 858 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 859 } 860 861 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 862 // directory, within `identity` ("owner/name"). 863 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 864 const route = ui.RoutablePage.repoFilesRoute(identity, .object, oid, "", 0) orelse return error.RouteTooLong; 865 const url = try route.urlAlloc(page_arena); 866 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 867 } 868 869 // the in-page "ai:" anchor for selecting `oid` in `identity`'s commit list. it 870 // points at the same route as commitsLink but the "ai:" prefix keeps wasm clicks 871 // in-page (crossPageLink ignores it); the href is only followed with js off. 872 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 873 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, 0) orelse return error.RouteTooLong; 874 const url = try route.urlAlloc(page_arena); 875 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 876 } 877