╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
855 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 856 if (self.session.is_terminal) { 857 const sc = self.diffScroll(); 858 sc.y += delta * 5; // magnify because it's a line count 859 self.diffScroll().clampToContent(); 860 try self.focusVisible(root_focus, delta > 0); 861 } else { 862 const inner = self.diffInner(); 863 const keys = inner.children.keys(); 864 if (keys.len == 0) return; 865 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 866 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 867 root_focus.setFocus(keys[target]); 868 } 869 } 870 871 // jump to the first or last hunk. on the web the browser scrolls to the 872 // focused hunk; on the terminal pin the scroll to the top/bottom too. 873 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 874 const inner = self.diffInner(); 875 const keys = inner.children.keys(); 876 if (keys.len == 0) return; 877 if (self.session.is_terminal) { 878 const sc = self.diffScroll(); 879 sc.y = if (to_end) std.math.maxInt(isize) else 0; 880 self.diffScroll().clampToContent(); 881 } 882 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 883 } 884 885 // whether hunk `index` is at least partly within the diff viewport, per the 886 // last build's layout rects (content space) and the current scroll offset. 887 fn hunkVisible(self: *View, index: usize) bool { 888 const inner = self.diffInner(); 889 const sc = self.diffScroll(); 890 const vp = sc.grid orelse return false; 891 const r = inner.children.values()[index].rect orelse return false; 892 const top = sc.y; 893 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 894 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 895 } 896 897 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 898 // the bottom-most visible (for downward motion), else the top-most. 899 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 900 const inner = self.diffInner(); 901 const sc = self.diffScroll(); 902 const vp = sc.grid orelse return; 903 const top = sc.y; 904 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 905 var chosen: ?usize = null; 906 for (inner.children.keys(), inner.children.values()) |id, *child| { 907 const r = child.rect orelse continue; // content-space layout rect 908 const r_top = r.y; 909 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 910 if (r_bot <= top or r_top >= bottom) continue; // not visible 911 chosen = id; 912 if (!prefer_last) break; // first visible 913 } 914 if (chosen) |id| root_focus.setFocus(id); 915 } 916 917 // enter the diff pane. the host arrives here on right-arrow or Enter from the 918 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 919 // case where the pane isn't laid out yet (it gets selected, then focused after 920 // the next build, landing on its remembered hunk). 921 fn focusDiff(self: *View, root_focus: *Focus) !void { 922 if (self.diffInner().children.count() == 0) return; 923 root_focus.setFocus(self.diffOuter().getFocus().id); 924 } 925 926 // return to the list. 927 fn focusList(self: *View, root_focus: *Focus) !void { 928 root_focus.setFocus(self.listScroll().getFocus().id); 929 } 930 931 pub fn clearGrid(self: *View) void { 932 self.box.clearGrid(); 933 } 934 935 pub fn getGrid(self: View) ?Grid { 936 return self.box.getGrid(); 937 } 938 939 pub fn getFocus(self: *View) *Focus { 940 return self.box.getFocus(); 941 } 942 943 fn contentAtTop(self: *View) bool { 944 if (self.diffActive()) { 945 const inner = self.diffInner(); 946 const cid = inner.getFocus().child_id orelse return true; 947 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 948 return first_focused and self.diffScroll().y == 0; 949 } 950 const lb = self.listBox(); 951 const cid = lb.getFocus().child_id orelse return true; 952 return lb.children.getIndex(cid) == 0; 953 } 954 955 // only the clone-url row sits directly below the repository header. 956 pub fn getSelectedIndex(self: *View) ?usize { 957 if (self.headerActive()) return 0; 958 return if (self.header().hasCloneUrl() or !self.contentAtTop()) 1 else 0; 959 } 960 961 pub fn focusCloneUrl(self: *View, root_focus: *Focus) bool { 962 return self.header().focusCloneUrl(root_focus); 963 } 964 }; 965 966 // the "a:" navigation link for the commits page walking from commit `oid` within 967 // `location`, windowing the selected commit's diff from hunk `start`, filtered 968 // to `path` ("" = every file). 969 fn commitsLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 970 const route = location.commitsRoute(.object, oid, start, path) orelse return error.RouteTooLong; 971 const url = try route.toUrl(page_arena); 972 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 973 } 974 975 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 976 // directory, within `location`. 977 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 978 const route = location.filesRoute(.object, oid, "", 0) orelse return error.RouteTooLong; 979 const url = try route.toUrl(page_arena); 980 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 981 } 982 983 // the in-page "ai:" anchor for selecting a commit with its current pane content 984 // and window. the href is only followed with js off. 985 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, commit: Commit, content: Content) ![]const u8 { 986 const route = (switch (content) { 987 .message => location.commitMessageRoute(.object, commit.oid), 988 .diff => |diff| location.commitsRoute(.object, commit.oid, commit.window_start, diff.path), 989 }) orelse return error.RouteTooLong; 990 const url = try route.toUrl(page_arena); 991 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 992 } 993 994 // the "a:" link to the page showing `oid`'s message on its own. 995 fn messageLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 996 const route = location.commitMessageRoute(.object, oid) orelse return error.RouteTooLong; 997 const url = try route.toUrl(page_arena); 998 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 999 } 1000 1001 // the "<ref_or_oid> <value>" banner shown above the log. 1002 pub const Header = struct { 1003 content: []const u8, 1004 1005 // `value` arrives url-encoded, so decode it for display. 1006 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 1007 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 1008 return .{ 1009 .content = try std.fmt.allocPrint(aa, "{s} {s}", .{ @tagName(ref_or_oid), decoded }), 1010 }; 1011 } 1012 1013 pub const View = struct { 1014 box: wgt.Box(ui.Widget), 1015 data: *const Header, 1016 1017 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !Header.View { 1018 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1019 errdefer box.deinit(allocator); 1020 1021 if (!session.data.is_local) { 1022 if (try ui.widget.CopyableText.initClone(allocator, session, location)) |value| { 1023 var clone_url = value; 1024 errdefer clone_url.deinit(allocator); 1025 const min_width = clone_url.minWidth(); 1026 box.getFocus().child_id = clone_url.getFocus().id; 1027 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 1028 } 1029 } 1030 1031 var spacer = try ui.widget.Spacer.init(allocator); 1032 errdefer spacer.deinit(allocator); 1033 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1034 1035 var text_box = try wgt.TextBox.init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 1036 errdefer text_box.deinit(allocator); 1037 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null, .flex = .shrink }); 1038 1039 return .{ .box = box, .data = data }; 1040 } 1041 1042 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 1043 self.box.deinit(allocator); 1044 } 1045 1046 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1047 self.clearGrid(); 1048 try self.box.build(allocator, constraint, root_focus); 1049 } 1050 1051 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1052 const clone_url = self.cloneUrl() orelse return; 1053 try clone_url.input(allocator, key, root_focus); 1054 } 1055 1056 pub fn focusCloneUrl(self: *Header.View, root_focus: *Focus) bool { 1057 const clone_url = self.cloneUrl() orelse return false; 1058 root_focus.setFocus(clone_url.getFocus().id); 1059 return true; 1060 } 1061 1062 pub fn hasCloneUrl(self: *Header.View) bool { 1063 return self.cloneUrl() != null; 1064 } 1065 1066 fn cloneUrl(self: *Header.View) ?*ui.widget.CopyableText { 1067 for (self.box.children.values()) |*child| switch (child.widget) { 1068 .copyable_text => |*copyable_text| return copyable_text, 1069 else => {}, 1070 }; 1071 return null; 1072 } 1073 1074 pub fn clearGrid(self: *Header.View) void { 1075 self.box.clearGrid(); 1076 } 1077 1078 pub fn getGrid(self: Header.View) ?Grid { 1079 return self.box.getGrid(); 1080 } 1081 1082 pub fn getFocus(self: *Header.View) *Focus { 1083 return self.box.getFocus(); 1084 } 1085 }; 1086 }; 1087