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