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