╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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