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