╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2251 session.refresh_requested = false; 2252 if (session.haxy_moment != null or session.local != null) { 2253 const arena = try allocator.create(std.heap.ArenaAllocator); 2254 arena.* = std.heap.ArenaAllocator.init(allocator); 2255 errdefer freeArena(allocator, arena); 2256 2257 session.page_arena = arena; 2258 2259 const route = session.data.current_page; 2260 const page = try arena.allocator().create(Page); 2261 page.* = try Page.init(arena, session, route); 2262 const new_root = try initRoot(allocator, page, session); 2263 2264 self.root.deinit(allocator); 2265 freeArena(allocator, self.arena); 2266 self.root = new_root; 2267 self.route = route; 2268 self.arena = arena; 2269 } 2270 return; 2271 } 2272 2273 if (session.nav_back) { 2274 session.nav_back = false; 2275 if (self.history.pop()) |entry| { 2276 self.root.deinit(allocator); 2277 freeArena(allocator, self.arena); 2278 self.root = entry.root; 2279 self.route = entry.route; 2280 self.arena = entry.arena; 2281 session.page_arena = entry.arena; 2282 session.data.current_page = entry.route; 2283 chooseAnsiArtForNavigation(session); 2284 return; 2285 } 2286 // nothing to go back to; switch to the quit confirmation 2287 const root_focus = self.root.getFocus(); 2288 var iter = root_focus.children.iterator(); 2289 while (iter.next()) |entry| { 2290 switch (entry.value_ptr.focus.kind) { 2291 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2292 root_focus.setFocus(entry.key_ptr.*); 2293 // a Stack only builds its selected child, so the quit 2294 // button isn't in the focus tree until a build runs with 2295 // the quit tab selected. build once, then send a 2296 // synthetic arrow_down to drop focus from the tab onto 2297 // the button so it's ready to confirm. 2298 try self.root.build(allocator, .{ 2299 .min_size = .{ .width = null, .height = 40 }, 2300 .max_size = .{ .width = 80, .height = null }, 2301 }, root_focus); 2302 try self.root.input(allocator, .arrow_down, root_focus); 2303 break; 2304 }, 2305 else => {}, 2306 } 2307 } 2308 return; 2309 } 2310 2311 // forward navigation: navigate() set next_page to the page to move to (a 2312 // cross-page link or tab change crossing pages) 2313 if (session.next_page) |route| { 2314 session.next_page = null; 2315 if (session.haxy_moment == null and session.local == null) return; 2316 // the page we navigated to becomes the current page 2317 session.data.current_page = route; 2318 2319 const arena = try allocator.create(std.heap.ArenaAllocator); 2320 arena.* = std.heap.ArenaAllocator.init(allocator); 2321 errdefer freeArena(allocator, arena); 2322 2323 session.page_arena = arena; 2324 2325 const page = try arena.allocator().create(Page); 2326 page.* = try Page.init(arena, session, route); 2327 const new_root = try initRoot(allocator, page, session); 2328 2329 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 2330 // drop the oldest entry (freeing its widget tree and arena) once over cap 2331 if (self.history.items.len > max_history) { 2332 var oldest = self.history.orderedRemove(0); 2333 oldest.root.deinit(allocator); 2334 freeArena(allocator, oldest.arena); 2335 } 2336 self.root = new_root; 2337 self.route = route; 2338 self.arena = arena; 2339 } 2340 } 2341 }; 2342 2343 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2344 const page_widget: Widget = switch (page.*) { 2345 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2346 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2347 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2348 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2349 }; 2350 2351 chooseAnsiArtForNavigation(session); 2352 2353 // on the TUI/SSH, the page sits above a one-row footer showing the url 2354 var root = if (session.is_terminal) blk: { 2355 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2356 errdefer box.deinit(allocator); 2357 const bg_id = bg_blk: { 2358 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2359 errdefer background.deinit(allocator); 2360 const id = background.getFocus().id; 2361 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2362 break :bg_blk id; 2363 }; 2364 { 2365 var footer = try widget.Footer.init(allocator, session); 2366 errdefer footer.deinit(allocator); 2367 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2368 } 2369 box.getFocus().child_id = bg_id; 2370 break :blk Widget{ .box = box }; 2371 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2372 errdefer root.deinit(allocator); 2373 2374 // input-owning views build their TextInputs in init, so reset their 2375 // registrations here 2376 session.text_inputs.clearRetainingCapacity(); 2377 2378 try root.build(allocator, .{ 2379 .min_size = .{ .width = null, .height = 40 }, 2380 .max_size = .{ .width = 100, .height = null }, 2381 }, root.getFocus()); 2382 2383 return root; 2384 } 2385 2386 // native and server-side sessions choose from the embedded collection. WASM 2387 // keeps the art serialized by the server instead. 2388 fn chooseAnsiArtForNavigation(session: *Session) void { 2389 if (comptime ansi_arts.len == 0) return; 2390 const io = session.io orelse return; 2391 var random: usize = undefined; 2392 io.random(std.mem.asBytes(&random)); 2393 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2394 } 2395