╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
3143 // so the email may borrow transient memory. 3144 pub fn initFromEmail( 3145 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 3146 arena: *std.heap.ArenaAllocator, 3147 author_email: ?[]const u8, 3148 ) !Author { 3149 const email = author_email orelse return .unknown; 3150 if (haxy_moment) |moment| { 3151 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 3152 return .{ .user_name = user.name }; 3153 } 3154 } 3155 return .{ .email = try arena.allocator().dupe(u8, email) }; 3156 } 3157 }; 3158 3159 // the "a:" link to user `name`'s page. 3160 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 3161 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 3162 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 3163 return page_arena.allocator().print("a:{s}", .{url}); 3164 } 3165 3166 // a focusable " author " box showing `author`, linking to their user page 3167 // when it is a known user 3168 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 3169 const text = switch (author) { 3170 .unknown => "", 3171 .email, .user_name => |t| t, 3172 }; 3173 var tb = try wgt.TextBox.init(allocator, text, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " author " } }); 3174 errdefer tb.deinit(allocator); 3175 tb.getFocus().mode = .all; 3176 switch (author) { 3177 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 3178 else => {}, 3179 } 3180 return tb; 3181 } 3182 3183 // native-TUI navigation 3184 pub const Nav = struct { 3185 root: Widget, 3186 // backs the current page (its Page data and the page-scoped strings its 3187 // widgets build). owned here, swapped on each navigation. session.page_arena 3188 // tracks whichever of these is current so widgets allocate into it. 3189 arena: *std.heap.ArenaAllocator, 3190 // the login the current root was built for 3191 user_id: ?[evt.event_id_size]u8, 3192 history: std.ArrayList(Entry), 3193 3194 // each retained page keeps its own arena, freed when the page leaves the 3195 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 3196 // long-lived session from accumulating every page it ever visited. 3197 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator, user_id: ?[evt.event_id_size]u8 }; 3198 3199 // cap on retained back-history so the chain can't grow memory without bound 3200 const max_history: usize = 16; 3201 3202 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 3203 session.back = .unavailable; 3204 const arena = try allocator.create(std.heap.ArenaAllocator); 3205 arena.* = std.heap.ArenaAllocator.init(allocator); 3206 errdefer { 3207 arena.deinit(); 3208 allocator.destroy(arena); 3209 } 3210 3211 return .{ 3212 .root = try initCurrentRoot(allocator, arena, session), 3213 .arena = arena, 3214 .user_id = session.userId(), 3215 .history = .empty, 3216 }; 3217 } 3218 3219 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 3220 self.root.deinit(allocator); 3221 freeArena(allocator, self.arena); 3222 for (self.history.items) |*entry| { 3223 entry.root.deinit(allocator); 3224 freeArena(allocator, entry.arena); 3225 } 3226 self.history.deinit(allocator); 3227 } 3228 3229 // rebuild the current page in place from a current moment 3230 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 3231 if (session.haxy_moment == null and session.local == null) return; 3232 const arena = try allocator.create(std.heap.ArenaAllocator); 3233 arena.* = std.heap.ArenaAllocator.init(allocator); 3234 errdefer freeArena(allocator, arena); 3235 3236 const new_root = try initCurrentRoot(allocator, arena, session); 3237 3238 self.root.deinit(allocator); 3239 freeArena(allocator, self.arena); 3240 self.root = new_root; 3241 self.arena = arena; 3242 self.user_id = session.userId(); 3243 } 3244 3245 // build the current page's root in `arena`, or the not-found page's when 3246 // the route names nothing 3247 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 3248 session.page_arena = arena; 3249 const page = try arena.allocator().create(Page); 3250 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 3251 error.NotFound => blk: { 3252 session.data.current_page = .not_found; 3253 break :blk .not_found; 3254 }, 3255 else => |e| return e, 3256 }; 3257 return initRoot(allocator, page, session); 3258 } 3259 3260 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3261 arena.deinit(); 3262 allocator.destroy(arena); 3263 } 3264 3265 // reconcile the displayed root with the session's navigation state. a 3266 // forward nav (current_page moved to a different parent page) pushes the 3267 // current root and builds the new page in a fresh arena; a back request 3268 // frees the current page and restores the previous one. when escape is 3269 // pressed with no history left we switch to the quit tab instead of quitting. 3270 // returns whether the root was replaced. 3271 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3272 // refresh: rebuild the current page in place from a current moment 3273 if (session.refresh_requested) { 3274 session.refresh_requested = false; 3275 try self.rebuild(allocator, session); 3276 return true; 3277 } 3278 3279 if (session.back == .requested) { 3280 if (self.history.pop()) |entry| { 3281 session.back = if (self.history.items.len == 0) .unavailable else .available; 3282 self.root.deinit(allocator); 3283 freeArena(allocator, self.arena); 3284 self.root = entry.root; 3285 self.arena = entry.arena; 3286 self.user_id = entry.user_id; 3287 session.page_arena = entry.arena; 3288 session.data.current_page = entry.route; 3289 // a page built under another login is rebuilt rather than restored 3290 if (!std.meta.eql(entry.user_id, session.userId())) { 3291 try self.rebuild(allocator, session); 3292 return true; 3293 } 3294 chooseAnsiArtForNavigation(session); 3295 return true; 3296 } 3297 session.back = .unavailable; 3298 // nothing to go back to; switch to the quit confirmation 3299 const root_focus = self.root.getFocus(); 3300 var iter = root_focus.children.iterator(); 3301 while (iter.next()) |entry| { 3302 switch (entry.value_ptr.focus.kind) { 3303 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3304 root_focus.setFocus(entry.key_ptr.*); 3305 // a Stack only builds its selected child, so the quit 3306 // button isn't in the focus tree until a build runs with 3307 // the quit tab selected. build once, then send a 3308 // synthetic arrow_down to drop focus from the tab onto 3309 // the button so it's ready to confirm. 3310 try self.root.build(allocator, .{ 3311 .min_size = .{ .width = null, .height = 40 }, 3312 .max_size = .{ .width = 80, .height = null }, 3313 }, root_focus); 3314 try self.root.input(allocator, .arrow_down, root_focus); 3315 break; 3316 }, 3317 else => {}, 3318 } 3319 } 3320 return false; 3321 } 3322 3323 // forward navigation: navigate() set next_page to the page to move to (a 3324 // cross-page link or tab change crossing pages) 3325 if (session.next_page) |route| { 3326 session.next_page = null; 3327 if (session.haxy_moment == null and session.local == null) return false; 3328 const previous_route = session.data.current_page; 3329 // the page we navigated to becomes the current page 3330 session.data.current_page = route; 3331 session.back = .available; 3332 3333 const arena = try allocator.create(std.heap.ArenaAllocator); 3334 arena.* = std.heap.ArenaAllocator.init(allocator); 3335 errdefer freeArena(allocator, arena); 3336 3337 const new_root = try initCurrentRoot(allocator, arena, session); 3338 3339 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3340 // drop the oldest entry (freeing its widget tree and arena) once over cap 3341 if (self.history.items.len > max_history) { 3342 var oldest = self.history.orderedRemove(0); 3343 oldest.root.deinit(allocator); 3344 freeArena(allocator, oldest.arena); 3345 } 3346 self.root = new_root; 3347 self.arena = arena; 3348 self.user_id = session.userId(); 3349 return true; 3350 } 3351 return false; 3352 } 3353 }; 3354 3355 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3356 const page_widget: Widget = switch (page.*) { 3357 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3358 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3359 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3360 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3361 .not_found => .{ .not_found = try .init(allocator, session) }, 3362 }; 3363 3364 chooseAnsiArtForNavigation(session); 3365 3366 // on the TUI/SSH, the page sits above a one-row footer showing its web 3367 // url, when there's a web UI to point at 3368 var root = if (session.is_terminal) blk: { 3369 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3370 errdefer box.deinit(allocator); 3371 const bg_id = bg_blk: { 3372 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3373 errdefer background.deinit(allocator); 3374 const id = background.getFocus().id; 3375 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3376 break :bg_blk id; 3377 }; 3378 if (session.web_url != null) { 3379 var footer = try widget.Footer.init(allocator, session); 3380 errdefer footer.deinit(allocator); 3381 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3382 } 3383 box.getFocus().child_id = bg_id; 3384 break :blk Widget{ .box = box }; 3385 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3386 errdefer root.deinit(allocator); 3387 3388 // input-owning views build their TextInputs in init, so reset their 3389 // registrations here 3390 session.text_inputs.clearRetainingCapacity(); 3391 3392 try root.build(allocator, .{ 3393 .min_size = .{ .width = null, .height = 40 }, 3394 .max_size = .{ .width = 100, .height = null }, 3395 }, root.getFocus()); 3396 3397 return root; 3398 } 3399 3400 // native and server-side sessions choose from the embedded collection. WASM 3401 // keeps the art serialized by the server instead. 3402 fn chooseAnsiArtForNavigation(session: *Session) void { 3403 if (comptime ansi_art.art.len == 0) return; 3404 if (session.data.current_page == .not_found) { 3405 session.data.ansi_art = ansi_art.not_found; 3406 return; 3407 } 3408 const io = session.io orelse return; 3409 var random: usize = undefined; 3410 io.random(std.mem.asBytes(&random)); 3411 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3412 } 3413