╭ Refs.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../../event.zig"); 3 const ui = @import("../../ui.zig"); 4 const xit = @import("xit"); 5 const rp = xit.repo; 6 const xitui = xit.xitui; 7 const wgt = xitui.widget; 8 const layout = xitui.layout; 9 const inp = xitui.input; 10 const Grid = xitui.grid.Grid; 11 const Focus = xitui.focus.Focus; 12 13 // how many refs one window of a column shows. 14 pub const page_size = 20; 15 16 // "owner/name", needed to build the columns' window-navigation links. 17 identity: []const u8, 18 // only one column paginates at a time: `after` is the window start of `kind`'s 19 // column; the other column always starts at 0. 20 kind: ui.RoutablePage.RefKind, 21 after: usize, 22 // the current window of each column, plus the next window's start (null at the 23 // end) so the view can decide whether to show a "next →" row. 24 branches: []const []const u8, 25 tags: []const []const u8, 26 branches_next_after: ?usize, 27 tags_next_after: ?usize, 28 // the columns' headers, in the half-height SubTitle font. 29 branches_label: ui.SubTitle, 30 tags_label: ui.SubTitle, 31 32 const Self = @This(); 33 34 const Window = struct { names: []const []const u8, next_after: ?usize }; 35 36 pub fn init( 37 arena: *std.heap.ArenaAllocator, 38 session: *ui.Session, 39 event_id: *const [evt.event_id_size]u8, 40 identity: []const u8, 41 kind: ui.RoutablePage.RefKind, 42 after: usize, 43 ) !Self { 44 const aa = arena.allocator(); 45 // the offset only applies to its own column; the other stays at 0. 46 const branches_after: usize = if (kind == .branch) after else 0; 47 const tags_after: usize = if (kind == .tag) after else 0; 48 const branches_label = try ui.SubTitle.init(arena, "branches"); 49 const tags_label = try ui.SubTitle.init(arena, "tags"); 50 const empty: Self = .{ 51 .identity = try aa.dupe(u8, identity), 52 .kind = kind, 53 .after = after, 54 .branches = &.{}, 55 .tags = &.{}, 56 .branches_next_after = null, 57 .tags_next_after = null, 58 .branches_label = branches_label, 59 .tags_label = tags_label, 60 }; 61 62 // no filesystem (wasm) or nowhere to look: empty lists. the wasm path never 63 // calls init anyway — it rebuilds from the serialized snapshot. 64 const io = session.io orelse return empty; 65 const repos_dir = session.repos_dir orelse return empty; 66 67 // the repo's working copy lives at <repos_dir>/<hex event id>. 68 const hex = std.fmt.bytesToHex(event_id.*, .lower); 69 const repo_path = try std.fs.path.join(aa, &.{ repos_dir, &hex }); 70 71 // open with the arena's backing allocator (transient; the ref names are 72 // duped into the page arena so they outlive the repo handle). 73 const gpa = arena.child_allocator; 74 var any_repo = rp.AnyRepo(.xit, .{}).open(io, gpa, .{ .path = repo_path }) catch return empty; 75 defer any_repo.deinit(io, gpa); 76 77 const branches, const tags = switch (any_repo) { 78 inline else => |*repo| blk: { 79 var branch_iter = repo.listBranches(io, gpa, .{ .index = branches_after }) catch return empty; 80 defer branch_iter.deinit(io); 81 const b = try collectWindow(io, aa, &branch_iter, branches_after); 82 83 var tag_iter = repo.listTags(io, gpa, .{ .index = tags_after }) catch return empty; 84 defer tag_iter.deinit(io); 85 const t = try collectWindow(io, aa, &tag_iter, tags_after); 86 break :blk .{ b, t }; 87 }, 88 }; 89 90 return .{ 91 .identity = empty.identity, 92 .kind = kind, 93 .after = after, 94 .branches = branches.names, 95 .tags = tags.names, 96 .branches_next_after = branches.next_after, 97 .tags_next_after = tags.next_after, 98 .branches_label = branches_label, 99 .tags_label = tags_label, 100 }; 101 } 102 103 // window a ref iterator without materializing the whole list 104 fn collectWindow(io: std.Io, aa: std.mem.Allocator, iter: anytype, after: usize) !Window { 105 var names = try std.ArrayListUnmanaged([]const u8).initCapacity(aa, page_size); 106 while (names.items.len < page_size) { 107 const ref = try iter.next(io) orelse break; 108 names.appendAssumeCapacity(try aa.dupe(u8, ref.name)); 109 } 110 const has_more = (try iter.next(io)) != null; 111 return .{ .names = names.items, .next_after = if (has_more) after + page_size else null }; 112 } 113 114 pub const View = struct { 115 // a horizontal box of two vertical columns. each column is a fixed, 116 // centered, non-focusable label above a Scroll of focusable rows, so the 117 // label stays visible while the rows scroll. focus tracks a single selected 118 // row across both columns: the box's focus path points at the active column, 119 // that column's at its scroll, and the scroll's (its rows') at the row. 120 box: wgt.Box(ui.Widget), 121 data: *const Self, 122 123 const left_col = 0; 124 const right_col = 1; 125 126 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 127 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 128 errdefer box.deinit(allocator); 129 130 // the offset only applies to its own column; the other starts at 0. 131 const branches_after: usize = if (data.kind == .branch) data.after else 0; 132 const tags_after: usize = if (data.kind == .tag) data.after else 0; 133 try addColumn(allocator, &box, session, data.identity, .branch, &data.branches_label, data.branches, branches_after, data.branches_next_after); 134 try addColumn(allocator, &box, session, data.identity, .tag, &data.tags_label, data.tags, tags_after, data.tags_next_after); 135 136 var self = View{ .box = box, .data = data }; 137 // select the first row of the first column that has one. 138 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 139 if (child.widget.box.getFocus().child_id != null) { 140 self.box.getFocus().child_id = id; 141 break; 142 } 143 } 144 return self; 145 } 146 147 fn addColumn( 148 allocator: std.mem.Allocator, 149 box: *wgt.Box(ui.Widget), 150 session: *ui.Session, 151 identity: []const u8, 152 kind: ui.RoutablePage.RefKind, 153 label: *const ui.SubTitle, 154 names: []const []const u8, 155 after: usize, 156 next_after: ?usize, 157 ) !void { 158 var column = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 159 errdefer column.deinit(allocator); 160 161 // a fixed, non-focusable header in the SubTitle font, nudged off the 162 // left edge by a one-column space. it stays visible while the rows 163 // scroll below, and declares its height (2 rows) as a min so the (fill) 164 // scroll reserves room for it rather than consuming the whole column. 165 { 166 var header_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 167 errdefer header_box.deinit(allocator); 168 { 169 var space = try wgt.Text(ui.Widget).init(allocator, " "); 170 errdefer space.deinit(allocator); 171 try header_box.children.put(allocator, space.getFocus().id, .{ .widget = .{ .text = space }, .rect = null, .min_size = null }); 172 } 173 { 174 var header = try ui.SubTitle.View.init(allocator, label); 175 errdefer header.deinit(allocator); 176 try header_box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .sub_title = header }, .rect = null, .min_size = null }); 177 } 178 try column.children.put(allocator, header_box.getFocus().id, .{ .widget = .{ .box = header_box }, .rect = null, .min_size = .{ .width = null, .height = 2 } }); 179 } 180 181 // a long ref list overflows the page height; the rows scroll on their 182 // own (the label above stays put) so they lay out under a fixed height. 183 { 184 var scroll = blk: { 185 var rows = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 186 errdefer rows.deinit(allocator); 187 188 // window-navigation rows bracket the names: "← previous" off the 189 // first window, "next →" when more remain. each is a full reload. 190 if (after > 0) try addRow(allocator, &rows, "← previous", try windowLink(session.page_arena, identity, kind, after -| page_size)); 191 // each ref name links to the files tab at that ref's root. 192 for (names) |name| try addRow(allocator, &rows, name, try refLink(session.page_arena, identity, kind, name)); 193 if (next_after) |na| try addRow(allocator, &rows, "next →", try windowLink(session.page_arena, identity, kind, na)); 194 195 if (rows.children.count() > 0) rows.getFocus().child_id = rows.children.keys()[0]; 196 197 // fill the column (rows top-aligned, scroll bar pinned to the 198 // edge) rather than shrinking to the widest row. 199 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = rows }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 200 }; 201 errdefer scroll.deinit(allocator); 202 203 // the column's focus path skips the label and points at the scroll. 204 if (scroll.getFocus().child_id != null) column.getFocus().child_id = scroll.getFocus().id; 205 try column.children.put(allocator, scroll.getFocus().id, .{ .widget = .{ .scroll = scroll }, .rect = null, .min_size = null }); 206 } 207 208 try box.children.put(allocator, column.getFocus().id, .{ .widget = .{ .box = column }, .rect = null, .min_size = null }); 209 } 210 211 // [0] = the fixed label, [1] = the scrollable rows. 212 fn columnScroll(column: *wgt.Box(ui.Widget)) *wgt.Scroll(ui.Widget) { 213 return &column.children.values()[1].widget.scroll; 214 } 215 216 // a focusable row in a column. `link`, when present, is the row's `a:` 217 // navigation kind (the window-navigation rows); ref names pass null. 218 fn addRow(allocator: std.mem.Allocator, col: *wgt.Box(ui.Widget), label: []const u8, link: ?[]const u8) !void { 219 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 220 errdefer row.deinit(allocator); 221 row.getFocus().focusable = true; 222 if (link) |l| row.getFocus().kind = .{ .custom = l }; 223 try col.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 224 } 225 226 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 227 self.box.deinit(allocator); 228 } 229 230 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 231 self.clearGrid(); 232 233 // the selected row (in the active column) shows a single border; the 234 // focused TextBox upgrades it to a double border itself. 235 const active_col_id = self.box.getFocus().child_id; 236 for (self.box.children.values()) |*child| { 237 const column = &child.widget.box; 238 const col = &columnScroll(column).child.box; 239 const active = column.getFocus().id == active_col_id; 240 for (col.children.keys(), col.children.values()) |id, *row| { 241 switch (row.widget) { 242 .text_box => |*tb| tb.options.border_style = if (active and col.getFocus().child_id == id) .single else .hidden, 243 else => {}, 244 } 245 } 246 } 247 248 // split the available width evenly between the two columns when it's 249 // known; otherwise let them size to their content. each column's Scroll 250 // has `fill = true`, so it stretches across the column (rows/scroll bar) 251 // rather than hugging the widest row. 252 if (constraint.max_size.width) |w| { 253 const half = w / 2; 254 for (self.box.children.values()) |*child| { 255 child.min_size = .{ .width = half, .height = null }; 256 child.max_size = .{ .width = half, .height = null }; 257 } 258 } else { 259 for (self.box.children.values()) |*child| { 260 child.min_size = null; 261 child.max_size = null; 262 } 263 } 264 265 // clear the incoming min height; each column's Scroll fills the viewport 266 // height itself (via `fill`), keeping its bar pinned to the edge. 267 try self.box.build(allocator, .{ 268 .min_size = .{ .width = null, .height = null }, 269 .max_size = constraint.max_size, 270 }, root_focus); 271 } 272 273 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 274 _ = allocator; 275 // up/down (and the scroll wheel) move within the active column a row; 276 // page up/down and home/end jump; left/right switch columns, keeping the 277 // same row offset. the parent (Repo) intercepts up at the top row to move 278 // focus to the header. 279 switch (key) { 280 .arrow_up => try self.moveRow(root_focus, -1), 281 .arrow_down => try self.moveRow(root_focus, 1), 282 .arrow_left => try self.switchColumn(root_focus, left_col), 283 .arrow_right => try self.switchColumn(root_focus, right_col), 284 .page_down => try self.moveRow(root_focus, 10), 285 .page_up => try self.moveRow(root_focus, -10), 286 .end => try self.moveRow(root_focus, self.columnRowCount()), 287 .home => try self.moveRow(root_focus, -self.columnRowCount()), 288 .mouse => |mouse| switch (mouse.action) { 289 .scroll => |dir| try self.moveRow(root_focus, if (dir == .up) -1 else 1), 290 else => {}, 291 }, 292 else => {}, 293 } 294 } 295 296 // the active column's row count, used as a clamp-to-end delta for home/end. 297 fn columnRowCount(self: *View) isize { 298 const col = self.activeColumn() orelse return 0; 299 return @intCast(col.children.count()); 300 } 301 302 fn activeScroll(self: *View) ?*wgt.Scroll(ui.Widget) { 303 const id = self.box.getFocus().child_id orelse return null; 304 const index = self.box.children.getIndex(id) orelse return null; 305 return columnScroll(&self.box.children.values()[index].widget.box); 306 } 307 308 fn activeColumn(self: *View) ?*wgt.Box(ui.Widget) { 309 return &(self.activeScroll() orelse return null).child.box; 310 } 311 312 fn moveRow(self: *View, root_focus: *Focus, delta: isize) !void { 313 const scroll = self.activeScroll() orelse return; 314 const col = &scroll.child.box; 315 const keys = col.children.keys(); 316 if (keys.len == 0) return; 317 const cur_id = col.getFocus().child_id orelse return; 318 const cur: isize = @intCast(col.children.getIndex(cur_id) orelse return); 319 // clamp a delta past either end to the first/last row. 320 const last: isize = @intCast(keys.len - 1); 321 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 322 if (next == @as(usize, @intCast(cur))) return; 323 root_focus.setFocus(keys[next]); 324 if (col.children.values()[next].rect) |rect| scroll.scrollToRect(rect); 325 } 326 327 fn switchColumn(self: *View, root_focus: *Focus, target: usize) !void { 328 if (target >= self.box.children.count()) return; 329 const cur_col = self.activeColumn() orelse return; 330 const target_scroll = columnScroll(&self.box.children.values()[target].widget.box); 331 const target_col = &target_scroll.child.box; 332 // an empty column has nothing to select; ignore. 333 if (target_col.children.count() == 0) return; 334 // keep the same row offset, clamped to the target column's last row. 335 const cur_id = cur_col.getFocus().child_id orelse return; 336 const cur = cur_col.children.getIndex(cur_id) orelse return; 337 const last = target_col.children.count() - 1; 338 const next = @min(cur, last); 339 root_focus.setFocus(target_col.children.keys()[next]); 340 if (target_col.children.values()[next].rect) |rect| target_scroll.scrollToRect(rect); 341 } 342 343 pub fn clearGrid(self: *View) void { 344 self.box.clearGrid(); 345 } 346 347 pub fn getGrid(self: View) ?Grid { 348 return self.box.getGrid(); 349 } 350 351 pub fn getFocus(self: *View) *Focus { 352 return self.box.getFocus(); 353 } 354 355 // the selected row's offset within the active column (0 = first row), so the 356 // parent knows when up should escape to the header. 357 pub fn getSelectedIndex(self: View) ?usize { 358 var copy = self; 359 const col = copy.activeColumn() orelse return null; 360 const cur_id = col.getFocus().child_id orelse return null; 361 return col.children.getIndex(cur_id); 362 } 363 }; 364 365 // the "a:" link to `kind`'s column paginated to `after` within `identity` 366 // ("owner/name"). the other column resets to its first window. 367 fn windowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, kind: ui.RoutablePage.RefKind, after: usize) ![]const u8 { 368 const route = ui.RoutablePage.repoRefsRoute(identity, kind, after) orelse return error.RouteTooLong; 369 const url = try route.urlAlloc(page_arena); 370 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 371 } 372 373 // the "a:" link to the files tab at ref `name` (a branch or tag) within 374 // `identity` ("owner/name"), at its root directory. the name is percent-encoded 375 // since it can contain a '/'. 376 fn refLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, kind: ui.RoutablePage.RefKind, name: []const u8) ![]const u8 { 377 const aa = page_arena.allocator(); 378 const ref_or_oid: ui.RoutablePage.RefOrOid = switch (kind) { 379 .branch => .branch, 380 .tag => .tag, 381 }; 382 const value = try ui.urlEncodeRef(aa, name); 383 const route = ui.RoutablePage.repoFilesRoute(identity, ref_or_oid, value, "", 0) orelse return error.RouteTooLong; 384 const url = try route.urlAlloc(page_arena); 385 return std.fmt.allocPrint(aa, "a:{s}", .{url}); 386 } 387