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