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