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