╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
3127 3128 // native-TUI navigation 3129 pub const Nav = struct { 3130 root: Widget, 3131 // backs the current page (its Page data and the page-scoped strings its 3132 // widgets build). owned here, swapped on each navigation. session.page_arena 3133 // tracks whichever of these is current so widgets allocate into it. 3134 arena: *std.heap.ArenaAllocator, 3135 // the login the current root was built for 3136 user_id: ?[evt.event_id_size]u8, 3137 history: std.ArrayList(Entry), 3138 3139 // each retained page keeps its own arena, freed when the page leaves the 3140 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 3141 // long-lived session from accumulating every page it ever visited. 3142 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 3143 3144 // cap on retained back-history so the chain can't grow memory without bound 3145 const max_history: usize = 16; 3146 3147 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 3148 session.back = .unavailable; 3149 const arena = try allocator.create(std.heap.ArenaAllocator); 3150 arena.* = std.heap.ArenaAllocator.init(allocator); 3151 errdefer { 3152 arena.deinit(); 3153 allocator.destroy(arena); 3154 } 3155 3156 return .{ 3157 .root = try initCurrentRoot(allocator, arena, session), 3158 .arena = arena, 3159 .user_id = session.userId(), 3160 .history = .empty, 3161 }; 3162 } 3163 3164 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 3165 self.root.deinit(allocator); 3166 freeArena(allocator, self.arena); 3167 for (self.history.items) |*entry| { 3168 entry.root.deinit(allocator); 3169 freeArena(allocator, entry.arena); 3170 } 3171 self.history.deinit(allocator); 3172 } 3173 3174 // rebuild the current page in place from a current moment 3175 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 3176 if (session.haxy_moment == null and session.local == null) return; 3177 const arena = try allocator.create(std.heap.ArenaAllocator); 3178 arena.* = std.heap.ArenaAllocator.init(allocator); 3179 errdefer freeArena(allocator, arena); 3180 3181 const new_root = try initCurrentRoot(allocator, arena, session); 3182 3183 self.root.deinit(allocator); 3184 freeArena(allocator, self.arena); 3185 self.root = new_root; 3186 self.arena = arena; 3187 self.user_id = session.userId(); 3188 } 3189 3190 // build the current page's root in `arena`, or the not-found page's when 3191 // the route names nothing 3192 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 3193 session.page_arena = arena; 3194 const page = try arena.allocator().create(Page); 3195 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 3196 error.NotFound => blk: { 3197 session.data.current_page = .not_found; 3198 break :blk .not_found; 3199 }, 3200 else => |e| return e, 3201 }; 3202 return initRoot(allocator, page, session); 3203 } 3204 3205 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3206 arena.deinit(); 3207 allocator.destroy(arena); 3208 } 3209 3210 // reconcile the displayed root with the session's navigation state. a 3211 // forward nav (current_page moved to a different parent page) pushes the 3212 // current root and builds the new page in a fresh arena; a back request 3213 // frees the current page and restores the previous one. when escape is 3214 // pressed with no history left we switch to the quit tab instead of quitting. 3215 // returns whether the root was replaced. 3216 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3217 // refresh: rebuild the current page in place from a current moment 3218 if (session.refresh_requested) { 3219 session.refresh_requested = false; 3220 try self.rebuild(allocator, session); 3221 return true; 3222 } 3223 3224 if (session.back == .requested) { 3225 if (self.history.pop()) |entry| { 3226 session.back = if (self.history.items.len == 0) .unavailable else .available; 3227 self.root.deinit(allocator); 3228 freeArena(allocator, self.arena); 3229 self.root = entry.root; 3230 self.arena = entry.arena; 3231 self.user_id = entry.user_id; 3232 session.page_arena = entry.arena; 3233 session.data.current_page = entry.route; 3234 // a page built under another login is rebuilt rather than restored 3235 if (!std.meta.eql(entry.user_id, session.userId())) { 3236 try self.rebuild(allocator, session); 3237 return true; 3238 } 3239 chooseAnsiArtForNavigation(session); 3240 return true; 3241 } 3242 session.back = .unavailable; 3243 // nothing to go back to; switch to the quit confirmation 3244 const root_focus = self.root.getFocus(); 3245 var iter = root_focus.children.iterator(); 3246 while (iter.next()) |entry| { 3247 switch (entry.value_ptr.focus.kind) { 3248 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3249 root_focus.setFocus(entry.key_ptr.*); 3250 // a Stack only builds its selected child, so the quit 3251 // button isn't in the focus tree until a build runs with 3252 // the quit tab selected. build once, then send a 3253 // synthetic arrow_down to drop focus from the tab onto 3254 // the button so it's ready to confirm. 3255 try self.root.build(allocator, .{ 3256 .min_size = .{ .width = null, .height = 40 }, 3257 .max_size = .{ .width = 80, .height = null }, 3258 }, root_focus); 3259 try self.root.input(allocator, .arrow_down, root_focus); 3260 break; 3261 }, 3262 else => {}, 3263 } 3264 } 3265 return false; 3266 } 3267 3268 // forward navigation: navigate() set next_page to the page to move to (a 3269 // cross-page link or tab change crossing pages) 3270 if (session.next_page) |route| { 3271 session.next_page = null; 3272 if (session.haxy_moment == null and session.local == null) return false; 3273 const previous_route = session.data.current_page; 3274 // the page we navigated to becomes the current page 3275 session.data.current_page = route; 3276 session.back = .available; 3277 3278 const arena = try allocator.create(std.heap.ArenaAllocator); 3279 arena.* = std.heap.ArenaAllocator.init(allocator); 3280 errdefer freeArena(allocator, arena); 3281 3282 const new_root = try initCurrentRoot(allocator, arena, session); 3283 3284 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3285 // drop the oldest entry (freeing its widget tree and arena) once over cap 3286 if (self.history.items.len > max_history) { 3287 var oldest = self.history.orderedRemove(0); 3288 oldest.root.deinit(allocator); 3289 freeArena(allocator, oldest.arena); 3290 } 3291 self.root = new_root; 3292 self.arena = arena; 3293 self.user_id = session.userId(); 3294 return true; 3295 } 3296 return false; 3297 } 3298 }; 3299 3300 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3301 const page_widget: Widget = switch (page.*) { 3302 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3303 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3304 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3305 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3306 .not_found => .{ .not_found = try .init(allocator, session) }, 3307 }; 3308 3309 chooseAnsiArtForNavigation(session); 3310 3311 // on the TUI/SSH, the page sits above a one-row footer showing its web 3312 // url, when there's a web UI to point at 3313 var root = if (session.is_terminal) blk: { 3314 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3315 errdefer box.deinit(allocator); 3316 const bg_id = bg_blk: { 3317 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3318 errdefer background.deinit(allocator); 3319 const id = background.getFocus().id; 3320 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3321 break :bg_blk id; 3322 }; 3323 if (session.web_port != null) { 3324 var footer = try widget.Footer.init(allocator, session); 3325 errdefer footer.deinit(allocator); 3326 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3327 } 3328 box.getFocus().child_id = bg_id; 3329 break :blk Widget{ .box = box }; 3330 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3331 errdefer root.deinit(allocator); 3332 3333 // input-owning views build their TextInputs in init, so reset their 3334 // registrations here 3335 session.text_inputs.clearRetainingCapacity(); 3336 3337 try root.build(allocator, .{ 3338 .min_size = .{ .width = null, .height = 40 }, 3339 .max_size = .{ .width = 100, .height = null }, 3340 }, root.getFocus()); 3341 3342 return root; 3343 } 3344 3345 // native and server-side sessions choose from the embedded collection. WASM 3346 // keeps the art serialized by the server instead. 3347 fn chooseAnsiArtForNavigation(session: *Session) void { 3348 if (comptime ansi_art.art.len == 0) return; 3349 if (session.data.current_page == .not_found) { 3350 session.data.ansi_art = ansi_art.not_found; 3351 return; 3352 } 3353 const io = session.io orelse return; 3354 var random: usize = undefined; 3355 io.random(std.mem.asBytes(&random)); 3356 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3357 } 3358