╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2601 activate_in_page_link = true; 2602 try root.input(allocator, key, root_focus); 2603 }, 2604 .mouse => |mouse| { 2605 if (mouse.action == .press and mouse.action.press == .left) { 2606 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2607 const focus_id = hit.id; 2608 if (hit.kind == .background) { 2609 root_focus.setFocus(focus_id); 2610 return; 2611 } 2612 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2613 if (session.back == .available) session.back = .requested; 2614 return; 2615 } 2616 // follow a cross-page link 2617 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2618 return session.navigate(route); 2619 } 2620 if (rawLink(root_focus, focus_id)) |url| { 2621 if (!mouse.ctrl) try requestRawLink(session, url); 2622 return; 2623 } 2624 root_focus.setFocus(focus_id); 2625 activate_in_page_link = true; 2626 } 2627 // forward the press into the widget tree so buttons (and any 2628 // future click-aware widgets) can react. widgets that don't 2629 // care about presses ignore it. 2630 try root.input(allocator, key, root_focus); 2631 } else { 2632 try root.input(allocator, key, root_focus); 2633 } 2634 }, 2635 else => try root.input(allocator, key, root_focus), 2636 } 2637 2638 const focused_after = root_focus.grandchild_id; 2639 if (!activate_in_page_link and focused_before == focused_after) return; 2640 const focus_id = focused_after orelse return; 2641 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2642 } 2643 2644 // a link to bytes the server serves directly rather than to a page route, so 2645 // it never resolves to a RoutablePage 2646 pub const raw_link_prefix = "ax:"; 2647 2648 // a route already loaded into the current page; focusing it changes the url 2649 // without rebuilding the page 2650 pub const in_page_link_prefix = "ai:"; 2651 2652 // an in-page tab's base route, or the exact current route when it is selected 2653 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2654 const target = if (selected) session.data.current_page else route; 2655 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2656 } 2657 2658 // a button the web renderer covers with a file picker that posts the chosen 2659 // file to the url after the prefix 2660 pub const file_input_prefix = "file:"; 2661 2662 // a submit button that overrides its enclosing form's action 2663 pub const submit_action_prefix = "submit:"; 2664 2665 // one option of a radio field, as `radio:<name>=<value>` 2666 pub const radio_prefix = "radio:"; 2667 2668 // the url an `ax:` link points at, or null for anything else. a host that 2669 // suppresses the anchor's own navigation needs this to follow the link. 2670 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2671 const child = root_focus.children.get(focus_id) orelse return null; 2672 const custom = switch (child.focus.kind) { 2673 .custom => |c| c, 2674 else => return null, 2675 }; 2676 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2677 return custom[raw_link_prefix.len..]; 2678 } 2679 2680 fn requestRawLink(session: *Session, url: []const u8) !void { 2681 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2682 } 2683 2684 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2685 // an absolute url is already complete 2686 if (!std.mem.startsWith(u8, path, "/")) return path; 2687 return if (session.web_port) |port| 2688 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2689 else 2690 path; 2691 } 2692 2693 // if the focus target at focus_id is an `a:` link that should navigate (a 2694 // different parent page, or a different files directory within the repo page), 2695 // return its route; otherwise null. lets a host turn a click / enter on such a 2696 // link into a navigation rather than a focus change. 2697 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2698 const current = data.current_page; 2699 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2700 // a link to a different parent page always navigates; within a page, a 2701 // files-directory / commits-page / list-window change navigates while tab 2702 // links stay in-page. 2703 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; 2704 return null; 2705 } 2706 2707 // the route carried by an `ai:` focus target, or null for any other target 2708 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2709 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2710 } 2711 2712 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2713 const child = root_focus.children.get(focus_id) orelse return null; 2714 const custom = switch (child.focus.kind) { 2715 .custom => |c| c, 2716 else => return null, 2717 }; 2718 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2719 const path = custom[prefix.len..]; 2720 // local sessions build (and parse) links with the repo identity elided 2721 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2722 } 2723 2724 // a display author, resolved against the admin db at read time 2725 pub const Author = union(enum) { 2726 unknown, 2727 email: []const u8, // an email no user matches 2728 user_name: []const u8, 2729 2730 // the commit author line's email, resolved like initFromEmail 2731 pub fn init( 2732 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2733 arena: *std.heap.ArenaAllocator, 2734 author_line: []const u8, 2735 ) !Author { 2736 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2737 } 2738 2739 // from a record's stored author email: it resolves to the user currently 2740 // holding it, else stays the email. the result is allocated in `arena`, 2741 // so the email may borrow transient memory. 2742 pub fn initFromEmail( 2743 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2744 arena: *std.heap.ArenaAllocator, 2745 author_email: ?[]const u8, 2746 ) !Author { 2747 const email = author_email orelse return .unknown; 2748 if (haxy_moment) |moment| { 2749 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2750 return .{ .user_name = user.name }; 2751 } 2752 } 2753 return .{ .email = try arena.allocator().dupe(u8, email) }; 2754 } 2755 }; 2756 2757 // the "a:" link to user `name`'s page. 2758 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2759 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2760 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2761 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2762 } 2763 2764 // a focusable " author " box showing `author`, linking to their user page 2765 // when it is a known user 2766 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2767 const text = switch (author) { 2768 .unknown => "", 2769 .email, .user_name => |t| t, 2770 }; 2771 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .label = " author " }); 2772 errdefer tb.deinit(allocator); 2773 tb.getFocus().mode = .all; 2774 switch (author) { 2775 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2776 else => {}, 2777 } 2778 return tb; 2779 } 2780 2781 // native-TUI navigation 2782 pub const Nav = struct { 2783 root: Widget, 2784 // backs the current page (its Page data and the page-scoped strings its 2785 // widgets build). owned here, swapped on each navigation. session.page_arena 2786 // tracks whichever of these is current so widgets allocate into it. 2787 arena: *std.heap.ArenaAllocator, 2788 // the login the current root was built for 2789 user_id: ?[evt.event_id_size]u8, 2790 history: std.ArrayList(Entry), 2791 2792 // each retained page keeps its own arena, freed when the page leaves the 2793 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2794 // long-lived session from accumulating every page it ever visited. 2795 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 2796 2797 // cap on retained back-history so the chain can't grow memory without bound 2798 const max_history: usize = 16; 2799 2800 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2801 session.back = .unavailable; 2802 const arena = try allocator.create(std.heap.ArenaAllocator); 2803 arena.* = std.heap.ArenaAllocator.init(allocator); 2804 errdefer { 2805 arena.deinit(); 2806 allocator.destroy(arena); 2807 } 2808 2809 return .{ 2810 .root = try initCurrentRoot(allocator, arena, session), 2811 .arena = arena, 2812 .user_id = session.userId(), 2813 .history = .empty, 2814 }; 2815 } 2816 2817 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2818 self.root.deinit(allocator); 2819 freeArena(allocator, self.arena); 2820 for (self.history.items) |*entry| { 2821 entry.root.deinit(allocator); 2822 freeArena(allocator, entry.arena); 2823 } 2824 self.history.deinit(allocator); 2825 } 2826 2827 // rebuild the current page in place from a current moment 2828 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2829 if (session.haxy_moment == null and session.local == null) return; 2830 const arena = try allocator.create(std.heap.ArenaAllocator); 2831 arena.* = std.heap.ArenaAllocator.init(allocator); 2832 errdefer freeArena(allocator, arena); 2833 2834 const new_root = try initCurrentRoot(allocator, arena, session); 2835 2836 self.root.deinit(allocator); 2837 freeArena(allocator, self.arena); 2838 self.root = new_root; 2839 self.arena = arena; 2840 self.user_id = session.userId(); 2841 } 2842 2843 // build the current page's root in `arena`, or the not-found page's when 2844 // the route names nothing 2845 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 2846 session.page_arena = arena; 2847 const page = try arena.allocator().create(Page); 2848 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 2849 error.NotFound => blk: { 2850 session.data.current_page = .not_found; 2851 break :blk .not_found; 2852 }, 2853 else => |e| return e, 2854 }; 2855 return initRoot(allocator, page, session); 2856 } 2857 2858 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2859 arena.deinit(); 2860 allocator.destroy(arena); 2861 } 2862 2863 // reconcile the displayed root with the session's navigation state. a 2864 // forward nav (current_page moved to a different parent page) pushes the 2865 // current root and builds the new page in a fresh arena; a back request 2866 // frees the current page and restores the previous one. when escape is 2867 // pressed with no history left we switch to the quit tab instead of quitting. 2868 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2869 // refresh: rebuild the current page in place from a current moment 2870 if (session.refresh_requested) { 2871 session.refresh_requested = false; 2872 try self.rebuild(allocator, session); 2873 return; 2874 } 2875 2876 if (session.back == .requested) { 2877 if (self.history.pop()) |entry| { 2878 session.back = if (self.history.items.len == 0) .unavailable else .available; 2879 self.root.deinit(allocator); 2880 freeArena(allocator, self.arena); 2881 self.root = entry.root; 2882 self.arena = entry.arena; 2883 self.user_id = entry.user_id; 2884 session.page_arena = entry.arena; 2885 session.data.current_page = entry.route; 2886 // a page built under another login is rebuilt rather than restored 2887 if (!std.meta.eql(entry.user_id, session.userId())) { 2888 try self.rebuild(allocator, session); 2889 return; 2890 } 2891 chooseAnsiArtForNavigation(session); 2892 return; 2893 } 2894 session.back = .unavailable; 2895 // nothing to go back to; switch to the quit confirmation 2896 const root_focus = self.root.getFocus(); 2897 var iter = root_focus.children.iterator(); 2898 while (iter.next()) |entry| { 2899 switch (entry.value_ptr.focus.kind) { 2900 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2901 root_focus.setFocus(entry.key_ptr.*); 2902 // a Stack only builds its selected child, so the quit 2903 // button isn't in the focus tree until a build runs with 2904 // the quit tab selected. build once, then send a 2905 // synthetic arrow_down to drop focus from the tab onto 2906 // the button so it's ready to confirm. 2907 try self.root.build(allocator, .{ 2908 .min_size = .{ .width = null, .height = 40 }, 2909 .max_size = .{ .width = 80, .height = null }, 2910 }, root_focus); 2911 try self.root.input(allocator, .arrow_down, root_focus); 2912 break; 2913 }, 2914 else => {}, 2915 } 2916 } 2917 return; 2918 } 2919 2920 // forward navigation: navigate() set next_page to the page to move to (a 2921 // cross-page link or tab change crossing pages) 2922 if (session.next_page) |route| { 2923 session.next_page = null; 2924 if (session.haxy_moment == null and session.local == null) return; 2925 const previous_route = session.data.current_page; 2926 // the page we navigated to becomes the current page 2927 session.data.current_page = route; 2928 session.back = .available; 2929 2930 const arena = try allocator.create(std.heap.ArenaAllocator); 2931 arena.* = std.heap.ArenaAllocator.init(allocator); 2932 errdefer freeArena(allocator, arena); 2933 2934 const new_root = try initCurrentRoot(allocator, arena, session); 2935 2936 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 2937 // drop the oldest entry (freeing its widget tree and arena) once over cap 2938 if (self.history.items.len > max_history) { 2939 var oldest = self.history.orderedRemove(0); 2940 oldest.root.deinit(allocator); 2941 freeArena(allocator, oldest.arena); 2942 } 2943 self.root = new_root; 2944 self.arena = arena; 2945 self.user_id = session.userId(); 2946 } 2947 } 2948 }; 2949 2950 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2951 const page_widget: Widget = switch (page.*) { 2952 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2953 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2954 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2955 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2956 .not_found => .{ .not_found = try .init(allocator, session) }, 2957 }; 2958 2959 chooseAnsiArtForNavigation(session); 2960 2961 // on the TUI/SSH, the page sits above a one-row footer showing its web 2962 // url, when there's a web UI to point at 2963 var root = if (session.is_terminal) blk: { 2964 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2965 errdefer box.deinit(allocator); 2966 const bg_id = bg_blk: { 2967 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2968 errdefer background.deinit(allocator); 2969 const id = background.getFocus().id; 2970 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2971 break :bg_blk id; 2972 }; 2973 if (session.web_port != null) { 2974 var footer = try widget.Footer.init(allocator, session); 2975 errdefer footer.deinit(allocator); 2976 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2977 } 2978 box.getFocus().child_id = bg_id; 2979 break :blk Widget{ .box = box }; 2980 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2981 errdefer root.deinit(allocator); 2982 2983 // input-owning views build their TextInputs in init, so reset their 2984 // registrations here 2985 session.text_inputs.clearRetainingCapacity(); 2986 2987 try root.build(allocator, .{ 2988 .min_size = .{ .width = null, .height = 40 }, 2989 .max_size = .{ .width = 100, .height = null }, 2990 }, root.getFocus()); 2991 2992 return root; 2993 } 2994 2995 // native and server-side sessions choose from the embedded collection. WASM 2996 // keeps the art serialized by the server instead. 2997 fn chooseAnsiArtForNavigation(session: *Session) void { 2998 if (comptime ansi_art.art.len == 0) return; 2999 if (session.data.current_page == .not_found) { 3000 session.data.ansi_art = ansi_art.not_found; 3001 return; 3002 } 3003 const io = session.io orelse return; 3004 var random: usize = undefined; 3005 io.random(std.mem.asBytes(&random)); 3006 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3007 } 3008