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