╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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.git_http_port != null or session.data.git_ssh_port != null)) { 1011 var clone_url = try ui.CopyableText.View.initClone(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 = .{ .copyable_text = 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.CopyableText.View { 1054 for (self.box.children.values()) |*child| switch (child.widget) { 1055 .copyable_text => |*copyable_text| return copyable_text, 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