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