╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
184 }; 185 186 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 187 return .{ 188 .focus = try Focus.create(allocator, .container), 189 .grid = null, 190 .text_boxes = .empty, 191 .arena = std.heap.ArenaAllocator.init(allocator), 192 .last_cols = 1, 193 .options = options, 194 }; 195 } 196 197 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 198 self.focus.destroy(allocator); 199 if (self.grid) |*grid| { 200 grid.deinit(); 201 self.grid = null; 202 } 203 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 204 self.text_boxes.deinit(allocator); 205 self.arena.deinit(); 206 } 207 208 // one flow item: its display text plus an optional link. a non-empty `link` 209 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 210 // the web renderer turns into an anchor. 211 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 212 213 // the text box copies the item text; links are copied into the arena, so the 214 // caller's slices needn't outlive this call. 215 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 216 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 217 self.text_boxes.clearAndFree(allocator); 218 219 // the old text boxes and their borrowed links are gone now, so the link 220 // arena can be reclaimed. 221 _ = self.arena.reset(.retain_capacity); 222 const aa = self.arena.allocator(); 223 224 self.focus.clear(); 225 self.focus.child_id = null; 226 227 for (items) |item| { 228 var text_box = try wgt.TextBox.init(allocator, item.text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 229 errdefer text_box.deinit(allocator); 230 text_box.getFocus().mode = .all; 231 232 if (item.link.len > 0) { 233 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 234 } 235 236 try self.text_boxes.append(allocator, text_box); 237 } 238 239 if (self.text_boxes.items.len > 0) { 240 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 241 } 242 } 243 244 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 245 self.clearGrid(); 246 self.focus.clear(); 247 248 const cell_width = self.options.cell_width; 249 const max_width = constraint.max_size.width orelse cell_width; 250 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 251 self.last_cols = cols; 252 253 const count = self.text_boxes.items.len; 254 if (count == 0) return; 255 const slot_height = self.options.cell_height + border_rows; 256 if (slot_height == 0) return; 257 258 // build at the slot size so every text box fits a single grid cell 259 for (self.text_boxes.items) |*tb| { 260 tb.options.border_style = if (root_focus.grandchild_id == tb.getFocus().id) .double else .hidden; 261 try tb.build(allocator, .{ 262 .min_size = .{ .width = cell_width, .height = null }, 263 .max_size = .{ .width = cell_width, .height = slot_height }, 264 }, root_focus); 265 } 266 267 const rows = (count + cols - 1) / cols; 268 const content_height = rows * slot_height; 269 const total_width = cols * cell_width; 270 if (total_width == 0 or content_height == 0) return; 271 272 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 273 errdefer grid.deinit(); 274 275 for (self.text_boxes.items, 0..) |*tb, i| { 276 const tb_grid = tb.getGrid() orelse continue; 277 const col = i % cols; 278 const row = i / cols; 279 const cell_x = col * cell_width; 280 const cell_y = row * slot_height; 281 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 282 try grid.drawGrid(tb_grid, cell_x, cell_y); 283 } 284 285 self.grid = grid; 286 } 287 288 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 289 _ = self; 290 _ = allocator; 291 _ = key; 292 _ = root_focus; 293 } 294 295 pub fn clearGrid(self: *FlowBox) void { 296 if (self.grid) |*grid| { 297 grid.deinit(); 298 self.grid = null; 299 } 300 for (self.text_boxes.items) |*tb| tb.clearGrid(); 301 } 302 303 pub fn getGrid(self: FlowBox) ?Grid { 304 return self.grid; 305 } 306 307 pub fn getFocus(self: *FlowBox) *Focus { 308 return self.focus; 309 } 310 311 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 312 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 313 const slot_height = self.options.cell_height + border_rows; 314 const col = index % self.last_cols; 315 const row = index / self.last_cols; 316 return .{ 317 .x = @intCast(col * self.options.cell_width), 318 .y = @intCast(row * slot_height), 319 .size = .{ .width = self.options.cell_width, .height = slot_height }, 320 }; 321 } 322 323 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 324 for (self.text_boxes.items, 0..) |tb, i| { 325 if (tb.focus.id == focus_id) return i; 326 } 327 return null; 328 } 329 330 pub const Scroll = struct { 331 scroll: wgt.Scroll(Widget), 332 333 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 334 var layout_inner = try FlowBox.init(allocator, options); 335 errdefer layout_inner.deinit(allocator); 336 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 337 errdefer scroll.deinit(allocator); 338 return .{ .scroll = scroll }; 339 } 340 341 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 342 self.scroll.deinit(allocator); 343 } 344 345 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 346 self.scroll.x = 0; 347 self.scroll.y = 0; 348 try self.inner().setItems(allocator, items); 349 } 350 351 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 352 try self.scroll.build(allocator, constraint, root_focus); 353 } 354 355 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 356 _ = allocator; 357 const in = self.inner(); 358 const child_id = in.focus.child_id orelse return; 359 const current_index = in.indexOfFocusId(child_id) orelse return; 360 const count = in.text_boxes.items.len; 361 if (count == 0) return; 362 const cols = in.last_cols; 363 const slot_height = in.options.cell_height + FlowBox.border_rows; 364 365 var index = current_index; 366 switch (key) { 367 .arrow_up => index -|= cols, 368 .arrow_down => if (index + cols < count) { 369 index += cols; 370 }, 371 .arrow_left => index -|= 1, 372 .arrow_right => if (index + 1 < count) { 373 index += 1; 374 }, 375 .home => index = 0, 376 .end => index = count - 1, 377 .page_up => { 378 if (self.scroll.grid) |grid| if (slot_height > 0) { 379 const rows_per_page = grid.size.height / slot_height; 380 index -|= rows_per_page * cols; 381 }; 382 }, 383 .page_down => { 384 if (self.scroll.grid) |grid| if (slot_height > 0) { 385 const rows_per_page = grid.size.height / slot_height; 386 index = @min(index + rows_per_page * cols, count - 1); 387 }; 388 }, 389 // scroll wheel moves the focused cell by a full row so the 390 // viewport (via scrollToRect) follows in row-sized steps, 391 // matching how a scroll wheel feels in a grid view. 392 .mouse => |mouse| switch (mouse.action) { 393 .scroll => |dir| switch (dir) { 394 .up => index -|= cols, 395 .down => if (index + cols < count) { 396 index += cols; 397 }, 398 }, 399 else => {}, 400 }, 401 else => {}, 402 } 403 404 if (index != current_index) { 405 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 406 if (in.cellRect(index)) |rect| { 407 self.scroll.scrollToRect(rect); 408 } 409 } 410 } 411 412 pub fn clearGrid(self: *Scroll) void { 413 self.scroll.clearGrid(); 414 } 415 416 pub fn getGrid(self: Scroll) ?Grid { 417 return self.scroll.getGrid(); 418 } 419 420 pub fn getFocus(self: *Scroll) *Focus { 421 return self.scroll.getFocus(); 422 } 423 424 pub fn atTop(self: Scroll) bool { 425 const in = self.scroll.child.flow_box; 426 const child_id = in.focus.child_id orelse return false; 427 return in.indexOfFocusId(child_id) == 0; 428 } 429 430 fn inner(self: *Scroll) *FlowBox { 431 return &self.scroll.child.flow_box; 432 } 433 }; 434 }; 435 436 // a left-to-right flow of variable-width focusable items that wraps at the 437 // available width, like words in a paragraph. unlike FlowBox there is no cell 438 // grid: each item is as wide as its text. selection movement is driven by the 439 // owning view via indexOfFocusId/verticalNeighbor. 440 pub const TagFlow = struct { 441 focus: *Focus, 442 grid: ?Grid, 443 text_boxes: std.ArrayList(wgt.TextBox), 444 // last build's item rects (content space), for vertical navigation and 445 // scroll-into-view. 446 rects: std.ArrayList(layout.IRect), 447 // backs each link item's `.custom` focus kind. 448 arena: std.heap.ArenaAllocator, 449 450 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 451 452 pub fn init(allocator: std.mem.Allocator) !TagFlow { 453 return .{ 454 .focus = try Focus.create(allocator, .container), 455 .grid = null, 456 .text_boxes = .empty, 457 .rects = .empty, 458 .arena = std.heap.ArenaAllocator.init(allocator), 459 }; 460 } 461 462 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 463 self.focus.destroy(allocator); 464 if (self.grid) |*grid| { 465 grid.deinit(); 466 self.grid = null; 467 } 468 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 469 self.text_boxes.deinit(allocator); 470 self.rects.deinit(allocator); 471 self.arena.deinit(); 472 } 473 474 // the text box copies the item text; links are copied into the arena, so the 475 // caller's slices needn't outlive this call. 476 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 477 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 478 self.text_boxes.clearAndFree(allocator); 479 480 _ = self.arena.reset(.retain_capacity); 481 const aa = self.arena.allocator(); 482 483 self.focus.clear(); 484 self.focus.child_id = null; 485 486 for (items) |item| { 487 var text_box = try wgt.TextBox.init(allocator, item.text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 488 errdefer text_box.deinit(allocator); 489 text_box.getFocus().mode = .all; 490 491 if (item.link.len > 0) { 492 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 493 } 494 495 try self.text_boxes.append(allocator, text_box); 496 } 497 498 if (self.text_boxes.items.len > 0) { 499 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 500 } 501 } 502 503 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 504 self.clearGrid(); 505 self.focus.clear(); 506 507 if (self.text_boxes.items.len == 0) return; 508 const max_width = constraint.max_size.width orelse 80; 509 if (max_width == 0) return; 510 511 // lay the items out left to right, wrapping to a new row when the next 512 // one wouldn't fit. 513 self.rects.clearRetainingCapacity(); 514 var x: usize = 0; 515 var y: usize = 0; 516 var row_height: usize = 0; 517 var total_width: usize = 0; 518 for (self.text_boxes.items) |*tb| { 519 try tb.build(allocator, .{ 520 .min_size = .{ .width = null, .height = null }, 521 .max_size = .{ .width = max_width, .height = null }, 522 }, root_focus); 523 const tb_grid_maybe = tb.getGrid(); 524 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 525 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 526 if (x > 0 and x + w > max_width) { 527 x = 0; 528 y += row_height; 529 row_height = 0; 530 } 531 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 532 if (h > row_height) row_height = h; 533 x += w; 534 if (x > total_width) total_width = x; 535 } 536 537 const total_height = y + row_height; 538 if (total_width == 0 or total_height == 0) return; 539 540 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 541 errdefer grid.deinit(); 542 543 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 544 const tb_grid = tb.getGrid() orelse continue; 545 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 546 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 547 } 548 549 self.grid = grid; 550 } 551 552 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 553 _ = self; 554 _ = allocator; 555 _ = key; 556 _ = root_focus; 557 } 558 559 pub fn clearGrid(self: *TagFlow) void { 560 if (self.grid) |*grid| { 561 grid.deinit(); 562 self.grid = null; 563 } 564 for (self.text_boxes.items) |*tb| tb.clearGrid(); 565 } 566 567 pub fn getGrid(self: TagFlow) ?Grid { 568 return self.grid; 569 } 570 571 pub fn getFocus(self: *TagFlow) *Focus { 572 return self.focus; 573 } 574 575 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 576 for (self.text_boxes.items, 0..) |tb, i| { 577 if (tb.focus.id == focus_id) return i; 578 } 579 return null; 580 } 581 582 // the first item of the row below (when `downward`) or above, or null at 583 // the flow's edge. items are laid out in order, so y is nondecreasing. 584 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 585 const rects = self.rects.items; 586 if (index >= rects.len) return null; 587 const cur_y = rects[index].y; 588 if (downward) { 589 for (rects[index + 1 ..], index + 1..) |r, i| { 590 if (r.y > cur_y) return i; 591 } 592 return null; 593 } 594 // walk the earlier rows; the last one visited is the row above. 595 var prev_first: ?usize = null; 596 var i: usize = 0; 597 while (i < rects.len and rects[i].y < cur_y) { 598 prev_first = i; 599 const y = rects[i].y; 600 while (i < rects.len and rects[i].y == y) i += 1; 601 } 602 return prev_first; 603 } 604 }; 605 606 // an invisible widget that fills the horizontal space granted by its parent. 607 // used inside Box(horiz) with a min_size so the box reserves space for the 608 // children that follow, pushing them to the right. 609 pub const Spacer = struct { 610 focus: *Focus, 611 grid: ?Grid, 612 613 pub fn init(allocator: std.mem.Allocator) !Spacer { 614 return .{ 615 .focus = try Focus.create(allocator, .container), 616 .grid = null, 617 }; 618 } 619 620 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 621 self.focus.destroy(allocator); 622 if (self.grid) |*grid| { 623 grid.deinit(); 624 self.grid = null; 625 } 626 } 627 628 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 629 _ = root_focus; 630 self.clearGrid(); 631 const width = constraint.max_size.width orelse return; 632 if (width == 0) return; 633 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 634 } 635 636 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 637 _ = self; 638 _ = allocator; 639 _ = key; 640 _ = root_focus; 641 } 642 643 pub fn clearGrid(self: *Spacer) void { 644 if (self.grid) |*grid| { 645 grid.deinit(); 646 self.grid = null; 647 } 648 } 649 650 pub fn getGrid(self: Spacer) ?Grid { 651 return self.grid; 652 } 653 654 pub fn getFocus(self: *Spacer) *Focus { 655 return self.focus; 656 } 657 }; 658 659 // a one-row, non-focusable status bar showing the current page's url 660 pub const Footer = struct { 661 focus: *Focus, 662 grid: ?Grid, 663 session: *ui.Session, 664 arena: std.heap.ArenaAllocator, 665 url: []const u8 = "", 666 url_width: usize = 0, 667 668 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 669 return .{ 670 .focus = try Focus.create(allocator, .container), 671 .grid = null, 672 .session = session, 673 .arena = std.heap.ArenaAllocator.init(allocator), 674 }; 675 } 676 677 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 678 self.focus.destroy(allocator); 679 if (self.grid) |*grid| grid.deinit(); 680 self.arena.deinit(); 681 } 682 683 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 684 _ = root_focus; 685 self.clearGrid(); 686 const width = constraint.max_size.width orelse return; 687 if (width == 0) return; 688 689 _ = self.arena.reset(.retain_capacity); 690 self.url = ""; 691 self.url_width = 0; 692 const aa = self.arena.allocator(); 693 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 694 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 695 self.url = text; 696 697 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 698 errdefer grid.deinit(); 699 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 700 var i: usize = 0; 701 while (utf8.nextCodepoint()) |ch| { 702 const rune_width = xitui.width.cellWidth(ch); 703 if (i + rune_width > width) break; 704 try grid.setRune(i, 0, ch); 705 i += rune_width; 706 } 707 self.url_width = i; 708 self.grid = grid; 709 } 710 711 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 712 _ = allocator; 713 switch (key) { 714 .mouse => |mouse| { 715 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 716 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 717 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 718 }, 719 else => {}, 720 } 721 } 722 723 pub fn clearGrid(self: *Footer) void { 724 if (self.grid) |*grid| { 725 grid.deinit(); 726 self.grid = null; 727 } 728 } 729 730 pub fn getGrid(self: Footer) ?Grid { 731 return self.grid; 732 } 733 734 pub fn getFocus(self: *Footer) *Focus { 735 return self.focus; 736 } 737 }; 738 739 // a single-child wrapper that builds the child at its natural size and 740 // positions its grid in the middle of the area granted by the parent 741 // a bordered label, indented past the content it precedes 742 pub const SectionLabel = struct { 743 box: wgt.Box(Widget), 744 745 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 746 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 747 errdefer box.deinit(allocator); 748 749 { 750 var gap = try wgt.Text.init(allocator, " "); 751 errdefer gap.deinit(allocator); 752 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 753 } 754 755 { 756 var tb = try wgt.TextBox.init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 757 errdefer tb.deinit(allocator); 758 tb.getFocus().mode = .all; 759 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 760 } 761 762 box.getFocus().child_id = box.children.keys()[1]; 763 return .{ .box = box }; 764 } 765 766 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 767 self.box.deinit(allocator); 768 } 769 770 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 771 try self.box.build(allocator, constraint, root_focus); 772 } 773 774 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 775 _ = self; 776 _ = allocator; 777 _ = key; 778 _ = root_focus; 779 } 780 781 pub fn clearGrid(self: *SectionLabel) void { 782 self.box.clearGrid(); 783 } 784 785 pub fn getGrid(self: SectionLabel) ?Grid { 786 return self.box.getGrid(); 787 } 788 789 pub fn getFocus(self: *SectionLabel) *Focus { 790 return self.box.getFocus(); 791 } 792 }; 793 794 // a submit button, indented past its form's content. the web form POSTs 795 // the button's form; terminal hosts detect presses through `buttonId`. 796 pub const SubmitButton = struct { 797 box: wgt.Box(Widget), 798 799 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 800 return initLabeled(allocator, "submit"); 801 } 802 803 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 804 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 805 errdefer box.deinit(allocator); 806 807 { 808 var gap = try wgt.Text.init(allocator, " "); 809 errdefer gap.deinit(allocator); 810 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 811 } 812 813 { 814 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 815 errdefer button.deinit(allocator); 816 button.getFocus().mode = .all; 817 // the renderer distinguishes plain clickables from buttons that 818 // should POST to a server route by this kind. 819 button.getFocus().kind = .{ .custom = "submit" }; 820 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 821 } 822 823 box.getFocus().child_id = box.children.keys()[1]; 824 return .{ .box = box }; 825 } 826 827 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 828 self.box.deinit(allocator); 829 } 830 831 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 832 try self.box.build(allocator, constraint, root_focus); 833 } 834 835 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 836 _ = self; 837 _ = allocator; 838 _ = key; 839 _ = root_focus; 840 } 841 842 pub fn clearGrid(self: *SubmitButton) void { 843 self.box.clearGrid(); 844 } 845 846 pub fn getGrid(self: SubmitButton) ?Grid { 847 return self.box.getGrid(); 848 } 849 850 pub fn getFocus(self: *SubmitButton) *Focus { 851 return self.box.getFocus(); 852 } 853 854 // the button's focus id, for click hit-testing 855 pub fn buttonId(self: *const SubmitButton) usize { 856 return self.box.children.keys()[1]; 857 } 858 }; 859 860 pub const Center = struct { 861 focus: *Focus, 862 grid: ?Grid, 863 child: *Widget, 864 865 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 866 const child = try allocator.create(Widget); 867 errdefer allocator.destroy(child); 868 child.* = child_widget; 869 return .{ 870 .focus = try Focus.create(allocator, .container), 871 .grid = null, 872 .child = child, 873 }; 874 } 875 876 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 877 self.focus.destroy(allocator); 878 if (self.grid) |*grid| { 879 grid.deinit(); 880 self.grid = null; 881 } 882 self.child.deinit(allocator); 883 allocator.destroy(self.child); 884 } 885 886 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 887 self.clearGrid(); 888 self.getFocus().clear(); 889 890 // build the child without forcing it to fill min_size; it sizes 891 // itself to its content within the available max. 892 try self.child.build(allocator, .{ 893 .min_size = .{ .width = null, .height = null }, 894 .max_size = constraint.max_size, 895 }, root_focus); 896 897 const child_grid = self.child.getGrid() orelse return; 898 899 // prefer max when given; otherwise grow to max(min, child) so that 900 // when only a min is set (e.g. the wasm path passes viewport rows 901 // as min_height with no max), we can still vertically center while 902 // letting taller content extend past the min. 903 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; 904 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; 905 if (width == 0 or height == 0) return; 906 907 const offset_x: usize = (width -| child_grid.size.width) / 2; 908 const offset_y: usize = (height -| child_grid.size.height) / 2; 909 910 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 911 errdefer grid.deinit(); 912 try grid.drawGrid(child_grid, offset_x, offset_y); 913 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 914 self.getFocus().child_id = self.child.getFocus().id; 915 916 self.grid = grid; 917 } 918 919 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 920 try self.child.input(allocator, key, root_focus); 921 } 922 923 pub fn clearGrid(self: *Center) void { 924 if (self.grid) |*grid| { 925 grid.deinit(); 926 self.grid = null; 927 } 928 self.child.clearGrid(); 929 } 930 931 pub fn getGrid(self: Center) ?Grid { 932 return self.grid; 933 } 934 935 pub fn getFocus(self: *Center) *Focus { 936 return self.focus; 937 } 938 }; 939 940 // renders truecolor ANSI art 941 pub const AnsiArt = struct { 942 focus: *Focus, 943 grid: ?Grid, 944 content: []const u8, 945 946 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 947 return .{ 948 .focus = try Focus.create(allocator, .container), 949 .grid = null, 950 .content = content, 951 }; 952 } 953 954 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 955 self.focus.destroy(allocator); 956 if (self.grid) |*grid| { 957 grid.deinit(); 958 self.grid = null; 959 } 960 } 961 962 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 963 _ = root_focus; 964 self.clearGrid(); 965 966 // parse into rows of cells, then copy into a rectangular grid 967 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 968 defer { 969 for (rows.items) |*row| row.deinit(allocator); 970 rows.deinit(allocator); 971 } 972 var row: std.ArrayList(Grid.Cell) = .empty; 973 errdefer row.deinit(allocator); 974 975 var style: Grid.Style = .{}; 976 var width: usize = 0; 977 const content = self.content; 978 var i: usize = 0; 979 while (i < content.len) { 980 const byte = content[i]; 981 if (byte == '\n') { 982 width = @max(width, row.items.len); 983 try rows.append(allocator, row); 984 row = .empty; 985 i += 1; 986 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 987 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 988 var j = i + 2; 989 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 990 if (j >= content.len) { 991 i = content.len; // malformed trailing escape; stop 992 } else { 993 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 994 i = j + 1; 995 } 996 } else { 997 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 998 const end = @min(content.len, i + len); 999 const rune = content[i..end]; 1000 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1001 // a foreground color does not make a space visible. only an 1002 // effective background makes it opaque; foreground state often 1003 // remains active through the padding at the end of a row. 1004 const effective_background = if (style.inverted) style.fg else style.bg; 1005 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1006 try row.append(allocator, .{ 1007 .rune = if (transparent) null else codepoint, 1008 .style = style, 1009 }); 1010 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1011 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1012 } 1013 i = end; 1014 } 1015 } 1016 // a trailing row with content but no closing newline 1017 if (row.items.len > 0) { 1018 width = @max(width, row.items.len); 1019 try rows.append(allocator, row); 1020 } else { 1021 row.deinit(allocator); 1022 } 1023 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1024 1025 const height = rows.items.len; 1026 if (width == 0 or height == 0) return; 1027 1028 const clamped_w = @min(width, constraint.max_size.width orelse width); 1029 const clamped_h = @min(height, constraint.max_size.height orelse height); 1030 1031 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1032 errdefer grid.deinit(); 1033 for (rows.items[0..clamped_h], 0..) |r, y| { 1034 const n = @min(r.items.len, clamped_w); 1035 for (r.items[0..n], 0..) |cell, x| { 1036 if (cell.continuation) continue; 1037 (try grid.cell(x, y)).style = cell.style; 1038 try grid.setRune(x, y, cell.rune); 1039 } 1040 } 1041 self.grid = grid; 1042 } 1043 1044 pub fn clearGrid(self: *AnsiArt) void { 1045 if (self.grid) |*grid| { 1046 grid.deinit(); 1047 self.grid = null; 1048 } 1049 } 1050 1051 pub fn getGrid(self: AnsiArt) ?Grid { 1052 return self.grid; 1053 } 1054 1055 pub fn getFocus(self: *AnsiArt) *Focus { 1056 return self.focus; 1057 } 1058 1059 fn applySgr(style: *Grid.Style, params: []const u8) void { 1060 var nums: [16]u32 = undefined; 1061 var n: usize = 0; 1062 var it = std.mem.splitScalar(u8, params, ';'); 1063 while (it.next()) |tok| { 1064 if (n >= nums.len) break; 1065 // an empty parameter (e.g. bare "\x1b[m") means 0 1066 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1067 n += 1; 1068 } 1069 if (n == 0) { 1070 style.* = .{}; 1071 return; 1072 } 1073 var i: usize = 0; 1074 while (i < n) : (i += 1) { 1075 switch (nums[i]) { 1076 0 => style.* = .{}, 1077 7 => style.inverted = true, 1078 27 => style.inverted = false, 1079 39 => style.fg = null, 1080 49 => style.bg = null, 1081 38, 48 => { 1082 // truecolor form: 38;2;r;g;b — anything else is ignored 1083 if (i + 4 < n and nums[i + 1] == 2) { 1084 const c = Grid.Color{ 1085 .r = @truncate(nums[i + 2]), 1086 .g = @truncate(nums[i + 3]), 1087 .b = @truncate(nums[i + 4]), 1088 }; 1089 if (nums[i] == 38) style.fg = c else style.bg = c; 1090 i += 4; 1091 } 1092 }, 1093 else => {}, 1094 } 1095 } 1096 } 1097 }; 1098 1099 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1100 pub const AnsiBackground = struct { 1101 grid: ?Grid, 1102 child: *Widget, 1103 art: AnsiArt, 1104 session: *ui.Session, 1105 1106 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1107 var cw = child_widget; 1108 const child = allocator.create(Widget) catch |e| { 1109 cw.deinit(allocator); 1110 return e; 1111 }; 1112 child.* = cw; 1113 errdefer { 1114 child.deinit(allocator); 1115 allocator.destroy(child); 1116 } 1117 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1118 } 1119 1120 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1121 if (self.grid) |*grid| grid.deinit(); 1122 self.art.deinit(allocator); 1123 self.child.deinit(allocator); 1124 allocator.destroy(self.child); 1125 } 1126 1127 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1128 self.clearGrid(); 1129 1130 // make the wrapped page fill the whole available area 1131 try self.child.build(allocator, .{ 1132 .min_size = .{ 1133 .width = constraint.max_size.width orelse constraint.min_size.width, 1134 .height = constraint.max_size.height orelse constraint.min_size.height, 1135 }, 1136 .max_size = constraint.max_size, 1137 }, root_focus); 1138 1139 if (!self.session.data.enable_ansi) return; 1140 const foreground = self.child.getGrid() orelse return; 1141 self.art.content = self.session.data.ansi_art; 1142 try self.buildArt(allocator, foreground.size, root_focus); 1143 if (!self.session.is_terminal) return; 1144 const art_grid = self.art.getGrid() orelse return; 1145 self.grid = try artBehind(allocator, foreground, art_grid); 1146 } 1147 1148 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1149 try self.child.input(allocator, key, root_focus); 1150 } 1151 1152 pub fn clearGrid(self: *AnsiBackground) void { 1153 if (self.grid) |*grid| { 1154 grid.deinit(); 1155 self.grid = null; 1156 } 1157 self.child.clearGrid(); 1158 self.art.clearGrid(); 1159 } 1160 1161 pub fn getGrid(self: AnsiBackground) ?Grid { 1162 return self.grid orelse self.child.getGrid(); 1163 } 1164 1165 pub fn getFocus(self: *AnsiBackground) *Focus { 1166 return self.child.getFocus(); 1167 } 1168 1169 // a foreground cell counts as blank if it's empty or a bare space without styling 1170 fn cellIsBlank(cell: Grid.Cell) bool { 1171 if (cell.continuation) return false; 1172 if (cell.rune) |rune| { 1173 return rune == ' ' and 1174 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1175 } 1176 return true; 1177 } 1178 1179 // the art is dimmed to this fraction of its brightness so foreground text 1180 // stays legible over it 1181 const art_brightness = 35; // percent 1182 1183 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1184 const c = color orelse return null; 1185 return .{ 1186 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1187 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1188 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1189 }; 1190 } 1191 1192 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1193 try self.art.build(allocator, .{ 1194 .min_size = .{ .width = null, .height = null }, 1195 .max_size = .{ .width = size.width, .height = size.height }, 1196 }, root_focus); 1197 if (self.art.grid) |*grid| { 1198 for (grid.cells) |*cell| { 1199 cell.style.fg = dimColor(cell.style.fg); 1200 cell.style.bg = dimColor(cell.style.bg); 1201 } 1202 } 1203 } 1204 1205 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1206 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1207 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1208 // snap such channels clear of that range so the backdrop doesn't blink or 1209 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1210 // actually render truecolor. 1211 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1212 const c = color orelse return null; 1213 const snap = struct { 1214 fn f(v: u8) u8 { 1215 return if (v >= 1 and v <= 9) 10 else v; 1216 } 1217 }.f; 1218 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1219 } 1220 1221 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1222 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1223 dst.style.bg = background_maybe; 1224 // change unstyled text to contrast with the art behind it 1225 if (dst.style.fg == null) { 1226 if (background_maybe) |background| { 1227 // use near-black and near-white, which is easier on the eyes 1228 // while retaining high contrast 1229 const luminance = (@as(u32, background.r) * 299 + 1230 @as(u32, background.g) * 587 + 1231 @as(u32, background.b) * 114) / 1000; 1232 dst.style.fg = if (luminance >= 128) 1233 .{ .r = 16, .g = 16, .b = 16 } 1234 else 1235 .{ .r = 240, .g = 240, .b = 240 }; 1236 } 1237 } 1238 } 1239 1240 // composites ANSI art behind a foreground grid 1241 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1242 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1243 errdefer out.deinit(); 1244 1245 const anchor_x = foreground.size.width -| art_grid.size.width; 1246 for (0..art_grid.size.height) |y| { 1247 for (0..art_grid.size.width) |x| { 1248 const src = (try art_grid.cell(x, y)).*; 1249 if (src.rune == null) continue; 1250 const dst = try out.cell(anchor_x + x, y); 1251 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1252 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1253 // blank cells take the art; occupied cells take its background 1254 if (cellIsBlank(dst.*) and (next_dst == null or cellIsBlank(next_dst.?.*))) { 1255 dst.style = src.style; 1256 dst.style.fg = sgrSafe(dst.style.fg); 1257 dst.style.bg = sgrSafe(dst.style.bg); 1258 try out.setRune(anchor_x + x, y, src.rune); 1259 } else { 1260 applyArtBackground(dst, src); 1261 if (next_dst) |next| applyArtBackground(next, src); 1262 } 1263 } 1264 } 1265 return out; 1266 } 1267 }; 1268 1269 pub const CopyableText = struct { 1270 pub const Choice = struct { 1271 selector: []const u8 = "", 1272 text: []const u8, 1273 copyable_text: ?[]const u8 = null, 1274 label: []const u8 = "", 1275 bottom_label: []const u8 = "", 1276 }; 1277 1278 box: wgt.Box(Widget), 1279 session: *ui.Session, 1280 choices: []Choice, 1281 selected: usize = 0, 1282 1283 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1284 if (choices.len == 0) return error.MissingCopyableText; 1285 1286 const owned_choices = try allocator.dupe(Choice, choices); 1287 errdefer allocator.free(owned_choices); 1288 1289 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1290 errdefer box.deinit(allocator); 1291 1292 if (choices.len > 1) for (choices) |choice| { 1293 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .wrap_kind = .none }); 1294 errdefer selector_box.deinit(allocator); 1295 selector_box.getFocus().mode = .all; 1296 try box.children.put(allocator, selector_box.getFocus().id, .{ .widget = .{ .text_box = selector_box }, .rect = null, .min_size = .{ .width = choice.selector.len + 2, .height = 3 } }); 1297 }; 1298 1299 var text_input = try wgt.TextInput.init(allocator, .{ 1300 .border_style = .single, 1301 .label = choices[0].label, 1302 .bottom_label = choices[0].bottom_label, 1303 .read_only = true, 1304 .render_content = session.is_terminal, 1305 .visible_width = null, 1306 }); 1307 errdefer text_input.deinit(allocator); 1308 text_input.getFocus().mode = .all; 1309 try text_input.setContent(allocator, choices[0].text); 1310 box.getFocus().child_id = text_input.getFocus().id; 1311 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1312 1313 return .{ .box = box, .session = session, .choices = owned_choices }; 1314 } 1315 1316 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1317 const aa = session.page_arena.allocator(); 1318 var choices: [2]Choice = undefined; 1319 var count: usize = 0; 1320 switch (location) { 1321 .repo => |identity| { 1322 if (session.data.git_http_port) |port| { 1323 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1324 choices[count] = .{ 1325 .selector = "http", 1326 .text = url, 1327 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1328 .label = " clone ", 1329 }; 1330 count += 1; 1331 } 1332 if (session.data.git_ssh_port) |port| { 1333 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1334 choices[count] = .{ 1335 .selector = "ssh", 1336 .text = url, 1337 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1338 .label = " clone ", 1339 }; 1340 count += 1; 1341 } 1342 }, 1343 .fork => |f| if (session.data.git_ssh_port) |port| { 1344 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1345 choices[count] = .{ 1346 .text = url, 1347 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1348 .label = " clone ", 1349 }; 1350 count += 1; 1351 }, 1352 } 1353 if (count == 0) return null; 1354 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1355 } 1356 1357 pub fn minWidth(self: *const CopyableText) usize { 1358 var width = maxWidth(self.choices) + 2; 1359 if (self.choices.len > 1) { 1360 for (self.choices) |choice| width += choice.selector.len + 2; 1361 } 1362 return width; 1363 } 1364 1365 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1366 self.box.deinit(allocator); 1367 allocator.free(self.choices); 1368 } 1369 1370 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1371 self.clearGrid(); 1372 1373 if (self.choices.len > 1) { 1374 if (root_focus.grandchild_id) |id| { 1375 for (0..self.choices.len) |i| { 1376 if (id != self.selector(i).getFocus().id) continue; 1377 if (i != self.selected) try self.select(allocator, i); 1378 root_focus.setFocus(self.textInput().getFocus().id); 1379 break; 1380 } 1381 } 1382 1383 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1384 for (0..self.choices.len) |i| { 1385 self.selector(i).options.border_style = if (i == self.selected) 1386 (if (focused) .double else .single) 1387 else 1388 .hidden; 1389 } 1390 } 1391 self.textInput().options.border_style = .single; 1392 self.textInput().cursor = 0; 1393 self.textInput().scroll_offset = 0; 1394 try self.box.build(allocator, constraint, root_focus); 1395 1396 if (!self.session.is_terminal) { 1397 const aa = self.session.arena.allocator(); 1398 const id = self.textInput().getFocus().id; 1399 try self.session.text_inputs.put(aa, id, self.textInput()); 1400 } 1401 } 1402 1403 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1404 switch (key) { 1405 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1406 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1407 .enter => if (self.session.is_terminal) self.show(), 1408 .mouse => |mouse| { 1409 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1410 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1411 try self.select(allocator, i); 1412 root_focus.setFocus(self.textInput().getFocus().id); 1413 return; 1414 }; 1415 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1416 }, 1417 else => {}, 1418 } 1419 } 1420 1421 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1422 self.selected = selected; 1423 const choice = self.choices[selected]; 1424 const text_input = self.textInput(); 1425 text_input.options.label = choice.label; 1426 text_input.options.bottom_label = choice.bottom_label; 1427 try text_input.setContent(allocator, choice.text); 1428 } 1429 1430 fn show(self: *CopyableText) void { 1431 const choice = self.choices[self.selected]; 1432 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1433 } 1434 1435 fn textInput(self: *CopyableText) *wgt.TextInput { 1436 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1437 } 1438 1439 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1440 return &self.box.children.values()[index].widget.text_box; 1441 } 1442 1443 fn maxWidth(choices: []const Choice) usize { 1444 var width: usize = 0; 1445 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1446 return width; 1447 } 1448 1449 pub fn clearGrid(self: *CopyableText) void { 1450 self.box.clearGrid(); 1451 } 1452 1453 pub fn getGrid(self: CopyableText) ?Grid { 1454 return self.box.getGrid(); 1455 } 1456 1457 pub fn getFocus(self: *CopyableText) *Focus { 1458 return self.box.getFocus(); 1459 } 1460 }; 1461