╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2428 freeArena(allocator, self.arena); 2429 for (self.history.items) |*entry| { 2430 entry.root.deinit(allocator); 2431 freeArena(allocator, entry.arena); 2432 } 2433 self.history.deinit(allocator); 2434 } 2435 2436 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2437 arena.deinit(); 2438 allocator.destroy(arena); 2439 } 2440 2441 // reconcile the displayed root with the session's navigation state. a 2442 // forward nav (current_page moved to a different parent page) pushes the 2443 // current root and builds the new page in a fresh arena; a back request 2444 // frees the current page and restores the previous one. when escape is 2445 // pressed with no history left we switch to the quit tab instead of quitting. 2446 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2447 // refresh: rebuild the current page in place from a current moment 2448 if (session.refresh_requested) { 2449 session.refresh_requested = false; 2450 if (session.haxy_moment != null or session.local != null) { 2451 const arena = try allocator.create(std.heap.ArenaAllocator); 2452 arena.* = std.heap.ArenaAllocator.init(allocator); 2453 errdefer freeArena(allocator, arena); 2454 2455 session.page_arena = arena; 2456 2457 const route = session.data.current_page; 2458 const page = try arena.allocator().create(Page); 2459 page.* = try Page.init(arena, session, route); 2460 const new_root = try initRoot(allocator, page, session); 2461 2462 self.root.deinit(allocator); 2463 freeArena(allocator, self.arena); 2464 self.root = new_root; 2465 self.arena = arena; 2466 } 2467 return; 2468 } 2469 2470 if (session.back == .requested) { 2471 if (self.history.pop()) |entry| { 2472 session.back = if (self.history.items.len == 0) .unavailable else .available; 2473 self.root.deinit(allocator); 2474 freeArena(allocator, self.arena); 2475 self.root = entry.root; 2476 self.arena = entry.arena; 2477 session.page_arena = entry.arena; 2478 session.data.current_page = entry.route; 2479 chooseAnsiArtForNavigation(session); 2480 return; 2481 } 2482 session.back = .unavailable; 2483 // nothing to go back to; switch to the quit confirmation 2484 const root_focus = self.root.getFocus(); 2485 var iter = root_focus.children.iterator(); 2486 while (iter.next()) |entry| { 2487 switch (entry.value_ptr.focus.kind) { 2488 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2489 root_focus.setFocus(entry.key_ptr.*); 2490 // a Stack only builds its selected child, so the quit 2491 // button isn't in the focus tree until a build runs with 2492 // the quit tab selected. build once, then send a 2493 // synthetic arrow_down to drop focus from the tab onto 2494 // the button so it's ready to confirm. 2495 try self.root.build(allocator, .{ 2496 .min_size = .{ .width = null, .height = 40 }, 2497 .max_size = .{ .width = 80, .height = null }, 2498 }, root_focus); 2499 try self.root.input(allocator, .arrow_down, root_focus); 2500 break; 2501 }, 2502 else => {}, 2503 } 2504 } 2505 return; 2506 } 2507 2508 // forward navigation: navigate() set next_page to the page to move to (a 2509 // cross-page link or tab change crossing pages) 2510 if (session.next_page) |route| { 2511 session.next_page = null; 2512 if (session.haxy_moment == null and session.local == null) return; 2513 const previous_route = session.data.current_page; 2514 // the page we navigated to becomes the current page 2515 session.data.current_page = route; 2516 session.back = .available; 2517 2518 const arena = try allocator.create(std.heap.ArenaAllocator); 2519 arena.* = std.heap.ArenaAllocator.init(allocator); 2520 errdefer freeArena(allocator, arena); 2521 2522 session.page_arena = arena; 2523 2524 const page = try arena.allocator().create(Page); 2525 page.* = try Page.init(arena, session, route); 2526 const new_root = try initRoot(allocator, page, session); 2527 2528 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2529 // drop the oldest entry (freeing its widget tree and arena) once over cap 2530 if (self.history.items.len > max_history) { 2531 var oldest = self.history.orderedRemove(0); 2532 oldest.root.deinit(allocator); 2533 freeArena(allocator, oldest.arena); 2534 } 2535 self.root = new_root; 2536 self.arena = arena; 2537 } 2538 } 2539 }; 2540 2541 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2542 const page_widget: Widget = switch (page.*) { 2543 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2544 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2545 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2546 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2547 }; 2548 2549 chooseAnsiArtForNavigation(session); 2550 2551 // on the TUI/SSH, the page sits above a one-row footer showing the url 2552 var root = if (session.is_terminal) blk: { 2553 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2554 errdefer box.deinit(allocator); 2555 const bg_id = bg_blk: { 2556 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2557 errdefer background.deinit(allocator); 2558 const id = background.getFocus().id; 2559 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2560 break :bg_blk id; 2561 }; 2562 { 2563 var footer = try widget.Footer.init(allocator, session); 2564 errdefer footer.deinit(allocator); 2565 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2566 } 2567 box.getFocus().child_id = bg_id; 2568 break :blk Widget{ .box = box }; 2569 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2570 errdefer root.deinit(allocator); 2571 2572 // input-owning views build their TextInputs in init, so reset their 2573 // registrations here 2574 session.text_inputs.clearRetainingCapacity(); 2575 2576 try root.build(allocator, .{ 2577 .min_size = .{ .width = null, .height = 40 }, 2578 .max_size = .{ .width = 100, .height = null }, 2579 }, root.getFocus()); 2580 2581 return root; 2582 } 2583 2584 // native and server-side sessions choose from the embedded collection. WASM 2585 // keeps the art serialized by the server instead. 2586 fn chooseAnsiArtForNavigation(session: *Session) void { 2587 if (comptime ansi_arts.len == 0) return; 2588 const io = session.io orelse return; 2589 var random: usize = undefined; 2590 io.random(std.mem.asBytes(&random)); 2591 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2592 } 2593