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