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