╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
913 const top = sc.y; 914 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 915 var chosen: ?usize = null; 916 for (inner.children.keys(), inner.children.values()) |id, *child| { 917 const r = child.rect orelse continue; // content-space layout rect 918 const r_top = r.y; 919 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 920 if (r_bot <= top or r_top >= bottom) continue; // not visible 921 chosen = id; 922 if (!prefer_last) break; // first visible 923 } 924 if (chosen) |id| root_focus.setFocus(id); 925 } 926 927 // enter the diff pane. the host arrives here on right-arrow or Enter from the 928 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 929 // case where the pane isn't laid out yet (it gets selected, then focused after 930 // the next build, landing on its remembered hunk). 931 fn focusDiff(self: *View, root_focus: *Focus) !void { 932 if (self.diffInner().children.count() == 0) return; 933 root_focus.setFocus(self.diffOuter().getFocus().id); 934 } 935 936 // return to the list. 937 fn focusList(self: *View, root_focus: *Focus) !void { 938 root_focus.setFocus(self.listScroll().getFocus().id); 939 } 940 941 pub fn clearGrid(self: *View) void { 942 self.box.clearGrid(); 943 } 944 945 pub fn getGrid(self: View) ?Grid { 946 return self.box.getGrid(); 947 } 948 949 pub fn getFocus(self: *View) *Focus { 950 return self.box.getFocus(); 951 } 952 953 fn contentAtTop(self: *View) bool { 954 if (self.diffActive()) { 955 const inner = self.diffInner(); 956 const cid = inner.getFocus().child_id orelse return true; 957 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 958 return first_focused and self.diffScroll().y == 0; 959 } 960 const lb = self.listBox(); 961 const cid = lb.getFocus().child_id orelse return true; 962 return lb.children.getIndex(cid) == 0; 963 } 964 965 // only the clone-url row sits directly below the repository header. 966 pub fn atTop(self: *View) bool { 967 return self.headerActive() or (!self.header().hasCloneUrl() and self.contentAtTop()); 968 } 969 970 pub fn focusCloneUrl(self: *View, root_focus: *Focus) bool { 971 return self.header().focusCloneUrl(root_focus); 972 } 973 }; 974 975 // the "a:" navigation link for the commits page walking from commit `oid` within 976 // `location`, windowing the selected commit's diff from hunk `start`, filtered 977 // to `path` ("" = every file). 978 fn commitsLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 979 const route = location.commitsRoute(.object, oid, start, path) orelse return error.RouteTooLong; 980 const url = try route.toUrl(page_arena); 981 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 982 } 983 984 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 985 // directory, within `location`. 986 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 987 const route = location.filesRoute(.object, oid, "", 0) orelse return error.RouteTooLong; 988 const url = try route.toUrl(page_arena); 989 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 990 } 991 992 // the in-page "ai:" anchor for selecting a commit with its current pane content 993 // and window. the href is only followed with js off. 994 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, commit: Commit, content: Content) ![]const u8 { 995 const route = (switch (content) { 996 .message => location.commitMessageRoute(.object, commit.oid), 997 .diff => |diff| location.commitsRoute(.object, commit.oid, commit.window_start, diff.path), 998 }) orelse return error.RouteTooLong; 999 const url = try route.toUrl(page_arena); 1000 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 1001 } 1002 1003 // the "a:" link to the page showing `oid`'s message on its own. 1004 fn messageLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 1005 const route = location.commitMessageRoute(.object, oid) orelse return error.RouteTooLong; 1006 const url = try route.toUrl(page_arena); 1007 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1008 } 1009 1010 // the "<ref_or_oid> <value>" banner shown above the log. 1011 pub const Header = struct { 1012 content: []const u8, 1013 1014 // `value` arrives url-encoded, so decode it for display. 1015 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 1016 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 1017 return .{ 1018 .content = try std.fmt.allocPrint(aa, "{s} {s}", .{ @tagName(ref_or_oid), decoded }), 1019 }; 1020 } 1021 1022 pub const View = struct { 1023 box: wgt.Box(ui.Widget), 1024 data: *const Header, 1025 1026 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !Header.View { 1027 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1028 errdefer box.deinit(allocator); 1029 1030 if (!session.data.is_local) { 1031 if (try ui.widget.CopyableText.initClone(allocator, session, location)) |value| { 1032 var clone_url = value; 1033 errdefer clone_url.deinit(allocator); 1034 const min_width = clone_url.minWidth(); 1035 box.getFocus().child_id = clone_url.getFocus().id; 1036 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 } }); 1037 } 1038 } 1039 1040 var spacer = try ui.widget.Spacer.init(allocator); 1041 errdefer spacer.deinit(allocator); 1042 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1043 1044 var text_box = try wgt.TextBox.init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 1045 errdefer text_box.deinit(allocator); 1046 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null, .flex = .shrink }); 1047 1048 return .{ .box = box, .data = data }; 1049 } 1050 1051 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 1052 self.box.deinit(allocator); 1053 } 1054 1055 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1056 self.clearGrid(); 1057 try self.box.build(allocator, constraint, root_focus); 1058 } 1059 1060 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1061 const clone_url = self.cloneUrl() orelse return; 1062 try clone_url.input(allocator, key, root_focus); 1063 } 1064 1065 pub fn focusCloneUrl(self: *Header.View, root_focus: *Focus) bool { 1066 const clone_url = self.cloneUrl() orelse return false; 1067 root_focus.setFocus(clone_url.getFocus().id); 1068 return true; 1069 } 1070 1071 pub fn hasCloneUrl(self: *Header.View) bool { 1072 return self.cloneUrl() != null; 1073 } 1074 1075 fn cloneUrl(self: *Header.View) ?*ui.widget.CopyableText { 1076 for (self.box.children.values()) |*child| switch (child.widget) { 1077 .copyable_text => |*copyable_text| return copyable_text, 1078 else => {}, 1079 }; 1080 return null; 1081 } 1082 1083 pub fn clearGrid(self: *Header.View) void { 1084 self.box.clearGrid(); 1085 } 1086 1087 pub fn getGrid(self: Header.View) ?Grid { 1088 return self.box.getGrid(); 1089 } 1090 1091 pub fn getFocus(self: *Header.View) *Focus { 1092 return self.box.getFocus(); 1093 } 1094 }; 1095 }; 1096