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