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