╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2721 const focused_after = root_focus.grandchild_id; 2722 if (!activate_in_page_link and focused_before == focused_after) return; 2723 const focus_id = focused_after orelse return; 2724 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2725 } 2726 2727 // the innermost vertically scrolling widget laid out under `position` 2728 fn scrollUnder(root_focus: *const Focus, position: xitui.input.Position) ?usize { 2729 var found: ?usize = null; 2730 var found_area: usize = std.math.maxInt(usize); 2731 var iter = root_focus.children.iterator(); 2732 while (iter.next()) |entry| { 2733 const info = entry.value_ptr.focus.scroll orelse continue; 2734 if (info.direction == .horiz) continue; 2735 const r = entry.value_ptr.rect; 2736 if (position.x < r.x or position.y < r.y or position.x >= r.x + r.size.width or position.y >= r.y + r.size.height) continue; 2737 // a nested scroll lies within its parent, so the smallest is innermost 2738 const area = r.size.width * r.size.height; 2739 if (area < found_area) { 2740 found = entry.key_ptr.*; 2741 found_area = area; 2742 } 2743 } 2744 return found; 2745 } 2746 2747 // the innermost vertically scrolling widget the focus lies in, unless the 2748 // focused widget is a text input 2749 fn focusedPane(root_focus: *const Focus) ?usize { 2750 var current = root_focus.grandchild_id orelse return null; 2751 switch ((root_focus.children.get(current) orelse return null).focus.kind) { 2752 .text_input, .text_input_password, .text_area => return null, 2753 else => {}, 2754 } 2755 while (root_focus.children.get(current)) |child| { 2756 if (child.focus.scroll) |info| if (info.direction != .horiz) return current; 2757 current = child.parent_id; 2758 } 2759 return null; 2760 } 2761 2762 // a link to bytes the server serves directly rather than to a page route, so 2763 // it never resolves to a RoutablePage 2764 pub const raw_link_prefix = "ax:"; 2765 2766 // a route already loaded into the current page; focusing it changes the url 2767 // without rebuilding the page 2768 pub const in_page_link_prefix = "ai:"; 2769 2770 // an in-page tab's base route, or the exact current route when it is selected 2771 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2772 const target = if (selected) session.data.current_page else route; 2773 return session.page_arena.allocator().print("{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2774 } 2775 2776 // a button the web renderer covers with a file picker that posts the chosen 2777 // file to the url after the prefix 2778 pub const file_input_prefix = "file:"; 2779 2780 // a submit button that overrides its enclosing form's action 2781 pub const submit_action_prefix = "submit:"; 2782 2783 // one option of a radio field, as `radio:<name>=<value>` 2784 pub const radio_prefix = "radio:"; 2785 2786 // the url an `ax:` link points at, or null for anything else. a host that 2787 // suppresses the anchor's own navigation needs this to follow the link. 2788 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2789 const child = root_focus.children.get(focus_id) orelse return null; 2790 const custom = switch (child.focus.kind) { 2791 .custom => |c| c, 2792 else => return null, 2793 }; 2794 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2795 return custom[raw_link_prefix.len..]; 2796 } 2797 2798 fn requestRawLink(session: *Session, url: []const u8) !void { 2799 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2800 } 2801 2802 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2803 // an absolute url is already complete 2804 if (!std.mem.startsWith(u8, path, "/")) return path; 2805 return if (session.web_port) |port| 2806 try allocator.print("http://localhost:{d}{s}", .{ port, path }) 2807 else 2808 path; 2809 } 2810 2811 // if the focus target at focus_id is an `a:` link that should navigate (a 2812 // different parent page, or a different files directory within the repo page), 2813 // return its route; otherwise null. lets a host turn a click / enter on such a 2814 // link into a navigation rather than a focus change. 2815 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2816 const current = data.current_page; 2817 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2818 // a link to a different parent page always navigates; within a page, a 2819 // files-directory / commits-page / list-window change navigates while tab 2820 // links stay in-page. 2821 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; 2822 return null; 2823 } 2824 2825 // the page the clicked cell links to, from a link inside a text box's content 2826 fn cellPageLink(root: *Widget, x: usize, y: usize, data: Session.Data) ?RoutablePage { 2827 const grid = root.getGrid() orelse return null; 2828 const cell = grid.cell(x, y) catch return null; 2829 const link = cell.link orelse return null; 2830 if (link.len == 0 or link[0] != '/') return null; 2831 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(link) else RoutablePage.fromUrl(link); 2832 } 2833 2834 // whether the `a:` focus target links to the page already shown, which a click 2835 // leaves alone rather than focusing 2836 pub fn currentPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) bool { 2837 const route = pageLink(root_focus, focus_id, data, "a:") orelse return false; 2838 return route.eql(data.current_page); 2839 } 2840 2841 // the route carried by an `ai:` focus target, or null for any other target 2842 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2843 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2844 } 2845 2846 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2847 const child = root_focus.children.get(focus_id) orelse return null; 2848 const custom = switch (child.focus.kind) { 2849 .custom => |c| c, 2850 else => return null, 2851 }; 2852 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2853 const path = custom[prefix.len..]; 2854 // local sessions build (and parse) links with the repo identity elided 2855 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2856 } 2857 2858 // an assignee as the detail view shows it: the stored email, and the name of 2859 // the user holding it on this instance when there is one 2860 pub const Assignee = struct { 2861 email: []const u8, 2862 name: ?[]const u8 = null, 2863 2864 pub fn display(self: Assignee) []const u8 { 2865 return self.name orelse self.email; 2866 } 2867 }; 2868 2869 // a display author, resolved against the admin db at read time 2870 pub const Author = union(enum) { 2871 unknown, 2872 email: []const u8, // an email no user matches 2873 user_name: []const u8, 2874 2875 // the commit author line's email, resolved like initFromEmail 2876 pub fn init( 2877 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2878 arena: *std.heap.ArenaAllocator, 2879 author_line: []const u8, 2880 ) !Author { 2881 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2882 } 2883 2884 // from a record's stored author email: it resolves to the user currently 2885 // holding it, else stays the email. the result is allocated in `arena`, 2886 // so the email may borrow transient memory. 2887 pub fn initFromEmail( 2888 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2889 arena: *std.heap.ArenaAllocator, 2890 author_email: ?[]const u8, 2891 ) !Author { 2892 const email = author_email orelse return .unknown; 2893 if (haxy_moment) |moment| { 2894 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2895 return .{ .user_name = user.name }; 2896 } 2897 } 2898 return .{ .email = try arena.allocator().dupe(u8, email) }; 2899 } 2900 }; 2901 2902 // the "a:" link to user `name`'s page. 2903 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2904 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2905 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2906 return page_arena.allocator().print("a:{s}", .{url}); 2907 } 2908 2909 // a focusable " author " box showing `author`, linking to their user page 2910 // when it is a known user 2911 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2912 const text = switch (author) { 2913 .unknown => "", 2914 .email, .user_name => |t| t, 2915 }; 2916 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " author " } }); 2917 errdefer tb.deinit(allocator); 2918 tb.getFocus().mode = .all; 2919 switch (author) { 2920 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2921 else => {}, 2922 } 2923 return tb; 2924 } 2925 2926 // native-TUI navigation 2927 pub const Nav = struct { 2928 root: Widget, 2929 // backs the current page (its Page data and the page-scoped strings its 2930 // widgets build). owned here, swapped on each navigation. session.page_arena 2931 // tracks whichever of these is current so widgets allocate into it. 2932 arena: *std.heap.ArenaAllocator, 2933 // the login the current root was built for 2934 user_id: ?[evt.event_id_size]u8, 2935 history: std.ArrayList(Entry), 2936 2937 // each retained page keeps its own arena, freed when the page leaves the 2938 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2939 // long-lived session from accumulating every page it ever visited. 2940 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 2941 2942 // cap on retained back-history so the chain can't grow memory without bound 2943 const max_history: usize = 16; 2944 2945 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2946 session.back = .unavailable; 2947 const arena = try allocator.create(std.heap.ArenaAllocator); 2948 arena.* = std.heap.ArenaAllocator.init(allocator); 2949 errdefer { 2950 arena.deinit(); 2951 allocator.destroy(arena); 2952 } 2953 2954 return .{ 2955 .root = try initCurrentRoot(allocator, arena, session), 2956 .arena = arena, 2957 .user_id = session.userId(), 2958 .history = .empty, 2959 }; 2960 } 2961 2962 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2963 self.root.deinit(allocator); 2964 freeArena(allocator, self.arena); 2965 for (self.history.items) |*entry| { 2966 entry.root.deinit(allocator); 2967 freeArena(allocator, entry.arena); 2968 } 2969 self.history.deinit(allocator); 2970 } 2971 2972 // rebuild the current page in place from a current moment 2973 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2974 if (session.haxy_moment == null and session.local == null) return; 2975 const arena = try allocator.create(std.heap.ArenaAllocator); 2976 arena.* = std.heap.ArenaAllocator.init(allocator); 2977 errdefer freeArena(allocator, arena); 2978 2979 const new_root = try initCurrentRoot(allocator, arena, session); 2980 2981 self.root.deinit(allocator); 2982 freeArena(allocator, self.arena); 2983 self.root = new_root; 2984 self.arena = arena; 2985 self.user_id = session.userId(); 2986 } 2987 2988 // build the current page's root in `arena`, or the not-found page's when 2989 // the route names nothing 2990 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 2991 session.page_arena = arena; 2992 const page = try arena.allocator().create(Page); 2993 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 2994 error.NotFound => blk: { 2995 session.data.current_page = .not_found; 2996 break :blk .not_found; 2997 }, 2998 else => |e| return e, 2999 }; 3000 return initRoot(allocator, page, session); 3001 } 3002 3003 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3004 arena.deinit(); 3005 allocator.destroy(arena); 3006 } 3007 3008 // reconcile the displayed root with the session's navigation state. a 3009 // forward nav (current_page moved to a different parent page) pushes the 3010 // current root and builds the new page in a fresh arena; a back request 3011 // frees the current page and restores the previous one. when escape is 3012 // pressed with no history left we switch to the quit tab instead of quitting. 3013 // returns whether the root was replaced. 3014 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3015 // refresh: rebuild the current page in place from a current moment 3016 if (session.refresh_requested) { 3017 session.refresh_requested = false; 3018 try self.rebuild(allocator, session); 3019 return true; 3020 } 3021 3022 if (session.back == .requested) { 3023 if (self.history.pop()) |entry| { 3024 session.back = if (self.history.items.len == 0) .unavailable else .available; 3025 self.root.deinit(allocator); 3026 freeArena(allocator, self.arena); 3027 self.root = entry.root; 3028 self.arena = entry.arena; 3029 self.user_id = entry.user_id; 3030 session.page_arena = entry.arena; 3031 session.data.current_page = entry.route; 3032 // a page built under another login is rebuilt rather than restored 3033 if (!std.meta.eql(entry.user_id, session.userId())) { 3034 try self.rebuild(allocator, session); 3035 return true; 3036 } 3037 chooseAnsiArtForNavigation(session); 3038 return true; 3039 } 3040 session.back = .unavailable; 3041 // nothing to go back to; switch to the quit confirmation 3042 const root_focus = self.root.getFocus(); 3043 var iter = root_focus.children.iterator(); 3044 while (iter.next()) |entry| { 3045 switch (entry.value_ptr.focus.kind) { 3046 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3047 root_focus.setFocus(entry.key_ptr.*); 3048 // a Stack only builds its selected child, so the quit 3049 // button isn't in the focus tree until a build runs with 3050 // the quit tab selected. build once, then send a 3051 // synthetic arrow_down to drop focus from the tab onto 3052 // the button so it's ready to confirm. 3053 try self.root.build(allocator, .{ 3054 .min_size = .{ .width = null, .height = 40 }, 3055 .max_size = .{ .width = 80, .height = null }, 3056 }, root_focus); 3057 try self.root.input(allocator, .arrow_down, root_focus); 3058 break; 3059 }, 3060 else => {}, 3061 } 3062 } 3063 return false; 3064 } 3065 3066 // forward navigation: navigate() set next_page to the page to move to (a 3067 // cross-page link or tab change crossing pages) 3068 if (session.next_page) |route| { 3069 session.next_page = null; 3070 if (session.haxy_moment == null and session.local == null) return false; 3071 const previous_route = session.data.current_page; 3072 // the page we navigated to becomes the current page 3073 session.data.current_page = route; 3074 session.back = .available; 3075 3076 const arena = try allocator.create(std.heap.ArenaAllocator); 3077 arena.* = std.heap.ArenaAllocator.init(allocator); 3078 errdefer freeArena(allocator, arena); 3079 3080 const new_root = try initCurrentRoot(allocator, arena, session); 3081 3082 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3083 // drop the oldest entry (freeing its widget tree and arena) once over cap 3084 if (self.history.items.len > max_history) { 3085 var oldest = self.history.orderedRemove(0); 3086 oldest.root.deinit(allocator); 3087 freeArena(allocator, oldest.arena); 3088 } 3089 self.root = new_root; 3090 self.arena = arena; 3091 self.user_id = session.userId(); 3092 return true; 3093 } 3094 return false; 3095 } 3096 }; 3097 3098 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3099 const page_widget: Widget = switch (page.*) { 3100 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3101 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3102 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3103 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3104 .not_found => .{ .not_found = try .init(allocator, session) }, 3105 }; 3106 3107 chooseAnsiArtForNavigation(session); 3108 3109 // on the TUI/SSH, the page sits above a one-row footer showing its web 3110 // url, when there's a web UI to point at 3111 var root = if (session.is_terminal) blk: { 3112 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3113 errdefer box.deinit(allocator); 3114 const bg_id = bg_blk: { 3115 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3116 errdefer background.deinit(allocator); 3117 const id = background.getFocus().id; 3118 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3119 break :bg_blk id; 3120 }; 3121 if (session.web_port != null) { 3122 var footer = try widget.Footer.init(allocator, session); 3123 errdefer footer.deinit(allocator); 3124 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3125 } 3126 box.getFocus().child_id = bg_id; 3127 break :blk Widget{ .box = box }; 3128 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3129 errdefer root.deinit(allocator); 3130 3131 // input-owning views build their TextInputs in init, so reset their 3132 // registrations here 3133 session.text_inputs.clearRetainingCapacity(); 3134 3135 try root.build(allocator, .{ 3136 .min_size = .{ .width = null, .height = 40 }, 3137 .max_size = .{ .width = 100, .height = null }, 3138 }, root.getFocus()); 3139 3140 return root; 3141 } 3142 3143 // native and server-side sessions choose from the embedded collection. WASM 3144 // keeps the art serialized by the server instead. 3145 fn chooseAnsiArtForNavigation(session: *Session) void { 3146 if (comptime ansi_art.art.len == 0) return; 3147 if (session.data.current_page == .not_found) { 3148 session.data.ansi_art = ansi_art.not_found; 3149 return; 3150 } 3151 const io = session.io orelse return; 3152 var random: usize = undefined; 3153 io.random(std.mem.asBytes(&random)); 3154 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3155 } 3156