╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
3229 if (session.haxy_moment == null and session.local == null) return; 3230 const arena = try allocator.create(std.heap.ArenaAllocator); 3231 arena.* = std.heap.ArenaAllocator.init(allocator); 3232 errdefer freeArena(allocator, arena); 3233 3234 const new_root = try initCurrentRoot(allocator, arena, session); 3235 3236 self.root.deinit(allocator); 3237 freeArena(allocator, self.arena); 3238 self.root = new_root; 3239 self.arena = arena; 3240 self.user_id = session.userId(); 3241 } 3242 3243 // build the current page's root in `arena`, or the not-found page's when 3244 // the route names nothing 3245 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 3246 session.page_arena = arena; 3247 const page = try arena.allocator().create(Page); 3248 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 3249 error.NotFound => blk: { 3250 session.data.current_page = .not_found; 3251 break :blk .not_found; 3252 }, 3253 else => |e| return e, 3254 }; 3255 return initRoot(allocator, page, session); 3256 } 3257 3258 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3259 arena.deinit(); 3260 allocator.destroy(arena); 3261 } 3262 3263 // reconcile the displayed root with the session's navigation state. a 3264 // forward nav (current_page moved to a different parent page) pushes the 3265 // current root and builds the new page in a fresh arena; a back request 3266 // frees the current page and restores the previous one. when escape is 3267 // pressed with no history left we switch to the quit tab instead of quitting. 3268 // returns whether the root was replaced. 3269 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3270 // refresh: rebuild the current page in place from a current moment 3271 if (session.refresh_requested) { 3272 session.refresh_requested = false; 3273 try self.rebuild(allocator, session); 3274 return true; 3275 } 3276 3277 if (session.back == .requested) { 3278 if (self.history.pop()) |entry| { 3279 session.back = if (self.history.items.len == 0) .unavailable else .available; 3280 self.root.deinit(allocator); 3281 freeArena(allocator, self.arena); 3282 self.root = entry.root; 3283 self.arena = entry.arena; 3284 self.user_id = entry.user_id; 3285 session.page_arena = entry.arena; 3286 session.data.current_page = entry.route; 3287 // a page built under another login is rebuilt rather than restored 3288 if (!std.meta.eql(entry.user_id, session.userId())) { 3289 try self.rebuild(allocator, session); 3290 return true; 3291 } 3292 chooseAnsiArtForNavigation(session); 3293 return true; 3294 } 3295 session.back = .unavailable; 3296 // nothing to go back to; switch to the quit confirmation 3297 const root_focus = self.root.getFocus(); 3298 var iter = root_focus.children.iterator(); 3299 while (iter.next()) |entry| { 3300 switch (entry.value_ptr.focus.kind) { 3301 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3302 root_focus.setFocus(entry.key_ptr.*); 3303 // a Stack only builds its selected child, so the quit 3304 // button isn't in the focus tree until a build runs with 3305 // the quit tab selected. build once, then send a 3306 // synthetic arrow_down to drop focus from the tab onto 3307 // the button so it's ready to confirm. 3308 try self.root.build(allocator, .{ 3309 .min_size = .{ .width = null, .height = 40 }, 3310 .max_size = .{ .width = 80, .height = null }, 3311 }, root_focus); 3312 try self.root.input(allocator, .arrow_down, root_focus); 3313 break; 3314 }, 3315 else => {}, 3316 } 3317 } 3318 return false; 3319 } 3320 3321 // forward navigation: navigate() set next_page to the page to move to (a 3322 // cross-page link or tab change crossing pages) 3323 if (session.next_page) |route| { 3324 session.next_page = null; 3325 if (session.haxy_moment == null and session.local == null) return false; 3326 const previous_route = session.data.current_page; 3327 // the page we navigated to becomes the current page 3328 session.data.current_page = route; 3329 session.back = .available; 3330 3331 const arena = try allocator.create(std.heap.ArenaAllocator); 3332 arena.* = std.heap.ArenaAllocator.init(allocator); 3333 errdefer freeArena(allocator, arena); 3334 3335 const new_root = try initCurrentRoot(allocator, arena, session); 3336 3337 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3338 // drop the oldest entry (freeing its widget tree and arena) once over cap 3339 if (self.history.items.len > max_history) { 3340 var oldest = self.history.orderedRemove(0); 3341 oldest.root.deinit(allocator); 3342 freeArena(allocator, oldest.arena); 3343 } 3344 self.root = new_root; 3345 self.arena = arena; 3346 self.user_id = session.userId(); 3347 return true; 3348 } 3349 return false; 3350 } 3351 }; 3352 3353 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3354 const page_widget: Widget = switch (page.*) { 3355 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3356 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3357 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3358 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3359 .not_found => .{ .not_found = try .init(allocator, session) }, 3360 }; 3361 3362 chooseAnsiArtForNavigation(session); 3363 3364 // on the TUI/SSH, the page sits above a one-row footer showing its web 3365 // url, when there's a web UI to point at 3366 var root = if (session.is_terminal) blk: { 3367 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3368 errdefer box.deinit(allocator); 3369 const bg_id = bg_blk: { 3370 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3371 errdefer background.deinit(allocator); 3372 const id = background.getFocus().id; 3373 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3374 break :bg_blk id; 3375 }; 3376 if (session.web_url != null) { 3377 var footer = try widget.Footer.init(allocator, session); 3378 errdefer footer.deinit(allocator); 3379 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3380 } 3381 box.getFocus().child_id = bg_id; 3382 break :blk Widget{ .box = box }; 3383 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3384 errdefer root.deinit(allocator); 3385 3386 // input-owning views build their TextInputs in init, so reset their 3387 // registrations here 3388 session.text_inputs.clearRetainingCapacity(); 3389 3390 try root.build(allocator, .{ 3391 .min_size = .{ .width = null, .height = 40 }, 3392 .max_size = .{ .width = 100, .height = null }, 3393 }, root.getFocus()); 3394 3395 return root; 3396 } 3397 3398 // native and server-side sessions choose from the embedded collection. WASM 3399 // keeps the art serialized by the server instead. 3400 fn chooseAnsiArtForNavigation(session: *Session) void { 3401 if (comptime ansi_art.art.len == 0) return; 3402 if (session.data.current_page == .not_found) { 3403 session.data.ansi_art = ansi_art.not_found; 3404 return; 3405 } 3406 const io = session.io orelse return; 3407 var random: usize = undefined; 3408 io.random(std.mem.asBytes(&random)); 3409 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3410 } 3411