╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
2965 // tracks whichever of these is current so widgets allocate into it. 2966 arena: *std.heap.ArenaAllocator, 2967 // the login the current root was built for 2968 user_id: ?[evt.event_id_size]u8, 2969 history: std.ArrayList(Entry), 2970 2971 // each retained page keeps its own arena, freed when the page leaves the 2972 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2973 // long-lived session from accumulating every page it ever visited. 2974 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 2975 2976 // cap on retained back-history so the chain can't grow memory without bound 2977 const max_history: usize = 16; 2978 2979 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2980 session.back = .unavailable; 2981 const arena = try allocator.create(std.heap.ArenaAllocator); 2982 arena.* = std.heap.ArenaAllocator.init(allocator); 2983 errdefer { 2984 arena.deinit(); 2985 allocator.destroy(arena); 2986 } 2987 2988 return .{ 2989 .root = try initCurrentRoot(allocator, arena, session), 2990 .arena = arena, 2991 .user_id = session.userId(), 2992 .history = .empty, 2993 }; 2994 } 2995 2996 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2997 self.root.deinit(allocator); 2998 freeArena(allocator, self.arena); 2999 for (self.history.items) |*entry| { 3000 entry.root.deinit(allocator); 3001 freeArena(allocator, entry.arena); 3002 } 3003 self.history.deinit(allocator); 3004 } 3005 3006 // rebuild the current page in place from a current moment 3007 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 3008 if (session.haxy_moment == null and session.local == null) return; 3009 const arena = try allocator.create(std.heap.ArenaAllocator); 3010 arena.* = std.heap.ArenaAllocator.init(allocator); 3011 errdefer freeArena(allocator, arena); 3012 3013 const new_root = try initCurrentRoot(allocator, arena, session); 3014 3015 self.root.deinit(allocator); 3016 freeArena(allocator, self.arena); 3017 self.root = new_root; 3018 self.arena = arena; 3019 self.user_id = session.userId(); 3020 } 3021 3022 // build the current page's root in `arena`, or the not-found page's when 3023 // the route names nothing 3024 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 3025 session.page_arena = arena; 3026 const page = try arena.allocator().create(Page); 3027 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 3028 error.NotFound => blk: { 3029 session.data.current_page = .not_found; 3030 break :blk .not_found; 3031 }, 3032 else => |e| return e, 3033 }; 3034 return initRoot(allocator, page, session); 3035 } 3036 3037 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3038 arena.deinit(); 3039 allocator.destroy(arena); 3040 } 3041 3042 // reconcile the displayed root with the session's navigation state. a 3043 // forward nav (current_page moved to a different parent page) pushes the 3044 // current root and builds the new page in a fresh arena; a back request 3045 // frees the current page and restores the previous one. when escape is 3046 // pressed with no history left we switch to the quit tab instead of quitting. 3047 // returns whether the root was replaced. 3048 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3049 // refresh: rebuild the current page in place from a current moment 3050 if (session.refresh_requested) { 3051 session.refresh_requested = false; 3052 try self.rebuild(allocator, session); 3053 return true; 3054 } 3055 3056 if (session.back == .requested) { 3057 if (self.history.pop()) |entry| { 3058 session.back = if (self.history.items.len == 0) .unavailable else .available; 3059 self.root.deinit(allocator); 3060 freeArena(allocator, self.arena); 3061 self.root = entry.root; 3062 self.arena = entry.arena; 3063 self.user_id = entry.user_id; 3064 session.page_arena = entry.arena; 3065 session.data.current_page = entry.route; 3066 // a page built under another login is rebuilt rather than restored 3067 if (!std.meta.eql(entry.user_id, session.userId())) { 3068 try self.rebuild(allocator, session); 3069 return true; 3070 } 3071 chooseAnsiArtForNavigation(session); 3072 return true; 3073 } 3074 session.back = .unavailable; 3075 // nothing to go back to; switch to the quit confirmation 3076 const root_focus = self.root.getFocus(); 3077 var iter = root_focus.children.iterator(); 3078 while (iter.next()) |entry| { 3079 switch (entry.value_ptr.focus.kind) { 3080 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3081 root_focus.setFocus(entry.key_ptr.*); 3082 // a Stack only builds its selected child, so the quit 3083 // button isn't in the focus tree until a build runs with 3084 // the quit tab selected. build once, then send a 3085 // synthetic arrow_down to drop focus from the tab onto 3086 // the button so it's ready to confirm. 3087 try self.root.build(allocator, .{ 3088 .min_size = .{ .width = null, .height = 40 }, 3089 .max_size = .{ .width = 80, .height = null }, 3090 }, root_focus); 3091 try self.root.input(allocator, .arrow_down, root_focus); 3092 break; 3093 }, 3094 else => {}, 3095 } 3096 } 3097 return false; 3098 } 3099 3100 // forward navigation: navigate() set next_page to the page to move to (a 3101 // cross-page link or tab change crossing pages) 3102 if (session.next_page) |route| { 3103 session.next_page = null; 3104 if (session.haxy_moment == null and session.local == null) return false; 3105 const previous_route = session.data.current_page; 3106 // the page we navigated to becomes the current page 3107 session.data.current_page = route; 3108 session.back = .available; 3109 3110 const arena = try allocator.create(std.heap.ArenaAllocator); 3111 arena.* = std.heap.ArenaAllocator.init(allocator); 3112 errdefer freeArena(allocator, arena); 3113 3114 const new_root = try initCurrentRoot(allocator, arena, session); 3115 3116 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3117 // drop the oldest entry (freeing its widget tree and arena) once over cap 3118 if (self.history.items.len > max_history) { 3119 var oldest = self.history.orderedRemove(0); 3120 oldest.root.deinit(allocator); 3121 freeArena(allocator, oldest.arena); 3122 } 3123 self.root = new_root; 3124 self.arena = arena; 3125 self.user_id = session.userId(); 3126 return true; 3127 } 3128 return false; 3129 } 3130 }; 3131 3132 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3133 const page_widget: Widget = switch (page.*) { 3134 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3135 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3136 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3137 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3138 .not_found => .{ .not_found = try .init(allocator, session) }, 3139 }; 3140 3141 chooseAnsiArtForNavigation(session); 3142 3143 // on the TUI/SSH, the page sits above a one-row footer showing its web 3144 // url, when there's a web UI to point at 3145 var root = if (session.is_terminal) blk: { 3146 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3147 errdefer box.deinit(allocator); 3148 const bg_id = bg_blk: { 3149 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3150 errdefer background.deinit(allocator); 3151 const id = background.getFocus().id; 3152 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3153 break :bg_blk id; 3154 }; 3155 if (session.web_port != null) { 3156 var footer = try widget.Footer.init(allocator, session); 3157 errdefer footer.deinit(allocator); 3158 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3159 } 3160 box.getFocus().child_id = bg_id; 3161 break :blk Widget{ .box = box }; 3162 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3163 errdefer root.deinit(allocator); 3164 3165 // input-owning views build their TextInputs in init, so reset their 3166 // registrations here 3167 session.text_inputs.clearRetainingCapacity(); 3168 3169 try root.build(allocator, .{ 3170 .min_size = .{ .width = null, .height = 40 }, 3171 .max_size = .{ .width = 100, .height = null }, 3172 }, root.getFocus()); 3173 3174 return root; 3175 } 3176 3177 // native and server-side sessions choose from the embedded collection. WASM 3178 // keeps the art serialized by the server instead. 3179 fn chooseAnsiArtForNavigation(session: *Session) void { 3180 if (comptime ansi_art.art.len == 0) return; 3181 if (session.data.current_page == .not_found) { 3182 session.data.ansi_art = ansi_art.not_found; 3183 return; 3184 } 3185 const io = session.io orelse return; 3186 var random: usize = undefined; 3187 io.random(std.mem.asBytes(&random)); 3188 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3189 } 3190