╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Refs.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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