╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2433 .mouse => |mouse| { 2434 if (mouse.action == .press and mouse.action.press == .left) { 2435 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2436 const focus_id = hit.id; 2437 if (hit.kind == .background) { 2438 root_focus.setFocus(focus_id); 2439 return; 2440 } 2441 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2442 if (session.back == .available) session.back = .requested; 2443 return; 2444 } 2445 // follow a cross-page link 2446 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2447 return session.navigate(route); 2448 } 2449 if (rawLink(root_focus, focus_id)) |url| { 2450 if (!mouse.ctrl) try requestRawLink(session, url); 2451 return; 2452 } 2453 root_focus.setFocus(focus_id); 2454 activate_in_page_link = true; 2455 } 2456 // forward the press into the widget tree so buttons (and any 2457 // future click-aware widgets) can react. widgets that don't 2458 // care about presses ignore it. 2459 try root.input(allocator, key, root_focus); 2460 } else { 2461 try root.input(allocator, key, root_focus); 2462 } 2463 }, 2464 else => try root.input(allocator, key, root_focus), 2465 } 2466 2467 const focused_after = root_focus.grandchild_id; 2468 if (!activate_in_page_link and focused_before == focused_after) return; 2469 const focus_id = focused_after orelse return; 2470 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2471 } 2472 2473 // a link to bytes the server serves directly rather than to a page route, so 2474 // it never resolves to a RoutablePage 2475 pub const raw_link_prefix = "ax:"; 2476 2477 // a route already loaded into the current page; focusing it changes the url 2478 // without rebuilding the page 2479 pub const in_page_link_prefix = "ai:"; 2480 2481 // an in-page tab's base route, or the exact current route when it is selected 2482 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2483 const target = if (selected) session.data.current_page else route; 2484 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2485 } 2486 2487 // a button the web renderer covers with a file picker that posts the chosen 2488 // file to the url after the prefix 2489 pub const file_input_prefix = "file:"; 2490 2491 // a submit button that overrides its enclosing form's action 2492 pub const submit_action_prefix = "submit:"; 2493 2494 // the url an `ax:` link points at, or null for anything else. a host that 2495 // suppresses the anchor's own navigation needs this to follow the link. 2496 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2497 const child = root_focus.children.get(focus_id) orelse return null; 2498 const custom = switch (child.focus.kind) { 2499 .custom => |c| c, 2500 else => return null, 2501 }; 2502 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2503 return custom[raw_link_prefix.len..]; 2504 } 2505 2506 fn requestRawLink(session: *Session, url: []const u8) !void { 2507 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2508 } 2509 2510 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2511 return if (session.web_port) |port| 2512 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2513 else 2514 path; 2515 } 2516 2517 // if the focus target at focus_id is an `a:` link that should navigate (a 2518 // different parent page, or a different files directory within the repo page), 2519 // return its route; otherwise null. lets a host turn a click / enter on such a 2520 // link into a navigation rather than a focus change. 2521 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2522 const current = data.current_page; 2523 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2524 // a link to a different parent page always navigates; within a page, a 2525 // files-directory / commits-page / list-window change navigates while tab 2526 // links stay in-page. 2527 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; 2528 return null; 2529 } 2530 2531 // the route carried by an `ai:` focus target, or null for any other target 2532 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2533 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2534 } 2535 2536 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2537 const child = root_focus.children.get(focus_id) orelse return null; 2538 const custom = switch (child.focus.kind) { 2539 .custom => |c| c, 2540 else => return null, 2541 }; 2542 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2543 const path = custom[prefix.len..]; 2544 // local sessions build (and parse) links with the repo identity elided 2545 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2546 } 2547 2548 // a display author, resolved against the admin db at read time 2549 pub const Author = union(enum) { 2550 unknown, 2551 email: []const u8, // an email no user matches 2552 user_name: []const u8, 2553 2554 // the commit author line's email, resolved like initFromEmail 2555 pub fn init( 2556 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2557 arena: *std.heap.ArenaAllocator, 2558 author_line: []const u8, 2559 ) !Author { 2560 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2561 } 2562 2563 // from a record's stored author email: it resolves to the user currently 2564 // holding it, else stays the email. the result is allocated in `arena`, 2565 // so the email may borrow transient memory. 2566 pub fn initFromEmail( 2567 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2568 arena: *std.heap.ArenaAllocator, 2569 author_email: ?[]const u8, 2570 ) !Author { 2571 const email = author_email orelse return .unknown; 2572 if (haxy_moment) |moment| { 2573 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2574 return .{ .user_name = user.event.name }; 2575 } 2576 } 2577 return .{ .email = try arena.allocator().dupe(u8, email) }; 2578 } 2579 }; 2580 2581 // the "a:" link to user `name`'s page. 2582 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2583 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2584 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2585 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2586 } 2587 2588 // a focusable " author " box showing `author`, linking to their user page 2589 // when it is a known user 2590 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2591 const text = switch (author) { 2592 .unknown => "", 2593 .email, .user_name => |t| t, 2594 }; 2595 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2596 errdefer tb.deinit(allocator); 2597 tb.getFocus().mode = .all; 2598 switch (author) { 2599 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2600 else => {}, 2601 } 2602 return tb; 2603 } 2604 2605 // native-TUI navigation 2606 pub const Nav = struct { 2607 root: Widget, 2608 // backs the current page (its Page data and the page-scoped strings its 2609 // widgets build). owned here, swapped on each navigation. session.page_arena 2610 // tracks whichever of these is current so widgets allocate into it. 2611 arena: *std.heap.ArenaAllocator, 2612 history: std.ArrayList(Entry), 2613 2614 // each retained page keeps its own arena, freed when the page leaves the 2615 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2616 // long-lived session from accumulating every page it ever visited. 2617 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2618 2619 // cap on retained back-history so the chain can't grow memory without bound 2620 const max_history: usize = 16; 2621 2622 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2623 session.back = .unavailable; 2624 const arena = try allocator.create(std.heap.ArenaAllocator); 2625 arena.* = std.heap.ArenaAllocator.init(allocator); 2626 errdefer { 2627 arena.deinit(); 2628 allocator.destroy(arena); 2629 } 2630 2631 session.page_arena = arena; 2632 const route = session.data.current_page; 2633 2634 const page = try arena.allocator().create(Page); 2635 page.* = try Page.init(arena, session, route); 2636 return .{ 2637 .root = try initRoot(allocator, page, session), 2638 .arena = arena, 2639 .history = .empty, 2640 }; 2641 } 2642 2643 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2644 self.root.deinit(allocator); 2645 freeArena(allocator, self.arena); 2646 for (self.history.items) |*entry| { 2647 entry.root.deinit(allocator); 2648 freeArena(allocator, entry.arena); 2649 } 2650 self.history.deinit(allocator); 2651 } 2652 2653 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2654 arena.deinit(); 2655 allocator.destroy(arena); 2656 } 2657 2658 // reconcile the displayed root with the session's navigation state. a 2659 // forward nav (current_page moved to a different parent page) pushes the 2660 // current root and builds the new page in a fresh arena; a back request 2661 // frees the current page and restores the previous one. when escape is 2662 // pressed with no history left we switch to the quit tab instead of quitting. 2663 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2664 // refresh: rebuild the current page in place from a current moment 2665 if (session.refresh_requested) { 2666 session.refresh_requested = false; 2667 if (session.haxy_moment != null or session.local != null) { 2668 const arena = try allocator.create(std.heap.ArenaAllocator); 2669 arena.* = std.heap.ArenaAllocator.init(allocator); 2670 errdefer freeArena(allocator, arena); 2671 2672 session.page_arena = arena; 2673 2674 const route = session.data.current_page; 2675 const page = try arena.allocator().create(Page); 2676 page.* = try Page.init(arena, session, route); 2677 const new_root = try initRoot(allocator, page, session); 2678 2679 self.root.deinit(allocator); 2680 freeArena(allocator, self.arena); 2681 self.root = new_root; 2682 self.arena = arena; 2683 } 2684 return; 2685 } 2686 2687 if (session.back == .requested) { 2688 if (self.history.pop()) |entry| { 2689 session.back = if (self.history.items.len == 0) .unavailable else .available; 2690 self.root.deinit(allocator); 2691 freeArena(allocator, self.arena); 2692 self.root = entry.root; 2693 self.arena = entry.arena; 2694 session.page_arena = entry.arena; 2695 session.data.current_page = entry.route; 2696 chooseAnsiArtForNavigation(session); 2697 return; 2698 } 2699 session.back = .unavailable; 2700 // nothing to go back to; switch to the quit confirmation 2701 const root_focus = self.root.getFocus(); 2702 var iter = root_focus.children.iterator(); 2703 while (iter.next()) |entry| { 2704 switch (entry.value_ptr.focus.kind) { 2705 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2706 root_focus.setFocus(entry.key_ptr.*); 2707 // a Stack only builds its selected child, so the quit 2708 // button isn't in the focus tree until a build runs with 2709 // the quit tab selected. build once, then send a 2710 // synthetic arrow_down to drop focus from the tab onto 2711 // the button so it's ready to confirm. 2712 try self.root.build(allocator, .{ 2713 .min_size = .{ .width = null, .height = 40 }, 2714 .max_size = .{ .width = 80, .height = null }, 2715 }, root_focus); 2716 try self.root.input(allocator, .arrow_down, root_focus); 2717 break; 2718 }, 2719 else => {}, 2720 } 2721 } 2722 return; 2723 } 2724 2725 // forward navigation: navigate() set next_page to the page to move to (a 2726 // cross-page link or tab change crossing pages) 2727 if (session.next_page) |route| { 2728 session.next_page = null; 2729 if (session.haxy_moment == null and session.local == null) return; 2730 const previous_route = session.data.current_page; 2731 // the page we navigated to becomes the current page 2732 session.data.current_page = route; 2733 session.back = .available; 2734 2735 const arena = try allocator.create(std.heap.ArenaAllocator); 2736 arena.* = std.heap.ArenaAllocator.init(allocator); 2737 errdefer freeArena(allocator, arena); 2738 2739 session.page_arena = arena; 2740 2741 const page = try arena.allocator().create(Page); 2742 page.* = try Page.init(arena, session, route); 2743 const new_root = try initRoot(allocator, page, session); 2744 2745 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2746 // drop the oldest entry (freeing its widget tree and arena) once over cap 2747 if (self.history.items.len > max_history) { 2748 var oldest = self.history.orderedRemove(0); 2749 oldest.root.deinit(allocator); 2750 freeArena(allocator, oldest.arena); 2751 } 2752 self.root = new_root; 2753 self.arena = arena; 2754 } 2755 } 2756 }; 2757 2758 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2759 const page_widget: Widget = switch (page.*) { 2760 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2761 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2762 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2763 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2764 }; 2765 2766 chooseAnsiArtForNavigation(session); 2767 2768 // on the TUI/SSH, the page sits above a one-row footer showing the url 2769 var root = if (session.is_terminal) blk: { 2770 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2771 errdefer box.deinit(allocator); 2772 const bg_id = bg_blk: { 2773 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2774 errdefer background.deinit(allocator); 2775 const id = background.getFocus().id; 2776 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2777 break :bg_blk id; 2778 }; 2779 { 2780 var footer = try widget.Footer.init(allocator, session); 2781 errdefer footer.deinit(allocator); 2782 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2783 } 2784 box.getFocus().child_id = bg_id; 2785 break :blk Widget{ .box = box }; 2786 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2787 errdefer root.deinit(allocator); 2788 2789 // input-owning views build their TextInputs in init, so reset their 2790 // registrations here 2791 session.text_inputs.clearRetainingCapacity(); 2792 2793 try root.build(allocator, .{ 2794 .min_size = .{ .width = null, .height = 40 }, 2795 .max_size = .{ .width = 100, .height = null }, 2796 }, root.getFocus()); 2797 2798 return root; 2799 } 2800 2801 // native and server-side sessions choose from the embedded collection. WASM 2802 // keeps the art serialized by the server instead. 2803 fn chooseAnsiArtForNavigation(session: *Session) void { 2804 if (comptime ansi_arts.len == 0) return; 2805 const io = session.io orelse return; 2806 var random: usize = undefined; 2807 io.random(std.mem.asBytes(&random)); 2808 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2809 } 2810