╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2055 _ = key; 2056 _ = root_focus; 2057 } 2058 2059 pub fn clearGrid(self: *FlowBox) void { 2060 if (self.grid) |*grid| { 2061 grid.deinit(); 2062 self.grid = null; 2063 } 2064 for (self.text_boxes.items) |*tb| tb.clearGrid(); 2065 } 2066 2067 pub fn getGrid(self: FlowBox) ?Grid { 2068 return self.grid; 2069 } 2070 2071 pub fn getFocus(self: *FlowBox) *Focus { 2072 return self.focus; 2073 } 2074 2075 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 2076 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 2077 const slot_height = self.options.cell_height + border_rows; 2078 const col = index % self.last_cols; 2079 const row = index / self.last_cols; 2080 return .{ 2081 .x = @intCast(col * self.options.cell_width), 2082 .y = @intCast(row * slot_height), 2083 .size = .{ .width = self.options.cell_width, .height = slot_height }, 2084 }; 2085 } 2086 2087 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 2088 for (self.text_boxes.items, 0..) |tb, i| { 2089 if (tb.box.focus.id == focus_id) return i; 2090 } 2091 return null; 2092 } 2093 2094 pub const Scroll = struct { 2095 scroll: wgt.Scroll(Widget), 2096 2097 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 2098 var layout_inner = try FlowBox.init(allocator, options); 2099 errdefer layout_inner.deinit(allocator); 2100 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 2101 errdefer scroll.deinit(allocator); 2102 return .{ .scroll = scroll }; 2103 } 2104 2105 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 2106 self.scroll.deinit(allocator); 2107 } 2108 2109 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 2110 self.scroll.x = 0; 2111 self.scroll.y = 0; 2112 try self.inner().setItems(allocator, items); 2113 } 2114 2115 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2116 try self.scroll.build(allocator, constraint, root_focus); 2117 } 2118 2119 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2120 _ = allocator; 2121 const in = self.inner(); 2122 const child_id = in.focus.child_id orelse return; 2123 const current_index = in.indexOfFocusId(child_id) orelse return; 2124 const count = in.text_boxes.items.len; 2125 if (count == 0) return; 2126 const cols = in.last_cols; 2127 const slot_height = in.options.cell_height + FlowBox.border_rows; 2128 2129 var index = current_index; 2130 switch (key) { 2131 .arrow_up => index -|= cols, 2132 .arrow_down => if (index + cols < count) { 2133 index += cols; 2134 }, 2135 .arrow_left => index -|= 1, 2136 .arrow_right => if (index + 1 < count) { 2137 index += 1; 2138 }, 2139 .home => index = 0, 2140 .end => index = count - 1, 2141 .page_up => { 2142 if (self.scroll.grid) |grid| if (slot_height > 0) { 2143 const rows_per_page = grid.size.height / slot_height; 2144 index -|= rows_per_page * cols; 2145 }; 2146 }, 2147 .page_down => { 2148 if (self.scroll.grid) |grid| if (slot_height > 0) { 2149 const rows_per_page = grid.size.height / slot_height; 2150 index = @min(index + rows_per_page * cols, count - 1); 2151 }; 2152 }, 2153 // scroll wheel moves the focused cell by a full row so the 2154 // viewport (via scrollToRect) follows in row-sized steps, 2155 // matching how a scroll wheel feels in a grid view. 2156 .mouse => |mouse| switch (mouse.action) { 2157 .scroll => |dir| switch (dir) { 2158 .up => index -|= cols, 2159 .down => if (index + cols < count) { 2160 index += cols; 2161 }, 2162 }, 2163 else => {}, 2164 }, 2165 else => {}, 2166 } 2167 2168 if (index != current_index) { 2169 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 2170 if (in.cellRect(index)) |rect| { 2171 self.scroll.scrollToRect(rect); 2172 } 2173 } 2174 } 2175 2176 pub fn clearGrid(self: *Scroll) void { 2177 self.scroll.clearGrid(); 2178 } 2179 2180 pub fn getGrid(self: Scroll) ?Grid { 2181 return self.scroll.getGrid(); 2182 } 2183 2184 pub fn getFocus(self: *Scroll) *Focus { 2185 return self.scroll.getFocus(); 2186 } 2187 2188 pub fn getSelectedIndex(self: Scroll) ?usize { 2189 const in = self.scroll.child.flow_box; 2190 const child_id = in.focus.child_id orelse return null; 2191 return in.indexOfFocusId(child_id); 2192 } 2193 2194 fn inner(self: *Scroll) *FlowBox { 2195 return &self.scroll.child.flow_box; 2196 } 2197 }; 2198 }; 2199 2200 // a left-to-right flow of variable-width focusable items that wraps at the 2201 // available width, like words in a paragraph. unlike FlowBox there is no cell 2202 // grid: each item is as wide as its text. selection movement is driven by the 2203 // owning view via indexOfFocusId/verticalNeighbor. 2204 pub const TagFlow = struct { 2205 focus: *Focus, 2206 grid: ?Grid, 2207 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 2208 // last build's item rects (content space), for vertical navigation and 2209 // scroll-into-view. 2210 rects: std.ArrayList(layout.IRect), 2211 // backs the per-item strings the text boxes borrow. 2212 arena: std.heap.ArenaAllocator, 2213 2214 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 2215 2216 pub fn init(allocator: std.mem.Allocator) !TagFlow { 2217 return .{ 2218 .focus = try Focus.create(allocator, .container), 2219 .grid = null, 2220 .text_boxes = .empty, 2221 .rects = .empty, 2222 .arena = std.heap.ArenaAllocator.init(allocator), 2223 }; 2224 } 2225 2226 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 2227 self.focus.destroy(allocator); 2228 if (self.grid) |*grid| { 2229 grid.deinit(); 2230 self.grid = null; 2231 } 2232 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 2233 self.text_boxes.deinit(allocator); 2234 self.rects.deinit(allocator); 2235 self.arena.deinit(); 2236 } 2237 2238 // both the item text and the link are copied into the arena, so the caller's 2239 // slices needn't outlive this call. 2240 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 2241 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 2242 self.text_boxes.clearAndFree(allocator); 2243 2244 _ = self.arena.reset(.retain_capacity); 2245 const aa = self.arena.allocator(); 2246 2247 self.focus.clear(); 2248 self.focus.child_id = null; 2249 2250 for (items) |item| { 2251 const line = try aa.dupe(u8, item.text); 2252 2253 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2254 errdefer text_box.deinit(allocator); 2255 text_box.getFocus().focusable = true; 2256 2257 if (item.link.len > 0) { 2258 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 2259 } 2260 2261 try self.text_boxes.append(allocator, text_box); 2262 } 2263 2264 if (self.text_boxes.items.len > 0) { 2265 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 2266 } 2267 } 2268 2269 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2270 self.clearGrid(); 2271 self.focus.clear(); 2272 2273 if (self.text_boxes.items.len == 0) return; 2274 const max_width = constraint.max_size.width orelse 80; 2275 if (max_width == 0) return; 2276 2277 // lay the items out left to right, wrapping to a new row when the next 2278 // one wouldn't fit. 2279 self.rects.clearRetainingCapacity(); 2280 var x: usize = 0; 2281 var y: usize = 0; 2282 var row_height: usize = 0; 2283 var total_width: usize = 0; 2284 for (self.text_boxes.items) |*tb| { 2285 try tb.build(allocator, .{ 2286 .min_size = .{ .width = null, .height = null }, 2287 .max_size = .{ .width = max_width, .height = null }, 2288 }, root_focus); 2289 const tb_grid_maybe = tb.getGrid(); 2290 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 2291 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 2292 if (x > 0 and x + w > max_width) { 2293 x = 0; 2294 y += row_height; 2295 row_height = 0; 2296 } 2297 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 2298 if (h > row_height) row_height = h; 2299 x += w; 2300 if (x > total_width) total_width = x; 2301 } 2302 2303 const total_height = y + row_height; 2304 if (total_width == 0 or total_height == 0) return; 2305 2306 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 2307 errdefer grid.deinit(); 2308 2309 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 2310 const tb_grid = tb.getGrid() orelse continue; 2311 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 2312 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 2313 } 2314 2315 self.grid = grid; 2316 } 2317 2318 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2319 _ = self; 2320 _ = allocator; 2321 _ = key; 2322 _ = root_focus; 2323 } 2324 2325 pub fn clearGrid(self: *TagFlow) void { 2326 if (self.grid) |*grid| { 2327 grid.deinit(); 2328 self.grid = null; 2329 } 2330 for (self.text_boxes.items) |*tb| tb.clearGrid(); 2331 } 2332 2333 pub fn getGrid(self: TagFlow) ?Grid { 2334 return self.grid; 2335 } 2336 2337 pub fn getFocus(self: *TagFlow) *Focus { 2338 return self.focus; 2339 } 2340 2341 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 2342 for (self.text_boxes.items, 0..) |tb, i| { 2343 if (tb.box.focus.id == focus_id) return i; 2344 } 2345 return null; 2346 } 2347 2348 // the first item of the row below (when `downward`) or above, or null at 2349 // the flow's edge. items are laid out in order, so y is nondecreasing. 2350 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 2351 const rects = self.rects.items; 2352 if (index >= rects.len) return null; 2353 const cur_y = rects[index].y; 2354 if (downward) { 2355 for (rects[index + 1 ..], index + 1..) |r, i| { 2356 if (r.y > cur_y) return i; 2357 } 2358 return null; 2359 } 2360 // walk the earlier rows; the last one visited is the row above. 2361 var prev_first: ?usize = null; 2362 var i: usize = 0; 2363 while (i < rects.len and rects[i].y < cur_y) { 2364 prev_first = i; 2365 const y = rects[i].y; 2366 while (i < rects.len and rects[i].y == y) i += 1; 2367 } 2368 return prev_first; 2369 } 2370 }; 2371 2372 // an invisible widget that fills the horizontal space granted by its parent. 2373 // used inside Box(horiz) with a min_size so the box reserves space for the 2374 // children that follow, pushing them to the right. 2375 pub const Spacer = struct { 2376 focus: *Focus, 2377 grid: ?Grid, 2378 2379 pub fn init(allocator: std.mem.Allocator) !Spacer { 2380 return .{ 2381 .focus = try Focus.create(allocator, .container), 2382 .grid = null, 2383 }; 2384 } 2385 2386 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 2387 self.focus.destroy(allocator); 2388 if (self.grid) |*grid| { 2389 grid.deinit(); 2390 self.grid = null; 2391 } 2392 } 2393 2394 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2395 _ = root_focus; 2396 self.clearGrid(); 2397 const width = constraint.max_size.width orelse return; 2398 if (width == 0) return; 2399 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2400 } 2401 2402 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2403 _ = self; 2404 _ = allocator; 2405 _ = key; 2406 _ = root_focus; 2407 } 2408 2409 pub fn clearGrid(self: *Spacer) void { 2410 if (self.grid) |*grid| { 2411 grid.deinit(); 2412 self.grid = null; 2413 } 2414 } 2415 2416 pub fn getGrid(self: Spacer) ?Grid { 2417 return self.grid; 2418 } 2419 2420 pub fn getFocus(self: *Spacer) *Focus { 2421 return self.focus; 2422 } 2423 }; 2424 2425 // a one-row, non-focusable status bar showing the current page's url 2426 pub const Footer = struct { 2427 focus: *Focus, 2428 grid: ?Grid, 2429 session: *Session, 2430 arena: std.heap.ArenaAllocator, 2431 url: []const u8 = "", 2432 url_width: usize = 0, 2433 2434 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 2435 return .{ 2436 .focus = try Focus.create(allocator, .container), 2437 .grid = null, 2438 .session = session, 2439 .arena = std.heap.ArenaAllocator.init(allocator), 2440 }; 2441 } 2442 2443 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2444 self.focus.destroy(allocator); 2445 if (self.grid) |*grid| grid.deinit(); 2446 self.arena.deinit(); 2447 } 2448 2449 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2450 _ = root_focus; 2451 self.clearGrid(); 2452 const width = constraint.max_size.width orelse return; 2453 if (width == 0) return; 2454 2455 _ = self.arena.reset(.retain_capacity); 2456 self.url = ""; 2457 self.url_width = 0; 2458 const aa = self.arena.allocator(); 2459 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2460 const text = terminalWebUrl(aa, self.session, path) catch return; 2461 self.url = text; 2462 2463 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2464 errdefer grid.deinit(); 2465 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2466 var i: usize = 0; 2467 while (utf8.nextCodepointSlice()) |ch| { 2468 if (i == width) break; 2469 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2470 i += 1; 2471 } 2472 self.url_width = i; 2473 self.grid = grid; 2474 } 2475 2476 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2477 _ = allocator; 2478 switch (key) { 2479 .mouse => |mouse| { 2480 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 2481 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 2482 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 2483 }, 2484 else => {}, 2485 } 2486 } 2487 2488 pub fn clearGrid(self: *Footer) void { 2489 if (self.grid) |*grid| { 2490 grid.deinit(); 2491 self.grid = null; 2492 } 2493 } 2494 2495 pub fn getGrid(self: Footer) ?Grid { 2496 return self.grid; 2497 } 2498 2499 pub fn getFocus(self: *Footer) *Focus { 2500 return self.focus; 2501 } 2502 }; 2503 2504 // a single-child wrapper that builds the child at its natural size and 2505 // positions its grid in the middle of the area granted by the parent 2506 // a bordered label, indented past the content it precedes 2507 pub const SectionLabel = struct { 2508 box: wgt.Box(Widget), 2509 2510 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 2511 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2512 errdefer box.deinit(allocator); 2513 2514 { 2515 var gap = try wgt.Text(Widget).init(allocator, " "); 2516 errdefer gap.deinit(allocator); 2517 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2518 } 2519 2520 { 2521 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2522 errdefer tb.deinit(allocator); 2523 tb.getFocus().focusable = true; 2524 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2525 } 2526 2527 box.getFocus().child_id = box.children.keys()[1]; 2528 return .{ .box = box }; 2529 } 2530 2531 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2532 self.box.deinit(allocator); 2533 } 2534 2535 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2536 try self.box.build(allocator, constraint, root_focus); 2537 } 2538 2539 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2540 _ = self; 2541 _ = allocator; 2542 _ = key; 2543 _ = root_focus; 2544 } 2545 2546 pub fn clearGrid(self: *SectionLabel) void { 2547 self.box.clearGrid(); 2548 } 2549 2550 pub fn getGrid(self: SectionLabel) ?Grid { 2551 return self.box.getGrid(); 2552 } 2553 2554 pub fn getFocus(self: *SectionLabel) *Focus { 2555 return self.box.getFocus(); 2556 } 2557 }; 2558 2559 // a submit button, indented past its form's content. the web overlay POSTs 2560 // the button's form; terminal hosts detect presses through `buttonId`. 2561 pub const SubmitButton = struct { 2562 box: wgt.Box(Widget), 2563 2564 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2565 return initLabeled(allocator, "submit"); 2566 } 2567 2568 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 2569 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2570 errdefer box.deinit(allocator); 2571 2572 { 2573 var gap = try wgt.Text(Widget).init(allocator, " "); 2574 errdefer gap.deinit(allocator); 2575 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2576 } 2577 2578 { 2579 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2580 errdefer button.deinit(allocator); 2581 button.getFocus().focusable = true; 2582 // the renderer distinguishes plain clickables from buttons that 2583 // should POST to a server route by this kind. 2584 button.getFocus().kind = .{ .custom = "submit" }; 2585 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2586 } 2587 2588 box.getFocus().child_id = box.children.keys()[1]; 2589 return .{ .box = box }; 2590 } 2591 2592 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2593 self.box.deinit(allocator); 2594 } 2595 2596 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2597 try self.box.build(allocator, constraint, root_focus); 2598 } 2599 2600 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2601 _ = self; 2602 _ = allocator; 2603 _ = key; 2604 _ = root_focus; 2605 } 2606 2607 pub fn clearGrid(self: *SubmitButton) void { 2608 self.box.clearGrid(); 2609 } 2610 2611 pub fn getGrid(self: SubmitButton) ?Grid { 2612 return self.box.getGrid(); 2613 } 2614 2615 pub fn getFocus(self: *SubmitButton) *Focus { 2616 return self.box.getFocus(); 2617 } 2618 2619 // the button's focus id, for click hit-testing 2620 pub fn buttonId(self: *const SubmitButton) usize { 2621 return self.box.children.keys()[1]; 2622 } 2623 }; 2624 2625 pub const Center = struct { 2626 focus: *Focus, 2627 grid: ?Grid, 2628 child: *Widget, 2629 2630 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2631 const child = try allocator.create(Widget); 2632 errdefer allocator.destroy(child); 2633 child.* = child_widget; 2634 return .{ 2635 .focus = try Focus.create(allocator, .container), 2636 .grid = null, 2637 .child = child, 2638 }; 2639 } 2640 2641 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2642 self.focus.destroy(allocator); 2643 if (self.grid) |*grid| { 2644 grid.deinit(); 2645 self.grid = null; 2646 } 2647 self.child.deinit(allocator); 2648 allocator.destroy(self.child); 2649 } 2650 2651 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2652 self.clearGrid(); 2653 self.getFocus().clear(); 2654 2655 // build the child without forcing it to fill min_size; it sizes 2656 // itself to its content within the available max. 2657 try self.child.build(allocator, .{ 2658 .min_size = .{ .width = null, .height = null }, 2659 .max_size = constraint.max_size, 2660 }, root_focus); 2661 2662 const child_grid = self.child.getGrid() orelse return; 2663 2664 // prefer max when given; otherwise grow to max(min, child) so that 2665 // when only a min is set (e.g. the wasm path passes viewport rows 2666 // as min_height with no max), we can still vertically center while 2667 // letting taller content extend past the min. 2668 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; 2669 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; 2670 if (width == 0 or height == 0) return; 2671 2672 const offset_x: usize = (width -| child_grid.size.width) / 2; 2673 const offset_y: usize = (height -| child_grid.size.height) / 2; 2674 2675 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2676 errdefer grid.deinit(); 2677 try grid.drawGrid(child_grid, offset_x, offset_y); 2678 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2679 self.getFocus().child_id = self.child.getFocus().id; 2680 2681 self.grid = grid; 2682 } 2683 2684 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2685 try self.child.input(allocator, key, root_focus); 2686 } 2687 2688 pub fn clearGrid(self: *Center) void { 2689 if (self.grid) |*grid| { 2690 grid.deinit(); 2691 self.grid = null; 2692 } 2693 self.child.clearGrid(); 2694 } 2695 2696 pub fn getGrid(self: Center) ?Grid { 2697 return self.grid; 2698 } 2699 2700 pub fn getFocus(self: *Center) *Focus { 2701 return self.focus; 2702 } 2703 }; 2704 2705 // renders truecolor ANSI art 2706 pub const AnsiArt = struct { 2707 focus: *Focus, 2708 grid: ?Grid, 2709 content: []const u8, 2710 2711 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2712 return .{ 2713 .focus = try Focus.create(allocator, .container), 2714 .grid = null, 2715 .content = content, 2716 }; 2717 } 2718 2719 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2720 self.focus.destroy(allocator); 2721 if (self.grid) |*grid| { 2722 grid.deinit(); 2723 self.grid = null; 2724 } 2725 } 2726 2727 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2728 _ = root_focus; 2729 self.clearGrid(); 2730 2731 // parse into rows of cells, then copy into a rectangular grid 2732 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2733 defer { 2734 for (rows.items) |*row| row.deinit(allocator); 2735 rows.deinit(allocator); 2736 } 2737 var row: std.ArrayList(Grid.Cell) = .empty; 2738 errdefer row.deinit(allocator); 2739 2740 var style: Grid.Style = .{}; 2741 var width: usize = 0; 2742 const content = self.content; 2743 var i: usize = 0; 2744 while (i < content.len) { 2745 const byte = content[i]; 2746 if (byte == '\n') { 2747 width = @max(width, row.items.len); 2748 try rows.append(allocator, row); 2749 row = .empty; 2750 i += 1; 2751 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2752 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2753 var j = i + 2; 2754 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2755 if (j >= content.len) { 2756 i = content.len; // malformed trailing escape; stop 2757 } else { 2758 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2759 i = j + 1; 2760 } 2761 } else { 2762 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2763 const end = @min(content.len, i + len); 2764 const rune = content[i..end]; 2765 const transparent = end - i == 1 and rune[0] == ' ' and 2766 style.fg == null and style.bg == null and !style.inverted; 2767 try row.append(allocator, .{ 2768 .rune = if (transparent) null else rune, 2769 .style = style, 2770 }); 2771 i = end; 2772 } 2773 } 2774 // a trailing row with content but no closing newline 2775 if (row.items.len > 0) { 2776 width = @max(width, row.items.len); 2777 try rows.append(allocator, row); 2778 } else { 2779 row.deinit(allocator); 2780 } 2781 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2782 2783 const height = rows.items.len; 2784 if (width == 0 or height == 0) return; 2785 2786 const clamped_w = @min(width, constraint.max_size.width orelse width); 2787 const clamped_h = @min(height, constraint.max_size.height orelse height); 2788 2789 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2790 errdefer grid.deinit(); 2791 for (rows.items[0..clamped_h], 0..) |r, y| { 2792 const n = @min(r.items.len, clamped_w); 2793 for (r.items[0..n], 0..) |cell, x| { 2794 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2795 } 2796 } 2797 self.grid = grid; 2798 } 2799 2800 pub fn clearGrid(self: *AnsiArt) void { 2801 if (self.grid) |*grid| { 2802 grid.deinit(); 2803 self.grid = null; 2804 } 2805 } 2806 2807 pub fn getGrid(self: AnsiArt) ?Grid { 2808 return self.grid; 2809 } 2810 2811 pub fn getFocus(self: *AnsiArt) *Focus { 2812 return self.focus; 2813 } 2814 2815 fn applySgr(style: *Grid.Style, params: []const u8) void { 2816 var nums: [16]u32 = undefined; 2817 var n: usize = 0; 2818 var it = std.mem.splitScalar(u8, params, ';'); 2819 while (it.next()) |tok| { 2820 if (n >= nums.len) break; 2821 // an empty parameter (e.g. bare "\x1b[m") means 0 2822 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2823 n += 1; 2824 } 2825 if (n == 0) { 2826 style.* = .{}; 2827 return; 2828 } 2829 var i: usize = 0; 2830 while (i < n) : (i += 1) { 2831 switch (nums[i]) { 2832 0 => style.* = .{}, 2833 7 => style.inverted = true, 2834 27 => style.inverted = false, 2835 39 => style.fg = null, 2836 49 => style.bg = null, 2837 38, 48 => { 2838 // truecolor form: 38;2;r;g;b — anything else is ignored 2839 if (i + 4 < n and nums[i + 1] == 2) { 2840 const c = Grid.Color{ 2841 .r = @truncate(nums[i + 2]), 2842 .g = @truncate(nums[i + 3]), 2843 .b = @truncate(nums[i + 4]), 2844 }; 2845 if (nums[i] == 38) style.fg = c else style.bg = c; 2846 i += 4; 2847 } 2848 }, 2849 else => {}, 2850 } 2851 } 2852 } 2853 }; 2854 2855 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2856 pub const AnsiBackground = struct { 2857 grid: ?Grid, 2858 child: *Widget, 2859 art: AnsiArt, 2860 session: *Session, 2861 2862 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2863 var cw = child_widget; 2864 const child = allocator.create(Widget) catch |e| { 2865 cw.deinit(allocator); 2866 return e; 2867 }; 2868 child.* = cw; 2869 errdefer { 2870 child.deinit(allocator); 2871 allocator.destroy(child); 2872 } 2873 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2874 } 2875 2876 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2877 if (self.grid) |*grid| grid.deinit(); 2878 self.art.deinit(allocator); 2879 self.child.deinit(allocator); 2880 allocator.destroy(self.child); 2881 } 2882 2883 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2884 self.clearGrid(); 2885 2886 // make the wrapped page fill the whole available area 2887 try self.child.build(allocator, .{ 2888 .min_size = .{ 2889 .width = constraint.max_size.width orelse constraint.min_size.width, 2890 .height = constraint.max_size.height orelse constraint.min_size.height, 2891 }, 2892 .max_size = constraint.max_size, 2893 }, root_focus); 2894 2895 if (self.session.data.enable_ansi) { 2896 if (self.child.getGrid()) |fg| { 2897 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2898 } 2899 } 2900 } 2901 2902 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2903 try self.child.input(allocator, key, root_focus); 2904 } 2905 2906 pub fn clearGrid(self: *AnsiBackground) void { 2907 if (self.grid) |*grid| { 2908 grid.deinit(); 2909 self.grid = null; 2910 } 2911 self.child.clearGrid(); 2912 self.art.clearGrid(); 2913 } 2914 2915 pub fn getGrid(self: AnsiBackground) ?Grid { 2916 return self.grid orelse self.child.getGrid(); 2917 } 2918 2919 pub fn getFocus(self: *AnsiBackground) *Focus { 2920 return self.child.getFocus(); 2921 } 2922 2923 // which top corner the art hugs. 2924 const ArtAnchor = enum { top_left, top_right }; 2925 2926 // a foreground cell counts as blank if it's empty or a bare space without styling 2927 fn cellIsBlank(cell: Grid.Cell) bool { 2928 if (cell.rune) |rune| { 2929 return std.mem.eql(u8, rune, " ") and 2930 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2931 } 2932 return true; 2933 } 2934 2935 // the art is dimmed to this fraction of its brightness so foreground text 2936 // stays legible over it 2937 const art_brightness = 45; // percent 2938 2939 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2940 const c = color orelse return null; 2941 return .{ 2942 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2943 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2944 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2945 }; 2946 } 2947 2948 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2949 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2950 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2951 // snap such channels clear of that range so the backdrop doesn't blink or 2952 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2953 // actually render truecolor. 2954 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2955 const c = color orelse return null; 2956 const snap = struct { 2957 fn f(v: u8) u8 { 2958 return if (v >= 1 and v <= 9) 10 else v; 2959 } 2960 }.f; 2961 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2962 } 2963 2964 // composites ANSI art behind a foreground grid 2965 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2966 art.clearGrid(); 2967 try art.build(allocator, .{ 2968 .min_size = .{ .width = null, .height = null }, 2969 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2970 }, root_focus); 2971 2972 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2973 errdefer out.deinit(); 2974 2975 if (art.getGrid()) |art_grid| { 2976 const anchor_x = switch (anchor) { 2977 .top_left => 0, 2978 .top_right => foreground.size.width -| art_grid.size.width, 2979 }; 2980 for (0..art_grid.size.height) |y| { 2981 for (0..art_grid.size.width) |x| { 2982 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2983 if (src.rune == null) continue; 2984 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2985 const dst = &out.cells.items[idx]; 2986 // composite the dimmed art behind every cell so it shows 2987 // through the whole UI: a blank cell becomes the art, while a 2988 // cell with a glyph keeps the glyph but takes the dimmed art as 2989 // its background (replacing whatever background it had, which 2990 // would otherwise obscure the art). 2991 if (cellIsBlank(dst.*)) { 2992 dst.* = src; 2993 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2994 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2995 } else { 2996 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2997 dst.style.bg = background_maybe; 2998 // change the color of text overlaying ANSI art to ensure 2999 // it is readable. don't do this in the web UI, since we 3000 // have direct control of the theme there (also, inner 3001 // scroll panes allow text to move without a re-render 3002 // by the TUI, which would break this). also don't do this 3003 // if the text is explicitly styled. 3004 if (is_terminal and dst.style.fg == null) { 3005 if (background_maybe) |background| { 3006 // use near-black and near-white, which is easier on the eyes 3007 // while retaining high contrast 3008 const luminance = (@as(u32, background.r) * 299 + 3009 @as(u32, background.g) * 587 + 3010 @as(u32, background.b) * 114) / 1000; 3011 dst.style.fg = if (luminance >= 128) 3012 .{ .r = 16, .g = 16, .b = 16 } 3013 else 3014 .{ .r = 240, .g = 240, .b = 240 }; 3015 } 3016 } 3017 } 3018 } 3019 } 3020 } 3021 return out; 3022 } 3023 }; 3024