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