╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Refs.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
276 } 277 278 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 279 _ = allocator; 280 // up/down (and the scroll wheel) move within the active column a row; 281 // page up/down and home/end jump; left/right switch columns, keeping the 282 // same row offset. the parent (Repo) intercepts up at the top row to move 283 // focus to the header. 284 if (inp.rowDelta(key, self.columnRowCount())) |delta| { 285 const scroll = self.activeScroll() orelse return; 286 ui.moveRowFocus(&scroll.child.box, scroll, root_focus, delta); 287 return; 288 } 289 switch (key) { 290 .arrow_left => try self.switchColumn(root_focus, left_col), 291 .arrow_right => try self.switchColumn(root_focus, right_col), 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 switchColumn(self: *View, root_focus: *Focus, target: usize) !void { 313 if (target >= self.box.children.count()) return; 314 const cur_col = self.activeColumn() orelse return; 315 const target_scroll = columnScroll(&self.box.children.values()[target].widget.box); 316 const target_col = &target_scroll.child.box; 317 // an empty column has nothing to select; ignore. 318 if (target_col.children.count() == 0) return; 319 // keep the same row offset, clamped to the target column's last row. 320 const cur_id = cur_col.getFocus().child_id orelse return; 321 const cur = cur_col.children.getIndex(cur_id) orelse return; 322 const last = target_col.children.count() - 1; 323 const next = @min(cur, last); 324 root_focus.setFocus(target_col.children.keys()[next]); 325 if (target_col.children.values()[next].rect) |rect| target_scroll.scrollToRect(rect); 326 } 327 328 pub fn clearGrid(self: *View) void { 329 self.box.clearGrid(); 330 } 331 332 pub fn getGrid(self: View) ?Grid { 333 return self.box.getGrid(); 334 } 335 336 pub fn getFocus(self: *View) *Focus { 337 return self.box.getFocus(); 338 } 339 340 // the selected row's offset within the active column (0 = first row), so the 341 // parent knows when up should escape to the header. 342 pub fn getSelectedIndex(self: View) ?usize { 343 var copy = self; 344 const col = copy.activeColumn() orelse return null; 345 const cur_id = col.getFocus().child_id orelse return null; 346 return col.children.getIndex(cur_id); 347 } 348 }; 349 350 // the "a:" link to `kind`'s column windowed from ref `name` (raw; "" = the 351 // first window) within `identity` ("owner/name"). the other column resets to 352 // its first window. 353 fn windowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, kind: ui.RoutablePage.RefKind, name: []const u8) ![]const u8 { 354 const encoded = try ui.urlEncodeRef(page_arena.allocator(), name); 355 const route = ui.RoutablePage.repoRefsRoute(identity, kind, encoded) orelse return error.RouteTooLong; 356 const url = try route.toUrl(page_arena); 357 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 358 } 359 360 // the "a:" link to the files tab at ref `name` (a branch or tag) within 361 // `identity` ("owner/name"), at its root directory. the name is percent-encoded 362 // since it can contain a '/'. 363 fn refLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, kind: ui.RoutablePage.RefKind, name: []const u8) ![]const u8 { 364 const aa = page_arena.allocator(); 365 const ref_or_oid: ui.RoutablePage.RefOrOid = switch (kind) { 366 .branch => .branch, 367 .tag => .tag, 368 }; 369 const value = try ui.urlEncodeRef(aa, name); 370 const route = ui.RoutablePage.repoFilesRoute(identity, ref_or_oid, value, "", 0) orelse return error.RouteTooLong; 371 const url = try route.toUrl(page_arena); 372 return std.fmt.allocPrint(aa, "a:{s}", .{url}); 373 } 374