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