╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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().hasGitRemote() or !self.contentAtTop()) 1 else 0; 948 } 949 950 pub fn focusGitRemote(self: *View, root_focus: *Focus) bool { 951 return self.header().focusGitRemote(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.git_http_port != null or session.data.git_ssh_port != null)) { 1011 var git_remote = try ui.GitRemote.View.initClone(allocator, session, identity); 1012 errdefer git_remote.deinit(allocator); 1013 const min_width = git_remote.minWidth(); 1014 box.getFocus().child_id = git_remote.getFocus().id; 1015 try box.children.put(allocator, git_remote.getFocus().id, .{ .widget = .{ .git_remote = git_remote }, .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 git_remote = self.gitRemote() orelse return; 1040 try git_remote.input(allocator, key, root_focus); 1041 } 1042 1043 pub fn focusGitRemote(self: *Header.View, root_focus: *Focus) bool { 1044 const git_remote = self.gitRemote() orelse return false; 1045 root_focus.setFocus(git_remote.getFocus().id); 1046 return true; 1047 } 1048 1049 pub fn hasGitRemote(self: *Header.View) bool { 1050 return self.gitRemote() != null; 1051 } 1052 1053 fn gitRemote(self: *Header.View) ?*ui.GitRemote.View { 1054 for (self.box.children.values()) |*child| switch (child.widget) { 1055 .git_remote => |*git_remote| return git_remote, 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