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