╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
3084 // the quit tab selected. build once, then send a 3085 // synthetic arrow_down to drop focus from the tab onto 3086 // the button so it's ready to confirm. 3087 try self.root.build(allocator, .{ 3088 .min_size = .{ .width = null, .height = 40 }, 3089 .max_size = .{ .width = 80, .height = null }, 3090 }, root_focus); 3091 try self.root.input(allocator, .arrow_down, root_focus); 3092 break; 3093 }, 3094 else => {}, 3095 } 3096 } 3097 return false; 3098 } 3099 3100 // forward navigation: navigate() set next_page to the page to move to (a 3101 // cross-page link or tab change crossing pages) 3102 if (session.next_page) |route| { 3103 session.next_page = null; 3104 if (session.haxy_moment == null and session.local == null) return false; 3105 const previous_route = session.data.current_page; 3106 // the page we navigated to becomes the current page 3107 session.data.current_page = route; 3108 session.back = .available; 3109 3110 const arena = try allocator.create(std.heap.ArenaAllocator); 3111 arena.* = std.heap.ArenaAllocator.init(allocator); 3112 errdefer freeArena(allocator, arena); 3113 3114 const new_root = try initCurrentRoot(allocator, arena, session); 3115 3116 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3117 // drop the oldest entry (freeing its widget tree and arena) once over cap 3118 if (self.history.items.len > max_history) { 3119 var oldest = self.history.orderedRemove(0); 3120 oldest.root.deinit(allocator); 3121 freeArena(allocator, oldest.arena); 3122 } 3123 self.root = new_root; 3124 self.arena = arena; 3125 self.user_id = session.userId(); 3126 return true; 3127 } 3128 return false; 3129 } 3130 }; 3131 3132 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3133 const page_widget: Widget = switch (page.*) { 3134 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3135 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3136 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3137 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3138 .not_found => .{ .not_found = try .init(allocator, session) }, 3139 }; 3140 3141 chooseAnsiArtForNavigation(session); 3142 3143 // on the TUI/SSH, the page sits above a one-row footer showing its web 3144 // url, when there's a web UI to point at 3145 var root = if (session.is_terminal) blk: { 3146 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3147 errdefer box.deinit(allocator); 3148 const bg_id = bg_blk: { 3149 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3150 errdefer background.deinit(allocator); 3151 const id = background.getFocus().id; 3152 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3153 break :bg_blk id; 3154 }; 3155 if (session.web_port != null) { 3156 var footer = try widget.Footer.init(allocator, session); 3157 errdefer footer.deinit(allocator); 3158 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3159 } 3160 box.getFocus().child_id = bg_id; 3161 break :blk Widget{ .box = box }; 3162 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3163 errdefer root.deinit(allocator); 3164 3165 // input-owning views build their TextInputs in init, so reset their 3166 // registrations here 3167 session.text_inputs.clearRetainingCapacity(); 3168 3169 try root.build(allocator, .{ 3170 .min_size = .{ .width = null, .height = 40 }, 3171 .max_size = .{ .width = 100, .height = null }, 3172 }, root.getFocus()); 3173 3174 return root; 3175 } 3176 3177 // native and server-side sessions choose from the embedded collection. WASM 3178 // keeps the art serialized by the server instead. 3179 fn chooseAnsiArtForNavigation(session: *Session) void { 3180 if (comptime ansi_art.art.len == 0) return; 3181 if (session.data.current_page == .not_found) { 3182 session.data.ansi_art = ansi_art.not_found; 3183 return; 3184 } 3185 const io = session.io orelse return; 3186 var random: usize = undefined; 3187 io.random(std.mem.asBytes(&random)); 3188 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3189 } 3190