╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1843 1844 // cap on retained back-history so the chain can't grow memory without bound 1845 const max_history: usize = 16; 1846 1847 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 1848 const arena = try allocator.create(std.heap.ArenaAllocator); 1849 arena.* = std.heap.ArenaAllocator.init(allocator); 1850 errdefer { 1851 arena.deinit(); 1852 allocator.destroy(arena); 1853 } 1854 1855 session.page_arena = arena; 1856 const route = session.data.current_page; 1857 1858 const page = try arena.allocator().create(Page); 1859 page.* = try Page.init(arena, session, route); 1860 return .{ 1861 .root = try initRoot(allocator, page, session), 1862 .route = route, 1863 .arena = arena, 1864 .history = .empty, 1865 }; 1866 } 1867 1868 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 1869 self.root.deinit(allocator); 1870 freeArena(allocator, self.arena); 1871 for (self.history.items) |*entry| { 1872 entry.root.deinit(allocator); 1873 freeArena(allocator, entry.arena); 1874 } 1875 self.history.deinit(allocator); 1876 } 1877 1878 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 1879 arena.deinit(); 1880 allocator.destroy(arena); 1881 } 1882 1883 // reconcile the displayed root with the session's navigation state. a 1884 // forward nav (current_page moved to a different parent page) pushes the 1885 // current root and builds the new page in a fresh arena; a back request 1886 // frees the current page and restores the previous one. when escape is 1887 // pressed with no history left we switch to the quit tab instead of quitting. 1888 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 1889 // refresh: rebuild the current page in place from a current moment 1890 if (session.refresh_requested) { 1891 session.refresh_requested = false; 1892 if (session.haxy_moment != null or session.local != null) { 1893 const arena = try allocator.create(std.heap.ArenaAllocator); 1894 arena.* = std.heap.ArenaAllocator.init(allocator); 1895 errdefer freeArena(allocator, arena); 1896 1897 session.page_arena = arena; 1898 1899 const route = session.data.current_page; 1900 const page = try arena.allocator().create(Page); 1901 page.* = try Page.init(arena, session, route); 1902 const new_root = try initRoot(allocator, page, session); 1903 1904 self.root.deinit(allocator); 1905 freeArena(allocator, self.arena); 1906 self.root = new_root; 1907 self.route = route; 1908 self.arena = arena; 1909 } 1910 return; 1911 } 1912 1913 if (session.nav_back) { 1914 session.nav_back = false; 1915 if (self.history.pop()) |entry| { 1916 self.root.deinit(allocator); 1917 freeArena(allocator, self.arena); 1918 self.root = entry.root; 1919 self.route = entry.route; 1920 self.arena = entry.arena; 1921 session.page_arena = entry.arena; 1922 session.data.current_page = entry.route; 1923 chooseAnsiArtForNavigation(session); 1924 return; 1925 } 1926 // nothing to go back to; switch to the quit confirmation 1927 const root_focus = self.root.getFocus(); 1928 var iter = root_focus.children.iterator(); 1929 while (iter.next()) |entry| { 1930 switch (entry.value_ptr.focus.kind) { 1931 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 1932 root_focus.setFocus(entry.key_ptr.*); 1933 // a Stack only builds its selected child, so the quit 1934 // button isn't in the focus tree until a build runs with 1935 // the quit tab selected. build once, then send a 1936 // synthetic arrow_down to drop focus from the tab onto 1937 // the button so it's ready to confirm. 1938 try self.root.build(allocator, .{ 1939 .min_size = .{ .width = null, .height = 40 }, 1940 .max_size = .{ .width = 80, .height = null }, 1941 }, root_focus); 1942 try self.root.input(allocator, .arrow_down, root_focus); 1943 break; 1944 }, 1945 else => {}, 1946 } 1947 } 1948 return; 1949 } 1950 1951 // forward navigation: navigate() set next_page to the page to move to (a 1952 // cross-page link or tab change crossing pages) 1953 if (session.next_page) |route| { 1954 session.next_page = null; 1955 if (session.haxy_moment == null and session.local == null) return; 1956 // the page we navigated to becomes the current page 1957 session.data.current_page = route; 1958 1959 const arena = try allocator.create(std.heap.ArenaAllocator); 1960 arena.* = std.heap.ArenaAllocator.init(allocator); 1961 errdefer freeArena(allocator, arena); 1962 1963 session.page_arena = arena; 1964 1965 const page = try arena.allocator().create(Page); 1966 page.* = try Page.init(arena, session, route); 1967 const new_root = try initRoot(allocator, page, session); 1968 1969 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 1970 // drop the oldest entry (freeing its widget tree and arena) once over cap 1971 if (self.history.items.len > max_history) { 1972 var oldest = self.history.orderedRemove(0); 1973 oldest.root.deinit(allocator); 1974 freeArena(allocator, oldest.arena); 1975 } 1976 self.root = new_root; 1977 self.route = route; 1978 self.arena = arena; 1979 } 1980 } 1981 }; 1982 1983 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 1984 const page_widget: Widget = switch (page.*) { 1985 .home => |*p| .{ .home = try .init(allocator, p, session) }, 1986 .user => |*p| .{ .user = try .init(allocator, p, session) }, 1987 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 1988 }; 1989 1990 chooseAnsiArtForNavigation(session); 1991 1992 // on the TUI/SSH, the page sits above a one-row footer showing the url 1993 var root = if (session.is_terminal) blk: { 1994 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 1995 errdefer box.deinit(allocator); 1996 const bg_id = bg_blk: { 1997 var background = try AnsiBackground.init(allocator, page_widget, session); 1998 errdefer background.deinit(allocator); 1999 const id = background.getFocus().id; 2000 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2001 break :bg_blk id; 2002 }; 2003 { 2004 var footer = try Footer.init(allocator, session); 2005 errdefer footer.deinit(allocator); 2006 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2007 } 2008 box.getFocus().child_id = bg_id; 2009 break :blk Widget{ .box = box }; 2010 } else Widget{ .background = try AnsiBackground.init(allocator, page_widget, session) }; 2011 errdefer root.deinit(allocator); 2012 2013 // input-owning views build their TextInputs in init — so reset the 2014 // focus-id -> *TextInput map here 2015 session.text_inputs.clearRetainingCapacity(); 2016 session.input_values.clearRetainingCapacity(); 2017 2018 try root.build(allocator, .{ 2019 .min_size = .{ .width = null, .height = 40 }, 2020 .max_size = .{ .width = 100, .height = null }, 2021 }, root.getFocus()); 2022 2023 return root; 2024 } 2025 2026 // native and server-side sessions choose from the embedded collection. WASM 2027 // keeps the art serialized by the server instead. 2028 fn chooseAnsiArtForNavigation(session: *Session) void { 2029 if (comptime ansi_arts.len == 0) return; 2030 const io = session.io orelse return; 2031 var random: usize = undefined; 2032 io.random(std.mem.asBytes(&random)); 2033 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2034 } 2035 2036 pub const Widget = union(enum) { 2037 text: wgt.Text(Widget), 2038 box: wgt.Box(Widget), 2039 text_box: wgt.TextBox(Widget), 2040 text_input: wgt.TextInput(Widget), 2041 scroll: wgt.Scroll(Widget), 2042 stack: wgt.Stack(Widget), 2043 flow_box: FlowBox, 2044 flow_box_scroll: FlowBox.Scroll, 2045 tag_flow: TagFlow, 2046 spacer: Spacer, 2047 center: Center, 2048 section_label: SectionLabel, 2049 submit_button: SubmitButton, 2050 background: AnsiBackground, 2051 home: Home.View, 2052 user: User.View, 2053 repo: Repo.View, 2054 quit: Quit.View, 2055 unauthorized: Unauthorized.View, 2056 title: Title.View, 2057 sub_title: SubTitle.View, 2058 home_header: Home.Header.View, 2059 user_header: User.Header.View, 2060 repo_header: Repo.Header.View, 2061 repo_files_header: Repo.Files.Header.View, 2062 repo_commits_header: Repo.Commits.Header.View, 2063 repo_issues_header: Repo.Issues.Header, 2064 repo_patches_header: Repo.Patches.Header, 2065 repo_discussions_header: Repo.Discussions.Header, 2066 repo_files: Repo.Files.View, 2067 repo_commits: Repo.Commits.View, 2068 repo_refs: Repo.Refs.View, 2069 repo_issues: Repo.Issues.View, 2070 repo_patches: Repo.Patches.View, 2071 repo_discussions: Repo.Discussions.View, 2072 repo_events_header: Repo.Events.Header.View, 2073 repo_events: Repo.Events.View, 2074 repo_comment: Repo.Comment.Item, 2075 clone_url: CloneUrl.View, 2076 home_users: Home.Users.View, 2077 home_repos: Home.Repos.View, 2078 auth_tab: Home.Header.AuthTab.View, 2079 home_settings: Home.Settings.View, 2080 home_auth: Home.Auth.View, 2081 footer: Footer, 2082 2083 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 2084 switch (self.*) { 2085 inline else => |*case| case.deinit(allocator), 2086 } 2087 } 2088 2089 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 2090 switch (self.*) { 2091 inline else => |*case| try case.build(allocator, constraint, root_focus), 2092 } 2093 } 2094 2095 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) anyerror!void { 2096 switch (self.*) { 2097 inline else => |*case| try case.input(allocator, key, root_focus), 2098 } 2099 } 2100 2101 pub fn clearGrid(self: *Widget) void { 2102 switch (self.*) { 2103 inline else => |*case| case.clearGrid(), 2104 } 2105 } 2106 2107 pub fn getGrid(self: Widget) ?Grid { 2108 switch (self) { 2109 inline else => |*case| return case.getGrid(), 2110 } 2111 } 2112 2113 pub fn getFocus(self: *Widget) *Focus { 2114 switch (self.*) { 2115 inline else => |*case| return case.getFocus(), 2116 } 2117 } 2118 }; 2119 2120 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 2121 // `scroll` to keep it visible. 2122 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 2123 const keys = box.children.keys(); 2124 if (keys.len == 0) return; 2125 const cur_id = box.getFocus().child_id orelse return; 2126 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 2127 const last: isize = @intCast(keys.len - 1); 2128 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 2129 if (next == @as(usize, @intCast(cur))) return; 2130 root_focus.setFocus(keys[next]); 2131 if (box.children.values()[next].rect) |rect| scroll.scrollToRect(rect); 2132 } 2133 2134 pub const FlowBox = struct { 2135 focus: *Focus, 2136 grid: ?Grid, 2137 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 2138 // backs each link item's `.custom` focus kind (e.g. "a:/user/foo"). reset 2139 // wholesale on each setItems so there's no per-string ownership to track. 2140 arena: std.heap.ArenaAllocator, 2141 // column count from the last build — FlowBox.Scroll.input uses it so arrow 2142 // up/down can step by a row's worth of items. 2143 last_cols: usize, 2144 options: Options, 2145 2146 const border_rows: usize = 2; 2147 2148 pub const Options = struct { 2149 cell_width: usize = 40, 2150 cell_height: usize = 3, 2151 }; 2152 2153 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 2154 return .{ 2155 .focus = try Focus.create(allocator, .container), 2156 .grid = null, 2157 .text_boxes = .empty, 2158 .arena = std.heap.ArenaAllocator.init(allocator), 2159 .last_cols = 1, 2160 .options = options, 2161 }; 2162 } 2163 2164 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 2165 self.focus.destroy(allocator); 2166 if (self.grid) |*grid| { 2167 grid.deinit(); 2168 self.grid = null; 2169 } 2170 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 2171 self.text_boxes.deinit(allocator); 2172 self.arena.deinit(); 2173 } 2174 2175 // one flow item: its display text plus an optional link. a non-empty `link` 2176 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 2177 // the web renderer turns into an anchor. 2178 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 2179 2180 // the text box copies the item text; links are copied into the arena, so the 2181 // caller's slices needn't outlive this call. 2182 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 2183 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 2184 self.text_boxes.clearAndFree(allocator); 2185 2186 // the old text boxes and their borrowed links are gone now, so the link 2187 // arena can be reclaimed. 2188 _ = self.arena.reset(.retain_capacity); 2189 const aa = self.arena.allocator(); 2190 2191 self.focus.clear(); 2192 self.focus.child_id = null; 2193 2194 for (items) |item| { 2195 var text_box = try wgt.TextBox(Widget).init(allocator, item.text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 2196 errdefer text_box.deinit(allocator); 2197 text_box.getFocus().focusable = true; 2198 2199 if (item.link.len > 0) { 2200 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 2201 } 2202 2203 try self.text_boxes.append(allocator, text_box); 2204 } 2205 2206 if (self.text_boxes.items.len > 0) { 2207 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 2208 } 2209 } 2210 2211 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2212 self.clearGrid(); 2213 self.focus.clear(); 2214 2215 const cell_width = self.options.cell_width; 2216 const max_width = constraint.max_size.width orelse cell_width; 2217 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 2218 self.last_cols = cols; 2219 2220 const count = self.text_boxes.items.len; 2221 if (count == 0) return; 2222 const slot_height = self.options.cell_height + border_rows; 2223 if (slot_height == 0) return; 2224 2225 // build at the slot size so every text box fits a single grid cell 2226 for (self.text_boxes.items) |*tb| { 2227 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 2228 try tb.build(allocator, .{ 2229 .min_size = .{ .width = cell_width, .height = null }, 2230 .max_size = .{ .width = cell_width, .height = slot_height }, 2231 }, root_focus); 2232 } 2233 2234 const rows = (count + cols - 1) / cols; 2235 const content_height = rows * slot_height; 2236 const total_width = cols * cell_width; 2237 if (total_width == 0 or content_height == 0) return; 2238 2239 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 2240 errdefer grid.deinit(); 2241 2242 for (self.text_boxes.items, 0..) |*tb, i| { 2243 const tb_grid = tb.getGrid() orelse continue; 2244 const col = i % cols; 2245 const row = i / cols; 2246 const cell_x = col * cell_width; 2247 const cell_y = row * slot_height; 2248 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 2249 try grid.drawGrid(tb_grid, cell_x, cell_y); 2250 } 2251 2252 self.grid = grid; 2253 } 2254 2255 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2256 _ = self; 2257 _ = allocator; 2258 _ = key; 2259 _ = root_focus; 2260 } 2261 2262 pub fn clearGrid(self: *FlowBox) void { 2263 if (self.grid) |*grid| { 2264 grid.deinit(); 2265 self.grid = null; 2266 } 2267 for (self.text_boxes.items) |*tb| tb.clearGrid(); 2268 } 2269 2270 pub fn getGrid(self: FlowBox) ?Grid { 2271 return self.grid; 2272 } 2273 2274 pub fn getFocus(self: *FlowBox) *Focus { 2275 return self.focus; 2276 } 2277 2278 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 2279 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 2280 const slot_height = self.options.cell_height + border_rows; 2281 const col = index % self.last_cols; 2282 const row = index / self.last_cols; 2283 return .{ 2284 .x = @intCast(col * self.options.cell_width), 2285 .y = @intCast(row * slot_height), 2286 .size = .{ .width = self.options.cell_width, .height = slot_height }, 2287 }; 2288 } 2289 2290 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 2291 for (self.text_boxes.items, 0..) |tb, i| { 2292 if (tb.box.focus.id == focus_id) return i; 2293 } 2294 return null; 2295 } 2296 2297 pub const Scroll = struct { 2298 scroll: wgt.Scroll(Widget), 2299 2300 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 2301 var layout_inner = try FlowBox.init(allocator, options); 2302 errdefer layout_inner.deinit(allocator); 2303 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 2304 errdefer scroll.deinit(allocator); 2305 return .{ .scroll = scroll }; 2306 } 2307 2308 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 2309 self.scroll.deinit(allocator); 2310 } 2311 2312 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 2313 self.scroll.x = 0; 2314 self.scroll.y = 0; 2315 try self.inner().setItems(allocator, items); 2316 } 2317 2318 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2319 try self.scroll.build(allocator, constraint, root_focus); 2320 } 2321 2322 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2323 _ = allocator; 2324 const in = self.inner(); 2325 const child_id = in.focus.child_id orelse return; 2326 const current_index = in.indexOfFocusId(child_id) orelse return; 2327 const count = in.text_boxes.items.len; 2328 if (count == 0) return; 2329 const cols = in.last_cols; 2330 const slot_height = in.options.cell_height + FlowBox.border_rows; 2331 2332 var index = current_index; 2333 switch (key) { 2334 .arrow_up => index -|= cols, 2335 .arrow_down => if (index + cols < count) { 2336 index += cols; 2337 }, 2338 .arrow_left => index -|= 1, 2339 .arrow_right => if (index + 1 < count) { 2340 index += 1; 2341 }, 2342 .home => index = 0, 2343 .end => index = count - 1, 2344 .page_up => { 2345 if (self.scroll.grid) |grid| if (slot_height > 0) { 2346 const rows_per_page = grid.size.height / slot_height; 2347 index -|= rows_per_page * cols; 2348 }; 2349 }, 2350 .page_down => { 2351 if (self.scroll.grid) |grid| if (slot_height > 0) { 2352 const rows_per_page = grid.size.height / slot_height; 2353 index = @min(index + rows_per_page * cols, count - 1); 2354 }; 2355 }, 2356 // scroll wheel moves the focused cell by a full row so the 2357 // viewport (via scrollToRect) follows in row-sized steps, 2358 // matching how a scroll wheel feels in a grid view. 2359 .mouse => |mouse| switch (mouse.action) { 2360 .scroll => |dir| switch (dir) { 2361 .up => index -|= cols, 2362 .down => if (index + cols < count) { 2363 index += cols; 2364 }, 2365 }, 2366 else => {}, 2367 }, 2368 else => {}, 2369 } 2370 2371 if (index != current_index) { 2372 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 2373 if (in.cellRect(index)) |rect| { 2374 self.scroll.scrollToRect(rect); 2375 } 2376 } 2377 } 2378 2379 pub fn clearGrid(self: *Scroll) void { 2380 self.scroll.clearGrid(); 2381 } 2382 2383 pub fn getGrid(self: Scroll) ?Grid { 2384 return self.scroll.getGrid(); 2385 } 2386 2387 pub fn getFocus(self: *Scroll) *Focus { 2388 return self.scroll.getFocus(); 2389 } 2390 2391 pub fn getSelectedIndex(self: Scroll) ?usize { 2392 const in = self.scroll.child.flow_box; 2393 const child_id = in.focus.child_id orelse return null; 2394 return in.indexOfFocusId(child_id); 2395 } 2396 2397 fn inner(self: *Scroll) *FlowBox { 2398 return &self.scroll.child.flow_box; 2399 } 2400 }; 2401 }; 2402 2403 // a left-to-right flow of variable-width focusable items that wraps at the 2404 // available width, like words in a paragraph. unlike FlowBox there is no cell 2405 // grid: each item is as wide as its text. selection movement is driven by the 2406 // owning view via indexOfFocusId/verticalNeighbor. 2407 pub const TagFlow = struct { 2408 focus: *Focus, 2409 grid: ?Grid, 2410 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 2411 // last build's item rects (content space), for vertical navigation and 2412 // scroll-into-view. 2413 rects: std.ArrayList(layout.IRect), 2414 // backs each link item's `.custom` focus kind. 2415 arena: std.heap.ArenaAllocator, 2416 2417 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 2418 2419 pub fn init(allocator: std.mem.Allocator) !TagFlow { 2420 return .{ 2421 .focus = try Focus.create(allocator, .container), 2422 .grid = null, 2423 .text_boxes = .empty, 2424 .rects = .empty, 2425 .arena = std.heap.ArenaAllocator.init(allocator), 2426 }; 2427 } 2428 2429 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 2430 self.focus.destroy(allocator); 2431 if (self.grid) |*grid| { 2432 grid.deinit(); 2433 self.grid = null; 2434 } 2435 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 2436 self.text_boxes.deinit(allocator); 2437 self.rects.deinit(allocator); 2438 self.arena.deinit(); 2439 } 2440 2441 // the text box copies the item text; links are copied into the arena, so the 2442 // caller's slices needn't outlive this call. 2443 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 2444 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 2445 self.text_boxes.clearAndFree(allocator); 2446 2447 _ = self.arena.reset(.retain_capacity); 2448 const aa = self.arena.allocator(); 2449 2450 self.focus.clear(); 2451 self.focus.child_id = null; 2452 2453 for (items) |item| { 2454 var text_box = try wgt.TextBox(Widget).init(allocator, item.text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2455 errdefer text_box.deinit(allocator); 2456 text_box.getFocus().focusable = true; 2457 2458 if (item.link.len > 0) { 2459 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 2460 } 2461 2462 try self.text_boxes.append(allocator, text_box); 2463 } 2464 2465 if (self.text_boxes.items.len > 0) { 2466 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 2467 } 2468 } 2469 2470 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2471 self.clearGrid(); 2472 self.focus.clear(); 2473 2474 if (self.text_boxes.items.len == 0) return; 2475 const max_width = constraint.max_size.width orelse 80; 2476 if (max_width == 0) return; 2477 2478 // lay the items out left to right, wrapping to a new row when the next 2479 // one wouldn't fit. 2480 self.rects.clearRetainingCapacity(); 2481 var x: usize = 0; 2482 var y: usize = 0; 2483 var row_height: usize = 0; 2484 var total_width: usize = 0; 2485 for (self.text_boxes.items) |*tb| { 2486 try tb.build(allocator, .{ 2487 .min_size = .{ .width = null, .height = null }, 2488 .max_size = .{ .width = max_width, .height = null }, 2489 }, root_focus); 2490 const tb_grid_maybe = tb.getGrid(); 2491 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 2492 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 2493 if (x > 0 and x + w > max_width) { 2494 x = 0; 2495 y += row_height; 2496 row_height = 0; 2497 } 2498 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 2499 if (h > row_height) row_height = h; 2500 x += w; 2501 if (x > total_width) total_width = x; 2502 } 2503 2504 const total_height = y + row_height; 2505 if (total_width == 0 or total_height == 0) return; 2506 2507 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 2508 errdefer grid.deinit(); 2509 2510 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 2511 const tb_grid = tb.getGrid() orelse continue; 2512 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 2513 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 2514 } 2515 2516 self.grid = grid; 2517 } 2518 2519 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2520 _ = self; 2521 _ = allocator; 2522 _ = key; 2523 _ = root_focus; 2524 } 2525 2526 pub fn clearGrid(self: *TagFlow) void { 2527 if (self.grid) |*grid| { 2528 grid.deinit(); 2529 self.grid = null; 2530 } 2531 for (self.text_boxes.items) |*tb| tb.clearGrid(); 2532 } 2533 2534 pub fn getGrid(self: TagFlow) ?Grid { 2535 return self.grid; 2536 } 2537 2538 pub fn getFocus(self: *TagFlow) *Focus { 2539 return self.focus; 2540 } 2541 2542 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 2543 for (self.text_boxes.items, 0..) |tb, i| { 2544 if (tb.box.focus.id == focus_id) return i; 2545 } 2546 return null; 2547 } 2548 2549 // the first item of the row below (when `downward`) or above, or null at 2550 // the flow's edge. items are laid out in order, so y is nondecreasing. 2551 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 2552 const rects = self.rects.items; 2553 if (index >= rects.len) return null; 2554 const cur_y = rects[index].y; 2555 if (downward) { 2556 for (rects[index + 1 ..], index + 1..) |r, i| { 2557 if (r.y > cur_y) return i; 2558 } 2559 return null; 2560 } 2561 // walk the earlier rows; the last one visited is the row above. 2562 var prev_first: ?usize = null; 2563 var i: usize = 0; 2564 while (i < rects.len and rects[i].y < cur_y) { 2565 prev_first = i; 2566 const y = rects[i].y; 2567 while (i < rects.len and rects[i].y == y) i += 1; 2568 } 2569 return prev_first; 2570 } 2571 }; 2572 2573 // an invisible widget that fills the horizontal space granted by its parent. 2574 // used inside Box(horiz) with a min_size so the box reserves space for the 2575 // children that follow, pushing them to the right. 2576 pub const Spacer = struct { 2577 focus: *Focus, 2578 grid: ?Grid, 2579 2580 pub fn init(allocator: std.mem.Allocator) !Spacer { 2581 return .{ 2582 .focus = try Focus.create(allocator, .container), 2583 .grid = null, 2584 }; 2585 } 2586 2587 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 2588 self.focus.destroy(allocator); 2589 if (self.grid) |*grid| { 2590 grid.deinit(); 2591 self.grid = null; 2592 } 2593 } 2594 2595 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2596 _ = root_focus; 2597 self.clearGrid(); 2598 const width = constraint.max_size.width orelse return; 2599 if (width == 0) return; 2600 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2601 } 2602 2603 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2604 _ = self; 2605 _ = allocator; 2606 _ = key; 2607 _ = root_focus; 2608 } 2609 2610 pub fn clearGrid(self: *Spacer) void { 2611 if (self.grid) |*grid| { 2612 grid.deinit(); 2613 self.grid = null; 2614 } 2615 } 2616 2617 pub fn getGrid(self: Spacer) ?Grid { 2618 return self.grid; 2619 } 2620 2621 pub fn getFocus(self: *Spacer) *Focus { 2622 return self.focus; 2623 } 2624 }; 2625 2626 // a one-row, non-focusable status bar showing the current page's url 2627 pub const Footer = struct { 2628 focus: *Focus, 2629 grid: ?Grid, 2630 session: *Session, 2631 arena: std.heap.ArenaAllocator, 2632 url: []const u8 = "", 2633 url_width: usize = 0, 2634 2635 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 2636 return .{ 2637 .focus = try Focus.create(allocator, .container), 2638 .grid = null, 2639 .session = session, 2640 .arena = std.heap.ArenaAllocator.init(allocator), 2641 }; 2642 } 2643 2644 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2645 self.focus.destroy(allocator); 2646 if (self.grid) |*grid| grid.deinit(); 2647 self.arena.deinit(); 2648 } 2649 2650 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2651 _ = root_focus; 2652 self.clearGrid(); 2653 const width = constraint.max_size.width orelse return; 2654 if (width == 0) return; 2655 2656 _ = self.arena.reset(.retain_capacity); 2657 self.url = ""; 2658 self.url_width = 0; 2659 const aa = self.arena.allocator(); 2660 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2661 const text = terminalWebUrl(aa, self.session, path) catch return; 2662 self.url = text; 2663 2664 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2665 errdefer grid.deinit(); 2666 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2667 var i: usize = 0; 2668 while (utf8.nextCodepointSlice()) |ch| { 2669 if (i == width) break; 2670 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2671 i += 1; 2672 } 2673 self.url_width = i; 2674 self.grid = grid; 2675 } 2676 2677 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2678 _ = allocator; 2679 switch (key) { 2680 .mouse => |mouse| { 2681 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 2682 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 2683 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 2684 }, 2685 else => {}, 2686 } 2687 } 2688 2689 pub fn clearGrid(self: *Footer) void { 2690 if (self.grid) |*grid| { 2691 grid.deinit(); 2692 self.grid = null; 2693 } 2694 } 2695 2696 pub fn getGrid(self: Footer) ?Grid { 2697 return self.grid; 2698 } 2699 2700 pub fn getFocus(self: *Footer) *Focus { 2701 return self.focus; 2702 } 2703 }; 2704 2705 // a single-child wrapper that builds the child at its natural size and 2706 // positions its grid in the middle of the area granted by the parent 2707 // a bordered label, indented past the content it precedes 2708 pub const SectionLabel = struct { 2709 box: wgt.Box(Widget), 2710 2711 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 2712 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2713 errdefer box.deinit(allocator); 2714 2715 { 2716 var gap = try wgt.Text(Widget).init(allocator, " "); 2717 errdefer gap.deinit(allocator); 2718 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2719 } 2720 2721 { 2722 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2723 errdefer tb.deinit(allocator); 2724 tb.getFocus().focusable = true; 2725 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2726 } 2727 2728 box.getFocus().child_id = box.children.keys()[1]; 2729 return .{ .box = box }; 2730 } 2731 2732 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2733 self.box.deinit(allocator); 2734 } 2735 2736 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2737 try self.box.build(allocator, constraint, root_focus); 2738 } 2739 2740 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2741 _ = self; 2742 _ = allocator; 2743 _ = key; 2744 _ = root_focus; 2745 } 2746 2747 pub fn clearGrid(self: *SectionLabel) void { 2748 self.box.clearGrid(); 2749 } 2750 2751 pub fn getGrid(self: SectionLabel) ?Grid { 2752 return self.box.getGrid(); 2753 } 2754 2755 pub fn getFocus(self: *SectionLabel) *Focus { 2756 return self.box.getFocus(); 2757 } 2758 }; 2759 2760 // a submit button, indented past its form's content. the web overlay POSTs 2761 // the button's form; terminal hosts detect presses through `buttonId`. 2762 pub const SubmitButton = struct { 2763 box: wgt.Box(Widget), 2764 2765 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2766 return initLabeled(allocator, "submit"); 2767 } 2768 2769 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 2770 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2771 errdefer box.deinit(allocator); 2772 2773 { 2774 var gap = try wgt.Text(Widget).init(allocator, " "); 2775 errdefer gap.deinit(allocator); 2776 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2777 } 2778 2779 { 2780 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2781 errdefer button.deinit(allocator); 2782 button.getFocus().focusable = true; 2783 // the renderer distinguishes plain clickables from buttons that 2784 // should POST to a server route by this kind. 2785 button.getFocus().kind = .{ .custom = "submit" }; 2786 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2787 } 2788 2789 box.getFocus().child_id = box.children.keys()[1]; 2790 return .{ .box = box }; 2791 } 2792 2793 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2794 self.box.deinit(allocator); 2795 } 2796 2797 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2798 try self.box.build(allocator, constraint, root_focus); 2799 } 2800 2801 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2802 _ = self; 2803 _ = allocator; 2804 _ = key; 2805 _ = root_focus; 2806 } 2807 2808 pub fn clearGrid(self: *SubmitButton) void { 2809 self.box.clearGrid(); 2810 } 2811 2812 pub fn getGrid(self: SubmitButton) ?Grid { 2813 return self.box.getGrid(); 2814 } 2815 2816 pub fn getFocus(self: *SubmitButton) *Focus { 2817 return self.box.getFocus(); 2818 } 2819 2820 // the button's focus id, for click hit-testing 2821 pub fn buttonId(self: *const SubmitButton) usize { 2822 return self.box.children.keys()[1]; 2823 } 2824 }; 2825 2826 pub const Center = struct { 2827 focus: *Focus, 2828 grid: ?Grid, 2829 child: *Widget, 2830 2831 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2832 const child = try allocator.create(Widget); 2833 errdefer allocator.destroy(child); 2834 child.* = child_widget; 2835 return .{ 2836 .focus = try Focus.create(allocator, .container), 2837 .grid = null, 2838 .child = child, 2839 }; 2840 } 2841 2842 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2843 self.focus.destroy(allocator); 2844 if (self.grid) |*grid| { 2845 grid.deinit(); 2846 self.grid = null; 2847 } 2848 self.child.deinit(allocator); 2849 allocator.destroy(self.child); 2850 } 2851 2852 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2853 self.clearGrid(); 2854 self.getFocus().clear(); 2855 2856 // build the child without forcing it to fill min_size; it sizes 2857 // itself to its content within the available max. 2858 try self.child.build(allocator, .{ 2859 .min_size = .{ .width = null, .height = null }, 2860 .max_size = constraint.max_size, 2861 }, root_focus); 2862 2863 const child_grid = self.child.getGrid() orelse return; 2864 2865 // prefer max when given; otherwise grow to max(min, child) so that 2866 // when only a min is set (e.g. the wasm path passes viewport rows 2867 // as min_height with no max), we can still vertically center while 2868 // letting taller content extend past the min. 2869 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 2870 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 2871 if (width == 0 or height == 0) return; 2872 2873 const offset_x: usize = (width -| child_grid.size.width) / 2; 2874 const offset_y: usize = (height -| child_grid.size.height) / 2; 2875 2876 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2877 errdefer grid.deinit(); 2878 try grid.drawGrid(child_grid, offset_x, offset_y); 2879 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2880 self.getFocus().child_id = self.child.getFocus().id; 2881 2882 self.grid = grid; 2883 } 2884 2885 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2886 try self.child.input(allocator, key, root_focus); 2887 } 2888 2889 pub fn clearGrid(self: *Center) void { 2890 if (self.grid) |*grid| { 2891 grid.deinit(); 2892 self.grid = null; 2893 } 2894 self.child.clearGrid(); 2895 } 2896 2897 pub fn getGrid(self: Center) ?Grid { 2898 return self.grid; 2899 } 2900 2901 pub fn getFocus(self: *Center) *Focus { 2902 return self.focus; 2903 } 2904 }; 2905 2906 // renders truecolor ANSI art 2907 pub const AnsiArt = struct { 2908 focus: *Focus, 2909 grid: ?Grid, 2910 content: []const u8, 2911 2912 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2913 return .{ 2914 .focus = try Focus.create(allocator, .container), 2915 .grid = null, 2916 .content = content, 2917 }; 2918 } 2919 2920 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2921 self.focus.destroy(allocator); 2922 if (self.grid) |*grid| { 2923 grid.deinit(); 2924 self.grid = null; 2925 } 2926 } 2927 2928 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2929 _ = root_focus; 2930 self.clearGrid(); 2931 2932 // parse into rows of cells, then copy into a rectangular grid 2933 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2934 defer { 2935 for (rows.items) |*row| row.deinit(allocator); 2936 rows.deinit(allocator); 2937 } 2938 var row: std.ArrayList(Grid.Cell) = .empty; 2939 errdefer row.deinit(allocator); 2940 2941 var style: Grid.Style = .{}; 2942 var width: usize = 0; 2943 const content = self.content; 2944 var i: usize = 0; 2945 while (i < content.len) { 2946 const byte = content[i]; 2947 if (byte == '\n') { 2948 width = @max(width, row.items.len); 2949 try rows.append(allocator, row); 2950 row = .empty; 2951 i += 1; 2952 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2953 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2954 var j = i + 2; 2955 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2956 if (j >= content.len) { 2957 i = content.len; // malformed trailing escape; stop 2958 } else { 2959 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2960 i = j + 1; 2961 } 2962 } else { 2963 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2964 const end = @min(content.len, i + len); 2965 const rune = content[i..end]; 2966 // a foreground color does not make a space visible. only an 2967 // effective background makes it opaque; foreground state often 2968 // remains active through the padding at the end of a row. 2969 const effective_background = if (style.inverted) style.fg else style.bg; 2970 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 2971 try row.append(allocator, .{ 2972 .rune = if (transparent) null else rune, 2973 .style = style, 2974 }); 2975 i = end; 2976 } 2977 } 2978 // a trailing row with content but no closing newline 2979 if (row.items.len > 0) { 2980 width = @max(width, row.items.len); 2981 try rows.append(allocator, row); 2982 } else { 2983 row.deinit(allocator); 2984 } 2985 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2986 2987 const height = rows.items.len; 2988 if (width == 0 or height == 0) return; 2989 2990 const clamped_w = @min(width, constraint.max_size.width orelse width); 2991 const clamped_h = @min(height, constraint.max_size.height orelse height); 2992 2993 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2994 errdefer grid.deinit(); 2995 for (rows.items[0..clamped_h], 0..) |r, y| { 2996 const n = @min(r.items.len, clamped_w); 2997 for (r.items[0..n], 0..) |cell, x| { 2998 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2999 } 3000 } 3001 self.grid = grid; 3002 } 3003 3004 pub fn clearGrid(self: *AnsiArt) void { 3005 if (self.grid) |*grid| { 3006 grid.deinit(); 3007 self.grid = null; 3008 } 3009 } 3010 3011 pub fn getGrid(self: AnsiArt) ?Grid { 3012 return self.grid; 3013 } 3014 3015 pub fn getFocus(self: *AnsiArt) *Focus { 3016 return self.focus; 3017 } 3018 3019 fn applySgr(style: *Grid.Style, params: []const u8) void { 3020 var nums: [16]u32 = undefined; 3021 var n: usize = 0; 3022 var it = std.mem.splitScalar(u8, params, ';'); 3023 while (it.next()) |tok| { 3024 if (n >= nums.len) break; 3025 // an empty parameter (e.g. bare "\x1b[m") means 0 3026 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 3027 n += 1; 3028 } 3029 if (n == 0) { 3030 style.* = .{}; 3031 return; 3032 } 3033 var i: usize = 0; 3034 while (i < n) : (i += 1) { 3035 switch (nums[i]) { 3036 0 => style.* = .{}, 3037 7 => style.inverted = true, 3038 27 => style.inverted = false, 3039 39 => style.fg = null, 3040 49 => style.bg = null, 3041 38, 48 => { 3042 // truecolor form: 38;2;r;g;b — anything else is ignored 3043 if (i + 4 < n and nums[i + 1] == 2) { 3044 const c = Grid.Color{ 3045 .r = @truncate(nums[i + 2]), 3046 .g = @truncate(nums[i + 3]), 3047 .b = @truncate(nums[i + 4]), 3048 }; 3049 if (nums[i] == 38) style.fg = c else style.bg = c; 3050 i += 4; 3051 } 3052 }, 3053 else => {}, 3054 } 3055 } 3056 } 3057 }; 3058 3059 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 3060 pub const AnsiBackground = struct { 3061 grid: ?Grid, 3062 child: *Widget, 3063 art: AnsiArt, 3064 session: *Session, 3065 3066 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *Session) !AnsiBackground { 3067 var cw = child_widget; 3068 const child = allocator.create(Widget) catch |e| { 3069 cw.deinit(allocator); 3070 return e; 3071 }; 3072 child.* = cw; 3073 errdefer { 3074 child.deinit(allocator); 3075 allocator.destroy(child); 3076 } 3077 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 3078 } 3079 3080 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 3081 if (self.grid) |*grid| grid.deinit(); 3082 self.art.deinit(allocator); 3083 self.child.deinit(allocator); 3084 allocator.destroy(self.child); 3085 } 3086 3087 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 3088 self.clearGrid(); 3089 3090 // make the wrapped page fill the whole available area 3091 try self.child.build(allocator, .{ 3092 .min_size = .{ 3093 .width = constraint.max_size.width orelse constraint.min_size.width, 3094 .height = constraint.max_size.height orelse constraint.min_size.height, 3095 }, 3096 .max_size = constraint.max_size, 3097 }, root_focus); 3098 3099 if (self.session.data.enable_ansi) { 3100 self.art.content = self.session.data.ansi_art; 3101 if (self.child.getGrid()) |fg| { 3102 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 3103 } 3104 } 3105 } 3106 3107 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 3108 try self.child.input(allocator, key, root_focus); 3109 } 3110 3111 pub fn clearGrid(self: *AnsiBackground) void { 3112 if (self.grid) |*grid| { 3113 grid.deinit(); 3114 self.grid = null; 3115 } 3116 self.child.clearGrid(); 3117 self.art.clearGrid(); 3118 } 3119 3120 pub fn getGrid(self: AnsiBackground) ?Grid { 3121 return self.grid orelse self.child.getGrid(); 3122 } 3123 3124 pub fn getFocus(self: *AnsiBackground) *Focus { 3125 return self.child.getFocus(); 3126 } 3127 3128 // which top corner the art hugs. 3129 const ArtAnchor = enum { top_left, top_right }; 3130 3131 // a foreground cell counts as blank if it's empty or a bare space without styling 3132 fn cellIsBlank(cell: Grid.Cell) bool { 3133 if (cell.rune) |rune| { 3134 return std.mem.eql(u8, rune, " ") and 3135 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 3136 } 3137 return true; 3138 } 3139 3140 // the art is dimmed to this fraction of its brightness so foreground text 3141 // stays legible over it 3142 const art_brightness = 35; // percent 3143 3144 fn dimColor(color: ?Grid.Color) ?Grid.Color { 3145 const c = color orelse return null; 3146 return .{ 3147 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 3148 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 3149 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 3150 }; 3151 } 3152 3153 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 3154 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 3155 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 3156 // snap such channels clear of that range so the backdrop doesn't blink or 3157 // hide text there; the ≤9/255 shift is imperceptible on terminals that 3158 // actually render truecolor. 3159 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 3160 const c = color orelse return null; 3161 const snap = struct { 3162 fn f(v: u8) u8 { 3163 return if (v >= 1 and v <= 9) 10 else v; 3164 } 3165 }.f; 3166 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 3167 } 3168 3169 // composites ANSI art behind a foreground grid 3170 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 3171 art.clearGrid(); 3172 try art.build(allocator, .{ 3173 .min_size = .{ .width = null, .height = null }, 3174 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 3175 }, root_focus); 3176 3177 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 3178 errdefer out.deinit(); 3179 3180 if (art.getGrid()) |art_grid| { 3181 const anchor_x = switch (anchor) { 3182 .top_left => 0, 3183 .top_right => foreground.size.width -| art_grid.size.width, 3184 }; 3185 for (0..art_grid.size.height) |y| { 3186 for (0..art_grid.size.width) |x| { 3187 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 3188 if (src.rune == null) continue; 3189 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 3190 const dst = &out.cells.items[idx]; 3191 // composite the dimmed art behind every cell so it shows 3192 // through the whole UI: a blank cell becomes the art, while a 3193 // cell with a glyph keeps the glyph but takes the dimmed art as 3194 // its background (replacing whatever background it had, which 3195 // would otherwise obscure the art). 3196 if (cellIsBlank(dst.*)) { 3197 dst.* = src; 3198 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 3199 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 3200 } else { 3201 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 3202 dst.style.bg = background_maybe; 3203 // change the color of text overlaying ANSI art to ensure 3204 // it is readable. don't do this in the web UI, since we 3205 // have direct control of the theme there (also, inner 3206 // scroll panes allow text to move without a re-render 3207 // by the TUI, which would break this). also don't do this 3208 // if the text is explicitly styled. 3209 if (is_terminal and dst.style.fg == null) { 3210 if (background_maybe) |background| { 3211 // use near-black and near-white, which is easier on the eyes 3212 // while retaining high contrast 3213 const luminance = (@as(u32, background.r) * 299 + 3214 @as(u32, background.g) * 587 + 3215 @as(u32, background.b) * 114) / 1000; 3216 dst.style.fg = if (luminance >= 128) 3217 .{ .r = 16, .g = 16, .b = 16 } 3218 else 3219 .{ .r = 240, .g = 240, .b = 240 }; 3220 } 3221 } 3222 } 3223 } 3224 } 3225 } 3226 return out; 3227 } 3228 }; 3229