╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1714 return self.focus; 1715 } 1716 1717 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1718 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1719 const slot_height = self.options.cell_height + border_rows; 1720 const col = index % self.last_cols; 1721 const row = index / self.last_cols; 1722 return .{ 1723 .x = @intCast(col * self.options.cell_width), 1724 .y = @intCast(row * slot_height), 1725 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1726 }; 1727 } 1728 1729 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1730 for (self.text_boxes.items, 0..) |tb, i| { 1731 if (tb.box.focus.id == focus_id) return i; 1732 } 1733 return null; 1734 } 1735 1736 pub const Scroll = struct { 1737 scroll: wgt.Scroll(Widget), 1738 1739 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1740 var layout_inner = try FlowBox.init(allocator, options); 1741 errdefer layout_inner.deinit(allocator); 1742 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1743 errdefer scroll.deinit(allocator); 1744 return .{ .scroll = scroll }; 1745 } 1746 1747 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1748 self.scroll.deinit(allocator); 1749 } 1750 1751 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1752 self.scroll.x = 0; 1753 self.scroll.y = 0; 1754 try self.inner().setItems(allocator, items); 1755 } 1756 1757 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1758 try self.scroll.build(allocator, constraint, root_focus); 1759 } 1760 1761 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1762 _ = allocator; 1763 const in = self.inner(); 1764 const child_id = in.focus.child_id orelse return; 1765 const current_index = in.indexOfFocusId(child_id) orelse return; 1766 const count = in.text_boxes.items.len; 1767 if (count == 0) return; 1768 const cols = in.last_cols; 1769 const slot_height = in.options.cell_height + FlowBox.border_rows; 1770 1771 var index = current_index; 1772 switch (key) { 1773 .arrow_up => index -|= cols, 1774 .arrow_down => if (index + cols < count) { 1775 index += cols; 1776 }, 1777 .arrow_left => index -|= 1, 1778 .arrow_right => if (index + 1 < count) { 1779 index += 1; 1780 }, 1781 .home => index = 0, 1782 .end => index = count - 1, 1783 .page_up => { 1784 if (self.scroll.grid) |grid| if (slot_height > 0) { 1785 const rows_per_page = grid.size.height / slot_height; 1786 index -|= rows_per_page * cols; 1787 }; 1788 }, 1789 .page_down => { 1790 if (self.scroll.grid) |grid| if (slot_height > 0) { 1791 const rows_per_page = grid.size.height / slot_height; 1792 index = @min(index + rows_per_page * cols, count - 1); 1793 }; 1794 }, 1795 // scroll wheel moves the focused cell by a full row so the 1796 // viewport (via scrollToRect) follows in row-sized steps, 1797 // matching how a scroll wheel feels in a grid view. 1798 .mouse => |mouse| switch (mouse.action) { 1799 .scroll => |dir| switch (dir) { 1800 .up => index -|= cols, 1801 .down => if (index + cols < count) { 1802 index += cols; 1803 }, 1804 }, 1805 else => {}, 1806 }, 1807 else => {}, 1808 } 1809 1810 if (index != current_index) { 1811 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1812 if (in.cellRect(index)) |rect| { 1813 self.scroll.scrollToRect(rect); 1814 } 1815 } 1816 } 1817 1818 pub fn clearGrid(self: *Scroll) void { 1819 self.scroll.clearGrid(); 1820 } 1821 1822 pub fn getGrid(self: Scroll) ?Grid { 1823 return self.scroll.getGrid(); 1824 } 1825 1826 pub fn getFocus(self: *Scroll) *Focus { 1827 return self.scroll.getFocus(); 1828 } 1829 1830 pub fn getSelectedIndex(self: Scroll) ?usize { 1831 const in = self.scroll.child.flow_box; 1832 const child_id = in.focus.child_id orelse return null; 1833 return in.indexOfFocusId(child_id); 1834 } 1835 1836 fn inner(self: *Scroll) *FlowBox { 1837 return &self.scroll.child.flow_box; 1838 } 1839 }; 1840 }; 1841 1842 // a left-to-right flow of variable-width focusable items that wraps at the 1843 // available width, like words in a paragraph. unlike FlowBox there is no cell 1844 // grid: each item is as wide as its text. selection movement is driven by the 1845 // owning view via indexOfFocusId/verticalNeighbor. 1846 pub const TagFlow = struct { 1847 focus: *Focus, 1848 grid: ?Grid, 1849 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1850 // last build's item rects (content space), for vertical navigation and 1851 // scroll-into-view. 1852 rects: std.ArrayList(layout.IRect), 1853 // backs the per-item strings the text boxes borrow. 1854 arena: std.heap.ArenaAllocator, 1855 1856 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1857 1858 pub fn init(allocator: std.mem.Allocator) !TagFlow { 1859 return .{ 1860 .focus = try Focus.create(allocator, .container), 1861 .grid = null, 1862 .text_boxes = .empty, 1863 .rects = .empty, 1864 .arena = std.heap.ArenaAllocator.init(allocator), 1865 }; 1866 } 1867 1868 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 1869 self.focus.destroy(allocator); 1870 if (self.grid) |*grid| { 1871 grid.deinit(); 1872 self.grid = null; 1873 } 1874 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1875 self.text_boxes.deinit(allocator); 1876 self.rects.deinit(allocator); 1877 self.arena.deinit(); 1878 } 1879 1880 // both the item text and the link are copied into the arena, so the caller's 1881 // slices needn't outlive this call. 1882 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 1883 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1884 self.text_boxes.clearAndFree(allocator); 1885 1886 _ = self.arena.reset(.retain_capacity); 1887 const aa = self.arena.allocator(); 1888 1889 self.focus.clear(); 1890 self.focus.child_id = null; 1891 1892 for (items) |item| { 1893 const line = try aa.dupe(u8, item.text); 1894 1895 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1896 errdefer text_box.deinit(allocator); 1897 text_box.getFocus().focusable = true; 1898 1899 if (item.link.len > 0) { 1900 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1901 } 1902 1903 try self.text_boxes.append(allocator, text_box); 1904 } 1905 1906 if (self.text_boxes.items.len > 0) { 1907 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1908 } 1909 } 1910 1911 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1912 self.clearGrid(); 1913 self.focus.clear(); 1914 1915 if (self.text_boxes.items.len == 0) return; 1916 const max_width = constraint.max_size.width orelse 80; 1917 if (max_width == 0) return; 1918 1919 // lay the items out left to right, wrapping to a new row when the next 1920 // one wouldn't fit. 1921 self.rects.clearRetainingCapacity(); 1922 var x: usize = 0; 1923 var y: usize = 0; 1924 var row_height: usize = 0; 1925 var total_width: usize = 0; 1926 for (self.text_boxes.items) |*tb| { 1927 try tb.build(allocator, .{ 1928 .min_size = .{ .width = null, .height = null }, 1929 .max_size = .{ .width = max_width, .height = null }, 1930 }, root_focus); 1931 const tb_grid_maybe = tb.getGrid(); 1932 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 1933 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 1934 if (x > 0 and x + w > max_width) { 1935 x = 0; 1936 y += row_height; 1937 row_height = 0; 1938 } 1939 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 1940 if (h > row_height) row_height = h; 1941 x += w; 1942 if (x > total_width) total_width = x; 1943 } 1944 1945 const total_height = y + row_height; 1946 if (total_width == 0 or total_height == 0) return; 1947 1948 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 1949 errdefer grid.deinit(); 1950 1951 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 1952 const tb_grid = tb.getGrid() orelse continue; 1953 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 1954 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 1955 } 1956 1957 self.grid = grid; 1958 } 1959 1960 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1961 _ = self; 1962 _ = allocator; 1963 _ = key; 1964 _ = root_focus; 1965 } 1966 1967 pub fn clearGrid(self: *TagFlow) void { 1968 if (self.grid) |*grid| { 1969 grid.deinit(); 1970 self.grid = null; 1971 } 1972 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1973 } 1974 1975 pub fn getGrid(self: TagFlow) ?Grid { 1976 return self.grid; 1977 } 1978 1979 pub fn getFocus(self: *TagFlow) *Focus { 1980 return self.focus; 1981 } 1982 1983 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 1984 for (self.text_boxes.items, 0..) |tb, i| { 1985 if (tb.box.focus.id == focus_id) return i; 1986 } 1987 return null; 1988 } 1989 1990 // the first item of the row below (when `downward`) or above, or null at 1991 // the flow's edge. items are laid out in order, so y is nondecreasing. 1992 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 1993 const rects = self.rects.items; 1994 if (index >= rects.len) return null; 1995 const cur_y = rects[index].y; 1996 if (downward) { 1997 for (rects[index + 1 ..], index + 1..) |r, i| { 1998 if (r.y > cur_y) return i; 1999 } 2000 return null; 2001 } 2002 // walk the earlier rows; the last one visited is the row above. 2003 var prev_first: ?usize = null; 2004 var i: usize = 0; 2005 while (i < rects.len and rects[i].y < cur_y) { 2006 prev_first = i; 2007 const y = rects[i].y; 2008 while (i < rects.len and rects[i].y == y) i += 1; 2009 } 2010 return prev_first; 2011 } 2012 }; 2013 2014 // an invisible widget that fills the horizontal space granted by its parent. 2015 // used inside Box(horiz) with a min_size so the box reserves space for the 2016 // children that follow, pushing them to the right. 2017 pub const Spacer = struct { 2018 focus: *Focus, 2019 grid: ?Grid, 2020 2021 pub fn init(allocator: std.mem.Allocator) !Spacer { 2022 return .{ 2023 .focus = try Focus.create(allocator, .container), 2024 .grid = null, 2025 }; 2026 } 2027 2028 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 2029 self.focus.destroy(allocator); 2030 if (self.grid) |*grid| { 2031 grid.deinit(); 2032 self.grid = null; 2033 } 2034 } 2035 2036 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2037 _ = root_focus; 2038 self.clearGrid(); 2039 const width = constraint.max_size.width orelse return; 2040 if (width == 0) return; 2041 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2042 } 2043 2044 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2045 _ = self; 2046 _ = allocator; 2047 _ = key; 2048 _ = root_focus; 2049 } 2050 2051 pub fn clearGrid(self: *Spacer) void { 2052 if (self.grid) |*grid| { 2053 grid.deinit(); 2054 self.grid = null; 2055 } 2056 } 2057 2058 pub fn getGrid(self: Spacer) ?Grid { 2059 return self.grid; 2060 } 2061 2062 pub fn getFocus(self: *Spacer) *Focus { 2063 return self.focus; 2064 } 2065 }; 2066 2067 // a one-row, non-focusable status bar showing the current page's url 2068 pub const Footer = struct { 2069 focus: *Focus, 2070 grid: ?Grid, 2071 session: *Session, 2072 arena: std.heap.ArenaAllocator, 2073 2074 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 2075 return .{ 2076 .focus = try Focus.create(allocator, .container), 2077 .grid = null, 2078 .session = session, 2079 .arena = std.heap.ArenaAllocator.init(allocator), 2080 }; 2081 } 2082 2083 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2084 self.focus.destroy(allocator); 2085 if (self.grid) |*grid| grid.deinit(); 2086 self.arena.deinit(); 2087 } 2088 2089 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2090 _ = root_focus; 2091 self.clearGrid(); 2092 const width = constraint.max_size.width orelse return; 2093 if (width == 0) return; 2094 2095 _ = self.arena.reset(.retain_capacity); 2096 const aa = self.arena.allocator(); 2097 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2098 const text = if (self.session.web_port) |port| 2099 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 2100 else 2101 path; 2102 2103 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2104 errdefer grid.deinit(); 2105 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2106 var i: usize = 0; 2107 while (utf8.nextCodepointSlice()) |ch| { 2108 if (i == width) break; 2109 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2110 i += 1; 2111 } 2112 self.grid = grid; 2113 } 2114 2115 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2116 _ = self; 2117 _ = allocator; 2118 _ = key; 2119 _ = root_focus; 2120 } 2121 2122 pub fn clearGrid(self: *Footer) void { 2123 if (self.grid) |*grid| { 2124 grid.deinit(); 2125 self.grid = null; 2126 } 2127 } 2128 2129 pub fn getGrid(self: Footer) ?Grid { 2130 return self.grid; 2131 } 2132 2133 pub fn getFocus(self: *Footer) *Focus { 2134 return self.focus; 2135 } 2136 }; 2137 2138 // a single-child wrapper that builds the child at its natural size and 2139 // positions its grid in the middle of the area granted by the parent 2140 // a bordered label, indented past the content it precedes 2141 pub const SectionLabel = struct { 2142 box: wgt.Box(Widget), 2143 2144 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 2145 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2146 errdefer box.deinit(allocator); 2147 2148 { 2149 var gap = try wgt.Text(Widget).init(allocator, " "); 2150 errdefer gap.deinit(allocator); 2151 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2152 } 2153 2154 { 2155 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2156 errdefer tb.deinit(allocator); 2157 tb.getFocus().focusable = true; 2158 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2159 } 2160 2161 box.getFocus().child_id = box.children.keys()[1]; 2162 return .{ .box = box }; 2163 } 2164 2165 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2166 self.box.deinit(allocator); 2167 } 2168 2169 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2170 try self.box.build(allocator, constraint, root_focus); 2171 } 2172 2173 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2174 _ = self; 2175 _ = allocator; 2176 _ = key; 2177 _ = root_focus; 2178 } 2179 2180 pub fn clearGrid(self: *SectionLabel) void { 2181 self.box.clearGrid(); 2182 } 2183 2184 pub fn getGrid(self: SectionLabel) ?Grid { 2185 return self.box.getGrid(); 2186 } 2187 2188 pub fn getFocus(self: *SectionLabel) *Focus { 2189 return self.box.getFocus(); 2190 } 2191 }; 2192 2193 // a submit button, indented past its form's content. the web overlay POSTs 2194 // the button's form; terminal hosts detect presses through `buttonId`. 2195 pub const SubmitButton = struct { 2196 box: wgt.Box(Widget), 2197 2198 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2199 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2200 errdefer box.deinit(allocator); 2201 2202 { 2203 var gap = try wgt.Text(Widget).init(allocator, " "); 2204 errdefer gap.deinit(allocator); 2205 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2206 } 2207 2208 { 2209 const label = "submit"; 2210 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2211 errdefer button.deinit(allocator); 2212 button.getFocus().focusable = true; 2213 // the renderer distinguishes plain clickables from buttons that 2214 // should POST to a server route by this kind. 2215 button.getFocus().kind = .{ .custom = "submit" }; 2216 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2217 } 2218 2219 box.getFocus().child_id = box.children.keys()[1]; 2220 return .{ .box = box }; 2221 } 2222 2223 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2224 self.box.deinit(allocator); 2225 } 2226 2227 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2228 try self.box.build(allocator, constraint, root_focus); 2229 } 2230 2231 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2232 _ = self; 2233 _ = allocator; 2234 _ = key; 2235 _ = root_focus; 2236 } 2237 2238 pub fn clearGrid(self: *SubmitButton) void { 2239 self.box.clearGrid(); 2240 } 2241 2242 pub fn getGrid(self: SubmitButton) ?Grid { 2243 return self.box.getGrid(); 2244 } 2245 2246 pub fn getFocus(self: *SubmitButton) *Focus { 2247 return self.box.getFocus(); 2248 } 2249 2250 // the button's focus id, for click hit-testing 2251 pub fn buttonId(self: *const SubmitButton) usize { 2252 return self.box.children.keys()[1]; 2253 } 2254 }; 2255 2256 pub const Center = struct { 2257 focus: *Focus, 2258 grid: ?Grid, 2259 child: *Widget, 2260 2261 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2262 const child = try allocator.create(Widget); 2263 errdefer allocator.destroy(child); 2264 child.* = child_widget; 2265 return .{ 2266 .focus = try Focus.create(allocator, .container), 2267 .grid = null, 2268 .child = child, 2269 }; 2270 } 2271 2272 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2273 self.focus.destroy(allocator); 2274 if (self.grid) |*grid| { 2275 grid.deinit(); 2276 self.grid = null; 2277 } 2278 self.child.deinit(allocator); 2279 allocator.destroy(self.child); 2280 } 2281 2282 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2283 self.clearGrid(); 2284 self.getFocus().clear(); 2285 2286 // build the child without forcing it to fill min_size; it sizes 2287 // itself to its content within the available max. 2288 try self.child.build(allocator, .{ 2289 .min_size = .{ .width = null, .height = null }, 2290 .max_size = constraint.max_size, 2291 }, root_focus); 2292 2293 const child_grid = self.child.getGrid() orelse return; 2294 2295 // prefer max when given; otherwise grow to max(min, child) so that 2296 // when only a min is set (e.g. the wasm path passes viewport rows 2297 // as min_height with no max), we can still vertically center while 2298 // letting taller content extend past the min. 2299 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; 2300 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; 2301 if (width == 0 or height == 0) return; 2302 2303 const offset_x: usize = (width -| child_grid.size.width) / 2; 2304 const offset_y: usize = (height -| child_grid.size.height) / 2; 2305 2306 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2307 errdefer grid.deinit(); 2308 try grid.drawGrid(child_grid, offset_x, offset_y); 2309 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2310 self.getFocus().child_id = self.child.getFocus().id; 2311 2312 self.grid = grid; 2313 } 2314 2315 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2316 try self.child.input(allocator, key, root_focus); 2317 } 2318 2319 pub fn clearGrid(self: *Center) void { 2320 if (self.grid) |*grid| { 2321 grid.deinit(); 2322 self.grid = null; 2323 } 2324 self.child.clearGrid(); 2325 } 2326 2327 pub fn getGrid(self: Center) ?Grid { 2328 return self.grid; 2329 } 2330 2331 pub fn getFocus(self: *Center) *Focus { 2332 return self.focus; 2333 } 2334 }; 2335 2336 // renders truecolor ANSI art 2337 pub const AnsiArt = struct { 2338 focus: *Focus, 2339 grid: ?Grid, 2340 content: []const u8, 2341 2342 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2343 return .{ 2344 .focus = try Focus.create(allocator, .container), 2345 .grid = null, 2346 .content = content, 2347 }; 2348 } 2349 2350 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2351 self.focus.destroy(allocator); 2352 if (self.grid) |*grid| { 2353 grid.deinit(); 2354 self.grid = null; 2355 } 2356 } 2357 2358 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2359 _ = root_focus; 2360 self.clearGrid(); 2361 2362 // parse into rows of cells, then copy into a rectangular grid 2363 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2364 defer { 2365 for (rows.items) |*row| row.deinit(allocator); 2366 rows.deinit(allocator); 2367 } 2368 var row: std.ArrayList(Grid.Cell) = .empty; 2369 errdefer row.deinit(allocator); 2370 2371 var style: Grid.Style = .{}; 2372 var width: usize = 0; 2373 const content = self.content; 2374 var i: usize = 0; 2375 while (i < content.len) { 2376 const byte = content[i]; 2377 if (byte == '\n') { 2378 width = @max(width, row.items.len); 2379 try rows.append(allocator, row); 2380 row = .empty; 2381 i += 1; 2382 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2383 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2384 var j = i + 2; 2385 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2386 if (j >= content.len) { 2387 i = content.len; // malformed trailing escape; stop 2388 } else { 2389 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2390 i = j + 1; 2391 } 2392 } else { 2393 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2394 const end = @min(content.len, i + len); 2395 const rune = content[i..end]; 2396 const transparent = end - i == 1 and rune[0] == ' ' and 2397 style.fg == null and style.bg == null and !style.inverted; 2398 try row.append(allocator, .{ 2399 .rune = if (transparent) null else rune, 2400 .style = style, 2401 }); 2402 i = end; 2403 } 2404 } 2405 // a trailing row with content but no closing newline 2406 if (row.items.len > 0) { 2407 width = @max(width, row.items.len); 2408 try rows.append(allocator, row); 2409 } else { 2410 row.deinit(allocator); 2411 } 2412 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2413 2414 const height = rows.items.len; 2415 if (width == 0 or height == 0) return; 2416 2417 const clamped_w = @min(width, constraint.max_size.width orelse width); 2418 const clamped_h = @min(height, constraint.max_size.height orelse height); 2419 2420 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2421 errdefer grid.deinit(); 2422 for (rows.items[0..clamped_h], 0..) |r, y| { 2423 const n = @min(r.items.len, clamped_w); 2424 for (r.items[0..n], 0..) |cell, x| { 2425 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2426 } 2427 } 2428 self.grid = grid; 2429 } 2430 2431 pub fn clearGrid(self: *AnsiArt) void { 2432 if (self.grid) |*grid| { 2433 grid.deinit(); 2434 self.grid = null; 2435 } 2436 } 2437 2438 pub fn getGrid(self: AnsiArt) ?Grid { 2439 return self.grid; 2440 } 2441 2442 pub fn getFocus(self: *AnsiArt) *Focus { 2443 return self.focus; 2444 } 2445 2446 fn applySgr(style: *Grid.Style, params: []const u8) void { 2447 var nums: [16]u32 = undefined; 2448 var n: usize = 0; 2449 var it = std.mem.splitScalar(u8, params, ';'); 2450 while (it.next()) |tok| { 2451 if (n >= nums.len) break; 2452 // an empty parameter (e.g. bare "\x1b[m") means 0 2453 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2454 n += 1; 2455 } 2456 if (n == 0) { 2457 style.* = .{}; 2458 return; 2459 } 2460 var i: usize = 0; 2461 while (i < n) : (i += 1) { 2462 switch (nums[i]) { 2463 0 => style.* = .{}, 2464 7 => style.inverted = true, 2465 27 => style.inverted = false, 2466 39 => style.fg = null, 2467 49 => style.bg = null, 2468 38, 48 => { 2469 // truecolor form: 38;2;r;g;b — anything else is ignored 2470 if (i + 4 < n and nums[i + 1] == 2) { 2471 const c = Grid.Color{ 2472 .r = @truncate(nums[i + 2]), 2473 .g = @truncate(nums[i + 3]), 2474 .b = @truncate(nums[i + 4]), 2475 }; 2476 if (nums[i] == 38) style.fg = c else style.bg = c; 2477 i += 4; 2478 } 2479 }, 2480 else => {}, 2481 } 2482 } 2483 } 2484 }; 2485 2486 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2487 pub const AnsiBackground = struct { 2488 grid: ?Grid, 2489 child: *Widget, 2490 art: AnsiArt, 2491 session: *Session, 2492 2493 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2494 var cw = child_widget; 2495 const child = allocator.create(Widget) catch |e| { 2496 cw.deinit(allocator); 2497 return e; 2498 }; 2499 child.* = cw; 2500 errdefer { 2501 child.deinit(allocator); 2502 allocator.destroy(child); 2503 } 2504 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2505 } 2506 2507 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2508 if (self.grid) |*grid| grid.deinit(); 2509 self.art.deinit(allocator); 2510 self.child.deinit(allocator); 2511 allocator.destroy(self.child); 2512 } 2513 2514 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2515 self.clearGrid(); 2516 2517 // make the wrapped page fill the whole available area 2518 try self.child.build(allocator, .{ 2519 .min_size = .{ 2520 .width = constraint.max_size.width orelse constraint.min_size.width, 2521 .height = constraint.max_size.height orelse constraint.min_size.height, 2522 }, 2523 .max_size = constraint.max_size, 2524 }, root_focus); 2525 2526 if (self.session.data.enable_ansi) { 2527 if (self.child.getGrid()) |fg| { 2528 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2529 } 2530 } 2531 } 2532 2533 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2534 try self.child.input(allocator, key, root_focus); 2535 } 2536 2537 pub fn clearGrid(self: *AnsiBackground) void { 2538 if (self.grid) |*grid| { 2539 grid.deinit(); 2540 self.grid = null; 2541 } 2542 self.child.clearGrid(); 2543 self.art.clearGrid(); 2544 } 2545 2546 pub fn getGrid(self: AnsiBackground) ?Grid { 2547 return self.grid orelse self.child.getGrid(); 2548 } 2549 2550 pub fn getFocus(self: *AnsiBackground) *Focus { 2551 return self.child.getFocus(); 2552 } 2553 2554 // which top corner the art hugs. 2555 const ArtAnchor = enum { top_left, top_right }; 2556 2557 // a foreground cell counts as blank if it's empty or a bare space without styling 2558 fn cellIsBlank(cell: Grid.Cell) bool { 2559 if (cell.rune) |rune| { 2560 return std.mem.eql(u8, rune, " ") and 2561 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2562 } 2563 return true; 2564 } 2565 2566 // the art is dimmed to this fraction of its brightness so foreground text 2567 // stays legible over it 2568 const art_brightness = 45; // percent 2569 2570 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2571 const c = color orelse return null; 2572 return .{ 2573 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2574 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2575 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2576 }; 2577 } 2578 2579 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2580 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2581 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2582 // snap such channels clear of that range so the backdrop doesn't blink or 2583 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2584 // actually render truecolor. 2585 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2586 const c = color orelse return null; 2587 const snap = struct { 2588 fn f(v: u8) u8 { 2589 return if (v >= 1 and v <= 9) 10 else v; 2590 } 2591 }.f; 2592 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2593 } 2594 2595 // composites ANSI art behind a foreground grid 2596 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2597 art.clearGrid(); 2598 try art.build(allocator, .{ 2599 .min_size = .{ .width = null, .height = null }, 2600 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2601 }, root_focus); 2602 2603 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2604 errdefer out.deinit(); 2605 2606 if (art.getGrid()) |art_grid| { 2607 const anchor_x = switch (anchor) { 2608 .top_left => 0, 2609 .top_right => foreground.size.width -| art_grid.size.width, 2610 }; 2611 for (0..art_grid.size.height) |y| { 2612 for (0..art_grid.size.width) |x| { 2613 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2614 if (src.rune == null) continue; 2615 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2616 const dst = &out.cells.items[idx]; 2617 // composite the dimmed art behind every cell so it shows 2618 // through the whole UI: a blank cell becomes the art, while a 2619 // cell with a glyph keeps the glyph but takes the dimmed art as 2620 // its background (replacing whatever background it had, which 2621 // would otherwise obscure the art). 2622 if (cellIsBlank(dst.*)) { 2623 dst.* = src; 2624 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2625 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2626 } else { 2627 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2628 dst.style.bg = background_maybe; 2629 // change the color of text overlaying ANSI art to ensure 2630 // it is readable. don't do this in the web UI, since we 2631 // have direct control of the theme there (also, inner 2632 // scroll panes allow text to move without a re-render 2633 // by the TUI, which would break this). also don't do this 2634 // if the text is explicitly styled. 2635 if (is_terminal and dst.style.fg == null) { 2636 if (background_maybe) |background| { 2637 // use near-black and near-white, which is easier on the eyes 2638 // while retaining high contrast 2639 const luminance = (@as(u32, background.r) * 299 + 2640 @as(u32, background.g) * 587 + 2641 @as(u32, background.b) * 114) / 1000; 2642 dst.style.fg = if (luminance >= 128) 2643 .{ .r = 16, .g = 16, .b = 16 } 2644 else 2645 .{ .r = 240, .g = 240, .b = 240 }; 2646 } 2647 } 2648 } 2649 } 2650 } 2651 } 2652 return out; 2653 } 2654 }; 2655