╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2670 terminal_live = true; 2671 term.setActive(&terminal); 2672 return show_err; 2673 }; 2674 terminal = try term.Terminal.init(io, allocator); 2675 terminal.render_state.no_color = !session.color; 2676 terminal_live = true; 2677 term.setActive(&terminal); 2678 }, 2679 .undo => |target| Repo.Undo.handleRequest(allocator, session, target), 2680 .sync_events => { 2681 try nav.root.build(allocator, .{ 2682 .min_size = .{ .width = null, .height = null }, 2683 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2684 }, nav.root.getFocus()); 2685 _ = try terminal.render(&nav.root); 2686 try Repo.Events.performSync(allocator, session); 2687 }, 2688 } 2689 } 2690 2691 // reload so the next navigation builds its page from the current moment 2692 if (repo_maybe) |repo| try session.reloadMoment(allocator, repo); 2693 2694 // reconcile navigation: forward to a new page, or back on escape. 2695 // the terminal background can change at any time, so recheck it on each page 2696 if (try nav.sync(allocator, session)) try terminal.queryBackground(); 2697 2698 // the quit button (on the quit tab) asks the host to tear down. 2699 if (session.quit_requested) terminal.requestQuit(); 2700 2701 try nav.root.build(allocator, .{ 2702 .min_size = .{ .width = null, .height = null }, 2703 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2704 }, nav.root.getFocus()); 2705 } 2706 } 2707 2708 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2709 const tty: ?std.Io.File = if (builtin.target.os.tag == .windows) 2710 null 2711 else 2712 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2713 defer if (tty) |file| file.close(io); 2714 2715 const input = if (tty) |file| file else std.Io.File.stdin(); 2716 const output = if (tty) |file| file else std.Io.File.stdout(); 2717 2718 var write_buf: [1024]u8 = undefined; 2719 var writer = output.writer(io, &write_buf); 2720 try writer.interface.print("\x1b[2J\x1b[Hcopy the following text and then press enter to go back:\r\n\r\n{s}\r\n", .{copyable_text}); 2721 try writer.interface.flush(); 2722 2723 var read_buf: [1]u8 = undefined; 2724 var reader = input.reader(io, &read_buf); 2725 while (true) { 2726 const byte = try reader.interface.takeByte(); 2727 if (byte == '\r' or byte == '\n') return; 2728 } 2729 } 2730 2731 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2732 const root_focus = root.getFocus(); 2733 const focused_before = root_focus.grandchild_id; 2734 var activate_in_page_link = false; 2735 switch (key) { 2736 // request a navigation pop; the host's Nav.sync goes back a page, or 2737 // quits when there's no history left. 2738 .escape => { 2739 session.back = .requested; 2740 return; 2741 }, 2742 .ctrl => |letter| switch (letter) { 2743 'r' => { 2744 session.refresh_requested = true; 2745 return; 2746 }, 2747 else => try root.input(allocator, key, root_focus), 2748 }, 2749 .enter => { 2750 if (root_focus.grandchild_id) |gid| { 2751 // follow a cross-page link 2752 if (crossPageLink(root_focus, gid, session.data)) |route| { 2753 return session.navigate(route); 2754 } 2755 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2756 } 2757 activate_in_page_link = true; 2758 try root.input(allocator, key, root_focus); 2759 }, 2760 .mouse => |mouse| { 2761 if (mouse.button == .left) { 2762 if (cellPageLink(root, mouse.x, mouse.y, session.data)) |route| return session.navigate(route); 2763 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2764 const focus_id = hit.id; 2765 if (hit.kind == .background) { 2766 root_focus.setFocus(focus_id); 2767 return; 2768 } 2769 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2770 if (session.back == .available) session.back = .requested; 2771 return; 2772 } 2773 // follow a cross-page link 2774 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2775 return session.navigate(route); 2776 } 2777 if (currentPageLink(root_focus, focus_id, session.data)) return; 2778 if (rawLink(root_focus, focus_id)) |url| { 2779 if (!mouse.ctrl) try requestRawLink(session, url); 2780 return; 2781 } 2782 root_focus.setFocus(focus_id); 2783 activate_in_page_link = true; 2784 } 2785 // forward the press into the widget tree so buttons (and any 2786 // future click-aware widgets) can react. widgets that don't 2787 // care about presses ignore it. 2788 try root.input(allocator, key, root_focus); 2789 } else { 2790 try root.input(allocator, key, root_focus); 2791 } 2792 }, 2793 .event => |event| switch (event) { 2794 .background => |rgb| session.terminal_background = rgb, 2795 .resize => try root.input(allocator, key, root_focus), 2796 }, 2797 // a scroll tick goes to the pane under the pointer, focusing it first. 2798 // from outside any pane (the tabs, a sub-header) or from a text input 2799 // it moves like the arrows instead, so scrolling up from a search box 2800 // reaches the header and stops there. 2801 .scroll_up, .scroll_down => |position| { 2802 if (focusedPane(root_focus)) |pane| if (scrollUnder(root_focus, position)) |under| { 2803 if (under != pane) root_focus.setFocus(under); 2804 }; 2805 try root.input(allocator, key, root_focus); 2806 }, 2807 else => try root.input(allocator, key, root_focus), 2808 } 2809 2810 const focused_after = root_focus.grandchild_id; 2811 if (!activate_in_page_link and focused_before == focused_after) return; 2812 const focus_id = focused_after orelse return; 2813 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2814 } 2815 2816 // the innermost vertically scrolling widget laid out under `position` 2817 fn scrollUnder(root_focus: *const Focus, position: xitui.input.Position) ?usize { 2818 var found: ?usize = null; 2819 var found_area: usize = std.math.maxInt(usize); 2820 var iter = root_focus.children.iterator(); 2821 while (iter.next()) |entry| { 2822 const info = entry.value_ptr.focus.scroll orelse continue; 2823 if (info.direction == .horiz) continue; 2824 const r = entry.value_ptr.rect; 2825 if (position.x < r.x or position.y < r.y or position.x >= r.x + r.size.width or position.y >= r.y + r.size.height) continue; 2826 // a nested scroll lies within its parent, so the smallest is innermost 2827 const area = r.size.width * r.size.height; 2828 if (area < found_area) { 2829 found = entry.key_ptr.*; 2830 found_area = area; 2831 } 2832 } 2833 return found; 2834 } 2835 2836 // the innermost vertically scrolling widget the focus lies in, unless the 2837 // focused widget is a text input 2838 fn focusedPane(root_focus: *const Focus) ?usize { 2839 var current = root_focus.grandchild_id orelse return null; 2840 switch ((root_focus.children.get(current) orelse return null).focus.kind) { 2841 .text_input, .text_input_password, .text_area => return null, 2842 else => {}, 2843 } 2844 while (root_focus.children.get(current)) |child| { 2845 if (child.focus.scroll) |info| if (info.direction != .horiz) return current; 2846 current = child.parent_id; 2847 } 2848 return null; 2849 } 2850 2851 // a link to bytes the server serves directly rather than to a page route, so 2852 // it never resolves to a RoutablePage 2853 pub const raw_link_prefix = "ax:"; 2854 2855 // a route already loaded into the current page; focusing it changes the url 2856 // without rebuilding the page 2857 pub const in_page_link_prefix = "ai:"; 2858 2859 // an in-page tab's base route, or the exact current route when it is selected 2860 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2861 const target = if (selected) session.data.current_page else route; 2862 return session.page_arena.allocator().print("{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2863 } 2864 2865 // a button the web renderer covers with a file picker that posts the chosen 2866 // file to the url after the prefix 2867 pub const file_input_prefix = "file:"; 2868 2869 // a submit button that overrides its enclosing form's action 2870 pub const submit_action_prefix = "submit:"; 2871 2872 // one option of a radio field, as `radio:<name>=<value>` 2873 pub const radio_prefix = "radio:"; 2874 2875 // the url an `ax:` link points at, or null for anything else. a host that 2876 // suppresses the anchor's own navigation needs this to follow the link. 2877 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2878 const child = root_focus.children.get(focus_id) orelse return null; 2879 const custom = switch (child.focus.kind) { 2880 .custom => |c| c, 2881 else => return null, 2882 }; 2883 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2884 return custom[raw_link_prefix.len..]; 2885 } 2886 2887 fn requestRawLink(session: *Session, url: []const u8) !void { 2888 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2889 } 2890 2891 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2892 // an absolute url is already complete 2893 if (!std.mem.startsWith(u8, path, "/")) return path; 2894 return if (session.web_port) |port| 2895 try allocator.print("http://localhost:{d}{s}", .{ port, path }) 2896 else 2897 path; 2898 } 2899 2900 // if the focus target at focus_id is an `a:` link that should navigate (a 2901 // different parent page, or a different files directory within the repo page), 2902 // return its route; otherwise null. lets a host turn a click / enter on such a 2903 // link into a navigation rather than a focus change. 2904 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2905 const current = data.current_page; 2906 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2907 // a link to a different parent page always navigates; within a page, a 2908 // files-directory / commits-page / list-window change navigates while tab 2909 // links stay in-page. 2910 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.forkPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 2911 return null; 2912 } 2913 2914 // the page the clicked cell links to, from a link inside a text box's content 2915 fn cellPageLink(root: *Widget, x: usize, y: usize, data: Session.Data) ?RoutablePage { 2916 const grid = root.getGrid() orelse return null; 2917 const cell = grid.cell(x, y) catch return null; 2918 const link = cell.link orelse return null; 2919 if (link.len == 0 or link[0] != '/') return null; 2920 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(link) else RoutablePage.fromUrl(link); 2921 } 2922 2923 // whether the `a:` focus target links to the page already shown, which a click 2924 // leaves alone rather than focusing 2925 pub fn currentPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) bool { 2926 const route = pageLink(root_focus, focus_id, data, "a:") orelse return false; 2927 return route.eql(data.current_page); 2928 } 2929 2930 // the route carried by an `ai:` focus target, or null for any other target 2931 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2932 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2933 } 2934 2935 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2936 const child = root_focus.children.get(focus_id) orelse return null; 2937 const custom = switch (child.focus.kind) { 2938 .custom => |c| c, 2939 else => return null, 2940 }; 2941 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2942 const path = custom[prefix.len..]; 2943 // local sessions build (and parse) links with the repo identity elided 2944 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2945 } 2946 2947 // an assignee as the detail view shows it: the stored email, and the name of 2948 // the user holding it on this instance when there is one 2949 pub const Assignee = struct { 2950 email: []const u8, 2951 name: ?[]const u8 = null, 2952 2953 pub fn display(self: Assignee) []const u8 { 2954 return self.name orelse self.email; 2955 } 2956 }; 2957 2958 // a display author, resolved against the admin db at read time 2959 pub const Author = union(enum) { 2960 unknown, 2961 email: []const u8, // an email no user matches 2962 user_name: []const u8, 2963 2964 // the commit author line's email, resolved like initFromEmail 2965 pub fn init( 2966 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2967 arena: *std.heap.ArenaAllocator, 2968 author_line: []const u8, 2969 ) !Author { 2970 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2971 } 2972 2973 // from a record's stored author email: it resolves to the user currently 2974 // holding it, else stays the email. the result is allocated in `arena`, 2975 // so the email may borrow transient memory. 2976 pub fn initFromEmail( 2977 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2978 arena: *std.heap.ArenaAllocator, 2979 author_email: ?[]const u8, 2980 ) !Author { 2981 const email = author_email orelse return .unknown; 2982 if (haxy_moment) |moment| { 2983 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2984 return .{ .user_name = user.name }; 2985 } 2986 } 2987 return .{ .email = try arena.allocator().dupe(u8, email) }; 2988 } 2989 }; 2990 2991 // the "a:" link to user `name`'s page. 2992 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2993 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2994 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2995 return page_arena.allocator().print("a:{s}", .{url}); 2996 } 2997 2998 // a focusable " author " box showing `author`, linking to their user page 2999 // when it is a known user 3000 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 3001 const text = switch (author) { 3002 .unknown => "", 3003 .email, .user_name => |t| t, 3004 }; 3005 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " author " } }); 3006 errdefer tb.deinit(allocator); 3007 tb.getFocus().mode = .all; 3008 switch (author) { 3009 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 3010 else => {}, 3011 } 3012 return tb; 3013 } 3014 3015 // native-TUI navigation 3016 pub const Nav = struct { 3017 root: Widget, 3018 // backs the current page (its Page data and the page-scoped strings its 3019 // widgets build). owned here, swapped on each navigation. session.page_arena 3020 // tracks whichever of these is current so widgets allocate into it. 3021 arena: *std.heap.ArenaAllocator, 3022 // the login the current root was built for 3023 user_id: ?[evt.event_id_size]u8, 3024 history: std.ArrayList(Entry), 3025 3026 // each retained page keeps its own arena, freed when the page leaves the 3027 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 3028 // long-lived session from accumulating every page it ever visited. 3029 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 3030 3031 // cap on retained back-history so the chain can't grow memory without bound 3032 const max_history: usize = 16; 3033 3034 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 3035 session.back = .unavailable; 3036 const arena = try allocator.create(std.heap.ArenaAllocator); 3037 arena.* = std.heap.ArenaAllocator.init(allocator); 3038 errdefer { 3039 arena.deinit(); 3040 allocator.destroy(arena); 3041 } 3042 3043 return .{ 3044 .root = try initCurrentRoot(allocator, arena, session), 3045 .arena = arena, 3046 .user_id = session.userId(), 3047 .history = .empty, 3048 }; 3049 } 3050 3051 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 3052 self.root.deinit(allocator); 3053 freeArena(allocator, self.arena); 3054 for (self.history.items) |*entry| { 3055 entry.root.deinit(allocator); 3056 freeArena(allocator, entry.arena); 3057 } 3058 self.history.deinit(allocator); 3059 } 3060 3061 // rebuild the current page in place from a current moment 3062 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 3063 if (session.haxy_moment == null and session.local == null) return; 3064 const arena = try allocator.create(std.heap.ArenaAllocator); 3065 arena.* = std.heap.ArenaAllocator.init(allocator); 3066 errdefer freeArena(allocator, arena); 3067 3068 const new_root = try initCurrentRoot(allocator, arena, session); 3069 3070 self.root.deinit(allocator); 3071 freeArena(allocator, self.arena); 3072 self.root = new_root; 3073 self.arena = arena; 3074 self.user_id = session.userId(); 3075 } 3076 3077 // build the current page's root in `arena`, or the not-found page's when 3078 // the route names nothing 3079 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 3080 session.page_arena = arena; 3081 const page = try arena.allocator().create(Page); 3082 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 3083 error.NotFound => blk: { 3084 session.data.current_page = .not_found; 3085 break :blk .not_found; 3086 }, 3087 else => |e| return e, 3088 }; 3089 return initRoot(allocator, page, session); 3090 } 3091 3092 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3093 arena.deinit(); 3094 allocator.destroy(arena); 3095 } 3096 3097 // reconcile the displayed root with the session's navigation state. a 3098 // forward nav (current_page moved to a different parent page) pushes the 3099 // current root and builds the new page in a fresh arena; a back request 3100 // frees the current page and restores the previous one. when escape is 3101 // pressed with no history left we switch to the quit tab instead of quitting. 3102 // returns whether the root was replaced. 3103 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3104 // refresh: rebuild the current page in place from a current moment 3105 if (session.refresh_requested) { 3106 session.refresh_requested = false; 3107 try self.rebuild(allocator, session); 3108 return true; 3109 } 3110 3111 if (session.back == .requested) { 3112 if (self.history.pop()) |entry| { 3113 session.back = if (self.history.items.len == 0) .unavailable else .available; 3114 self.root.deinit(allocator); 3115 freeArena(allocator, self.arena); 3116 self.root = entry.root; 3117 self.arena = entry.arena; 3118 self.user_id = entry.user_id; 3119 session.page_arena = entry.arena; 3120 session.data.current_page = entry.route; 3121 // a page built under another login is rebuilt rather than restored 3122 if (!std.meta.eql(entry.user_id, session.userId())) { 3123 try self.rebuild(allocator, session); 3124 return true; 3125 } 3126 chooseAnsiArtForNavigation(session); 3127 return true; 3128 } 3129 session.back = .unavailable; 3130 // nothing to go back to; switch to the quit confirmation 3131 const root_focus = self.root.getFocus(); 3132 var iter = root_focus.children.iterator(); 3133 while (iter.next()) |entry| { 3134 switch (entry.value_ptr.focus.kind) { 3135 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3136 root_focus.setFocus(entry.key_ptr.*); 3137 // a Stack only builds its selected child, so the quit 3138 // button isn't in the focus tree until a build runs with 3139 // the quit tab selected. build once, then send a 3140 // synthetic arrow_down to drop focus from the tab onto 3141 // the button so it's ready to confirm. 3142 try self.root.build(allocator, .{ 3143 .min_size = .{ .width = null, .height = 40 }, 3144 .max_size = .{ .width = 80, .height = null }, 3145 }, root_focus); 3146 try self.root.input(allocator, .arrow_down, root_focus); 3147 break; 3148 }, 3149 else => {}, 3150 } 3151 } 3152 return false; 3153 } 3154 3155 // forward navigation: navigate() set next_page to the page to move to (a 3156 // cross-page link or tab change crossing pages) 3157 if (session.next_page) |route| { 3158 session.next_page = null; 3159 if (session.haxy_moment == null and session.local == null) return false; 3160 const previous_route = session.data.current_page; 3161 // the page we navigated to becomes the current page 3162 session.data.current_page = route; 3163 session.back = .available; 3164 3165 const arena = try allocator.create(std.heap.ArenaAllocator); 3166 arena.* = std.heap.ArenaAllocator.init(allocator); 3167 errdefer freeArena(allocator, arena); 3168 3169 const new_root = try initCurrentRoot(allocator, arena, session); 3170 3171 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3172 // drop the oldest entry (freeing its widget tree and arena) once over cap 3173 if (self.history.items.len > max_history) { 3174 var oldest = self.history.orderedRemove(0); 3175 oldest.root.deinit(allocator); 3176 freeArena(allocator, oldest.arena); 3177 } 3178 self.root = new_root; 3179 self.arena = arena; 3180 self.user_id = session.userId(); 3181 return true; 3182 } 3183 return false; 3184 } 3185 }; 3186 3187 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3188 const page_widget: Widget = switch (page.*) { 3189 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3190 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3191 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3192 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3193 .not_found => .{ .not_found = try .init(allocator, session) }, 3194 }; 3195 3196 chooseAnsiArtForNavigation(session); 3197 3198 // on the TUI/SSH, the page sits above a one-row footer showing its web 3199 // url, when there's a web UI to point at 3200 var root = if (session.is_terminal) blk: { 3201 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3202 errdefer box.deinit(allocator); 3203 const bg_id = bg_blk: { 3204 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3205 errdefer background.deinit(allocator); 3206 const id = background.getFocus().id; 3207 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3208 break :bg_blk id; 3209 }; 3210 if (session.web_port != null) { 3211 var footer = try widget.Footer.init(allocator, session); 3212 errdefer footer.deinit(allocator); 3213 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3214 } 3215 box.getFocus().child_id = bg_id; 3216 break :blk Widget{ .box = box }; 3217 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3218 errdefer root.deinit(allocator); 3219 3220 // input-owning views build their TextInputs in init, so reset their 3221 // registrations here 3222 session.text_inputs.clearRetainingCapacity(); 3223 3224 try root.build(allocator, .{ 3225 .min_size = .{ .width = null, .height = 40 }, 3226 .max_size = .{ .width = 100, .height = null }, 3227 }, root.getFocus()); 3228 3229 return root; 3230 } 3231 3232 // native and server-side sessions choose from the embedded collection. WASM 3233 // keeps the art serialized by the server instead. 3234 fn chooseAnsiArtForNavigation(session: *Session) void { 3235 if (comptime ansi_art.art.len == 0) return; 3236 if (session.data.current_page == .not_found) { 3237 session.data.ansi_art = ansi_art.not_found; 3238 return; 3239 } 3240 const io = session.io orelse return; 3241 var random: usize = undefined; 3242 io.random(std.mem.asBytes(&random)); 3243 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3244 } 3245