╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2721 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .label = " author " }); 2722 errdefer tb.deinit(allocator); 2723 tb.getFocus().mode = .all; 2724 switch (author) { 2725 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2726 else => {}, 2727 } 2728 return tb; 2729 } 2730 2731 // native-TUI navigation 2732 pub const Nav = struct { 2733 root: Widget, 2734 // backs the current page (its Page data and the page-scoped strings its 2735 // widgets build). owned here, swapped on each navigation. session.page_arena 2736 // tracks whichever of these is current so widgets allocate into it. 2737 arena: *std.heap.ArenaAllocator, 2738 // the login the current root was built for 2739 user_id: ?[evt.event_id_size]u8, 2740 history: std.ArrayList(Entry), 2741 2742 // each retained page keeps its own arena, freed when the page leaves the 2743 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2744 // long-lived session from accumulating every page it ever visited. 2745 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 2746 2747 // cap on retained back-history so the chain can't grow memory without bound 2748 const max_history: usize = 16; 2749 2750 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2751 session.back = .unavailable; 2752 const arena = try allocator.create(std.heap.ArenaAllocator); 2753 arena.* = std.heap.ArenaAllocator.init(allocator); 2754 errdefer { 2755 arena.deinit(); 2756 allocator.destroy(arena); 2757 } 2758 2759 return .{ 2760 .root = try initCurrentRoot(allocator, arena, session), 2761 .arena = arena, 2762 .user_id = session.userId(), 2763 .history = .empty, 2764 }; 2765 } 2766 2767 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2768 self.root.deinit(allocator); 2769 freeArena(allocator, self.arena); 2770 for (self.history.items) |*entry| { 2771 entry.root.deinit(allocator); 2772 freeArena(allocator, entry.arena); 2773 } 2774 self.history.deinit(allocator); 2775 } 2776 2777 // rebuild the current page in place from a current moment 2778 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2779 if (session.haxy_moment == null and session.local == null) return; 2780 const arena = try allocator.create(std.heap.ArenaAllocator); 2781 arena.* = std.heap.ArenaAllocator.init(allocator); 2782 errdefer freeArena(allocator, arena); 2783 2784 const new_root = try initCurrentRoot(allocator, arena, session); 2785 2786 self.root.deinit(allocator); 2787 freeArena(allocator, self.arena); 2788 self.root = new_root; 2789 self.arena = arena; 2790 self.user_id = session.userId(); 2791 } 2792 2793 // build the current page's root in `arena`, or the not-found page's when 2794 // the route names nothing 2795 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 2796 session.page_arena = arena; 2797 const page = try arena.allocator().create(Page); 2798 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 2799 error.NotFound => blk: { 2800 session.data.current_page = .not_found; 2801 break :blk .not_found; 2802 }, 2803 else => |e| return e, 2804 }; 2805 return initRoot(allocator, page, session); 2806 } 2807 2808 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2809 arena.deinit(); 2810 allocator.destroy(arena); 2811 } 2812 2813 // reconcile the displayed root with the session's navigation state. a 2814 // forward nav (current_page moved to a different parent page) pushes the 2815 // current root and builds the new page in a fresh arena; a back request 2816 // frees the current page and restores the previous one. when escape is 2817 // pressed with no history left we switch to the quit tab instead of quitting. 2818 // returns whether the root was replaced. 2819 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 2820 // refresh: rebuild the current page in place from a current moment 2821 if (session.refresh_requested) { 2822 session.refresh_requested = false; 2823 try self.rebuild(allocator, session); 2824 return true; 2825 } 2826 2827 if (session.back == .requested) { 2828 if (self.history.pop()) |entry| { 2829 session.back = if (self.history.items.len == 0) .unavailable else .available; 2830 self.root.deinit(allocator); 2831 freeArena(allocator, self.arena); 2832 self.root = entry.root; 2833 self.arena = entry.arena; 2834 self.user_id = entry.user_id; 2835 session.page_arena = entry.arena; 2836 session.data.current_page = entry.route; 2837 // a page built under another login is rebuilt rather than restored 2838 if (!std.meta.eql(entry.user_id, session.userId())) { 2839 try self.rebuild(allocator, session); 2840 return true; 2841 } 2842 chooseAnsiArtForNavigation(session); 2843 return true; 2844 } 2845 session.back = .unavailable; 2846 // nothing to go back to; switch to the quit confirmation 2847 const root_focus = self.root.getFocus(); 2848 var iter = root_focus.children.iterator(); 2849 while (iter.next()) |entry| { 2850 switch (entry.value_ptr.focus.kind) { 2851 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2852 root_focus.setFocus(entry.key_ptr.*); 2853 // a Stack only builds its selected child, so the quit 2854 // button isn't in the focus tree until a build runs with 2855 // the quit tab selected. build once, then send a 2856 // synthetic arrow_down to drop focus from the tab onto 2857 // the button so it's ready to confirm. 2858 try self.root.build(allocator, .{ 2859 .min_size = .{ .width = null, .height = 40 }, 2860 .max_size = .{ .width = 80, .height = null }, 2861 }, root_focus); 2862 try self.root.input(allocator, .arrow_down, root_focus); 2863 break; 2864 }, 2865 else => {}, 2866 } 2867 } 2868 return false; 2869 } 2870 2871 // forward navigation: navigate() set next_page to the page to move to (a 2872 // cross-page link or tab change crossing pages) 2873 if (session.next_page) |route| { 2874 session.next_page = null; 2875 if (session.haxy_moment == null and session.local == null) return false; 2876 const previous_route = session.data.current_page; 2877 // the page we navigated to becomes the current page 2878 session.data.current_page = route; 2879 session.back = .available; 2880 2881 const arena = try allocator.create(std.heap.ArenaAllocator); 2882 arena.* = std.heap.ArenaAllocator.init(allocator); 2883 errdefer freeArena(allocator, arena); 2884 2885 const new_root = try initCurrentRoot(allocator, arena, session); 2886 2887 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 2888 // drop the oldest entry (freeing its widget tree and arena) once over cap 2889 if (self.history.items.len > max_history) { 2890 var oldest = self.history.orderedRemove(0); 2891 oldest.root.deinit(allocator); 2892 freeArena(allocator, oldest.arena); 2893 } 2894 self.root = new_root; 2895 self.arena = arena; 2896 self.user_id = session.userId(); 2897 return true; 2898 } 2899 return false; 2900 } 2901 }; 2902 2903 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2904 const page_widget: Widget = switch (page.*) { 2905 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2906 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2907 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2908 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2909 .not_found => .{ .not_found = try .init(allocator, session) }, 2910 }; 2911 2912 chooseAnsiArtForNavigation(session); 2913 2914 // on the TUI/SSH, the page sits above a one-row footer showing its web 2915 // url, when there's a web UI to point at 2916 var root = if (session.is_terminal) blk: { 2917 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2918 errdefer box.deinit(allocator); 2919 const bg_id = bg_blk: { 2920 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2921 errdefer background.deinit(allocator); 2922 const id = background.getFocus().id; 2923 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2924 break :bg_blk id; 2925 }; 2926 if (session.web_port != null) { 2927 var footer = try widget.Footer.init(allocator, session); 2928 errdefer footer.deinit(allocator); 2929 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2930 } 2931 box.getFocus().child_id = bg_id; 2932 break :blk Widget{ .box = box }; 2933 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2934 errdefer root.deinit(allocator); 2935 2936 // input-owning views build their TextInputs in init, so reset their 2937 // registrations here 2938 session.text_inputs.clearRetainingCapacity(); 2939 2940 try root.build(allocator, .{ 2941 .min_size = .{ .width = null, .height = 40 }, 2942 .max_size = .{ .width = 100, .height = null }, 2943 }, root.getFocus()); 2944 2945 return root; 2946 } 2947 2948 // native and server-side sessions choose from the embedded collection. WASM 2949 // keeps the art serialized by the server instead. 2950 fn chooseAnsiArtForNavigation(session: *Session) void { 2951 if (comptime ansi_art.art.len == 0) return; 2952 if (session.data.current_page == .not_found) { 2953 session.data.ansi_art = ansi_art.not_found; 2954 return; 2955 } 2956 const io = session.io orelse return; 2957 var random: usize = undefined; 2958 io.random(std.mem.asBytes(&random)); 2959 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 2960 } 2961