╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
3164 } 3165 3166 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 3167 self.root.deinit(allocator); 3168 freeArena(allocator, self.arena); 3169 for (self.history.items) |*entry| { 3170 entry.root.deinit(allocator); 3171 freeArena(allocator, entry.arena); 3172 } 3173 self.history.deinit(allocator); 3174 } 3175 3176 // rebuild the current page in place from a current moment 3177 fn rebuild(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 3178 if (session.haxy_moment == null and session.local == null) return; 3179 const arena = try allocator.create(std.heap.ArenaAllocator); 3180 arena.* = std.heap.ArenaAllocator.init(allocator); 3181 errdefer freeArena(allocator, arena); 3182 3183 const new_root = try initCurrentRoot(allocator, arena, session); 3184 3185 self.root.deinit(allocator); 3186 freeArena(allocator, self.arena); 3187 self.root = new_root; 3188 self.arena = arena; 3189 self.user_id = session.userId(); 3190 } 3191 3192 // build the current page's root in `arena`, or the not-found page's when 3193 // the route names nothing 3194 fn initCurrentRoot(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator, session: *Session) !Widget { 3195 session.page_arena = arena; 3196 const page = try arena.allocator().create(Page); 3197 page.* = Page.init(arena, session, session.data.current_page) catch |err| switch (err) { 3198 error.NotFound => blk: { 3199 session.data.current_page = .not_found; 3200 break :blk .not_found; 3201 }, 3202 else => |e| return e, 3203 }; 3204 return initRoot(allocator, page, session); 3205 } 3206 3207 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 3208 arena.deinit(); 3209 allocator.destroy(arena); 3210 } 3211 3212 // reconcile the displayed root with the session's navigation state. a 3213 // forward nav (current_page moved to a different parent page) pushes the 3214 // current root and builds the new page in a fresh arena; a back request 3215 // frees the current page and restores the previous one. when escape is 3216 // pressed with no history left we switch to the quit tab instead of quitting. 3217 // returns whether the root was replaced. 3218 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !bool { 3219 // refresh: rebuild the current page in place from a current moment 3220 if (session.refresh_requested) { 3221 session.refresh_requested = false; 3222 try self.rebuild(allocator, session); 3223 return true; 3224 } 3225 3226 if (session.back == .requested) { 3227 if (self.history.pop()) |entry| { 3228 session.back = if (self.history.items.len == 0) .unavailable else .available; 3229 self.root.deinit(allocator); 3230 freeArena(allocator, self.arena); 3231 self.root = entry.root; 3232 self.arena = entry.arena; 3233 self.user_id = entry.user_id; 3234 session.page_arena = entry.arena; 3235 session.data.current_page = entry.route; 3236 // a page built under another login is rebuilt rather than restored 3237 if (!std.meta.eql(entry.user_id, session.userId())) { 3238 try self.rebuild(allocator, session); 3239 return true; 3240 } 3241 chooseAnsiArtForNavigation(session); 3242 return true; 3243 } 3244 session.back = .unavailable; 3245 // nothing to go back to; switch to the quit confirmation 3246 const root_focus = self.root.getFocus(); 3247 var iter = root_focus.children.iterator(); 3248 while (iter.next()) |entry| { 3249 switch (entry.value_ptr.focus.kind) { 3250 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3251 root_focus.setFocus(entry.key_ptr.*); 3252 // a Stack only builds its selected child, so the quit 3253 // button isn't in the focus tree until a build runs with 3254 // the quit tab selected. build once, then send a 3255 // synthetic arrow_down to drop focus from the tab onto 3256 // the button so it's ready to confirm. 3257 try self.root.build(allocator, .{ 3258 .min_size = .{ .width = null, .height = 40 }, 3259 .max_size = .{ .width = 80, .height = null }, 3260 }, root_focus); 3261 try self.root.input(allocator, .arrow_down, root_focus); 3262 break; 3263 }, 3264 else => {}, 3265 } 3266 } 3267 return false; 3268 } 3269 3270 // forward navigation: navigate() set next_page to the page to move to (a 3271 // cross-page link or tab change crossing pages) 3272 if (session.next_page) |route| { 3273 session.next_page = null; 3274 if (session.haxy_moment == null and session.local == null) return false; 3275 const previous_route = session.data.current_page; 3276 // the page we navigated to becomes the current page 3277 session.data.current_page = route; 3278 session.back = .available; 3279 3280 const arena = try allocator.create(std.heap.ArenaAllocator); 3281 arena.* = std.heap.ArenaAllocator.init(allocator); 3282 errdefer freeArena(allocator, arena); 3283 3284 const new_root = try initCurrentRoot(allocator, arena, session); 3285 3286 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3287 // drop the oldest entry (freeing its widget tree and arena) once over cap 3288 if (self.history.items.len > max_history) { 3289 var oldest = self.history.orderedRemove(0); 3290 oldest.root.deinit(allocator); 3291 freeArena(allocator, oldest.arena); 3292 } 3293 self.root = new_root; 3294 self.arena = arena; 3295 self.user_id = session.userId(); 3296 return true; 3297 } 3298 return false; 3299 } 3300 }; 3301 3302 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3303 const page_widget: Widget = switch (page.*) { 3304 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3305 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3306 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3307 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3308 .not_found => .{ .not_found = try .init(allocator, session) }, 3309 }; 3310 3311 chooseAnsiArtForNavigation(session); 3312 3313 // on the TUI/SSH, the page sits above a one-row footer showing its web 3314 // url, when there's a web UI to point at 3315 var root = if (session.is_terminal) blk: { 3316 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3317 errdefer box.deinit(allocator); 3318 const bg_id = bg_blk: { 3319 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3320 errdefer background.deinit(allocator); 3321 const id = background.getFocus().id; 3322 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3323 break :bg_blk id; 3324 }; 3325 if (session.web_port != null) { 3326 var footer = try widget.Footer.init(allocator, session); 3327 errdefer footer.deinit(allocator); 3328 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3329 } 3330 box.getFocus().child_id = bg_id; 3331 break :blk Widget{ .box = box }; 3332 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3333 errdefer root.deinit(allocator); 3334 3335 // input-owning views build their TextInputs in init, so reset their 3336 // registrations here 3337 session.text_inputs.clearRetainingCapacity(); 3338 3339 try root.build(allocator, .{ 3340 .min_size = .{ .width = null, .height = 40 }, 3341 .max_size = .{ .width = 100, .height = null }, 3342 }, root.getFocus()); 3343 3344 return root; 3345 } 3346 3347 // native and server-side sessions choose from the embedded collection. WASM 3348 // keeps the art serialized by the server instead. 3349 fn chooseAnsiArtForNavigation(session: *Session) void { 3350 if (comptime ansi_art.art.len == 0) return; 3351 if (session.data.current_page == .not_found) { 3352 session.data.ansi_art = ansi_art.not_found; 3353 return; 3354 } 3355 const io = session.io orelse return; 3356 var random: usize = undefined; 3357 io.random(std.mem.asBytes(&random)); 3358 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3359 } 3360