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