╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
691 const inner = self.diffInner(); 692 const keys = inner.children.keys(); 693 if (keys.len == 0) return; 694 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 695 const target = @as(isize, @intCast(cur)) + delta; 696 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 697 698 if (self.session.is_terminal) { 699 if (in_range and self.hunkVisible(@intCast(target))) { 700 root_focus.setFocus(keys[@intCast(target)]); 701 return; 702 } 703 704 const sc = self.diffScroll(); 705 sc.y += delta * 5; // magnify because it's a line count 706 self.diffScroll().clampToContent(); 707 if (in_range and self.hunkVisible(@intCast(target))) { 708 root_focus.setFocus(keys[@intCast(target)]); 709 } 710 } else { 711 if (in_range) { 712 root_focus.setFocus(keys[@intCast(target)]); 713 } 714 } 715 } 716 717 // page a fixed number of lines, then focus the leading visible hunk 718 // (bottom-most when paging down, top-most when paging up). in the 719 // terminal, `delta` is a line count, and on the web it's a hunk count. 720 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 721 if (self.session.is_terminal) { 722 const sc = self.diffScroll(); 723 sc.y += delta * 5; // magnify because it's a line count 724 self.diffScroll().clampToContent(); 725 try self.focusVisible(root_focus, delta > 0); 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.diffScroll().clampToContent(); 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 // enter the diff pane. the host arrives here on right-arrow or Enter from the 783 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 784 // case where the pane isn't laid out yet (it gets selected, then focused after 785 // the next build, landing on its remembered hunk). 786 fn focusDiff(self: *View, root_focus: *Focus) !void { 787 if (self.diffInner().children.count() == 0) return; 788 root_focus.setFocus(self.diffOuter().getFocus().id); 789 } 790 791 // return to the list. 792 fn focusList(self: *View, root_focus: *Focus) !void { 793 root_focus.setFocus(self.listScroll().getFocus().id); 794 } 795 796 pub fn clearGrid(self: *View) void { 797 self.box.clearGrid(); 798 } 799 800 pub fn getGrid(self: View) ?Grid { 801 return self.box.getGrid(); 802 } 803 804 pub fn getFocus(self: *View) *Focus { 805 return self.box.getFocus(); 806 } 807 808 // for the parent's "scroll up at the top jumps to the header" check. in the 809 // diff pane that means the first hunk is focused and the diff can't scroll up 810 // any further; otherwise up-arrow stays in the pane (scrolling or moving 811 // between hunks). when the list holds focus, report its selection directly. 812 pub fn getSelectedIndex(self: *View) ?usize { 813 if (self.diffActive()) { 814 const inner = self.diffInner(); 815 const cid = inner.getFocus().child_id orelse return 1; 816 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 817 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 818 } 819 const lb = self.listBox(); 820 const cid = lb.getFocus().child_id orelse return null; 821 return lb.children.getIndex(cid); 822 } 823 }; 824 825 // the "a:" navigation link for the commits page walking from commit `oid` within 826 // `identity`, showing the selected commit's first `after` hunks (0 = default). 827 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, after: usize) ![]const u8 { 828 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, after) orelse return error.RouteTooLong; 829 const url = try route.urlAlloc(page_arena); 830 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 831 } 832 833 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 834 // directory, within `identity` ("owner/name"). 835 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 836 const route = ui.RoutablePage.repoFilesRoute(identity, .object, oid, "", 0) orelse return error.RouteTooLong; 837 const url = try route.urlAlloc(page_arena); 838 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 839 } 840 841 // the in-page "ai:" anchor for selecting `oid` in `identity`'s commit list. it 842 // points at the same route as commitsLink but the "ai:" prefix keeps wasm clicks 843 // in-page (crossPageLink ignores it); the href is only followed with js off. 844 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 845 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, 0) orelse return error.RouteTooLong; 846 const url = try route.urlAlloc(page_arena); 847 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 848 } 849