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