╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2977 if (session.back == .requested) { 2978 if (self.history.pop()) |entry| { 2979 session.back = if (self.history.items.len == 0) .unavailable else .available; 2980 self.root.deinit(allocator); 2981 freeArena(allocator, self.arena); 2982 self.root = entry.root; 2983 self.arena = entry.arena; 2984 self.user_id = entry.user_id; 2985 session.page_arena = entry.arena; 2986 session.data.current_page = entry.route; 2987 // a page built under another login is rebuilt rather than restored 2988 if (!std.meta.eql(entry.user_id, session.userId())) { 2989 try self.rebuild(allocator, session); 2990 return true; 2991 } 2992 chooseAnsiArtForNavigation(session); 2993 return true; 2994 } 2995 session.back = .unavailable; 2996 // nothing to go back to; switch to the quit confirmation 2997 const root_focus = self.root.getFocus(); 2998 var iter = root_focus.children.iterator(); 2999 while (iter.next()) |entry| { 3000 switch (entry.value_ptr.focus.kind) { 3001 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 3002 root_focus.setFocus(entry.key_ptr.*); 3003 // a Stack only builds its selected child, so the quit 3004 // button isn't in the focus tree until a build runs with 3005 // the quit tab selected. build once, then send a 3006 // synthetic arrow_down to drop focus from the tab onto 3007 // the button so it's ready to confirm. 3008 try self.root.build(allocator, .{ 3009 .min_size = .{ .width = null, .height = 40 }, 3010 .max_size = .{ .width = 80, .height = null }, 3011 }, root_focus); 3012 try self.root.input(allocator, .arrow_down, root_focus); 3013 break; 3014 }, 3015 else => {}, 3016 } 3017 } 3018 return false; 3019 } 3020 3021 // forward navigation: navigate() set next_page to the page to move to (a 3022 // cross-page link or tab change crossing pages) 3023 if (session.next_page) |route| { 3024 session.next_page = null; 3025 if (session.haxy_moment == null and session.local == null) return false; 3026 const previous_route = session.data.current_page; 3027 // the page we navigated to becomes the current page 3028 session.data.current_page = route; 3029 session.back = .available; 3030 3031 const arena = try allocator.create(std.heap.ArenaAllocator); 3032 arena.* = std.heap.ArenaAllocator.init(allocator); 3033 errdefer freeArena(allocator, arena); 3034 3035 const new_root = try initCurrentRoot(allocator, arena, session); 3036 3037 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena, .user_id = self.user_id }); 3038 // drop the oldest entry (freeing its widget tree and arena) once over cap 3039 if (self.history.items.len > max_history) { 3040 var oldest = self.history.orderedRemove(0); 3041 oldest.root.deinit(allocator); 3042 freeArena(allocator, oldest.arena); 3043 } 3044 self.root = new_root; 3045 self.arena = arena; 3046 self.user_id = session.userId(); 3047 return true; 3048 } 3049 return false; 3050 } 3051 }; 3052 3053 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 3054 const page_widget: Widget = switch (page.*) { 3055 .home => |*p| .{ .home = try .init(allocator, p, session) }, 3056 .user => |*p| .{ .user = try .init(allocator, p, session) }, 3057 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 3058 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 3059 .not_found => .{ .not_found = try .init(allocator, session) }, 3060 }; 3061 3062 chooseAnsiArtForNavigation(session); 3063 3064 // on the TUI/SSH, the page sits above a one-row footer showing its web 3065 // url, when there's a web UI to point at 3066 var root = if (session.is_terminal) blk: { 3067 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .vert }); 3068 errdefer box.deinit(allocator); 3069 const bg_id = bg_blk: { 3070 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 3071 errdefer background.deinit(allocator); 3072 const id = background.getFocus().id; 3073 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 3074 break :bg_blk id; 3075 }; 3076 if (session.web_port != null) { 3077 var footer = try widget.Footer.init(allocator, session); 3078 errdefer footer.deinit(allocator); 3079 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 3080 } 3081 box.getFocus().child_id = bg_id; 3082 break :blk Widget{ .box = box }; 3083 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 3084 errdefer root.deinit(allocator); 3085 3086 // input-owning views build their TextInputs in init, so reset their 3087 // registrations here 3088 session.text_inputs.clearRetainingCapacity(); 3089 3090 try root.build(allocator, .{ 3091 .min_size = .{ .width = null, .height = 40 }, 3092 .max_size = .{ .width = 100, .height = null }, 3093 }, root.getFocus()); 3094 3095 return root; 3096 } 3097 3098 // native and server-side sessions choose from the embedded collection. WASM 3099 // keeps the art serialized by the server instead. 3100 fn chooseAnsiArtForNavigation(session: *Session) void { 3101 if (comptime ansi_art.art.len == 0) return; 3102 if (session.data.current_page == .not_found) { 3103 session.data.ansi_art = ansi_art.not_found; 3104 return; 3105 } 3106 const io = session.io orelse return; 3107 var random: usize = undefined; 3108 io.random(std.mem.asBytes(&random)); 3109 session.data.ansi_art = ansi_art.art[random % ansi_art.art.len]; 3110 } 3111