╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Refs.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
163 // left edge by a one-column space. it stays visible while the rows 164 // scroll below, and declares its height (2 rows) as a min so the (fill) 165 // scroll reserves room for it rather than consuming the whole column. 166 { 167 var header_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 168 errdefer header_box.deinit(allocator); 169 { 170 var space = try wgt.Text(ui.Widget).init(allocator, " "); 171 errdefer space.deinit(allocator); 172 try header_box.children.put(allocator, space.getFocus().id, .{ .widget = .{ .text = space }, .rect = null, .min_size = null }); 173 } 174 { 175 var header = try ui.SubTitle.View.init(allocator, label); 176 errdefer header.deinit(allocator); 177 try header_box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .sub_title = header }, .rect = null, .min_size = null }); 178 } 179 try column.children.put(allocator, header_box.getFocus().id, .{ .widget = .{ .box = header_box }, .rect = null, .min_size = .{ .width = null, .height = 2 } }); 180 } 181 182 // a long ref list overflows the page height; the rows scroll on their 183 // own (the label above stays put) so they lay out under a fixed height. 184 { 185 var scroll = blk: { 186 var rows = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 187 errdefer rows.deinit(allocator); 188 189 // window-navigation rows bracket the names: "← previous" off the 190 // first window, "next →" when more remain. each is a full reload. 191 if (after > 0) try addRow(allocator, &rows, "← previous", try windowLink(session.page_arena, identity, kind, after -| page_size)); 192 // each ref name links to the files tab at that ref's root. 193 for (names) |name| try addRow(allocator, &rows, name, try refLink(session.page_arena, identity, kind, name)); 194 if (next_after) |na| try addRow(allocator, &rows, "next →", try windowLink(session.page_arena, identity, kind, na)); 195 196 if (rows.children.count() > 0) rows.getFocus().child_id = rows.children.keys()[0]; 197 198 // fill the column (rows top-aligned, scroll bar pinned to the 199 // edge) rather than shrinking to the widest row. 200 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = rows }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 201 }; 202 errdefer scroll.deinit(allocator); 203 204 // the column's focus path skips the label and points at the scroll. 205 if (scroll.getFocus().child_id != null) column.getFocus().child_id = scroll.getFocus().id; 206 try column.children.put(allocator, scroll.getFocus().id, .{ .widget = .{ .scroll = scroll }, .rect = null, .min_size = null }); 207 } 208 209 try box.children.put(allocator, column.getFocus().id, .{ .widget = .{ .box = column }, .rect = null, .min_size = null }); 210 } 211 212 // [0] = the fixed label, [1] = the scrollable rows. 213 fn columnScroll(column: *wgt.Box(ui.Widget)) *wgt.Scroll(ui.Widget) { 214 return &column.children.values()[1].widget.scroll; 215 } 216 217 // a focusable row in a column. `link`, when present, is the row's `a:` 218 // navigation kind (the window-navigation rows); ref names pass null. 219 fn addRow(allocator: std.mem.Allocator, col: *wgt.Box(ui.Widget), label: []const u8, link: ?[]const u8) !void { 220 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 221 errdefer row.deinit(allocator); 222 row.getFocus().focusable = true; 223 if (link) |l| row.getFocus().kind = .{ .custom = l }; 224 try col.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 225 } 226 227 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 228 self.box.deinit(allocator); 229 } 230 231 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 232 self.clearGrid(); 233 234 // the selected row (in the active column) shows a single border; the 235 // focused TextBox upgrades it to a double border itself. 236 const active_col_id = self.box.getFocus().child_id; 237 for (self.box.children.values()) |*child| { 238 const column = &child.widget.box; 239 const col = &columnScroll(column).child.box; 240 const active = column.getFocus().id == active_col_id; 241 for (col.children.keys(), col.children.values()) |id, *row| { 242 switch (row.widget) { 243 .text_box => |*tb| tb.options.border_style = if (active and col.getFocus().child_id == id) .single else .hidden, 244 else => {}, 245 } 246 } 247 } 248 249 // split the available width evenly between the two columns when it's 250 // known; otherwise let them size to their content. each column's Scroll 251 // has `fill = true`, so it stretches across the column (rows/scroll bar) 252 // rather than hugging the widest row. 253 if (constraint.max_size.width) |w| { 254 const half = w / 2; 255 for (self.box.children.values()) |*child| { 256 child.min_size = .{ .width = half, .height = null }; 257 child.max_size = .{ .width = half, .height = null }; 258 } 259 } else { 260 for (self.box.children.values()) |*child| { 261 child.min_size = null; 262 child.max_size = null; 263 } 264 } 265 266 // clear the incoming min height; each column's Scroll fills the viewport 267 // height itself (via `fill`), keeping its bar pinned to the edge. 268 try self.box.build(allocator, .{ 269 .min_size = .{ .width = null, .height = null }, 270 .max_size = constraint.max_size, 271 }, root_focus); 272 } 273 274 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 275 _ = allocator; 276 // up/down (and the scroll wheel) move within the active column a row; 277 // page up/down and home/end jump; left/right switch columns, keeping the 278 // same row offset. the parent (Repo) intercepts up at the top row to move 279 // focus to the header. 280 if (inp.rowDelta(key, self.columnRowCount())) |delta| { 281 const scroll = self.activeScroll() orelse return; 282 ui.moveRowFocus(&scroll.child.box, scroll, root_focus, delta); 283 return; 284 } 285 switch (key) { 286 .arrow_left => try self.switchColumn(root_focus, left_col), 287 .arrow_right => try self.switchColumn(root_focus, right_col), 288 else => {}, 289 } 290 } 291 292 // the active column's row count, used as a clamp-to-end delta for home/end. 293 fn columnRowCount(self: *View) isize { 294 const col = self.activeColumn() orelse return 0; 295 return @intCast(col.children.count()); 296 } 297 298 fn activeScroll(self: *View) ?*wgt.Scroll(ui.Widget) { 299 const id = self.box.getFocus().child_id orelse return null; 300 const index = self.box.children.getIndex(id) orelse return null; 301 return columnScroll(&self.box.children.values()[index].widget.box); 302 } 303 304 fn activeColumn(self: *View) ?*wgt.Box(ui.Widget) { 305 return &(self.activeScroll() orelse return null).child.box; 306 } 307 308 fn switchColumn(self: *View, root_focus: *Focus, target: usize) !void { 309 if (target >= self.box.children.count()) return; 310 const cur_col = self.activeColumn() orelse return; 311 const target_scroll = columnScroll(&self.box.children.values()[target].widget.box); 312 const target_col = &target_scroll.child.box; 313 // an empty column has nothing to select; ignore. 314 if (target_col.children.count() == 0) return; 315 // keep the same row offset, clamped to the target column's last row. 316 const cur_id = cur_col.getFocus().child_id orelse return; 317 const cur = cur_col.children.getIndex(cur_id) orelse return; 318 const last = target_col.children.count() - 1; 319 const next = @min(cur, last); 320 root_focus.setFocus(target_col.children.keys()[next]); 321 if (target_col.children.values()[next].rect) |rect| target_scroll.scrollToRect(rect); 322 } 323 324 pub fn clearGrid(self: *View) void { 325 self.box.clearGrid(); 326 } 327 328 pub fn getGrid(self: View) ?Grid { 329 return self.box.getGrid(); 330 } 331 332 pub fn getFocus(self: *View) *Focus { 333 return self.box.getFocus(); 334 } 335 336 // the selected row's offset within the active column (0 = first row), so the 337 // parent knows when up should escape to the header. 338 pub fn getSelectedIndex(self: View) ?usize { 339 var copy = self; 340 const col = copy.activeColumn() orelse return null; 341 const cur_id = col.getFocus().child_id orelse return null; 342 return col.children.getIndex(cur_id); 343 } 344 }; 345 346 // the "a:" link to `kind`'s column paginated to `after` within `identity` 347 // ("owner/name"). the other column resets to its first window. 348 fn windowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, kind: ui.RoutablePage.RefKind, after: usize) ![]const u8 { 349 const route = ui.RoutablePage.repoRefsRoute(identity, kind, after) orelse return error.RouteTooLong; 350 const url = try route.urlAlloc(page_arena); 351 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 352 } 353 354 // the "a:" link to the files tab at ref `name` (a branch or tag) within 355 // `identity` ("owner/name"), at its root directory. the name is percent-encoded 356 // since it can contain a '/'. 357 fn refLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, kind: ui.RoutablePage.RefKind, name: []const u8) ![]const u8 { 358 const aa = page_arena.allocator(); 359 const ref_or_oid: ui.RoutablePage.RefOrOid = switch (kind) { 360 .branch => .branch, 361 .tag => .tag, 362 }; 363 const value = try ui.urlEncodeRef(aa, name); 364 const route = ui.RoutablePage.repoFilesRoute(identity, ref_or_oid, value, "", 0) orelse return error.RouteTooLong; 365 const url = try route.urlAlloc(page_arena); 366 return std.fmt.allocPrint(aa, "a:{s}", .{url}); 367 } 368