╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
867 const r = inner.children.values()[index].rect orelse return false; 868 const top = sc.y; 869 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 870 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 871 } 872 873 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 874 // the bottom-most visible (for downward motion), else the top-most. 875 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 876 const inner = self.diffInner(); 877 const sc = self.diffScroll(); 878 const vp = sc.grid orelse return; 879 const top = sc.y; 880 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 881 var chosen: ?usize = null; 882 for (inner.children.keys(), inner.children.values()) |id, *child| { 883 const r = child.rect orelse continue; // content-space layout rect 884 const r_top = r.y; 885 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 886 if (r_bot <= top or r_top >= bottom) continue; // not visible 887 chosen = id; 888 if (!prefer_last) break; // first visible 889 } 890 if (chosen) |id| root_focus.setFocus(id); 891 } 892 893 // enter the diff pane. the host arrives here on right-arrow or Enter from the 894 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 895 // case where the pane isn't laid out yet (it gets selected, then focused after 896 // the next build, landing on its remembered hunk). 897 fn focusDiff(self: *View, root_focus: *Focus) !void { 898 if (self.diffInner().children.count() == 0) return; 899 root_focus.setFocus(self.diffOuter().getFocus().id); 900 } 901 902 // return to the list. 903 fn focusList(self: *View, root_focus: *Focus) !void { 904 root_focus.setFocus(self.listScroll().getFocus().id); 905 } 906 907 pub fn clearGrid(self: *View) void { 908 self.box.clearGrid(); 909 } 910 911 pub fn getGrid(self: View) ?Grid { 912 return self.box.getGrid(); 913 } 914 915 pub fn getFocus(self: *View) *Focus { 916 return self.box.getFocus(); 917 } 918 919 // for the parent's "scroll up at the top jumps to the header" check. in the 920 // diff pane that means the first hunk is focused and the diff can't scroll up 921 // any further; otherwise up-arrow stays in the pane (scrolling or moving 922 // between hunks). when the list holds focus, report its selection directly. 923 pub fn getSelectedIndex(self: *View) ?usize { 924 if (self.diffActive()) { 925 const inner = self.diffInner(); 926 const cid = inner.getFocus().child_id orelse return 1; 927 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 928 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 929 } 930 const lb = self.listBox(); 931 const cid = lb.getFocus().child_id orelse return null; 932 return lb.children.getIndex(cid); 933 } 934 }; 935 936 // the "a:" navigation link for the commits page walking from commit `oid` within 937 // `identity`, windowing the selected commit's diff from hunk `start`, filtered 938 // to `path` ("" = every file). 939 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 940 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, start, path) orelse return error.RouteTooLong; 941 const url = try route.toUrl(page_arena); 942 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 943 } 944 945 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 946 // directory, within `identity` ("owner/name"). 947 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 948 const route = ui.RoutablePage.repoFilesRoute(identity, .object, oid, "", 0) orelse return error.RouteTooLong; 949 const url = try route.toUrl(page_arena); 950 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 951 } 952 953 // the in-page "ai:" anchor for selecting `oid` in `identity`'s commit list. it 954 // points at the same route as commitsLink but the "ai:" prefix keeps wasm clicks 955 // in-page (crossPageLink ignores it); the href is only followed with js off. 956 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, path: []const u8) ![]const u8 { 957 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, 0, path) orelse return error.RouteTooLong; 958 const url = try route.toUrl(page_arena); 959 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 960 } 961 962 // the "a:" link to the page showing `oid`'s message on its own. 963 fn messageLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 964 const route = ui.RoutablePage.repoCommitMessageRoute(identity, .object, oid) orelse return error.RouteTooLong; 965 const url = try route.toUrl(page_arena); 966 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 967 } 968 969 // the "viewing <ref_or_oid> <value>" banner shown above the log. 970 pub const Header = struct { 971 content: []const u8, 972 973 // `value` arrives url-encoded, so decode it for display. 974 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 975 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 976 return .{ 977 .content = try std.fmt.allocPrint(aa, "viewing {s} {s}", .{ @tagName(ref_or_oid), decoded }), 978 }; 979 } 980 981 pub const View = struct { 982 box: wgt.Box(ui.Widget), 983 data: *const Header, 984 985 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session) !Header.View { 986 _ = session; 987 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 988 errdefer box.deinit(allocator); 989 990 var text_box = try wgt.TextBox(ui.Widget).init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 991 errdefer text_box.deinit(allocator); 992 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 993 994 return .{ .box = box, .data = data }; 995 } 996 997 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 998 self.box.deinit(allocator); 999 } 1000 1001 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1002 self.clearGrid(); 1003 try self.box.build(allocator, constraint, root_focus); 1004 } 1005 1006 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1007 _ = self; 1008 _ = allocator; 1009 _ = key; 1010 _ = root_focus; 1011 } 1012 1013 pub fn clearGrid(self: *Header.View) void { 1014 self.box.clearGrid(); 1015 } 1016 1017 pub fn getGrid(self: Header.View) ?Grid { 1018 return self.box.getGrid(); 1019 } 1020 1021 pub fn getFocus(self: *Header.View) *Focus { 1022 return self.box.getFocus(); 1023 } 1024 }; 1025 }; 1026