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