╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2222 if (self.data.host_kind == .local) { 2223 const author = blk: { 2224 const src = self.local orelse break :blk local_author_fallback; 2225 const io = self.io orelse break :blk local_author_fallback; 2226 break :blk try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 2227 }; 2228 return .{ .author = author, .role = .owner }; 2229 } 2230 const user_id = self.userId() orelse return null; 2231 // the session's moment predates this input, so read the roles afresh 2232 const admin_repo = self.admin_repo orelse return null; 2233 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 2234 return switch (try authorizeUser(moment, self.page_arena, user_id, identity, min_role)) { 2235 .actor => |actor| actor, 2236 .login_required, .repo_not_found, .forbidden => null, 2237 }; 2238 } 2239 2240 // queue an action for the host to drain this frame. 2241 pub fn push(self: *Self, action: Action) !void { 2242 try self.pending.append(self.arena.allocator(), action); 2243 } 2244 2245 // apply a single action's in-memory effect only (no persistence). 2246 fn apply(self: *Self, action: Action) void { 2247 switch (action) { 2248 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 2249 } 2250 } 2251 2252 // drain the session's queued actions by applying them to the session. 2253 // used on the wasm path, which has no repo to persist to. 2254 pub fn applyPending(self: *Self) void { 2255 for (self.pending.items) |action| self.apply(action); 2256 self.pending.clearRetainingCapacity(); 2257 } 2258 2259 // drain the session's queued actions by applying them to the session and 2260 // writing them to the db 2261 pub fn applyAndWritePending( 2262 self: *Self, 2263 io: std.Io, 2264 allocator: std.mem.Allocator, 2265 repo: *rp.Repo(.xit, evt.admin_repo_opts), 2266 ) !void { 2267 defer self.pending.clearRetainingCapacity(); 2268 for (self.pending.items) |action| { 2269 self.apply(action); 2270 switch (action) { 2271 .toggle_ansi => if (self.data.user_id) |user_id| { 2272 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, user_id); 2273 }, 2274 } 2275 } 2276 } 2277 2278 // reload the moment from the admin repo and re-read user preferences 2279 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 2280 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 2281 self.haxy_moment = moment; 2282 2283 const user_id = self.userId() orelse return; 2284 var arena = std.heap.ArenaAllocator.init(allocator); 2285 defer arena.deinit(); 2286 const user = (try activeUser(moment, &arena, user_id)) orelse return self.logOut(); 2287 self.data.enable_ansi = user.event.enable_ansi; 2288 } 2289 2290 // request a forward navigation to `route`; the host consumes next_page 2291 // (Nav.sync on the terminal, the wasm tick on the web). 2292 pub fn navigate(self: *Session, route: RoutablePage) !void { 2293 self.clearFormFeedback(); 2294 self.next_page = route; 2295 } 2296 }; 2297 2298 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 2299 var nav = try Nav.init(allocator, session); 2300 defer nav.deinit(allocator); 2301 2302 var terminal = try term.Terminal.init(io, allocator); 2303 var terminal_live = true; 2304 defer if (terminal_live) terminal.deinit(io); 2305 2306 // set term as active so it will be properly cooked 2307 // when a panic/segfault happens 2308 term.setActive(&terminal); 2309 defer term.setActive(null); 2310 2311 while (!terminal.shouldQuit()) { 2312 const grid_changed = try terminal.render(&nav.root); 2313 2314 // process any inputs. 2315 // 2316 // if the grid didn't change, then first do a blocking 2317 // read, so the thread will sleep until further input. 2318 // after that, all remaining reads are non-blocking so 2319 // we can process the rest of the queued inputs. 2320 // 2321 // if the grid *did* change, then only do non-blocking 2322 // reads. we do not want to sleep the thread because 2323 // there may be an animation that requires more looping. 2324 var blocking = !grid_changed; 2325 while (try terminal.readKey(io, blocking)) |key| { 2326 blocking = false; 2327 try inputKey(allocator, &nav.root, key, session); 2328 } 2329 2330 if (session.host_request) |request| { 2331 session.host_request = null; 2332 switch (request) { 2333 .show_copyable_text => |copyable_text| { 2334 term.setActive(null); 2335 terminal.deinit(io); 2336 terminal_live = false; 2337 showCopyableText(io, copyable_text) catch |show_err| { 2338 terminal = try term.Terminal.init(io, allocator); 2339 terminal_live = true; 2340 term.setActive(&terminal); 2341 return show_err; 2342 }; 2343 terminal = try term.Terminal.init(io, allocator); 2344 terminal_live = true; 2345 term.setActive(&terminal); 2346 }, 2347 .undo => |target| Repo.Undo.handleRequest(allocator, session, target), 2348 .sync_events => { 2349 try nav.root.build(allocator, .{ 2350 .min_size = .{ .width = null, .height = null }, 2351 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2352 }, nav.root.getFocus()); 2353 _ = try terminal.render(&nav.root); 2354 try Repo.Events.performSync(allocator, session); 2355 }, 2356 } 2357 } 2358 2359 // persist queued actions when there's a repo; local mode has none, 2360 // so just apply in-memory 2361 if (repo_maybe) |repo| { 2362 try session.applyAndWritePending(io, allocator, repo); 2363 // reload so the next navigation builds its page from the current moment 2364 try session.reloadMoment(allocator, repo); 2365 } else { 2366 session.applyPending(); 2367 } 2368 2369 // reconcile navigation: forward to a new page, or back on escape. 2370 try nav.sync(allocator, session); 2371 2372 // the quit button (on the quit tab) asks the host to tear down. 2373 if (session.quit_requested) terminal.requestQuit(); 2374 2375 try nav.root.build(allocator, .{ 2376 .min_size = .{ .width = null, .height = null }, 2377 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2378 }, nav.root.getFocus()); 2379 } 2380 } 2381 2382 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2383 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 2384 null 2385 else 2386 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2387 defer if (tty) |file| file.close(io); 2388 2389 const input = if (tty) |file| file else std.Io.File.stdin(); 2390 const output = if (tty) |file| file else std.Io.File.stdout(); 2391 2392 var write_buf: [1024]u8 = undefined; 2393 var writer = output.writer(io, &write_buf); 2394 try writer.interface.print("\x1b[2J\x1b[Hcopy the following text and then press enter to go back:\r\n\r\n{s}\r\n", .{copyable_text}); 2395 try writer.interface.flush(); 2396 2397 var read_buf: [1]u8 = undefined; 2398 var reader = input.reader(io, &read_buf); 2399 while (true) { 2400 const byte = try reader.interface.takeByte(); 2401 if (byte == '\r' or byte == '\n') return; 2402 } 2403 } 2404 2405 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2406 const root_focus = root.getFocus(); 2407 const focused_before = root_focus.grandchild_id; 2408 var activate_in_page_link = false; 2409 switch (key) { 2410 // request a navigation pop; the host's Nav.sync goes back a page, or 2411 // quits when there's no history left. 2412 .escape => { 2413 session.back = .requested; 2414 return; 2415 }, 2416 .ctrl => |letter| switch (letter) { 2417 'r' => { 2418 session.refresh_requested = true; 2419 return; 2420 }, 2421 else => try root.input(allocator, key, root_focus), 2422 }, 2423 .enter => { 2424 if (root_focus.grandchild_id) |gid| { 2425 // follow a cross-page link 2426 if (crossPageLink(root_focus, gid, session.data)) |route| { 2427 return session.navigate(route); 2428 } 2429 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2430 } 2431 activate_in_page_link = true; 2432 try root.input(allocator, key, root_focus); 2433 }, 2434 .mouse => |mouse| { 2435 if (mouse.action == .press and mouse.action.press == .left) { 2436 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2437 const focus_id = hit.id; 2438 if (hit.kind == .background) { 2439 root_focus.setFocus(focus_id); 2440 return; 2441 } 2442 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2443 if (session.back == .available) session.back = .requested; 2444 return; 2445 } 2446 // follow a cross-page link 2447 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2448 return session.navigate(route); 2449 } 2450 if (rawLink(root_focus, focus_id)) |url| { 2451 if (!mouse.ctrl) try requestRawLink(session, url); 2452 return; 2453 } 2454 root_focus.setFocus(focus_id); 2455 activate_in_page_link = true; 2456 } 2457 // forward the press into the widget tree so buttons (and any 2458 // future click-aware widgets) can react. widgets that don't 2459 // care about presses ignore it. 2460 try root.input(allocator, key, root_focus); 2461 } else { 2462 try root.input(allocator, key, root_focus); 2463 } 2464 }, 2465 else => try root.input(allocator, key, root_focus), 2466 } 2467 2468 const focused_after = root_focus.grandchild_id; 2469 if (!activate_in_page_link and focused_before == focused_after) return; 2470 const focus_id = focused_after orelse return; 2471 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2472 } 2473 2474 // a link to bytes the server serves directly rather than to a page route, so 2475 // it never resolves to a RoutablePage 2476 pub const raw_link_prefix = "ax:"; 2477 2478 // a route already loaded into the current page; focusing it changes the url 2479 // without rebuilding the page 2480 pub const in_page_link_prefix = "ai:"; 2481 2482 // an in-page tab's base route, or the exact current route when it is selected 2483 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2484 const target = if (selected) session.data.current_page else route; 2485 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2486 } 2487 2488 // a button the web renderer covers with a file picker that posts the chosen 2489 // file to the url after the prefix 2490 pub const file_input_prefix = "file:"; 2491 2492 // a submit button that overrides its enclosing form's action 2493 pub const submit_action_prefix = "submit:"; 2494 2495 // the url an `ax:` link points at, or null for anything else. a host that 2496 // suppresses the anchor's own navigation needs this to follow the link. 2497 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2498 const child = root_focus.children.get(focus_id) orelse return null; 2499 const custom = switch (child.focus.kind) { 2500 .custom => |c| c, 2501 else => return null, 2502 }; 2503 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2504 return custom[raw_link_prefix.len..]; 2505 } 2506 2507 fn requestRawLink(session: *Session, url: []const u8) !void { 2508 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2509 } 2510 2511 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2512 return if (session.web_port) |port| 2513 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2514 else 2515 path; 2516 } 2517 2518 // if the focus target at focus_id is an `a:` link that should navigate (a 2519 // different parent page, or a different files directory within the repo page), 2520 // return its route; otherwise null. lets a host turn a click / enter on such a 2521 // link into a navigation rather than a focus change. 2522 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2523 const current = data.current_page; 2524 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2525 // a link to a different parent page always navigates; within a page, a 2526 // files-directory / commits-page / list-window change navigates while tab 2527 // links stay in-page. 2528 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.forkPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 2529 return null; 2530 } 2531 2532 // the route carried by an `ai:` focus target, or null for any other target 2533 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2534 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2535 } 2536 2537 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2538 const child = root_focus.children.get(focus_id) orelse return null; 2539 const custom = switch (child.focus.kind) { 2540 .custom => |c| c, 2541 else => return null, 2542 }; 2543 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2544 const path = custom[prefix.len..]; 2545 // local sessions build (and parse) links with the repo identity elided 2546 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2547 } 2548 2549 // a display author, resolved against the admin db at read time 2550 pub const Author = union(enum) { 2551 unknown, 2552 email: []const u8, // an email no user matches 2553 user_name: []const u8, 2554 2555 // the commit author line's email, resolved like initFromEmail 2556 pub fn init( 2557 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2558 arena: *std.heap.ArenaAllocator, 2559 author_line: []const u8, 2560 ) !Author { 2561 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2562 } 2563 2564 // from a record's stored author email: it resolves to the user currently 2565 // holding it, else stays the email. the result is allocated in `arena`, 2566 // so the email may borrow transient memory. 2567 pub fn initFromEmail( 2568 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2569 arena: *std.heap.ArenaAllocator, 2570 author_email: ?[]const u8, 2571 ) !Author { 2572 const email = author_email orelse return .unknown; 2573 if (haxy_moment) |moment| { 2574 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2575 return .{ .user_name = user.event.name }; 2576 } 2577 } 2578 return .{ .email = try arena.allocator().dupe(u8, email) }; 2579 } 2580 }; 2581 2582 // the "a:" link to user `name`'s page. 2583 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2584 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2585 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2586 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2587 } 2588 2589 // a focusable " author " box showing `author`, linking to their user page 2590 // when it is a known user 2591 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2592 const text = switch (author) { 2593 .unknown => "", 2594 .email, .user_name => |t| t, 2595 }; 2596 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2597 errdefer tb.deinit(allocator); 2598 tb.getFocus().mode = .all; 2599 switch (author) { 2600 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2601 else => {}, 2602 } 2603 return tb; 2604 } 2605 2606 // native-TUI navigation 2607 pub const Nav = struct { 2608 root: Widget, 2609 // backs the current page (its Page data and the page-scoped strings its 2610 // widgets build). owned here, swapped on each navigation. session.page_arena 2611 // tracks whichever of these is current so widgets allocate into it. 2612 arena: *std.heap.ArenaAllocator, 2613 history: std.ArrayList(Entry), 2614 2615 // each retained page keeps its own arena, freed when the page leaves the 2616 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2617 // long-lived session from accumulating every page it ever visited. 2618 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2619 2620 // cap on retained back-history so the chain can't grow memory without bound 2621 const max_history: usize = 16; 2622 2623 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2624 session.back = .unavailable; 2625 const arena = try allocator.create(std.heap.ArenaAllocator); 2626 arena.* = std.heap.ArenaAllocator.init(allocator); 2627 errdefer { 2628 arena.deinit(); 2629 allocator.destroy(arena); 2630 } 2631 2632 session.page_arena = arena; 2633 const route = session.data.current_page; 2634 2635 const page = try arena.allocator().create(Page); 2636 page.* = try Page.init(arena, session, route); 2637 return .{ 2638 .root = try initRoot(allocator, page, session), 2639 .arena = arena, 2640 .history = .empty, 2641 }; 2642 } 2643 2644 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2645 self.root.deinit(allocator); 2646 freeArena(allocator, self.arena); 2647 for (self.history.items) |*entry| { 2648 entry.root.deinit(allocator); 2649 freeArena(allocator, entry.arena); 2650 } 2651 self.history.deinit(allocator); 2652 } 2653 2654 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2655 arena.deinit(); 2656 allocator.destroy(arena); 2657 } 2658 2659 // reconcile the displayed root with the session's navigation state. a 2660 // forward nav (current_page moved to a different parent page) pushes the 2661 // current root and builds the new page in a fresh arena; a back request 2662 // frees the current page and restores the previous one. when escape is 2663 // pressed with no history left we switch to the quit tab instead of quitting. 2664 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2665 // refresh: rebuild the current page in place from a current moment 2666 if (session.refresh_requested) { 2667 session.refresh_requested = false; 2668 if (session.haxy_moment != null or session.local != null) { 2669 const arena = try allocator.create(std.heap.ArenaAllocator); 2670 arena.* = std.heap.ArenaAllocator.init(allocator); 2671 errdefer freeArena(allocator, arena); 2672 2673 session.page_arena = arena; 2674 2675 const route = session.data.current_page; 2676 const page = try arena.allocator().create(Page); 2677 page.* = try Page.init(arena, session, route); 2678 const new_root = try initRoot(allocator, page, session); 2679 2680 self.root.deinit(allocator); 2681 freeArena(allocator, self.arena); 2682 self.root = new_root; 2683 self.arena = arena; 2684 } 2685 return; 2686 } 2687 2688 if (session.back == .requested) { 2689 if (self.history.pop()) |entry| { 2690 session.back = if (self.history.items.len == 0) .unavailable else .available; 2691 self.root.deinit(allocator); 2692 freeArena(allocator, self.arena); 2693 self.root = entry.root; 2694 self.arena = entry.arena; 2695 session.page_arena = entry.arena; 2696 session.data.current_page = entry.route; 2697 chooseAnsiArtForNavigation(session); 2698 return; 2699 } 2700 session.back = .unavailable; 2701 // nothing to go back to; switch to the quit confirmation 2702 const root_focus = self.root.getFocus(); 2703 var iter = root_focus.children.iterator(); 2704 while (iter.next()) |entry| { 2705 switch (entry.value_ptr.focus.kind) { 2706 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2707 root_focus.setFocus(entry.key_ptr.*); 2708 // a Stack only builds its selected child, so the quit 2709 // button isn't in the focus tree until a build runs with 2710 // the quit tab selected. build once, then send a 2711 // synthetic arrow_down to drop focus from the tab onto 2712 // the button so it's ready to confirm. 2713 try self.root.build(allocator, .{ 2714 .min_size = .{ .width = null, .height = 40 }, 2715 .max_size = .{ .width = 80, .height = null }, 2716 }, root_focus); 2717 try self.root.input(allocator, .arrow_down, root_focus); 2718 break; 2719 }, 2720 else => {}, 2721 } 2722 } 2723 return; 2724 } 2725 2726 // forward navigation: navigate() set next_page to the page to move to (a 2727 // cross-page link or tab change crossing pages) 2728 if (session.next_page) |route| { 2729 session.next_page = null; 2730 if (session.haxy_moment == null and session.local == null) return; 2731 const previous_route = session.data.current_page; 2732 // the page we navigated to becomes the current page 2733 session.data.current_page = route; 2734 session.back = .available; 2735 2736 const arena = try allocator.create(std.heap.ArenaAllocator); 2737 arena.* = std.heap.ArenaAllocator.init(allocator); 2738 errdefer freeArena(allocator, arena); 2739 2740 session.page_arena = arena; 2741 2742 const page = try arena.allocator().create(Page); 2743 page.* = try Page.init(arena, session, route); 2744 const new_root = try initRoot(allocator, page, session); 2745 2746 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2747 // drop the oldest entry (freeing its widget tree and arena) once over cap 2748 if (self.history.items.len > max_history) { 2749 var oldest = self.history.orderedRemove(0); 2750 oldest.root.deinit(allocator); 2751 freeArena(allocator, oldest.arena); 2752 } 2753 self.root = new_root; 2754 self.arena = arena; 2755 } 2756 } 2757 }; 2758 2759 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2760 const page_widget: Widget = switch (page.*) { 2761 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2762 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2763 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2764 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2765 }; 2766 2767 chooseAnsiArtForNavigation(session); 2768 2769 // on the TUI/SSH, the page sits above a one-row footer showing the url 2770 var root = if (session.is_terminal) blk: { 2771 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2772 errdefer box.deinit(allocator); 2773 const bg_id = bg_blk: { 2774 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2775 errdefer background.deinit(allocator); 2776 const id = background.getFocus().id; 2777 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2778 break :bg_blk id; 2779 }; 2780 { 2781 var footer = try widget.Footer.init(allocator, session); 2782 errdefer footer.deinit(allocator); 2783 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2784 } 2785 box.getFocus().child_id = bg_id; 2786 break :blk Widget{ .box = box }; 2787 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2788 errdefer root.deinit(allocator); 2789 2790 // input-owning views build their TextInputs in init, so reset their 2791 // registrations here 2792 session.text_inputs.clearRetainingCapacity(); 2793 2794 try root.build(allocator, .{ 2795 .min_size = .{ .width = null, .height = 40 }, 2796 .max_size = .{ .width = 100, .height = null }, 2797 }, root.getFocus()); 2798 2799 return root; 2800 } 2801 2802 // native and server-side sessions choose from the embedded collection. WASM 2803 // keeps the art serialized by the server instead. 2804 fn chooseAnsiArtForNavigation(session: *Session) void { 2805 if (comptime ansi_arts.len == 0) return; 2806 const io = session.io orelse return; 2807 var random: usize = undefined; 2808 io.random(std.mem.asBytes(&random)); 2809 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2810 } 2811