╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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