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