╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2820 } 2821 } 2822 2823 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2824 const tty: ?std.Io.File = if (builtin.target.os.tag == .windows) 2825 null 2826 else 2827 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2828 defer if (tty) |file| file.close(io); 2829 2830 const input = if (tty) |file| file else std.Io.File.stdin(); 2831 const output = if (tty) |file| file else std.Io.File.stdout(); 2832 2833 var write_buf: [1024]u8 = undefined; 2834 var writer = output.writer(io, &write_buf); 2835 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}); 2836 try writer.interface.flush(); 2837 2838 var read_buf: [1]u8 = undefined; 2839 var reader = input.reader(io, &read_buf); 2840 while (true) { 2841 const byte = try reader.interface.takeByte(); 2842 if (byte == '\r' or byte == '\n') return; 2843 } 2844 } 2845 2846 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2847 const root_focus = root.getFocus(); 2848 const focused_before = root_focus.grandchild_id; 2849 var activate_in_page_link = false; 2850 switch (key) { 2851 // request a navigation pop; the host's Nav.sync goes back a page, or 2852 // quits when there's no history left. 2853 .escape => { 2854 session.back = .requested; 2855 return; 2856 }, 2857 .ctrl => |letter| switch (letter) { 2858 'r' => { 2859 session.refresh_requested = true; 2860 return; 2861 }, 2862 else => try root.input(allocator, key, root_focus), 2863 }, 2864 .enter => { 2865 if (root_focus.grandchild_id) |gid| { 2866 // follow a cross-page link 2867 if (crossPageLink(root_focus, gid, session.data)) |route| { 2868 return session.navigate(route); 2869 } 2870 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2871 } 2872 activate_in_page_link = true; 2873 try root.input(allocator, key, root_focus); 2874 }, 2875 .mouse => |mouse| { 2876 if (mouse.button == .left) { 2877 if (cellPageLink(root, mouse.x, mouse.y, session.data)) |route| return session.navigate(route); 2878 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2879 const focus_id = hit.id; 2880 if (hit.kind == .background) { 2881 root_focus.setFocus(focus_id); 2882 return; 2883 } 2884 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2885 if (session.back == .available) session.back = .requested; 2886 return; 2887 } 2888 // follow a cross-page link 2889 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2890 return session.navigate(route); 2891 } 2892 if (currentPageLink(root_focus, focus_id, session.data)) return; 2893 if (rawLink(root_focus, focus_id)) |url| { 2894 if (!mouse.ctrl) try requestRawLink(session, url); 2895 return; 2896 } 2897 root_focus.setFocus(focus_id); 2898 activate_in_page_link = true; 2899 } 2900 // forward the press into the widget tree so buttons (and any 2901 // future click-aware widgets) can react. widgets that don't 2902 // care about presses ignore it. 2903 try root.input(allocator, key, root_focus); 2904 } else { 2905 try root.input(allocator, key, root_focus); 2906 } 2907 }, 2908 .event => |event| switch (event) { 2909 .background => |rgb| session.terminal_background = rgb, 2910 .resize => try root.input(allocator, key, root_focus), 2911 }, 2912 // a scroll tick goes to the pane under the pointer, focusing it first. 2913 // from outside any pane (the tabs, a sub-header) or from a text input 2914 // it moves like the arrows instead, so scrolling up from a search box 2915 // reaches the header and stops there. 2916 .scroll_up, .scroll_down => |position| { 2917 if (focusedPane(root_focus)) |pane| if (scrollUnder(root_focus, position)) |under| { 2918 if (under != pane) root_focus.setFocus(under); 2919 }; 2920 try root.input(allocator, key, root_focus); 2921 }, 2922 else => try root.input(allocator, key, root_focus), 2923 } 2924 2925 const focused_after = root_focus.grandchild_id; 2926 if (!activate_in_page_link and focused_before == focused_after) return; 2927 const focus_id = focused_after orelse return; 2928 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2929 } 2930 2931 // the innermost vertically scrolling widget laid out under `position` 2932 fn scrollUnder(root_focus: *const Focus, position: xitui.input.Position) ?usize { 2933 var found: ?usize = null; 2934 var found_area: usize = std.math.maxInt(usize); 2935 var iter = root_focus.children.iterator(); 2936 while (iter.next()) |entry| { 2937 const info = entry.value_ptr.focus.scroll orelse continue; 2938 if (info.direction == .horiz) continue; 2939 const r = entry.value_ptr.rect; 2940 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; 2941 // a nested scroll lies within its parent, so the smallest is innermost 2942 const area = r.size.width * r.size.height; 2943 if (area < found_area) { 2944 found = entry.key_ptr.*; 2945 found_area = area; 2946 } 2947 } 2948 return found; 2949 } 2950 2951 // the innermost vertically scrolling widget the focus lies in, unless the 2952 // focused widget is a text input 2953 fn focusedPane(root_focus: *const Focus) ?usize { 2954 var current = root_focus.grandchild_id orelse return null; 2955 switch ((root_focus.children.get(current) orelse return null).focus.kind) { 2956 .text_input, .text_input_password, .text_area => return null, 2957 else => {}, 2958 } 2959 while (root_focus.children.get(current)) |child| { 2960 if (child.focus.scroll) |info| if (info.direction != .horiz) return current; 2961 current = child.parent_id; 2962 } 2963 return null; 2964 } 2965 2966 // a link to bytes the server serves directly rather than to a page route, so 2967 // it never resolves to a RoutablePage 2968 pub const raw_link_prefix = "ax:"; 2969 2970 // a route already loaded into the current page; focusing it changes the url 2971 // without rebuilding the page 2972 pub const in_page_link_prefix = "ai:"; 2973 2974 // an in-page tab's base route, or the exact current route when it is selected 2975 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2976 const target = if (selected) session.data.current_page else route; 2977 return session.page_arena.allocator().print("{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2978 } 2979 2980 // a button the web renderer covers with a file picker that posts the chosen 2981 // file to the url after the prefix 2982 pub const file_input_prefix = "file:"; 2983 2984 // a submit button that overrides its enclosing form's action 2985 pub const submit_action_prefix = "submit:"; 2986 2987 // one option of a radio field, as `radio:<name>=<value>` 2988 pub const radio_prefix = "radio:"; 2989 2990 // the url an `ax:` link points at, or null for anything else. a host that 2991 // suppresses the anchor's own navigation needs this to follow the link. 2992 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2993 const child = root_focus.children.get(focus_id) orelse return null; 2994 const custom = switch (child.focus.kind) { 2995 .custom => |c| c, 2996 else => return null, 2997 }; 2998 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2999 return custom[raw_link_prefix.len..]; 3000 } 3001 3002 fn requestRawLink(session: *Session, url: []const u8) !void { 3003 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 3004 } 3005 3006 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 3007 // an absolute url is already complete 3008 if (!std.mem.startsWith(u8, path, "/")) return path; 3009 return if (session.web_port) |port| 3010 try allocator.print("http://localhost:{d}{s}", .{ port, path }) 3011 else 3012 path; 3013 } 3014 3015 // if the focus target at focus_id is an `a:` link that should navigate (a 3016 // different parent page, or a different files directory within the repo page), 3017 // return its route; otherwise null. lets a host turn a click / enter on such a 3018 // link into a navigation rather than a focus change. 3019 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 3020 const current = data.current_page; 3021 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 3022 // a link to a different parent page always navigates; within a page, a 3023 // files-directory / commits-page / list-window change navigates while tab 3024 // links stay in-page. 3025 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; 3026 return null; 3027 } 3028 3029 // the page the clicked cell links to, from a link inside a text box's content 3030 fn cellPageLink(root: *Widget, x: usize, y: usize, data: Session.Data) ?RoutablePage { 3031 const grid = root.getGrid() orelse return null; 3032 const cell = grid.cell(x, y) catch return null; 3033 const link = cell.link orelse return null; 3034 if (link.len == 0 or link[0] != '/') return null; 3035 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(link) else RoutablePage.fromUrl(link); 3036 } 3037 3038 // whether the `a:` focus target links to the page already shown, which a click 3039 // leaves alone rather than focusing 3040 pub fn currentPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) bool { 3041 const route = pageLink(root_focus, focus_id, data, "a:") orelse return false; 3042 return route.eql(data.current_page); 3043 } 3044 3045 // the route carried by an `ai:` focus target, or null for any other target 3046 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 3047 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 3048 } 3049 3050 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 3051 const child = root_focus.children.get(focus_id) orelse return null; 3052 const custom = switch (child.focus.kind) { 3053 .custom => |c| c, 3054 else => return null, 3055 }; 3056 if (!std.mem.startsWith(u8, custom, prefix)) return null; 3057 const path = custom[prefix.len..]; 3058 // local sessions build (and parse) links with the repo identity elided 3059 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 3060 } 3061 3062 // an assignee as the detail view shows it: the stored email, and the name of 3063 // the user holding it on this instance when there is one 3064 pub const Assignee = struct { 3065 email: []const u8, 3066 name: ?[]const u8 = null, 3067 3068 pub fn display(self: Assignee) []const u8 { 3069 return self.name orelse self.email; 3070 } 3071 }; 3072 3073 // a display author, resolved against the admin db at read time 3074 pub const Author = union(enum) { 3075 unknown, 3076 email: []const u8, // an email no user matches 3077 user_name: []const u8, 3078 3079 // the commit author line's email, resolved like initFromEmail 3080 pub fn init( 3081 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 3082 arena: *std.heap.ArenaAllocator, 3083 author_line: []const u8, 3084 ) !Author { 3085 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 3086 } 3087 3088 // from a record's stored author email: it resolves to the user currently 3089 // holding it, else stays the email. the result is allocated in `arena`, 3090 // so the email may borrow transient memory. 3091 pub fn initFromEmail( 3092 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 3093 arena: *std.heap.ArenaAllocator, 3094 author_email: ?[]const u8, 3095 ) !Author { 3096 const email = author_email orelse return .unknown; 3097 if (haxy_moment) |moment| { 3098 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 3099 return .{ .user_name = user.name }; 3100 } 3101 } 3102 return .{ .email = try arena.allocator().dupe(u8, email) }; 3103 } 3104 }; 3105 3106 // the "a:" link to user `name`'s page. 3107 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 3108 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 3109 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 3110 return page_arena.allocator().print("a:{s}", .{url}); 3111 } 3112 3113 // a focusable " author " box showing `author`, linking to their user page 3114 // when it is a known user 3115 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 3116 const text = switch (author) { 3117 .unknown => "", 3118 .email, .user_name => |t| t, 3119 }; 3120 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " author " } }); 3121 errdefer tb.deinit(allocator); 3122 tb.getFocus().mode = .all; 3123 switch (author) { 3124 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 3125 else => {}, 3126 } 3127 return tb; 3128 } 3129 3130 // native-TUI navigation 3131 pub const Nav = struct { 3132 root: Widget, 3133 // backs the current page (its Page data and the page-scoped strings its 3134 // widgets build). owned here, swapped on each navigation. session.page_arena 3135 // tracks whichever of these is current so widgets allocate into it. 3136 arena: *std.heap.ArenaAllocator, 3137 // the login the current root was built for 3138 user_id: ?[evt.event_id_size]u8, 3139 history: std.ArrayList(Entry), 3140 3141 // each retained page keeps its own arena, freed when the page leaves the 3142 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 3143 // long-lived session from accumulating every page it ever visited. 3144 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 3145 3146 // cap on retained back-history so the chain can't grow memory without bound 3147 const max_history: usize = 16; 3148 3149 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 3150 session.back = .unavailable; 3151 const arena = try allocator.create(std.heap.ArenaAllocator); 3152 arena.* = std.heap.ArenaAllocator.init(allocator); 3153 errdefer { 3154 arena.deinit(); 3155 allocator.destroy(arena); 3156 } 3157 3158 return .{ 3159 .root = try initCurrentRoot(allocator, arena, session), 3160 .arena = arena, 3161 .user_id = session.userId(), 3162 .history = .empty, 3163 }; 3164 } 3165 3166 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 3167 self.root.deinit(allocator); 3168 freeArena(allocator, self.arena); 3169 for (self.history.items) |*entry| { 3170 entry.root.deinit(allocator); 3171 freeArena(allocator, entry.arena); 3172 } 3173 self.history.deinit(allocator); 3174 } 3175 3176 // rebuild the current page in place from a current moment 3177 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 3178 if (session.haxy_moment == null and session.local == null) return; 3179 const arena = try allocator.create(std.heap.ArenaAllocator); 3180 arena.* = std.heap.ArenaAllocator.init(allocator); 3181 errdefer freeArena(allocator, arena); 3182 3183 const new_root = try initCurrentRoot(allocator, arena, session); 3184 3185 self.root.deinit(allocator); 3186 freeArena(allocator, self.arena); 3187 self.root = new_root; 3188 self.arena = arena; 3189 self.user_id = session.userId(); 3190 } 3191 3192 // build the current page's root in `arena`, or the not-found page's when 3193 // the route names nothing 3194 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 3195 session.page_arena = arena; 3196 const page = try arena.allocator().create(Page); 3197 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 3198 error.NotFound => blk: { 3199 session.data.current_page = .not_found; 3200 break :blk .not_found; 3201 }, 3202 else => |e| return e, 3203 }; 3204 return initRoot(allocator, page, session); 3205 } 3206 3207 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3208 arena.deinit(); 3209 allocator.destroy(arena); 3210 } 3211 3212 // reconcile the displayed root with the session's navigation state. a 3213 // forward nav (current_page moved to a different parent page) pushes the 3214 // current root and builds the new page in a fresh arena; a back request 3215 // frees the current page and restores the previous one. when escape is 3216 // pressed with no history left we switch to the quit tab instead of quitting. 3217 // returns whether the root was replaced. 3218 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3219 // refresh: rebuild the current page in place from a current moment 3220 if (session.refresh_requested) { 3221 session.refresh_requested = false; 3222 try self.rebuild(allocator, session); 3223 return true; 3224 } 3225 3226 if (session.back == .requested) { 3227 if (self.history.pop()) |entry| { 3228 session.back = if (self.history.items.len == 0) .unavailable else .available; 3229 self.root.deinit(allocator); 3230 freeArena(allocator, self.arena); 3231 self.root = entry.root; 3232 self.arena = entry.arena; 3233 self.user_id = entry.user_id; 3234 session.page_arena = entry.arena; 3235 session.data.current_page = entry.route; 3236 // a page built under another login is rebuilt rather than restored 3237 if (!std.meta.eql(entry.user_id, session.userId())) { 3238 try self.rebuild(allocator, session); 3239 return true; 3240 } 3241 chooseAnsiArtForNavigation(session); 3242 return true; 3243 } 3244 session.back = .unavailable; 3245 // nothing to go back to; switch to the quit confirmation 3246 const root_focus = self.root.getFocus(); 3247 var iter = root_focus.children.iterator(); 3248 while (iter.next()) |entry| { 3249 switch (entry.value_ptr.focus.kind) { 3250 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3251 root_focus.setFocus(entry.key_ptr.*); 3252 // a Stack only builds its selected child, so the quit 3253 // button isn't in the focus tree until a build runs with 3254 // the quit tab selected. build once, then send a 3255 // synthetic arrow_down to drop focus from the tab onto 3256 // the button so it's ready to confirm. 3257 try self.root.build(allocator, .{ 3258 .min_size = .{ .width = null, .height = 40 }, 3259 .max_size = .{ .width = 80, .height = null }, 3260 }, root_focus); 3261 try self.root.input(allocator, .arrow_down, root_focus); 3262 break; 3263 }, 3264 else => {}, 3265 } 3266 } 3267 return false; 3268 } 3269 3270 // forward navigation: navigate() set next_page to the page to move to (a 3271 // cross-page link or tab change crossing pages) 3272 if (session.next_page) |route| { 3273 session.next_page = null; 3274 if (session.haxy_moment == null and session.local == null) return false; 3275 const previous_route = session.data.current_page; 3276 // the page we navigated to becomes the current page 3277 session.data.current_page = route; 3278 session.back = .available; 3279 3280 const arena = try allocator.create(std.heap.ArenaAllocator); 3281 arena.* = std.heap.ArenaAllocator.init(allocator); 3282 errdefer freeArena(allocator, arena); 3283 3284 const new_root = try initCurrentRoot(allocator, arena, session); 3285 3286 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3287 // drop the oldest entry (freeing its widget tree and arena) once over cap 3288 if (self.history.items.len > max_history) { 3289 var oldest = self.history.orderedRemove(0); 3290 oldest.root.deinit(allocator); 3291 freeArena(allocator, oldest.arena); 3292 } 3293 self.root = new_root; 3294 self.arena = arena; 3295 self.user_id = session.userId(); 3296 return true; 3297 } 3298 return false; 3299 } 3300 }; 3301 3302 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3303 const page_widget: Widget = switch (page.*) { 3304 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3305 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3306 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3307 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3308 .not_found => .{ .not_found = try .init(allocator, session) }, 3309 }; 3310 3311 chooseAnsiArtForNavigation(session); 3312 3313 // on the TUI/SSH, the page sits above a one-row footer showing its web 3314 // url, when there's a web UI to point at 3315 var root = if (session.is_terminal) blk: { 3316 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3317 errdefer box.deinit(allocator); 3318 const bg_id = bg_blk: { 3319 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3320 errdefer background.deinit(allocator); 3321 const id = background.getFocus().id; 3322 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3323 break :bg_blk id; 3324 }; 3325 if (session.web_port != null) { 3326 var footer = try widget.Footer.init(allocator, session); 3327 errdefer footer.deinit(allocator); 3328 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3329 } 3330 box.getFocus().child_id = bg_id; 3331 break :blk Widget{ .box = box }; 3332 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3333 errdefer root.deinit(allocator); 3334 3335 // input-owning views build their TextInputs in init, so reset their 3336 // registrations here 3337 session.text_inputs.clearRetainingCapacity(); 3338 3339 try root.build(allocator, .{ 3340 .min_size = .{ .width = null, .height = 40 }, 3341 .max_size = .{ .width = 100, .height = null }, 3342 }, root.getFocus()); 3343 3344 return root; 3345 } 3346 3347 // native and server-side sessions choose from the embedded collection. WASM 3348 // keeps the art serialized by the server instead. 3349 fn chooseAnsiArtForNavigation(session: *Session) void { 3350 if (comptime ansi_art.art.len == 0) return; 3351 if (session.data.current_page == .not_found) { 3352 session.data.ansi_art = ansi_art.not_found; 3353 return; 3354 } 3355 const io = session.io orelse return; 3356 var random: usize = undefined; 3357 io.random(std.mem.asBytes(&random)); 3358 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3359 } 3360