╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
488 var sel_after: usize = 0; 489 if (self.selectedEntry()) |entry| { 490 if (!entry.is_dir) { 491 sel_path = if (self.data.dir.len == 0) 492 entry.name 493 else 494 std.fmt.bufPrint(&buf, "{s}/{s}", .{ self.data.dir, entry.name }) catch self.data.dir; 495 sel_after = entry.window_start; 496 } 497 } 498 if (ui.RoutablePage.repoFilesRoute(self.data.identity, self.data.ref_or_oid, self.data.ref_or_oid_value, sel_path, sel_after)) |route| 499 self.session.data.current_page = route; 500 } 501 } 502 503 // the selected list row shows a border (the focused TextBox upgrades it 504 // to a double border itself); the rest stay borderless. 505 const lb = self.listBox(); 506 for (lb.children.keys(), lb.children.values()) |id, *child| { 507 switch (child.widget) { 508 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 509 else => {}, 510 } 511 } 512 513 // the content scroll's border is single normally and double when the 514 // content is focused (the content box itself is borderless). 515 const has_content = self.detailInner().children.count() > 0; 516 const content_focused = if (root_focus.grandchild_id) |g| self.detailInner().children.contains(g) else false; 517 self.detailScrollFrame().options.border_style = if (!has_content) null else if (content_focused) .double else .single; 518 519 // same for the "next" link above the content. 520 const nav = self.navBox(); 521 for (nav.children.keys(), nav.children.values()) |id, *child| { 522 switch (child.widget) { 523 .text_box => |*tb| tb.options.border_style = if (nav.getFocus().child_id == id) .single else .hidden, 524 else => {}, 525 } 526 } 527 528 // cap the list at list_max_width only while the detail pane fits beside 529 // it. the box drops the detail pane when the width can't hold both 530 // minimums, so when it's that narrow we lift the cap and let the list 531 // fill the whole width. 532 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 533 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 534 535 // stretch the detail pane and its bordered scroll frame across the rest 536 // of the width so the pane fills the area rather than shrinking to its 537 // content, like the refs view's columns. the content scroll inside keeps 538 // its natural width: forcing it to the full inner width would make it one 539 // column too wide whenever the vertical scrollbar appears, leaving a 540 // phantom horizontal scroll. (height fills via the scroll clipping to the 541 // viewport.) 542 if (constraint.max_size.width) |w| { 543 const detail_w = if (both_panes_fit) w - list_max_width else w; 544 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_w, .height = null }; 545 self.detailOuter().children.values()[detail_scroll_index].min_size = .{ .width = detail_w, .height = null }; 546 } else { 547 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 548 self.detailOuter().children.values()[detail_scroll_index].min_size = null; 549 } 550 551 // crossing panes only re-selects the content box's child (focusList / 552 // focusDetail); when the window is too narrow to show both, the pane that 553 // was holding focus is dropped here. the framework recovers focus after 554 // the top-level build by re-deriving it down the selected-child chain, so 555 // there's nothing to fix up afterward. 556 try self.box.build(allocator, constraint, root_focus); 557 } 558 559 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 560 const cur = self.selectedRowIndex(); 561 if (std.meta.eql(cur, self.shown_index)) return; 562 try self.populateDetail(allocator); 563 self.shown_index = cur; 564 } 565 566 fn populateDetail(self: *View, allocator: std.mem.Allocator) !void { 567 const nav = self.navBox(); 568 const inner = self.detailInner(); 569 570 for (nav.children.values()) |*child| child.widget.deinit(allocator); 571 nav.children.clearAndFree(allocator); 572 nav.getFocus().child_id = null; 573 for (inner.children.values()) |*child| child.widget.deinit(allocator); 574 inner.children.clearAndFree(allocator); 575 inner.getFocus().child_id = null; 576 577 // only files have contents to show; directories and the ".." row leave 578 // the pane empty. 579 if (self.selectedEntry()) |entry| { 580 if (!entry.is_dir) { 581 // the "next" window link sits above the scroll, so it stays put 582 // while the content scrolls. 583 if (entry.has_more) try self.addNavLink(allocator, nav, "next lines →", entry, entry.window_start + file_page); 584 if (entry.is_binary) { 585 try self.addContentBox(allocator, inner, "(binary file)"); 586 } else { 587 const text = try numberedContent(self.session.page_arena.allocator(), entry.lines, entry.window_start); 588 try self.addContentBox(allocator, inner, text); 589 } 590 } 591 } 592 // point each box at its first child so focus can descend into it. 593 if (nav.children.count() > 0) nav.getFocus().child_id = nav.children.keys()[0]; 594 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 595 596 // reset the scroll to the top for the newly-shown file: directly on the 597 // terminal (the wasm offset), and via a version bump on the web (so the 598 // renderer's scroll id changes and JS drops the preserved position). 599 const sc = self.detailScroll(); 600 sc.x = 0; 601 sc.y = 0; 602 sc.getFocus().version +%= 1; 603 } 604 605 // a focusable "next" link above the content. it's an `a:` link to the 606 // selected file at `target_after`, so activating it (the host follows the 607 // link) reloads the page on the next window. 608 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, entry: Entry, target_after: usize) !void { 609 const page_arena = self.session.page_arena; 610 const path = try childDir(page_arena.allocator(), self.data.dir, entry.name); 611 const route = ui.RoutablePage.repoFilesRoute(self.data.identity, self.data.ref_or_oid, self.data.ref_or_oid_value, path, target_after) orelse return error.RouteTooLong; 612 const link = try std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.urlAlloc(page_arena)}); 613 var tb = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 614 errdefer tb.deinit(allocator); 615 tb.getFocus().focusable = true; 616 tb.getFocus().kind = .{ .custom = link }; 617 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 618 } 619 620 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 621 _ = allocator; 622 if (self.detailActive()) { 623 try self.detailInput(key, root_focus); 624 } else { 625 try self.listInput(key, root_focus); 626 } 627 } 628 629 fn listInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 630 // up/down (and the scroll wheel) move the selection; page up/down jump a 631 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 632 // directory (or "..") row are turned into navigation by the host 633 // (crossPageLink) before reaching here. 634 switch (key) { 635 .enter, .arrow_right => try self.focusDetail(root_focus), 636 .arrow_down => try self.moveSelection(root_focus, 1), 637 .arrow_up => try self.moveSelection(root_focus, -1), 638 .page_down => try self.moveSelection(root_focus, 10), 639 .page_up => try self.moveSelection(root_focus, -10), 640 .end => try self.moveSelection(root_focus, @intCast(self.listBox().children.count())), 641 .home => try self.moveSelection(root_focus, -@as(isize, @intCast(self.listBox().children.count()))), 642 .mouse => |mouse| switch (mouse.action) { 643 .scroll => |dir| try self.moveSelection(root_focus, if (dir == .up) -1 else 1), 644 else => {}, 645 }, 646 else => {}, 647 } 648 } 649 650 // whether focus is on the content box (vs. a "previous"/"next" nav link). 651 fn focusOnContent(self: *View, root_focus: *Focus) bool { 652 const g = root_focus.grandchild_id orelse return false; 653 return self.detailInner().children.contains(g); 654 } 655 656 fn detailInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 657 const sc = self.detailScroll(); 658 const on_content = self.focusOnContent(root_focus); 659 // the "next" link sits above the content scroll. on the link, left returns 660 // to the file list and down drops into the content. in the content, the 661 // terminal scrolls by offset and reaching the top then pressing up crosses 662 // back up to the link; on the web vertical scrolling is the browser's job 663 // so up just crosses to the link. 664 switch (key) { 665 .arrow_left => { 666 if (on_content) { 667 if (sc.x > 0) { 668 sc.x -= 1; 669 self.clampDetailScroll(); 670 } else try self.focusList(root_focus); 671 } else if (!self.moveNav(root_focus, -1)) { 672 try self.focusList(root_focus); 673 } 674 }, 675 .arrow_right => { 676 if (on_content) { 677 sc.x += 1; 678 self.clampDetailScroll(); 679 } else _ = self.moveNav(root_focus, 1); 680 }, 681 .arrow_up => { 682 if (on_content) { 683 if (self.session.is_terminal and sc.y > 0) { 684 sc.y -= 1; 685 self.clampDetailScroll(); 686 } else self.focusNav(root_focus); // cross up to the links 687 } 688 }, 689 .arrow_down => { 690 if (on_content) { 691 if (self.session.is_terminal) { 692 sc.y += 1; 693 self.clampDetailScroll(); 694 } 695 } else self.focusContent(root_focus); // links -> content 696 }, 697 .page_up => if (on_content and self.session.is_terminal) { 698 sc.y -= 10; 699 self.clampDetailScroll(); 700 }, 701 .page_down => if (on_content and self.session.is_terminal) { 702 sc.y += 10; 703 self.clampDetailScroll(); 704 }, 705 .home => if (on_content and self.session.is_terminal) { 706 sc.y = 0; 707 self.clampDetailScroll(); 708 }, 709 .end => if (on_content and self.session.is_terminal) { 710 sc.y = std.math.maxInt(isize); 711 self.clampDetailScroll(); 712 }, 713 .mouse => |mouse| switch (mouse.action) { 714 .scroll => |dir| if (on_content and self.session.is_terminal) { 715 sc.y += if (dir == .up) -5 else 5; 716 self.clampDetailScroll(); 717 }, 718 else => {}, 719 }, 720 else => {}, 721 } 722 } 723 724 // focus the first (left-most) nav link; a no-op when there are none, leaving 725 // focus on the content. 726 fn focusNav(self: *View, root_focus: *Focus) void { 727 const nav = self.navBox(); 728 if (nav.children.count() == 0) return; 729 root_focus.setFocus(nav.children.keys()[0]); 730 } 731 732 fn focusContent(self: *View, root_focus: *Focus) void { 733 const inner = self.detailInner(); 734 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 735 root_focus.setFocus(id); 736 } 737 738 // move focus `delta` rows within the nav box; returns false (without moving) 739 // when that would step off either end. 740 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 741 const nav = self.navBox(); 742 const keys = nav.children.keys(); 743 const cur_id = nav.getFocus().child_id orelse return false; 744 const cur: isize = @intCast(nav.children.getIndex(cur_id) orelse return false); 745 const target = cur + delta; 746 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 747 root_focus.setFocus(keys[@intCast(target)]); 748 return true; 749 } 750 751 // keep the detail scroll within its content, using the last build's grids. 752 // the scroll bar's reserved column/row isn't part of the content viewport, 753 // so exclude it or the last column/row stays unreachable. 754 fn clampDetailScroll(self: *View) void { 755 const sc = self.detailScroll(); 756 const vp = sc.grid orelse return; 757 const content = sc.child.box.grid orelse return; 758 const view_w = vp.size.width - sc.bar_w; 759 const view_h = vp.size.height - sc.bar_h; 760 const max_y: isize = if (content.size.height > view_h) @intCast(content.size.height - view_h) else 0; 761 const max_x: isize = if (content.size.width > view_w) @intCast(content.size.width - view_w) else 0; 762 sc.y = std.math.clamp(sc.y, 0, max_y); 763 sc.x = std.math.clamp(sc.x, 0, max_x); 764 } 765 766 // enter the detail pane, landing on the "next" link when there is one (so 767 // right-arrow reaches it), else on the content. a directory or ".." row has 768 // neither, so there's nothing to enter. selecting the frame's child (then 769 // focusing the frame) also handles the too-narrow case where the pane isn't 770 // laid out yet — focus lands inside it after the next build. 771 fn focusDetail(self: *View, root_focus: *Focus) !void { 772 const frame = self.detailOuter(); 773 if (self.navBox().children.count() > 0) { 774 frame.getFocus().child_id = self.navBox().getFocus().id; 775 } else if (self.detailInner().children.count() > 0) { 776 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 777 } else return; 778 root_focus.setFocus(frame.getFocus().id); 779 } 780 781 // return to the list. 782 fn focusList(self: *View, root_focus: *Focus) !void { 783 root_focus.setFocus(self.listScroll().getFocus().id); 784 } 785 786 fn moveSelection(self: *View, root_focus: *Focus, delta: isize) !void { 787 const lb = self.listBox(); 788 const keys = lb.children.keys(); 789 if (keys.len == 0) return; 790 const cur_id = lb.getFocus().child_id orelse return; 791 const cur: isize = @intCast(lb.children.getIndex(cur_id) orelse return); 792 const last: isize = @intCast(keys.len - 1); 793 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 794 if (next == @as(usize, @intCast(cur))) return; 795 root_focus.setFocus(keys[next]); 796 if (lb.children.values()[next].rect) |rect| self.listScroll().scrollToRect(rect); 797 } 798 799 pub fn clearGrid(self: *View) void { 800 self.box.clearGrid(); 801 } 802 803 pub fn getGrid(self: View) ?Grid { 804 return self.box.getGrid(); 805 } 806 807 pub fn getFocus(self: *View) *Focus { 808 return self.box.getFocus(); 809 } 810 811 // for the parent's "scroll up at the top jumps to the header" check. inside 812 // the detail pane up-arrow stays in the pane (scrolling); when the list holds 813 // focus, report its selection directly so up-arrow at the top row ascends. 814 pub fn getSelectedIndex(self: *View) ?usize { 815 if (self.detailActive()) return 1; 816 const lb = self.listBox(); 817 const cid = lb.getFocus().child_id orelse return null; 818 return lb.children.getIndex(cid); 819 } 820 }; 821 822 // join `lines` into one string, each prefixed with its 1-based file line number 823 // (right-aligned in a column wide enough for the last number, then a space), 824 // like the diffs view. `start` is the window's 0-based first line, so numbers 825 // continue across paginated windows. 826 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 827 if (lines.len == 0) return ""; 828 const width = std.fmt.count("{d}", .{start + lines.len}); 829 const indent = try arena.alloc(u8, width); 830 @memset(indent, ' '); 831 832 var out: std.ArrayList(u8) = .empty; 833 for (lines, 0..) |line, i| { 834 if (i != 0) try out.append(arena, '\n'); 835 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 836 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 837 try out.appendSlice(arena, num_str); 838 try out.append(arena, ' '); 839 try out.appendSlice(arena, line); 840 } 841 return out.toOwnedSlice(arena); 842 } 843 844 // the index of the "README"/"README.md" file entry (case-insensitive), or null 845 // if none. used to pick the default selection at the repo root. 846 fn readmeIndex(entries: []const Entry) ?usize { 847 for (entries, 0..) |entry, i| { 848 if (entry.is_dir) continue; 849 if (std.ascii.eqlIgnoreCase(entry.name, "README") or std.ascii.eqlIgnoreCase(entry.name, "README.md")) 850 return i; 851 } 852 return null; 853 } 854 855 // the index of the entry the route named (data.selected_file), or null if none. 856 fn selectedFileIndex(data: *const Self) ?usize { 857 const name = data.selected_file orelse return null; 858 for (data.entries, 0..) |entry, i| { 859 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 860 } 861 return null; 862 } 863 864 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 865 // following it navigates to that directory's listing. 866 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 867 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 868 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.urlAlloc(page_arena)}); 869 } 870 871 // an "ai:" link to the file's route: crossPageLink ignores it, so a wasm click 872 // selects the row in place and shows its contents in the detail pane. 873 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 874 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 875 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.urlAlloc(page_arena)}); 876 } 877 878 // `dir`/`name`, or just `name` at the root. 879 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 880 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 881 } 882 883 // `dir` with its last segment removed ("" when `dir` has a single segment). 884 fn parentDir(dir: []const u8) []const u8 { 885 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 886 return dir[0..slash]; 887 } 888