╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2789 const new_root = try initCurrentRoot(allocator, arena, session); 2790 2791 self.root.deinit(allocator); 2792 freeArena(allocator, self.arena); 2793 self.root = new_root; 2794 self.arena = arena; 2795 self.user_id = session.userId(); 2796 } 2797 2798 // build the current page's root in `arena`, or the not-found page's when 2799 // the route names nothing 2800 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 2801 session.page_arena = arena; 2802 const page = try arena.allocator().create(Page); 2803 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 2804 error.NotFound => blk: { 2805 session.data.current_page = .not_found; 2806 break :blk .not_found; 2807 }, 2808 else => |e| return e, 2809 }; 2810 return initRoot(allocator, page, session); 2811 } 2812 2813 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2814 arena.deinit(); 2815 allocator.destroy(arena); 2816 } 2817 2818 // reconcile the displayed root with the session's navigation state. a 2819 // forward nav (current_page moved to a different parent page) pushes the 2820 // current root and builds the new page in a fresh arena; a back request 2821 // frees the current page and restores the previous one. when escape is 2822 // pressed with no history left we switch to the quit tab instead of quitting. 2823 // returns whether the root was replaced. 2824 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 2825 // refresh: rebuild the current page in place from a current moment 2826 if (session.refresh_requested) { 2827 session.refresh_requested = false; 2828 try self.rebuild(allocator, session); 2829 return true; 2830 } 2831 2832 if (session.back == .requested) { 2833 if (self.history.pop()) |entry| { 2834 session.back = if (self.history.items.len == 0) .unavailable else .available; 2835 self.root.deinit(allocator); 2836 freeArena(allocator, self.arena); 2837 self.root = entry.root; 2838 self.arena = entry.arena; 2839 self.user_id = entry.user_id; 2840 session.page_arena = entry.arena; 2841 session.data.current_page = entry.route; 2842 // a page built under another login is rebuilt rather than restored 2843 if (!std.meta.eql(entry.user_id, session.userId())) { 2844 try self.rebuild(allocator, session); 2845 return true; 2846 } 2847 chooseAnsiArtForNavigation(session); 2848 return true; 2849 } 2850 session.back = .unavailable; 2851 // nothing to go back to; switch to the quit confirmation 2852 const root_focus = self.root.getFocus(); 2853 var iter = root_focus.children.iterator(); 2854 while (iter.next()) |entry| { 2855 switch (entry.value_ptr.focus.kind) { 2856 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2857 root_focus.setFocus(entry.key_ptr.*); 2858 // a Stack only builds its selected child, so the quit 2859 // button isn't in the focus tree until a build runs with 2860 // the quit tab selected. build once, then send a 2861 // synthetic arrow_down to drop focus from the tab onto 2862 // the button so it's ready to confirm. 2863 try self.root.build(allocator, .{ 2864 .min_size = .{ .width = null, .height = 40 }, 2865 .max_size = .{ .width = 80, .height = null }, 2866 }, root_focus); 2867 try self.root.input(allocator, .arrow_down, root_focus); 2868 break; 2869 }, 2870 else => {}, 2871 } 2872 } 2873 return false; 2874 } 2875 2876 // forward navigation: navigate() set next_page to the page to move to (a 2877 // cross-page link or tab change crossing pages) 2878 if (session.next_page) |route| { 2879 session.next_page = null; 2880 if (session.haxy_moment == null and session.local == null) return false; 2881 const previous_route = session.data.current_page; 2882 // the page we navigated to becomes the current page 2883 session.data.current_page = route; 2884 session.back = .available; 2885 2886 const arena = try allocator.create(std.heap.ArenaAllocator); 2887 arena.* = std.heap.ArenaAllocator.init(allocator); 2888 errdefer freeArena(allocator, arena); 2889 2890 const new_root = try initCurrentRoot(allocator, arena, session); 2891 2892 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 2893 // drop the oldest entry (freeing its widget tree and arena) once over cap 2894 if (self.history.items.len > max_history) { 2895 var oldest = self.history.orderedRemove(0); 2896 oldest.root.deinit(allocator); 2897 freeArena(allocator, oldest.arena); 2898 } 2899 self.root = new_root; 2900 self.arena = arena; 2901 self.user_id = session.userId(); 2902 return true; 2903 } 2904 return false; 2905 } 2906 }; 2907 2908 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2909 const page_widget: Widget = switch (page.*) { 2910 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2911 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2912 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2913 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2914 .not_found => .{ .not_found = try .init(allocator, session) }, 2915 }; 2916 2917 chooseAnsiArtForNavigation(session); 2918 2919 // on the TUI/SSH, the page sits above a one-row footer showing its web 2920 // url, when there's a web UI to point at 2921 var root = if (session.is_terminal) blk: { 2922 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2923 errdefer box.deinit(allocator); 2924 const bg_id = bg_blk: { 2925 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2926 errdefer background.deinit(allocator); 2927 const id = background.getFocus().id; 2928 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2929 break :bg_blk id; 2930 }; 2931 if (session.web_port != null) { 2932 var footer = try widget.Footer.init(allocator, session); 2933 errdefer footer.deinit(allocator); 2934 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2935 } 2936 box.getFocus().child_id = bg_id; 2937 break :blk Widget{ .box = box }; 2938 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2939 errdefer root.deinit(allocator); 2940 2941 // input-owning views build their TextInputs in init, so reset their 2942 // registrations here 2943 session.text_inputs.clearRetainingCapacity(); 2944 2945 try root.build(allocator, .{ 2946 .min_size = .{ .width = null, .height = 40 }, 2947 .max_size = .{ .width = 100, .height = null }, 2948 }, root.getFocus()); 2949 2950 return root; 2951 } 2952 2953 // native and server-side sessions choose from the embedded collection. WASM 2954 // keeps the art serialized by the server instead. 2955 fn chooseAnsiArtForNavigation(session: *Session) void { 2956 if (comptime ansi_art.art.len == 0) return; 2957 if (session.data.current_page == .not_found) { 2958 session.data.ansi_art = ansi_art.not_found; 2959 return; 2960 } 2961 const io = session.io orelse return; 2962 var random: usize = undefined; 2963 io.random(std.mem.asBytes(&random)); 2964 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 2965 } 2966