╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Refs.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
310 const count = flow.text_boxes.items.len; 311 const cur_y = flow.rects.items[cur].y; 312 switch (inp.vertDirection(key)) { 313 .up => { 314 if (flow.rowStep(cur, false)) |i| self.focusName(index, i, root_focus) else _ = self.focusHeader(root_focus); 315 return; 316 }, 317 .down => { 318 if (flow.rowStep(cur, true)) |i| self.focusName(index, i, root_focus); 319 return; 320 }, 321 .none => {}, 322 } 323 switch (key) { 324 .arrow_left => if (index == right_col and rowStart(flow, cur)) { 325 self.crossTo(left_col, cur_y, root_focus); 326 } else if (cur > 0) self.focusName(index, cur - 1, root_focus), 327 .arrow_right => if (index == left_col and rowEnd(flow, cur)) { 328 self.crossTo(right_col, cur_y, root_focus); 329 } else if (cur + 1 < count) self.focusName(index, cur + 1, root_focus), 330 .home => self.focusName(index, 0, root_focus), 331 .end => self.focusName(index, count - 1, root_focus), 332 else => {}, 333 } 334 } 335 336 // both lists narrowed to `text`, each from its first window. an empty box 337 // on an unsearched page has nothing to clear. 338 fn submit(self: *View, text: []const u8) !void { 339 if (text.len == 0 and self.data.search == null) return; 340 const route = ui.RoutablePage.repoRefsRoute(self.data.identity, .branch, "") orelse return; 341 try self.session.navigate(route.withSearch(text) orelse return); 342 } 343 344 fn searchBox(self: *View) *ui.widget.SearchBox { 345 return &self.box.children.values()[header_index].widget.box.children.values()[0].widget.search_box; 346 } 347 348 fn headerActive(self: *View) bool { 349 return self.box.getFocus().child_id == self.box.children.keys()[header_index]; 350 } 351 352 fn columns(self: *View) *wgt.Box(ui.Widget) { 353 return &self.box.children.values()[columns_index].widget.box; 354 } 355 356 fn activeIndex(self: *View) ?usize { 357 const cols = self.columns(); 358 const id = cols.getFocus().child_id orelse return null; 359 return cols.children.getIndex(id); 360 } 361 362 // [0] = the fixed label, [1] = the scrolling flow 363 fn scrollAt(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 364 return &self.columns().children.values()[index].widget.box.children.values()[1].widget.scroll; 365 } 366 367 fn flowAt(self: *View, index: usize) *ui.widget.WordFlow { 368 return &self.scrollAt(index).child.word_flow; 369 } 370 371 fn rowStart(flow: *ui.widget.WordFlow, item: usize) bool { 372 const rects = flow.rects.items; 373 return item == 0 or rects[item - 1].y != rects[item].y; 374 } 375 376 fn rowEnd(flow: *ui.widget.WordFlow, item: usize) bool { 377 const rects = flow.rects.items; 378 return item + 1 == rects.len or rects[item + 1].y != rects[item].y; 379 } 380 381 // land on the other column's row at or above the current one: its first 382 // name when moving right, its last when moving left. names are laid out in 383 // order, so that row ends at the last name no lower than the current one. 384 fn crossTo(self: *View, target: usize, from_y: isize, root_focus: *Focus) void { 385 // the other column may have no names at all 386 const rects = self.flowAt(target).rects.items; 387 if (rects.len == 0) return; 388 var last: usize = 0; 389 for (rects, 0..) |rect, i| { 390 if (rect.y <= from_y) last = i; 391 } 392 var first = last; 393 while (first > 0 and rects[first - 1].y == rects[last].y) first -= 1; 394 self.focusName(target, if (target == right_col) first else last, root_focus); 395 } 396 397 fn focusName(self: *View, index: usize, item: usize, root_focus: *Focus) void { 398 const flow = self.flowAt(index); 399 root_focus.setFocus(flow.text_boxes.items[item].getFocus().id); 400 // the browser scrolls natively; the terminal brings the name into view 401 if (self.session.is_terminal and item < flow.rects.items.len) self.scrollAt(index).scrollToRect(flow.rects.items[item]); 402 } 403 404 pub fn clearGrid(self: *View) void { 405 self.box.clearGrid(); 406 } 407 408 pub fn getGrid(self: View) ?Grid { 409 return self.box.getGrid(); 410 } 411 412 pub fn getFocus(self: *View) *Focus { 413 return self.box.getFocus(); 414 } 415 416 // up leaves the tab from the search box 417 pub fn atTop(self: *View) bool { 418 return self.headerActive(); 419 } 420 421 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 422 root_focus.setFocus(self.searchBox().getFocus().id); 423 return true; 424 } 425 }; 426 427 // the "a:" link to `kind`'s column windowed from ref `name` (raw; "" = the 428 // first window) within `identity` ("owner:name"). the other column resets to 429 // its first window. 430 fn windowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, kind: ui.RoutablePage.RefKind, name: []const u8, search: []const u8) ![]const u8 { 431 const encoded = try ui.urlEncodeRef(page_arena.allocator(), name); 432 const window = ui.RoutablePage.repoRefsRoute(identity, kind, encoded) orelse return error.RouteTooLong; 433 const route = window.withSearch(search) orelse return error.RouteTooLong; 434 const url = try route.toUrl(page_arena); 435 return page_arena.allocator().print("a:{s}", .{url}); 436 } 437 438 // the "a:" link to the files tab at ref `name` (a branch or tag) within 439 // `identity` ("owner:name"), at its root directory. the name is percent-encoded 440 // since it can contain a '/'. 441 fn refLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, kind: ui.RoutablePage.RefKind, name: []const u8) ![]const u8 { 442 const aa = page_arena.allocator(); 443 const ref_or_oid: ui.RoutablePage.RefOrOid = switch (kind) { 444 .branch => .branch, 445 .tag => .tag, 446 }; 447 const value = try ui.urlEncodeRef(aa, name); 448 const route = ui.RoutablePage.repoFilesRoute(identity, ref_or_oid, value, "", 0) orelse return error.RouteTooLong; 449 const url = try route.toUrl(page_arena); 450 return aa.print("a:{s}", .{url}); 451 } 452