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