╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
3106 arena.deinit(); 3107 allocator.destroy(arena); 3108 } 3109 3110 return .{ 3111 .root = try initCurrentRoot(allocator, arena, session), 3112 .arena = arena, 3113 .user_id = session.userId(), 3114 .history = .empty, 3115 }; 3116 } 3117 3118 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 3119 self.root.deinit(allocator); 3120 freeArena(allocator, self.arena); 3121 for (self.history.items) |*entry| { 3122 entry.root.deinit(allocator); 3123 freeArena(allocator, entry.arena); 3124 } 3125 self.history.deinit(allocator); 3126 } 3127 3128 // rebuild the current page in place from a current moment 3129 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 3130 if (session.haxy_moment == null and session.local == null) return; 3131 const arena = try allocator.create(std.heap.ArenaAllocator); 3132 arena.* = std.heap.ArenaAllocator.init(allocator); 3133 errdefer freeArena(allocator, arena); 3134 3135 const new_root = try initCurrentRoot(allocator, arena, session); 3136 3137 self.root.deinit(allocator); 3138 freeArena(allocator, self.arena); 3139 self.root = new_root; 3140 self.arena = arena; 3141 self.user_id = session.userId(); 3142 } 3143 3144 // build the current page's root in `arena`, or the not-found page's when 3145 // the route names nothing 3146 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 3147 session.page_arena = arena; 3148 const page = try arena.allocator().create(Page); 3149 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 3150 error.NotFound => blk: { 3151 session.data.current_page = .not_found; 3152 break :blk .not_found; 3153 }, 3154 else => |e| return e, 3155 }; 3156 return initRoot(allocator, page, session); 3157 } 3158 3159 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3160 arena.deinit(); 3161 allocator.destroy(arena); 3162 } 3163 3164 // reconcile the displayed root with the session's navigation state. a 3165 // forward nav (current_page moved to a different parent page) pushes the 3166 // current root and builds the new page in a fresh arena; a back request 3167 // frees the current page and restores the previous one. when escape is 3168 // pressed with no history left we switch to the quit tab instead of quitting. 3169 // returns whether the root was replaced. 3170 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3171 // refresh: rebuild the current page in place from a current moment 3172 if (session.refresh_requested) { 3173 session.refresh_requested = false; 3174 try self.rebuild(allocator, session); 3175 return true; 3176 } 3177 3178 if (session.back == .requested) { 3179 if (self.history.pop()) |entry| { 3180 session.back = if (self.history.items.len == 0) .unavailable else .available; 3181 self.root.deinit(allocator); 3182 freeArena(allocator, self.arena); 3183 self.root = entry.root; 3184 self.arena = entry.arena; 3185 self.user_id = entry.user_id; 3186 session.page_arena = entry.arena; 3187 session.data.current_page = entry.route; 3188 // a page built under another login is rebuilt rather than restored 3189 if (!std.meta.eql(entry.user_id, session.userId())) { 3190 try self.rebuild(allocator, session); 3191 return true; 3192 } 3193 chooseAnsiArtForNavigation(session); 3194 return true; 3195 } 3196 session.back = .unavailable; 3197 // nothing to go back to; switch to the quit confirmation 3198 const root_focus = self.root.getFocus(); 3199 var iter = root_focus.children.iterator(); 3200 while (iter.next()) |entry| { 3201 switch (entry.value_ptr.focus.kind) { 3202 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3203 root_focus.setFocus(entry.key_ptr.*); 3204 // a Stack only builds its selected child, so the quit 3205 // button isn't in the focus tree until a build runs with 3206 // the quit tab selected. build once, then send a 3207 // synthetic arrow_down to drop focus from the tab onto 3208 // the button so it's ready to confirm. 3209 try self.root.build(allocator, .{ 3210 .min_size = .{ .width = null, .height = 40 }, 3211 .max_size = .{ .width = 80, .height = null }, 3212 }, root_focus); 3213 try self.root.input(allocator, .arrow_down, root_focus); 3214 break; 3215 }, 3216 else => {}, 3217 } 3218 } 3219 return false; 3220 } 3221 3222 // forward navigation: navigate() set next_page to the page to move to (a 3223 // cross-page link or tab change crossing pages) 3224 if (session.next_page) |route| { 3225 session.next_page = null; 3226 if (session.haxy_moment == null and session.local == null) return false; 3227 const previous_route = session.data.current_page; 3228 // the page we navigated to becomes the current page 3229 session.data.current_page = route; 3230 session.back = .available; 3231 3232 const arena = try allocator.create(std.heap.ArenaAllocator); 3233 arena.* = std.heap.ArenaAllocator.init(allocator); 3234 errdefer freeArena(allocator, arena); 3235 3236 const new_root = try initCurrentRoot(allocator, arena, session); 3237 3238 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3239 // drop the oldest entry (freeing its widget tree and arena) once over cap 3240 if (self.history.items.len > max_history) { 3241 var oldest = self.history.orderedRemove(0); 3242 oldest.root.deinit(allocator); 3243 freeArena(allocator, oldest.arena); 3244 } 3245 self.root = new_root; 3246 self.arena = arena; 3247 self.user_id = session.userId(); 3248 return true; 3249 } 3250 return false; 3251 } 3252 }; 3253 3254 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3255 const page_widget: Widget = switch (page.*) { 3256 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3257 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3258 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3259 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3260 .not_found => .{ .not_found = try .init(allocator, session) }, 3261 }; 3262 3263 chooseAnsiArtForNavigation(session); 3264 3265 // on the TUI/SSH, the page sits above a one-row footer showing its web 3266 // url, when there's a web UI to point at 3267 var root = if (session.is_terminal) blk: { 3268 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3269 errdefer box.deinit(allocator); 3270 const bg_id = bg_blk: { 3271 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3272 errdefer background.deinit(allocator); 3273 const id = background.getFocus().id; 3274 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3275 break :bg_blk id; 3276 }; 3277 if (session.web_port != null) { 3278 var footer = try widget.Footer.init(allocator, session); 3279 errdefer footer.deinit(allocator); 3280 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3281 } 3282 box.getFocus().child_id = bg_id; 3283 break :blk Widget{ .box = box }; 3284 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3285 errdefer root.deinit(allocator); 3286 3287 // input-owning views build their TextInputs in init, so reset their 3288 // registrations here 3289 session.text_inputs.clearRetainingCapacity(); 3290 3291 try root.build(allocator, .{ 3292 .min_size = .{ .width = null, .height = 40 }, 3293 .max_size = .{ .width = 100, .height = null }, 3294 }, root.getFocus()); 3295 3296 return root; 3297 } 3298 3299 // native and server-side sessions choose from the embedded collection. WASM 3300 // keeps the art serialized by the server instead. 3301 fn chooseAnsiArtForNavigation(session: *Session) void { 3302 if (comptime ansi_art.art.len == 0) return; 3303 if (session.data.current_page == .not_found) { 3304 session.data.ansi_art = ansi_art.not_found; 3305 return; 3306 } 3307 const io = session.io orelse return; 3308 var random: usize = undefined; 3309 io.random(std.mem.asBytes(&random)); 3310 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3311 } 3312