╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1906 entry.root.deinit(allocator); 1907 freeArena(allocator, entry.arena); 1908 } 1909 self.history.deinit(allocator); 1910 } 1911 1912 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 1913 arena.deinit(); 1914 allocator.destroy(arena); 1915 } 1916 1917 // reconcile the displayed root with the session's navigation state. a 1918 // forward nav (current_page moved to a different parent page) pushes the 1919 // current root and builds the new page in a fresh arena; a back request 1920 // frees the current page and restores the previous one. when escape is 1921 // pressed with no history left we switch to the quit tab instead of quitting. 1922 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 1923 // refresh: rebuild the current page in place from a current moment 1924 if (session.refresh_requested) { 1925 session.refresh_requested = false; 1926 if (session.haxy_moment != null or session.local != null) { 1927 const arena = try allocator.create(std.heap.ArenaAllocator); 1928 arena.* = std.heap.ArenaAllocator.init(allocator); 1929 errdefer freeArena(allocator, arena); 1930 1931 session.page_arena = arena; 1932 1933 const route = session.data.current_page; 1934 const page = try arena.allocator().create(Page); 1935 page.* = try Page.init(arena, session, route); 1936 const new_root = try initRoot(allocator, page, session); 1937 1938 self.root.deinit(allocator); 1939 freeArena(allocator, self.arena); 1940 self.root = new_root; 1941 self.route = route; 1942 self.arena = arena; 1943 } 1944 return; 1945 } 1946 1947 if (session.nav_back) { 1948 session.nav_back = false; 1949 if (self.history.pop()) |entry| { 1950 self.root.deinit(allocator); 1951 freeArena(allocator, self.arena); 1952 self.root = entry.root; 1953 self.route = entry.route; 1954 self.arena = entry.arena; 1955 session.page_arena = entry.arena; 1956 session.data.current_page = entry.route; 1957 chooseAnsiArtForNavigation(session); 1958 return; 1959 } 1960 // nothing to go back to; switch to the quit confirmation 1961 const root_focus = self.root.getFocus(); 1962 var iter = root_focus.children.iterator(); 1963 while (iter.next()) |entry| { 1964 switch (entry.value_ptr.focus.kind) { 1965 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 1966 root_focus.setFocus(entry.key_ptr.*); 1967 // a Stack only builds its selected child, so the quit 1968 // button isn't in the focus tree until a build runs with 1969 // the quit tab selected. build once, then send a 1970 // synthetic arrow_down to drop focus from the tab onto 1971 // the button so it's ready to confirm. 1972 try self.root.build(allocator, .{ 1973 .min_size = .{ .width = null, .height = 40 }, 1974 .max_size = .{ .width = 80, .height = null }, 1975 }, root_focus); 1976 try self.root.input(allocator, .arrow_down, root_focus); 1977 break; 1978 }, 1979 else => {}, 1980 } 1981 } 1982 return; 1983 } 1984 1985 // forward navigation: navigate() set next_page to the page to move to (a 1986 // cross-page link or tab change crossing pages) 1987 if (session.next_page) |route| { 1988 session.next_page = null; 1989 if (session.haxy_moment == null and session.local == null) return; 1990 // the page we navigated to becomes the current page 1991 session.data.current_page = route; 1992 1993 const arena = try allocator.create(std.heap.ArenaAllocator); 1994 arena.* = std.heap.ArenaAllocator.init(allocator); 1995 errdefer freeArena(allocator, arena); 1996 1997 session.page_arena = arena; 1998 1999 const page = try arena.allocator().create(Page); 2000 page.* = try Page.init(arena, session, route); 2001 const new_root = try initRoot(allocator, page, session); 2002 2003 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 2004 // drop the oldest entry (freeing its widget tree and arena) once over cap 2005 if (self.history.items.len > max_history) { 2006 var oldest = self.history.orderedRemove(0); 2007 oldest.root.deinit(allocator); 2008 freeArena(allocator, oldest.arena); 2009 } 2010 self.root = new_root; 2011 self.route = route; 2012 self.arena = arena; 2013 } 2014 } 2015 }; 2016 2017 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2018 const page_widget: Widget = switch (page.*) { 2019 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2020 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2021 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2022 }; 2023 2024 chooseAnsiArtForNavigation(session); 2025 2026 // on the TUI/SSH, the page sits above a one-row footer showing the url 2027 var root = if (session.is_terminal) blk: { 2028 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2029 errdefer box.deinit(allocator); 2030 const bg_id = bg_blk: { 2031 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2032 errdefer background.deinit(allocator); 2033 const id = background.getFocus().id; 2034 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2035 break :bg_blk id; 2036 }; 2037 { 2038 var footer = try widget.Footer.init(allocator, session); 2039 errdefer footer.deinit(allocator); 2040 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2041 } 2042 box.getFocus().child_id = bg_id; 2043 break :blk Widget{ .box = box }; 2044 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2045 errdefer root.deinit(allocator); 2046 2047 // input-owning views build their TextInputs in init, so reset their 2048 // registrations here 2049 session.text_inputs.clearRetainingCapacity(); 2050 session.input_values.clearRetainingCapacity(); 2051 2052 try root.build(allocator, .{ 2053 .min_size = .{ .width = null, .height = 40 }, 2054 .max_size = .{ .width = 100, .height = null }, 2055 }, root.getFocus()); 2056 2057 return root; 2058 } 2059 2060 // native and server-side sessions choose from the embedded collection. WASM 2061 // keeps the art serialized by the server instead. 2062 fn chooseAnsiArtForNavigation(session: *Session) void { 2063 if (comptime ansi_arts.len == 0) return; 2064 const io = session.io orelse return; 2065 var random: usize = undefined; 2066 io.random(std.mem.asBytes(&random)); 2067 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2068 } 2069