╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1954 }, 1955 .mouse => |mouse| { 1956 if (mouse.action == .press and mouse.action.press == .left) { 1957 var clicked: ?usize = null; 1958 var iter = root_focus.children.iterator(); 1959 while (iter.next()) |entry| { 1960 const child = entry.value_ptr.*; 1961 if (!child.focus.focusable) continue; 1962 const r = child.rect; 1963 if (mouse.x >= r.x and mouse.y >= r.y and 1964 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 1965 { 1966 clicked = entry.key_ptr.*; 1967 break; 1968 } 1969 } 1970 if (clicked) |focus_id| { 1971 // follow a cross-page link 1972 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 1973 return session.navigate(route); 1974 } 1975 if (rawLink(root_focus, focus_id)) |url| { 1976 if (!mouse.ctrl) try requestRawLink(session, url); 1977 return; 1978 } 1979 root_focus.setFocus(focus_id); 1980 activate_in_page_link = true; 1981 } 1982 // forward the press into the widget tree so buttons (and any 1983 // future click-aware widgets) can react. widgets that don't 1984 // care about presses ignore it. 1985 try root.input(allocator, key, root_focus); 1986 } else { 1987 try root.input(allocator, key, root_focus); 1988 } 1989 }, 1990 else => try root.input(allocator, key, root_focus), 1991 } 1992 1993 const focused_after = root_focus.grandchild_id; 1994 if (!activate_in_page_link and focused_before == focused_after) return; 1995 const focus_id = focused_after orelse return; 1996 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 1997 } 1998 1999 // a link to bytes the server serves directly rather than to a page route, so 2000 // it never resolves to a RoutablePage 2001 pub const raw_link_prefix = "ax:"; 2002 2003 // a route already loaded into the current page; focusing it changes the url 2004 // without rebuilding the page 2005 pub const in_page_link_prefix = "ai:"; 2006 2007 // an in-page tab's base route, or the exact current route when it is selected 2008 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2009 const target = if (selected) session.data.current_page else route; 2010 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2011 } 2012 2013 // a button the web renderer covers with a file picker that posts the chosen 2014 // file to the url after the prefix 2015 pub const file_input_prefix = "file:"; 2016 2017 // a submit button that overrides its enclosing form's action 2018 pub const submit_action_prefix = "submit:"; 2019 2020 // the url an `ax:` link points at, or null for anything else. a host that 2021 // suppresses the anchor's own navigation needs this to follow the link. 2022 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2023 const child = root_focus.children.get(focus_id) orelse return null; 2024 const custom = switch (child.focus.kind) { 2025 .custom => |c| c, 2026 else => return null, 2027 }; 2028 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2029 return custom[raw_link_prefix.len..]; 2030 } 2031 2032 fn requestRawLink(session: *Session, url: []const u8) !void { 2033 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2034 } 2035 2036 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2037 return if (session.web_port) |port| 2038 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2039 else 2040 path; 2041 } 2042 2043 // if the focus target at focus_id is an `a:` link that should navigate (a 2044 // different parent page, or a different files directory within the repo page), 2045 // return its route; otherwise null. lets a host turn a click / enter on such a 2046 // link into a navigation rather than a focus change. 2047 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2048 const current = data.current_page; 2049 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2050 // a link to a different parent page always navigates; within a page, a 2051 // files-directory / commits-page / list-window change navigates while tab 2052 // links stay in-page. 2053 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; 2054 return null; 2055 } 2056 2057 // the route carried by an `ai:` focus target, or null for any other target 2058 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2059 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2060 } 2061 2062 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2063 const child = root_focus.children.get(focus_id) orelse return null; 2064 const custom = switch (child.focus.kind) { 2065 .custom => |c| c, 2066 else => return null, 2067 }; 2068 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2069 const path = custom[prefix.len..]; 2070 // local sessions build (and parse) links with the repo identity elided 2071 return if (data.is_local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2072 } 2073 2074 // a display author, resolved against the admin db at read time 2075 pub const Author = union(enum) { 2076 unknown, 2077 email: []const u8, // an email no user matches 2078 user_name: []const u8, 2079 2080 // the commit author line's email, resolved like initFromEmail 2081 pub fn init( 2082 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2083 arena: *std.heap.ArenaAllocator, 2084 author_line: []const u8, 2085 ) !Author { 2086 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2087 } 2088 2089 // from a record's stored author email: it resolves to the user currently 2090 // holding it, else stays the email. the result is allocated in `arena`, 2091 // so the email may borrow transient memory. 2092 pub fn initFromEmail( 2093 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2094 arena: *std.heap.ArenaAllocator, 2095 author_email: ?[]const u8, 2096 ) !Author { 2097 const email = author_email orelse return .unknown; 2098 if (haxy_moment) |moment| { 2099 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2100 return .{ .user_name = user.event.name }; 2101 } 2102 } 2103 return .{ .email = try arena.allocator().dupe(u8, email) }; 2104 } 2105 }; 2106 2107 // the "a:" link to user `name`'s page. 2108 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2109 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2110 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2111 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2112 } 2113 2114 // a focusable " author " box showing `author`, linking to their user page 2115 // when it is a known user 2116 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2117 const text = switch (author) { 2118 .unknown => "", 2119 .email, .user_name => |t| t, 2120 }; 2121 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2122 errdefer tb.deinit(allocator); 2123 tb.getFocus().focusable = true; 2124 switch (author) { 2125 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2126 else => {}, 2127 } 2128 return tb; 2129 } 2130 2131 // native-TUI navigation 2132 pub const Nav = struct { 2133 root: Widget, 2134 route: RoutablePage, 2135 // backs the current page (its Page data and the page-scoped strings its 2136 // widgets build). owned here, swapped on each navigation. session.page_arena 2137 // tracks whichever of these is current so widgets allocate into it. 2138 arena: *std.heap.ArenaAllocator, 2139 history: std.ArrayList(Entry), 2140 2141 // each retained page keeps its own arena, freed when the page leaves the 2142 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2143 // long-lived session from accumulating every page it ever visited. 2144 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2145 2146 // cap on retained back-history so the chain can't grow memory without bound 2147 const max_history: usize = 16; 2148 2149 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2150 const arena = try allocator.create(std.heap.ArenaAllocator); 2151 arena.* = std.heap.ArenaAllocator.init(allocator); 2152 errdefer { 2153 arena.deinit(); 2154 allocator.destroy(arena); 2155 } 2156 2157 session.page_arena = arena; 2158 const route = session.data.current_page; 2159 2160 const page = try arena.allocator().create(Page); 2161 page.* = try Page.init(arena, session, route); 2162 return .{ 2163 .root = try initRoot(allocator, page, session), 2164 .route = route, 2165 .arena = arena, 2166 .history = .empty, 2167 }; 2168 } 2169 2170 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2171 self.root.deinit(allocator); 2172 freeArena(allocator, self.arena); 2173 for (self.history.items) |*entry| { 2174 entry.root.deinit(allocator); 2175 freeArena(allocator, entry.arena); 2176 } 2177 self.history.deinit(allocator); 2178 } 2179 2180 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2181 arena.deinit(); 2182 allocator.destroy(arena); 2183 } 2184 2185 // reconcile the displayed root with the session's navigation state. a 2186 // forward nav (current_page moved to a different parent page) pushes the 2187 // current root and builds the new page in a fresh arena; a back request 2188 // frees the current page and restores the previous one. when escape is 2189 // pressed with no history left we switch to the quit tab instead of quitting. 2190 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2191 // refresh: rebuild the current page in place from a current moment 2192 if (session.refresh_requested) { 2193 session.refresh_requested = false; 2194 if (session.haxy_moment != null or session.local != null) { 2195 const arena = try allocator.create(std.heap.ArenaAllocator); 2196 arena.* = std.heap.ArenaAllocator.init(allocator); 2197 errdefer freeArena(allocator, arena); 2198 2199 session.page_arena = arena; 2200 2201 const route = session.data.current_page; 2202 const page = try arena.allocator().create(Page); 2203 page.* = try Page.init(arena, session, route); 2204 const new_root = try initRoot(allocator, page, session); 2205 2206 self.root.deinit(allocator); 2207 freeArena(allocator, self.arena); 2208 self.root = new_root; 2209 self.route = route; 2210 self.arena = arena; 2211 } 2212 return; 2213 } 2214 2215 if (session.nav_back) { 2216 session.nav_back = false; 2217 if (self.history.pop()) |entry| { 2218 self.root.deinit(allocator); 2219 freeArena(allocator, self.arena); 2220 self.root = entry.root; 2221 self.route = entry.route; 2222 self.arena = entry.arena; 2223 session.page_arena = entry.arena; 2224 session.data.current_page = entry.route; 2225 chooseAnsiArtForNavigation(session); 2226 return; 2227 } 2228 // nothing to go back to; switch to the quit confirmation 2229 const root_focus = self.root.getFocus(); 2230 var iter = root_focus.children.iterator(); 2231 while (iter.next()) |entry| { 2232 switch (entry.value_ptr.focus.kind) { 2233 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2234 root_focus.setFocus(entry.key_ptr.*); 2235 // a Stack only builds its selected child, so the quit 2236 // button isn't in the focus tree until a build runs with 2237 // the quit tab selected. build once, then send a 2238 // synthetic arrow_down to drop focus from the tab onto 2239 // the button so it's ready to confirm. 2240 try self.root.build(allocator, .{ 2241 .min_size = .{ .width = null, .height = 40 }, 2242 .max_size = .{ .width = 80, .height = null }, 2243 }, root_focus); 2244 try self.root.input(allocator, .arrow_down, root_focus); 2245 break; 2246 }, 2247 else => {}, 2248 } 2249 } 2250 return; 2251 } 2252 2253 // forward navigation: navigate() set next_page to the page to move to (a 2254 // cross-page link or tab change crossing pages) 2255 if (session.next_page) |route| { 2256 session.next_page = null; 2257 if (session.haxy_moment == null and session.local == null) return; 2258 // the page we navigated to becomes the current page 2259 session.data.current_page = route; 2260 2261 const arena = try allocator.create(std.heap.ArenaAllocator); 2262 arena.* = std.heap.ArenaAllocator.init(allocator); 2263 errdefer freeArena(allocator, arena); 2264 2265 session.page_arena = arena; 2266 2267 const page = try arena.allocator().create(Page); 2268 page.* = try Page.init(arena, session, route); 2269 const new_root = try initRoot(allocator, page, session); 2270 2271 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 2272 // drop the oldest entry (freeing its widget tree and arena) once over cap 2273 if (self.history.items.len > max_history) { 2274 var oldest = self.history.orderedRemove(0); 2275 oldest.root.deinit(allocator); 2276 freeArena(allocator, oldest.arena); 2277 } 2278 self.root = new_root; 2279 self.route = route; 2280 self.arena = arena; 2281 } 2282 } 2283 }; 2284 2285 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2286 const page_widget: Widget = switch (page.*) { 2287 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2288 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2289 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2290 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2291 }; 2292 2293 chooseAnsiArtForNavigation(session); 2294 2295 // on the TUI/SSH, the page sits above a one-row footer showing the url 2296 var root = if (session.is_terminal) blk: { 2297 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2298 errdefer box.deinit(allocator); 2299 const bg_id = bg_blk: { 2300 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2301 errdefer background.deinit(allocator); 2302 const id = background.getFocus().id; 2303 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2304 break :bg_blk id; 2305 }; 2306 { 2307 var footer = try widget.Footer.init(allocator, session); 2308 errdefer footer.deinit(allocator); 2309 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2310 } 2311 box.getFocus().child_id = bg_id; 2312 break :blk Widget{ .box = box }; 2313 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2314 errdefer root.deinit(allocator); 2315 2316 // input-owning views build their TextInputs in init, so reset their 2317 // registrations here 2318 session.text_inputs.clearRetainingCapacity(); 2319 2320 try root.build(allocator, .{ 2321 .min_size = .{ .width = null, .height = 40 }, 2322 .max_size = .{ .width = 100, .height = null }, 2323 }, root.getFocus()); 2324 2325 return root; 2326 } 2327 2328 // native and server-side sessions choose from the embedded collection. WASM 2329 // keeps the art serialized by the server instead. 2330 fn chooseAnsiArtForNavigation(session: *Session) void { 2331 if (comptime ansi_arts.len == 0) return; 2332 const io = session.io orelse return; 2333 var random: usize = undefined; 2334 io.random(std.mem.asBytes(&random)); 2335 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2336 } 2337