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