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