╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2625 .email, .user_name => |t| t, 2626 }; 2627 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .label = " author " }); 2628 errdefer tb.deinit(allocator); 2629 tb.getFocus().mode = .all; 2630 switch (author) { 2631 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2632 else => {}, 2633 } 2634 return tb; 2635 } 2636 2637 // native-TUI navigation 2638 pub const Nav = struct { 2639 root: Widget, 2640 // backs the current page (its Page data and the page-scoped strings its 2641 // widgets build). owned here, swapped on each navigation. session.page_arena 2642 // tracks whichever of these is current so widgets allocate into it. 2643 arena: *std.heap.ArenaAllocator, 2644 history: std.ArrayList(Entry), 2645 2646 // each retained page keeps its own arena, freed when the page leaves the 2647 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2648 // long-lived session from accumulating every page it ever visited. 2649 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2650 2651 // cap on retained back-history so the chain can't grow memory without bound 2652 const max_history: usize = 16; 2653 2654 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2655 session.back = .unavailable; 2656 const arena = try allocator.create(std.heap.ArenaAllocator); 2657 arena.* = std.heap.ArenaAllocator.init(allocator); 2658 errdefer { 2659 arena.deinit(); 2660 allocator.destroy(arena); 2661 } 2662 2663 session.page_arena = arena; 2664 const route = session.data.current_page; 2665 2666 const page = try arena.allocator().create(Page); 2667 page.* = try Page.init(arena, session, route); 2668 return .{ 2669 .root = try initRoot(allocator, page, session), 2670 .arena = arena, 2671 .history = .empty, 2672 }; 2673 } 2674 2675 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2676 self.root.deinit(allocator); 2677 freeArena(allocator, self.arena); 2678 for (self.history.items) |*entry| { 2679 entry.root.deinit(allocator); 2680 freeArena(allocator, entry.arena); 2681 } 2682 self.history.deinit(allocator); 2683 } 2684 2685 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2686 arena.deinit(); 2687 allocator.destroy(arena); 2688 } 2689 2690 // reconcile the displayed root with the session's navigation state. a 2691 // forward nav (current_page moved to a different parent page) pushes the 2692 // current root and builds the new page in a fresh arena; a back request 2693 // frees the current page and restores the previous one. when escape is 2694 // pressed with no history left we switch to the quit tab instead of quitting. 2695 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2696 // refresh: rebuild the current page in place from a current moment 2697 if (session.refresh_requested) { 2698 session.refresh_requested = false; 2699 if (session.haxy_moment != null or session.local != null) { 2700 const arena = try allocator.create(std.heap.ArenaAllocator); 2701 arena.* = std.heap.ArenaAllocator.init(allocator); 2702 errdefer freeArena(allocator, arena); 2703 2704 session.page_arena = arena; 2705 2706 const route = session.data.current_page; 2707 const page = try arena.allocator().create(Page); 2708 page.* = try Page.init(arena, session, route); 2709 const new_root = try initRoot(allocator, page, session); 2710 2711 self.root.deinit(allocator); 2712 freeArena(allocator, self.arena); 2713 self.root = new_root; 2714 self.arena = arena; 2715 } 2716 return; 2717 } 2718 2719 if (session.back == .requested) { 2720 if (self.history.pop()) |entry| { 2721 session.back = if (self.history.items.len == 0) .unavailable else .available; 2722 self.root.deinit(allocator); 2723 freeArena(allocator, self.arena); 2724 self.root = entry.root; 2725 self.arena = entry.arena; 2726 session.page_arena = entry.arena; 2727 session.data.current_page = entry.route; 2728 chooseAnsiArtForNavigation(session); 2729 return; 2730 } 2731 session.back = .unavailable; 2732 // nothing to go back to; switch to the quit confirmation 2733 const root_focus = self.root.getFocus(); 2734 var iter = root_focus.children.iterator(); 2735 while (iter.next()) |entry| { 2736 switch (entry.value_ptr.focus.kind) { 2737 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2738 root_focus.setFocus(entry.key_ptr.*); 2739 // a Stack only builds its selected child, so the quit 2740 // button isn't in the focus tree until a build runs with 2741 // the quit tab selected. build once, then send a 2742 // synthetic arrow_down to drop focus from the tab onto 2743 // the button so it's ready to confirm. 2744 try self.root.build(allocator, .{ 2745 .min_size = .{ .width = null, .height = 40 }, 2746 .max_size = .{ .width = 80, .height = null }, 2747 }, root_focus); 2748 try self.root.input(allocator, .arrow_down, root_focus); 2749 break; 2750 }, 2751 else => {}, 2752 } 2753 } 2754 return; 2755 } 2756 2757 // forward navigation: navigate() set next_page to the page to move to (a 2758 // cross-page link or tab change crossing pages) 2759 if (session.next_page) |route| { 2760 session.next_page = null; 2761 if (session.haxy_moment == null and session.local == null) return; 2762 const previous_route = session.data.current_page; 2763 // the page we navigated to becomes the current page 2764 session.data.current_page = route; 2765 session.back = .available; 2766 2767 const arena = try allocator.create(std.heap.ArenaAllocator); 2768 arena.* = std.heap.ArenaAllocator.init(allocator); 2769 errdefer freeArena(allocator, arena); 2770 2771 session.page_arena = arena; 2772 2773 const page = try arena.allocator().create(Page); 2774 page.* = try Page.init(arena, session, route); 2775 const new_root = try initRoot(allocator, page, session); 2776 2777 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2778 // drop the oldest entry (freeing its widget tree and arena) once over cap 2779 if (self.history.items.len > max_history) { 2780 var oldest = self.history.orderedRemove(0); 2781 oldest.root.deinit(allocator); 2782 freeArena(allocator, oldest.arena); 2783 } 2784 self.root = new_root; 2785 self.arena = arena; 2786 } 2787 } 2788 }; 2789 2790 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2791 const page_widget: Widget = switch (page.*) { 2792 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2793 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2794 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2795 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2796 }; 2797 2798 chooseAnsiArtForNavigation(session); 2799 2800 // on the TUI/SSH, the page sits above a one-row footer showing the url 2801 var root = if (session.is_terminal) blk: { 2802 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2803 errdefer box.deinit(allocator); 2804 const bg_id = bg_blk: { 2805 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2806 errdefer background.deinit(allocator); 2807 const id = background.getFocus().id; 2808 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2809 break :bg_blk id; 2810 }; 2811 { 2812 var footer = try widget.Footer.init(allocator, session); 2813 errdefer footer.deinit(allocator); 2814 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2815 } 2816 box.getFocus().child_id = bg_id; 2817 break :blk Widget{ .box = box }; 2818 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2819 errdefer root.deinit(allocator); 2820 2821 // input-owning views build their TextInputs in init, so reset their 2822 // registrations here 2823 session.text_inputs.clearRetainingCapacity(); 2824 2825 try root.build(allocator, .{ 2826 .min_size = .{ .width = null, .height = 40 }, 2827 .max_size = .{ .width = 100, .height = null }, 2828 }, root.getFocus()); 2829 2830 return root; 2831 } 2832 2833 // native and server-side sessions choose from the embedded collection. WASM 2834 // keeps the art serialized by the server instead. 2835 fn chooseAnsiArtForNavigation(session: *Session) void { 2836 if (comptime ansi_arts.len == 0) return; 2837 const io = session.io orelse return; 2838 var random: usize = undefined; 2839 io.random(std.mem.asBytes(&random)); 2840 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2841 } 2842