╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
645 (if (both_panes_fit) w - list_max_width else w) 646 else 647 diff_min_width; 648 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_width, .height = null }; 649 650 // the message is the pane's only wrapping row, and wrapping needs a 651 // bounded width, which the pane's scroll doesn't grant (it scrolls 652 // horizontally too). cap it to the pane, leaving room for the border 653 // and the scrollbar column. re-capped each build so it tracks resizes. 654 for (self.diffInner().children.values()) |*child| switch (child.widget) { 655 .text_box => |text_box| if (text_box.options.wrap_kind == .word) { 656 child.max_size = .{ .width = diff_width -| 3, .height = null }; 657 }, 658 else => {}, 659 }; 660 661 // the web bounds the layout to the browser viewport like the terminal; 662 // each Scroll's web-native mode hands its full content to a real 663 // scrollable element, so we no longer build unbounded here. 664 // 665 // crossing panes only re-selects the content box's child (focusDiff / 666 // focusList); when the window is too narrow to show both, the pane that 667 // held focus is dropped here. the framework recovers focus after the 668 // top-level build by re-deriving it down the selected-child chain, so 669 // there's nothing to fix up afterward. 670 try self.box.build(allocator, constraint, root_focus); 671 } 672 673 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 674 const sel = self.selectedCommitIndex() orelse return; 675 if (self.diffed_index) |d| if (d == sel) return; 676 try self.populateDiff(allocator, sel); 677 self.diffed_index = sel; 678 } 679 680 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 681 const commit = self.data.commits[sel]; 682 const inner = self.diffInner(); 683 684 for (inner.children.values()) |*child| child.widget.deinit(allocator); 685 inner.children.clearAndFree(allocator); 686 inner.getFocus().child_id = null; 687 688 switch (self.paneContent(sel)) { 689 .message => { 690 try self.addNavLink(allocator, inner, "← back to diff", commit.oid, 0, ""); 691 try self.addMessageBox(allocator, inner, commit, .whole); 692 }, 693 .diff => |d| { 694 if (d.path.len != 0) { 695 // the file's path, then a row returning to this commit's unfiltered diff. 696 var label = try wgt.TextBox(ui.Widget).init(allocator, d.path, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 697 errdefer label.deinit(allocator); 698 label.getFocus().focusable = true; 699 try inner.children.put(allocator, label.getFocus().id, .{ .widget = .{ .text_box = label }, .rect = null, .min_size = null }); 700 try self.addNavLink(allocator, inner, "← all files", commit.oid, 0, ""); 701 } else { 702 try self.addMessageBox(allocator, inner, commit, .preview); 703 try self.addViewFilesLink(allocator, inner, commit.oid); 704 } 705 706 // a "previous" row and a "next" row at the bottom reload the page 707 // on the adjacent diff window. 708 if (commit.has_prev) try self.addNavLink(allocator, inner, "← previous", commit.oid, commit.window_start -| diff_page, d.path); 709 for (commit.hunks) |hunk| { 710 if (d.path.len == 0) if (hunk.path) |hunk_path| try self.addPathBox(allocator, inner, hunk_path, commit.oid); 711 try self.addHunkBox(allocator, inner, hunk.lines); 712 } 713 if (commit.has_more) try self.addNavLink(allocator, inner, "next →", commit.oid, commit.window_start + diff_page, d.path); 714 }, 715 } 716 717 // point the pane at its first row so focus recovery can land here. 718 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 719 720 // reset the scroll to the top for the newly-shown commit: directly on the 721 // terminal (the wasm offset), and via a version bump on the web (so the 722 // renderer's scroll id changes and JS drops the preserved position). 723 const sc = self.diffScroll(); 724 sc.x = 0; 725 sc.y = 0; 726 sc.getFocus().version +%= 1; 727 } 728 729 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 730 _ = allocator; 731 if (self.diffActive()) { 732 try self.diffInput(key, root_focus); 733 } else { 734 try self.listInput(key, root_focus); 735 } 736 } 737 738 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 739 // up/down (and the scroll wheel) move the selection a row; page up/down 740 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 741 // on the "next" row become navigation in the host before reaching here. 742 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 743 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 744 return; 745 } 746 switch (key) { 747 .enter => if (self.selectedCommitIndex() != null) 748 try self.focusDiff(root_focus) 749 else if (self.data.next_start) |next| { 750 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, next, 0, "")) |route| 751 try self.session.navigate(route); 752 }, 753 .arrow_right => try self.focusDiff(root_focus), 754 else => {}, 755 } 756 } 757 758 fn diffInput(self: *View, key: Key, root_focus: *Focus) !void { 759 const sc = self.diffScroll(); 760 switch (key) { 761 // left scrolls horizontally, then leaves for the list once flush left. 762 .arrow_left => { 763 if (sc.x > 0) { 764 sc.x -= 1; 765 self.diffScroll().clampToContent(); 766 } else try self.focusList(root_focus); 767 }, 768 .arrow_right => { 769 sc.x += 1; 770 self.diffScroll().clampToContent(); 771 }, 772 .arrow_up => try self.moveDiff(root_focus, -1), 773 .arrow_down => try self.moveDiff(root_focus, 1), 774 .page_up => try self.pageDiff(root_focus, -10), 775 .page_down => try self.pageDiff(root_focus, 10), 776 .home => try self.jumpDiff(root_focus, false), 777 .end => try self.jumpDiff(root_focus, true), 778 // Enter / a click on a "previous"/"next" row follow its "a:" link in 779 // the host (it reloads the adjacent window), so they don't reach here. 780 .mouse => |mouse| switch (mouse.action) { 781 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 782 else => {}, 783 }, 784 else => {}, 785 } 786 } 787 788 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 789 // line count unless there is a new hunk visible (in which case it is a 790 // hunk count). on the web `delta` is always a hunk count. 791 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) !void { 792 const inner = self.diffInner(); 793 const keys = inner.children.keys(); 794 if (keys.len == 0) return; 795 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 796 const target = @as(isize, @intCast(cur)) + delta; 797 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 798 799 if (self.session.is_terminal) { 800 if (in_range and self.hunkVisible(@intCast(target))) { 801 root_focus.setFocus(keys[@intCast(target)]); 802 return; 803 } 804 805 const sc = self.diffScroll(); 806 sc.y += delta * 5; // magnify because it's a line count 807 self.diffScroll().clampToContent(); 808 if (in_range and self.hunkVisible(@intCast(target))) { 809 root_focus.setFocus(keys[@intCast(target)]); 810 } 811 } else { 812 if (in_range) { 813 root_focus.setFocus(keys[@intCast(target)]); 814 } 815 } 816 } 817 818 // page a fixed number of lines, then focus the leading visible hunk 819 // (bottom-most when paging down, top-most when paging up). in the 820 // terminal, `delta` is a line count, and on the web it's a hunk count. 821 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 822 if (self.session.is_terminal) { 823 const sc = self.diffScroll(); 824 sc.y += delta * 5; // magnify because it's a line count 825 self.diffScroll().clampToContent(); 826 try self.focusVisible(root_focus, delta > 0); 827 } else { 828 const inner = self.diffInner(); 829 const keys = inner.children.keys(); 830 if (keys.len == 0) return; 831 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 832 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 833 root_focus.setFocus(keys[target]); 834 } 835 } 836 837 // jump to the first or last hunk. on the web the browser scrolls to the 838 // focused hunk; on the terminal pin the scroll to the top/bottom too. 839 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 840 const inner = self.diffInner(); 841 const keys = inner.children.keys(); 842 if (keys.len == 0) return; 843 if (self.session.is_terminal) { 844 const sc = self.diffScroll(); 845 sc.y = if (to_end) std.math.maxInt(isize) else 0; 846 self.diffScroll().clampToContent(); 847 } 848 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 849 } 850 851 // whether hunk `index` is at least partly within the diff viewport, per the 852 // last build's layout rects (content space) and the current scroll offset. 853 fn hunkVisible(self: *View, index: usize) bool { 854 const inner = self.diffInner(); 855 const sc = self.diffScroll(); 856 const vp = sc.grid orelse return false; 857 const r = inner.children.values()[index].rect orelse return false; 858 const top = sc.y; 859 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 860 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 861 } 862 863 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 864 // the bottom-most visible (for downward motion), else the top-most. 865 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 866 const inner = self.diffInner(); 867 const sc = self.diffScroll(); 868 const vp = sc.grid orelse return; 869 const top = sc.y; 870 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 871 var chosen: ?usize = null; 872 for (inner.children.keys(), inner.children.values()) |id, *child| { 873 const r = child.rect orelse continue; // content-space layout rect 874 const r_top = r.y; 875 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 876 if (r_bot <= top or r_top >= bottom) continue; // not visible 877 chosen = id; 878 if (!prefer_last) break; // first visible 879 } 880 if (chosen) |id| root_focus.setFocus(id); 881 } 882 883 // enter the diff pane. the host arrives here on right-arrow or Enter from the 884 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 885 // case where the pane isn't laid out yet (it gets selected, then focused after 886 // the next build, landing on its remembered hunk). 887 fn focusDiff(self: *View, root_focus: *Focus) !void { 888 if (self.diffInner().children.count() == 0) return; 889 root_focus.setFocus(self.diffOuter().getFocus().id); 890 } 891 892 // return to the list. 893 fn focusList(self: *View, root_focus: *Focus) !void { 894 root_focus.setFocus(self.listScroll().getFocus().id); 895 } 896 897 pub fn clearGrid(self: *View) void { 898 self.box.clearGrid(); 899 } 900 901 pub fn getGrid(self: View) ?Grid { 902 return self.box.getGrid(); 903 } 904 905 pub fn getFocus(self: *View) *Focus { 906 return self.box.getFocus(); 907 } 908 909 // for the parent's "scroll up at the top jumps to the header" check. in the 910 // diff pane that means the first hunk is focused and the diff can't scroll up 911 // any further; otherwise up-arrow stays in the pane (scrolling or moving 912 // between hunks). when the list holds focus, report its selection directly. 913 pub fn getSelectedIndex(self: *View) ?usize { 914 if (self.diffActive()) { 915 const inner = self.diffInner(); 916 const cid = inner.getFocus().child_id orelse return 1; 917 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 918 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 919 } 920 const lb = self.listBox(); 921 const cid = lb.getFocus().child_id orelse return null; 922 return lb.children.getIndex(cid); 923 } 924 }; 925 926 // the "a:" navigation link for the commits page walking from commit `oid` within 927 // `identity`, windowing the selected commit's diff from hunk `start`, filtered 928 // to `path` ("" = every file). 929 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 930 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, start, path) orelse return error.RouteTooLong; 931 const url = try route.toUrl(page_arena); 932 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 933 } 934 935 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 936 // directory, within `identity` ("owner/name"). 937 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 938 const route = ui.RoutablePage.repoFilesRoute(identity, .object, oid, "", 0) orelse return error.RouteTooLong; 939 const url = try route.toUrl(page_arena); 940 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 941 } 942 943 // the in-page "ai:" anchor for selecting `oid` in `identity`'s commit list. it 944 // points at the same route as commitsLink but the "ai:" prefix keeps wasm clicks 945 // in-page (crossPageLink ignores it); the href is only followed with js off. 946 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, path: []const u8) ![]const u8 { 947 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, 0, path) orelse return error.RouteTooLong; 948 const url = try route.toUrl(page_arena); 949 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 950 } 951 952 // the "a:" link to the page showing `oid`'s message on its own. 953 fn messageLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 954 const route = ui.RoutablePage.repoCommitMessageRoute(identity, .object, oid) orelse return error.RouteTooLong; 955 const url = try route.toUrl(page_arena); 956 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 957 } 958 959 // the "viewing <ref_or_oid> <value>" banner shown above the log. 960 pub const Header = struct { 961 content: []const u8, 962 963 // `value` arrives url-encoded, so decode it for display. 964 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 965 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 966 return .{ 967 .content = try std.fmt.allocPrint(aa, "viewing {s} {s}", .{ @tagName(ref_or_oid), decoded }), 968 }; 969 } 970 971 pub const View = struct { 972 box: wgt.Box(ui.Widget), 973 data: *const Header, 974 975 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session) !Header.View { 976 _ = session; 977 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 978 errdefer box.deinit(allocator); 979 980 var text_box = try wgt.TextBox(ui.Widget).init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 981 errdefer text_box.deinit(allocator); 982 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 983 984 return .{ .box = box, .data = data }; 985 } 986 987 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 988 self.box.deinit(allocator); 989 } 990 991 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 992 self.clearGrid(); 993 try self.box.build(allocator, constraint, root_focus); 994 } 995 996 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 997 _ = self; 998 _ = allocator; 999 _ = key; 1000 _ = root_focus; 1001 } 1002 1003 pub fn clearGrid(self: *Header.View) void { 1004 self.box.clearGrid(); 1005 } 1006 1007 pub fn getGrid(self: Header.View) ?Grid { 1008 return self.box.getGrid(); 1009 } 1010 1011 pub fn getFocus(self: *Header.View) *Focus { 1012 return self.box.getFocus(); 1013 } 1014 }; 1015 }; 1016