╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2743 2744 self.root.deinit(allocator); 2745 freeArena(allocator, self.arena); 2746 self.root = new_root; 2747 self.arena = arena; 2748 } 2749 return; 2750 } 2751 2752 if (session.back == .requested) { 2753 if (self.history.pop()) |entry| { 2754 session.back = if (self.history.items.len == 0) .unavailable else .available; 2755 self.root.deinit(allocator); 2756 freeArena(allocator, self.arena); 2757 self.root = entry.root; 2758 self.arena = entry.arena; 2759 session.page_arena = entry.arena; 2760 session.data.current_page = entry.route; 2761 chooseAnsiArtForNavigation(session); 2762 return; 2763 } 2764 session.back = .unavailable; 2765 // nothing to go back to; switch to the quit confirmation 2766 const root_focus = self.root.getFocus(); 2767 var iter = root_focus.children.iterator(); 2768 while (iter.next()) |entry| { 2769 switch (entry.value_ptr.focus.kind) { 2770 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2771 root_focus.setFocus(entry.key_ptr.*); 2772 // a Stack only builds its selected child, so the quit 2773 // button isn't in the focus tree until a build runs with 2774 // the quit tab selected. build once, then send a 2775 // synthetic arrow_down to drop focus from the tab onto 2776 // the button so it's ready to confirm. 2777 try self.root.build(allocator, .{ 2778 .min_size = .{ .width = null, .height = 40 }, 2779 .max_size = .{ .width = 80, .height = null }, 2780 }, root_focus); 2781 try self.root.input(allocator, .arrow_down, root_focus); 2782 break; 2783 }, 2784 else => {}, 2785 } 2786 } 2787 return; 2788 } 2789 2790 // forward navigation: navigate() set next_page to the page to move to (a 2791 // cross-page link or tab change crossing pages) 2792 if (session.next_page) |route| { 2793 session.next_page = null; 2794 if (session.haxy_moment == null and session.local == null) return; 2795 const previous_route = session.data.current_page; 2796 // the page we navigated to becomes the current page 2797 session.data.current_page = route; 2798 session.back = .available; 2799 2800 const arena = try allocator.create(std.heap.ArenaAllocator); 2801 arena.* = std.heap.ArenaAllocator.init(allocator); 2802 errdefer freeArena(allocator, arena); 2803 2804 session.page_arena = arena; 2805 2806 const page = try arena.allocator().create(Page); 2807 page.* = try Page.init(arena, session, route); 2808 const new_root = try initRoot(allocator, page, session); 2809 2810 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2811 // drop the oldest entry (freeing its widget tree and arena) once over cap 2812 if (self.history.items.len > max_history) { 2813 var oldest = self.history.orderedRemove(0); 2814 oldest.root.deinit(allocator); 2815 freeArena(allocator, oldest.arena); 2816 } 2817 self.root = new_root; 2818 self.arena = arena; 2819 } 2820 } 2821 }; 2822 2823 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2824 const page_widget: Widget = switch (page.*) { 2825 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2826 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2827 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2828 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2829 }; 2830 2831 chooseAnsiArtForNavigation(session); 2832 2833 // on the TUI/SSH, the page sits above a one-row footer showing its web 2834 // url, when there's a web UI to point at 2835 var root = if (session.is_terminal) blk: { 2836 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2837 errdefer box.deinit(allocator); 2838 const bg_id = bg_blk: { 2839 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2840 errdefer background.deinit(allocator); 2841 const id = background.getFocus().id; 2842 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2843 break :bg_blk id; 2844 }; 2845 if (session.web_port != null) { 2846 var footer = try widget.Footer.init(allocator, session); 2847 errdefer footer.deinit(allocator); 2848 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2849 } 2850 box.getFocus().child_id = bg_id; 2851 break :blk Widget{ .box = box }; 2852 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2853 errdefer root.deinit(allocator); 2854 2855 // input-owning views build their TextInputs in init, so reset their 2856 // registrations here 2857 session.text_inputs.clearRetainingCapacity(); 2858 2859 try root.build(allocator, .{ 2860 .min_size = .{ .width = null, .height = 40 }, 2861 .max_size = .{ .width = 100, .height = null }, 2862 }, root.getFocus()); 2863 2864 return root; 2865 } 2866 2867 // native and server-side sessions choose from the embedded collection. WASM 2868 // keeps the art serialized by the server instead. 2869 fn chooseAnsiArtForNavigation(session: *Session) void { 2870 if (comptime ansi_arts.len == 0) return; 2871 const io = session.io orelse return; 2872 var random: usize = undefined; 2873 io.random(std.mem.asBytes(&random)); 2874 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2875 } 2876