╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
401 .page_up => { 402 if (self.scroll.grid) |grid| if (slot_height > 0) { 403 const rows_per_page = grid.size.height / slot_height; 404 index -|= rows_per_page * cols; 405 }; 406 }, 407 .page_down => { 408 if (self.scroll.grid) |grid| if (slot_height > 0) { 409 const rows_per_page = grid.size.height / slot_height; 410 index = @min(index + rows_per_page * cols, count - 1); 411 }; 412 }, 413 // scroll wheel moves the focused cell by a full row so the 414 // viewport (via scrollToRect) follows in row-sized steps, 415 // matching how a scroll wheel feels in a grid view. 416 .mouse => |mouse| switch (mouse.action) { 417 .scroll => |dir| switch (dir) { 418 .up => index -|= cols, 419 .down => if (index + cols < count) { 420 index += cols; 421 }, 422 }, 423 else => {}, 424 }, 425 else => {}, 426 } 427 428 if (index != current_index) { 429 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 430 if (in.cellRect(index)) |rect| { 431 self.scroll.scrollToRect(rect); 432 } 433 } 434 } 435 436 pub fn clearGrid(self: *Scroll) void { 437 self.scroll.clearGrid(); 438 } 439 440 pub fn getGrid(self: Scroll) ?Grid { 441 return self.scroll.getGrid(); 442 } 443 444 pub fn getFocus(self: *Scroll) *Focus { 445 return self.scroll.getFocus(); 446 } 447 448 pub fn atTop(self: Scroll) bool { 449 const in = self.scroll.child.flow_box; 450 const child_id = in.focus.child_id orelse return false; 451 return in.indexOfFocusId(child_id) == 0; 452 } 453 454 fn inner(self: *Scroll) *FlowBox { 455 return &self.scroll.child.flow_box; 456 } 457 }; 458 }; 459 460 // a left-to-right flow of variable-width focusable items that wraps at the 461 // available width, like words in a paragraph. unlike FlowBox there is no cell 462 // grid: each item is as wide as its text. selection movement is driven by the 463 // owning view via indexOfFocusId/verticalNeighbor. 464 pub const WordFlow = struct { 465 focus: *Focus, 466 grid: ?Grid, 467 text_boxes: std.ArrayList(wgt.TextBox), 468 // last build's item rects (content space), for vertical navigation and 469 // scroll-into-view. 470 rects: std.ArrayList(layout.IRect), 471 // backs each link item's `.custom` focus kind. 472 arena: std.heap.ArenaAllocator, 473 474 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 475 476 pub fn init(allocator: std.mem.Allocator) !WordFlow { 477 return .{ 478 .focus = try Focus.create(allocator, .container), 479 .grid = null, 480 .text_boxes = .empty, 481 .rects = .empty, 482 .arena = std.heap.ArenaAllocator.init(allocator), 483 }; 484 } 485 486 pub fn deinit(self: *WordFlow, allocator: std.mem.Allocator) void { 487 self.focus.destroy(allocator); 488 if (self.grid) |*grid| { 489 grid.deinit(); 490 self.grid = null; 491 } 492 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 493 self.text_boxes.deinit(allocator); 494 self.rects.deinit(allocator); 495 self.arena.deinit(); 496 } 497 498 // the text box copies the item text; links are copied into the arena, so the 499 // caller's slices needn't outlive this call. 500 pub fn setItems(self: *WordFlow, allocator: std.mem.Allocator, items: []const Item) !void { 501 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 502 self.text_boxes.clearAndFree(allocator); 503 504 _ = self.arena.reset(.retain_capacity); 505 const aa = self.arena.allocator(); 506 507 self.focus.clear(); 508 self.focus.child_id = null; 509 510 for (items) |item| { 511 var text_box = try wgt.TextBox.init(allocator, item.text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 512 errdefer text_box.deinit(allocator); 513 text_box.getFocus().mode = .all; 514 515 if (item.link.len > 0) { 516 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 517 } 518 519 try self.text_boxes.append(allocator, text_box); 520 } 521 522 if (self.text_boxes.items.len > 0) { 523 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 524 } 525 } 526 527 pub fn build(self: *WordFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 528 self.clearGrid(); 529 self.focus.clear(); 530 531 if (self.text_boxes.items.len == 0) return; 532 const max_width = constraint.max_size.width orelse 80; 533 if (max_width == 0) return; 534 535 // lay the items out left to right, wrapping to a new row when the next 536 // one wouldn't fit. 537 self.rects.clearRetainingCapacity(); 538 var x: usize = 0; 539 var y: usize = 0; 540 var row_height: usize = 0; 541 var total_width: usize = 0; 542 for (self.text_boxes.items) |*tb| { 543 try tb.build(allocator, .{ 544 .min_size = .{ .width = null, .height = null }, 545 .max_size = .{ .width = max_width, .height = null }, 546 }, root_focus); 547 const tb_grid_maybe = tb.getGrid(); 548 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 549 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 550 if (x > 0 and x + w > max_width) { 551 x = 0; 552 y += row_height; 553 row_height = 0; 554 } 555 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 556 if (h > row_height) row_height = h; 557 x += w; 558 if (x > total_width) total_width = x; 559 } 560 561 const total_height = y + row_height; 562 if (total_width == 0 or total_height == 0) return; 563 564 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 565 errdefer grid.deinit(); 566 567 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 568 const tb_grid = tb.getGrid() orelse continue; 569 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 570 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 571 } 572 573 self.grid = grid; 574 } 575 576 pub fn input(self: *WordFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 577 _ = self; 578 _ = allocator; 579 _ = key; 580 _ = root_focus; 581 } 582 583 pub fn clearGrid(self: *WordFlow) void { 584 if (self.grid) |*grid| { 585 grid.deinit(); 586 self.grid = null; 587 } 588 for (self.text_boxes.items) |*tb| tb.clearGrid(); 589 } 590 591 pub fn getGrid(self: WordFlow) ?Grid { 592 return self.grid; 593 } 594 595 pub fn getFocus(self: *WordFlow) *Focus { 596 return self.focus; 597 } 598 599 pub fn indexOfFocusId(self: WordFlow, focus_id: usize) ?usize { 600 for (self.text_boxes.items, 0..) |tb, i| { 601 if (tb.focus.id == focus_id) return i; 602 } 603 return null; 604 } 605 606 // the first item of the row below (when `downward`) or above, or null at 607 // the flow's edge. items are laid out in order, so y is nondecreasing. 608 pub fn rowStep(self: WordFlow, index: usize, downward: bool) ?usize { 609 const rects = self.rects.items; 610 if (index >= rects.len) return null; 611 const cur_y = rects[index].y; 612 if (downward) { 613 for (rects[index + 1 ..], index + 1..) |r, i| { 614 if (r.y > cur_y) return i; 615 } 616 return null; 617 } 618 // walk the earlier rows; the last one visited is the row above. 619 var prev_first: ?usize = null; 620 var i: usize = 0; 621 while (i < rects.len and rects[i].y < cur_y) { 622 prev_first = i; 623 const y = rects[i].y; 624 while (i < rects.len and rects[i].y == y) i += 1; 625 } 626 return prev_first; 627 } 628 }; 629 630 // a word-wrapped paragraph of styled runs. each line a link touches is a 631 // focusable piece, and a focused link draws all its pieces inverted. 632 pub const RichText = struct { 633 focus: *Focus, 634 grid: ?Grid, 635 // backs the decoded content and the link targets 636 arena: std.heap.ArenaAllocator, 637 runes: []const u21, 638 // each rune's span 639 span_of: []const usize, 640 styles: []const Grid.Style, 641 // each span's link index, null outside a link 642 span_links: []const ?usize, 643 links: []const []const u8, 644 // each link's piece focuses in line order, kept across builds so a link 645 // keeps its ids when the text rewraps 646 link_focuses: []std.ArrayList(*Focus), 647 lines: std.ArrayList(wgt.Line), 648 // scratch for each build's link pieces 649 pieces: std.ArrayList(Piece), 650 651 pub const Span = struct { 652 text: []const u8, 653 style: Grid.Style = .{}, 654 // the focus kind a click or enter follows; empty for plain text 655 link: []const u8 = "", 656 }; 657 658 const Piece = struct { 659 link: usize, 660 rect: layout.URect, 661 }; 662 663 pub fn init(allocator: std.mem.Allocator, spans: []const Span) !RichText { 664 var arena = std.heap.ArenaAllocator.init(allocator); 665 errdefer arena.deinit(); 666 const aa = arena.allocator(); 667 668 var runes: std.ArrayList(u21) = .empty; 669 var span_of: std.ArrayList(usize) = .empty; 670 const styles = try aa.alloc(Grid.Style, spans.len); 671 const span_links = try aa.alloc(?usize, spans.len); 672 var links: std.ArrayList([]const u8) = .empty; 673 for (spans, 0..) |span, i| { 674 styles[i] = span.style; 675 span_links[i] = null; 676 if (span.link.len > 0) { 677 // adjacent spans with the same target are one link 678 const same = i > 0 and spans[i - 1].link.len > 0 and std.mem.eql(u8, spans[i - 1].link, span.link); 679 if (!same) try links.append(aa, try aa.dupe(u8, span.link)); 680 span_links[i] = links.items.len - 1; 681 } 682 var utf8 = (try std.unicode.Utf8View.init(span.text)).iterator(); 683 while (utf8.nextCodepoint()) |codepoint| { 684 try runes.append(aa, codepoint); 685 try span_of.append(aa, i); 686 } 687 } 688 689 const link_focuses = try aa.alloc(std.ArrayList(*Focus), links.items.len); 690 @memset(link_focuses, .empty); 691 692 return .{ 693 .focus = try Focus.create(allocator, .container), 694 .grid = null, 695 .arena = arena, 696 .runes = runes.items, 697 .span_of = span_of.items, 698 .styles = styles, 699 .span_links = span_links, 700 .links = links.items, 701 .link_focuses = link_focuses, 702 .lines = .empty, 703 .pieces = .empty, 704 }; 705 } 706 707 pub fn deinit(self: *RichText, allocator: std.mem.Allocator) void { 708 self.focus.destroy(allocator); 709 self.clearGrid(); 710 for (self.link_focuses) |*focuses| { 711 for (focuses.items) |focus| focus.destroy(allocator); 712 focuses.deinit(allocator); 713 } 714 self.pieces.deinit(allocator); 715 self.lines.deinit(allocator); 716 self.arena.deinit(); 717 } 718 719 // the url a terminal can open for a link: raw links to absolute urls 720 fn terminalLink(link: []const u8) ?[]const u8 { 721 if (!std.mem.startsWith(u8, link, ui.raw_link_prefix)) return null; 722 const url = link[ui.raw_link_prefix.len..]; 723 return if (std.mem.startsWith(u8, url, "/")) null else url; 724 } 725 726 // the link one of whose pieces has focus id `id` 727 fn linkOf(self: *const RichText, id: ?usize) ?usize { 728 for (self.link_focuses, 0..) |focuses, l| { 729 for (focuses.items) |focus| if (focus.id == id) return l; 730 } 731 return null; 732 } 733 734 pub fn build(self: *RichText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 735 self.clearGrid(); 736 self.focus.clear(); 737 const max_width = constraint.max_size.width; 738 if (max_width == 0 or constraint.max_size.height == 0) return; 739 const focused_link = self.linkOf(root_focus.grandchild_id); 740 741 try wgt.wrapLines(allocator, &self.lines, self.runes, .word, max_width); 742 // trailing spaces are dropped so a wrapped link doesn't highlight them 743 for (self.lines.items) |*line| { 744 while (line.end > line.start and self.runes[line.end - 1] == ' ') { 745 line.end -= 1; 746 line.width -= 1; 747 } 748 } 749 const visible = @min(self.lines.items.len, constraint.max_size.height orelse self.lines.items.len); 750 var width: usize = 1; 751 for (self.lines.items[0..visible]) |line| width = @max(width, line.width); 752 if (max_width) |w| width = @min(width, w); 753 754 var grid = try Grid.init(allocator, .{ .width = width, .height = visible }); 755 errdefer grid.deinit(); 756 self.pieces.clearRetainingCapacity(); 757 for (self.lines.items[0..visible], 0..) |line, y| { 758 var x: usize = 0; 759 for (self.runes[line.start..line.end], line.start..) |rune, i| { 760 const rune_width = xitui.width.cellWidth(rune); 761 if (x + rune_width > width) break; 762 const link = self.span_links[self.span_of[i]]; 763 const cell = try grid.cell(x, y); 764 cell.style = self.styles[self.span_of[i]]; 765 if (link) |l| { 766 cell.link = terminalLink(self.links[l]); 767 if (l == focused_link) cell.style.inverted = true; 768 // extend this line's piece of the link, or start one 769 const items = self.pieces.items; 770 if (items.len > 0 and items[items.len - 1].link == l and items[items.len - 1].rect.y == y) { 771 items[items.len - 1].rect.size.width += rune_width; 772 } else { 773 try self.pieces.append(allocator, .{ .link = l, .rect = .{ .x = x, .y = y, .size = .{ .width = rune_width, .height = 1 } } }); 774 } 775 } 776 try grid.setRune(x, y, rune); 777 x += rune_width; 778 } 779 } 780 781 // a link's pieces are consecutive, since its runes are 782 var piece_index: usize = 0; 783 for (self.pieces.items, 0..) |piece, i| { 784 piece_index = if (i > 0 and self.pieces.items[i - 1].link == piece.link) piece_index + 1 else 0; 785 const focuses = &self.link_focuses[piece.link]; 786 if (piece_index == focuses.items.len) { 787 const focus = try Focus.create(allocator, .{ .custom = self.links[piece.link] }); 788 errdefer focus.destroy(allocator); 789 focus.mode = .all; 790 try focuses.append(allocator, focus); 791 } 792 try self.focus.addChild(allocator, focuses.items[piece_index], piece.rect.size, piece.rect.x, piece.rect.y); 793 } 794 795 // focus rests on a link's first piece, which stays while the link shows 796 if (focused_link) |l| if (self.link_focuses[l].items.len > 0) { 797 const first = self.link_focuses[l].items[0].id; 798 if (self.focus.children.contains(first)) { 799 root_focus.grandchild_id = first; 800 self.focus.child_id = first; 801 } 802 }; 803 804 self.grid = grid; 805 } 806 807 pub fn input(self: *RichText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 808 _ = self; 809 _ = allocator; 810 _ = key; 811 _ = root_focus; 812 } 813 814 pub fn clearGrid(self: *RichText) void { 815 if (self.grid) |*grid| { 816 grid.deinit(); 817 self.grid = null; 818 } 819 } 820 821 pub fn getGrid(self: RichText) ?Grid { 822 return self.grid; 823 } 824 825 pub fn getFocus(self: *RichText) *Focus { 826 return self.focus; 827 } 828 }; 829 830 // an invisible widget that fills the horizontal space granted by its parent. 831 // used inside Box(horiz) with a min_size so the box reserves space for the 832 // children that follow, pushing them to the right. 833 pub const Spacer = struct { 834 focus: *Focus, 835 grid: ?Grid, 836 837 pub fn init(allocator: std.mem.Allocator) !Spacer { 838 return .{ 839 .focus = try Focus.create(allocator, .container), 840 .grid = null, 841 }; 842 } 843 844 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 845 self.focus.destroy(allocator); 846 if (self.grid) |*grid| { 847 grid.deinit(); 848 self.grid = null; 849 } 850 } 851 852 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 853 _ = root_focus; 854 self.clearGrid(); 855 const width = constraint.max_size.width orelse return; 856 if (width == 0) return; 857 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 858 } 859 860 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 861 _ = self; 862 _ = allocator; 863 _ = key; 864 _ = root_focus; 865 } 866 867 pub fn clearGrid(self: *Spacer) void { 868 if (self.grid) |*grid| { 869 grid.deinit(); 870 self.grid = null; 871 } 872 } 873 874 pub fn getGrid(self: Spacer) ?Grid { 875 return self.grid; 876 } 877 878 pub fn getFocus(self: *Spacer) *Focus { 879 return self.focus; 880 } 881 }; 882 883 // a sub-header's search box: a labeled text input whose enter the owning view 884 // turns into a navigation. shared by the files, commits and thread tabs. 885 pub const SearchBox = struct { 886 text_input: wgt.TextInput, 887 session: *ui.Session, 888 889 // the room the box asks its row for 890 pub const min_size: layout.MaybeSize = .{ .width = 20, .height = 3 }; 891 892 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, label: []const u8, name: []const u8, text_value: ?[]const u8) !SearchBox { 893 var text_input = try wgt.TextInput.init(allocator, .{ .label = label, .name = name, .round_corners = true, .visible_width = 18, .render_content = session.is_terminal }); 894 errdefer text_input.deinit(allocator); 895 text_input.getFocus().mode = .all; 896 if (text_value) |value| try text_input.setContent(allocator, value); 897 return .{ .text_input = text_input, .session = session }; 898 } 899 900 pub fn deinit(self: *SearchBox, allocator: std.mem.Allocator) void { 901 self.text_input.deinit(allocator); 902 } 903 904 pub fn build(self: *SearchBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 905 self.text_input.options.bottom_label = if (root_focus.grandchild_id == self.getFocus().id) " press enter " else ""; 906 try self.text_input.build(allocator, constraint, root_focus); 907 if (!self.session.is_terminal) { 908 try self.session.text_inputs.put(self.session.arena.allocator(), self.getFocus().id, &self.text_input); 909 } 910 } 911 912 pub fn input(self: *SearchBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 913 try self.text_input.input(allocator, key, root_focus); 914 } 915 916 // the typed text; the caller owns it 917 pub fn text(self: *SearchBox, allocator: std.mem.Allocator) ![]u8 { 918 return self.text_input.text(allocator); 919 } 920 921 // whether the cursor sits past the last character, where right-arrow leaves 922 // the box rather than moving within it 923 pub fn atEnd(self: *SearchBox) bool { 924 return self.text_input.cursor == self.text_input.content.items.len; 925 } 926 927 pub fn clearGrid(self: *SearchBox) void { 928 self.text_input.clearGrid(); 929 } 930 931 pub fn getGrid(self: SearchBox) ?Grid { 932 return self.text_input.getGrid(); 933 } 934 935 pub fn getFocus(self: *SearchBox) *Focus { 936 return self.text_input.getFocus(); 937 } 938 }; 939 940 // a tab's sub-header: an optional search box on the left, then a spacer, then 941 // the clone url on the right. the owning view turns a submitted query into a 942 // route, so route building stays with the tab that knows its own url. 943 pub const SearchHeader = struct { 944 box: wgt.Box(Widget), 945 session: *ui.Session, 946 947 pub fn init( 948 allocator: std.mem.Allocator, 949 session: *ui.Session, 950 location: ui.RoutablePage.RepoLocation, 951 label: []const u8, 952 name: []const u8, 953 text: ?[]const u8, 954 show_input: bool, 955 ) !SearchHeader { 956 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 957 errdefer box.deinit(allocator); 958 959 // the search box only appears where an index can serve it. 960 if (show_input) { 961 var search_box = try SearchBox.init(allocator, session, label, name, text); 962 errdefer search_box.deinit(allocator); 963 box.getFocus().child_id = search_box.getFocus().id; 964 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = SearchBox.min_size }); 965 } 966 967 // the spacer pushes the clone url to the right edge. 968 { 969 var spacer = try Spacer.init(allocator); 970 errdefer spacer.deinit(allocator); 971 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 972 } 973 974 if (session.data.host_kind == .server) { 975 if (try CopyableText.initClone(allocator, session, location)) |value| { 976 var clone_url = value; 977 errdefer clone_url.deinit(allocator); 978 const min_width = clone_url.minWidth(); 979 if (box.getFocus().child_id == null) box.getFocus().child_id = clone_url.getFocus().id; 980 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 981 } 982 } 983 984 return .{ .box = box, .session = session }; 985 } 986 987 pub fn deinit(self: *SearchHeader, allocator: std.mem.Allocator) void { 988 self.box.deinit(allocator); 989 } 990 991 pub fn build(self: *SearchHeader, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 992 self.clearGrid(); 993 try self.box.build(allocator, constraint, root_focus); 994 } 995 996 pub fn input(self: *SearchHeader, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 997 const current = self.box.getFocus().child_id orelse return; 998 if (self.searchBox()) |search_box| { 999 if (current == search_box.getFocus().id) { 1000 // tab, and right-arrow past the last character, step to the 1001 // clone url beside the box 1002 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 1003 if (self.cloneUrl()) |clone_url| { 1004 root_focus.setFocus(clone_url.getFocus().id); 1005 return; 1006 } 1007 } 1008 return search_box.input(allocator, key, root_focus); 1009 } 1010 } 1011 const clone_url = self.cloneUrl() orelse return; 1012 if (key == .back_tab or (key == .arrow_left and clone_url.selected == 0)) { 1013 if (self.searchBox()) |search_box| { 1014 root_focus.setFocus(search_box.getFocus().id); 1015 return; 1016 } 1017 } 1018 try clone_url.input(allocator, key, root_focus); 1019 } 1020 1021 // the typed query when enter should submit it: the box exists and holds 1022 // focus. the caller owns the returned text. 1023 pub fn submittedText(self: *SearchHeader, allocator: std.mem.Allocator) !?[]u8 { 1024 const search_box = self.searchBox() orelse return null; 1025 if (self.box.getFocus().child_id != search_box.getFocus().id) return null; 1026 return try search_box.text(allocator); 1027 } 1028 1029 // enter the header, preferring the search box. 1030 pub fn focusHeader(self: *SearchHeader, root_focus: *Focus) bool { 1031 if (self.searchBox()) |search_box| { 1032 root_focus.setFocus(search_box.getFocus().id); 1033 return true; 1034 } 1035 const clone_url = self.cloneUrl() orelse return false; 1036 root_focus.setFocus(clone_url.getFocus().id); 1037 return true; 1038 } 1039 1040 pub fn hasFocusable(self: *SearchHeader) bool { 1041 return self.cloneUrl() != null or self.searchBox() != null; 1042 } 1043 1044 fn cloneUrl(self: *SearchHeader) ?*CopyableText { 1045 for (self.box.children.values()) |*child| switch (child.widget) { 1046 .copyable_text => |*copyable_text| return copyable_text, 1047 else => {}, 1048 }; 1049 return null; 1050 } 1051 1052 fn searchBox(self: *SearchHeader) ?*SearchBox { 1053 for (self.box.children.values()) |*child| switch (child.widget) { 1054 .search_box => |*search_box| return search_box, 1055 else => {}, 1056 }; 1057 return null; 1058 } 1059 1060 pub fn clearGrid(self: *SearchHeader) void { 1061 self.box.clearGrid(); 1062 } 1063 1064 pub fn getGrid(self: SearchHeader) ?Grid { 1065 return self.box.getGrid(); 1066 } 1067 1068 pub fn getFocus(self: *SearchHeader) *Focus { 1069 return self.box.getFocus(); 1070 } 1071 }; 1072 1073 // a one-row, non-focusable status bar showing the current page's url 1074 pub const Footer = struct { 1075 focus: *Focus, 1076 grid: ?Grid, 1077 session: *ui.Session, 1078 arena: std.heap.ArenaAllocator, 1079 url: []const u8 = "", 1080 url_width: usize = 0, 1081 1082 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 1083 return .{ 1084 .focus = try Focus.create(allocator, .container), 1085 .grid = null, 1086 .session = session, 1087 .arena = std.heap.ArenaAllocator.init(allocator), 1088 }; 1089 } 1090 1091 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1092 self.focus.destroy(allocator); 1093 if (self.grid) |*grid| grid.deinit(); 1094 self.arena.deinit(); 1095 } 1096 1097 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1098 _ = root_focus; 1099 self.clearGrid(); 1100 const width = constraint.max_size.width orelse return; 1101 if (width == 0) return; 1102 1103 _ = self.arena.reset(.retain_capacity); 1104 self.url = ""; 1105 self.url_width = 0; 1106 const aa = self.arena.allocator(); 1107 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1108 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 1109 self.url = text; 1110 1111 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1112 errdefer grid.deinit(); 1113 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1114 var i: usize = 0; 1115 while (utf8.nextCodepoint()) |ch| { 1116 const rune_width = xitui.width.cellWidth(ch); 1117 if (i + rune_width > width) break; 1118 // a terminal hyperlink to the page 1119 (try grid.cell(i, 0)).link = text; 1120 try grid.setRune(i, 0, ch); 1121 i += rune_width; 1122 } 1123 self.url_width = i; 1124 self.grid = grid; 1125 } 1126 1127 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1128 _ = allocator; 1129 switch (key) { 1130 .mouse => |mouse| { 1131 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 1132 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 1133 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 1134 }, 1135 else => {}, 1136 } 1137 } 1138 1139 pub fn clearGrid(self: *Footer) void { 1140 if (self.grid) |*grid| { 1141 grid.deinit(); 1142 self.grid = null; 1143 } 1144 } 1145 1146 pub fn getGrid(self: Footer) ?Grid { 1147 return self.grid; 1148 } 1149 1150 pub fn getFocus(self: *Footer) *Focus { 1151 return self.focus; 1152 } 1153 }; 1154 1155 // a single-child wrapper that builds the child at its natural size and 1156 // positions its grid in the middle of the area granted by the parent 1157 // a bordered label, indented past the content it precedes 1158 pub const SectionLabel = struct { 1159 box: wgt.Box(Widget), 1160 1161 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 1162 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1163 errdefer box.deinit(allocator); 1164 1165 { 1166 var gap = try wgt.Text.init(allocator, " "); 1167 errdefer gap.deinit(allocator); 1168 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1169 } 1170 1171 { 1172 var tb = try wgt.TextBox.init(allocator, content, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1173 errdefer tb.deinit(allocator); 1174 tb.getFocus().mode = .all; 1175 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1176 } 1177 1178 box.getFocus().child_id = box.children.keys()[1]; 1179 return .{ .box = box }; 1180 } 1181 1182 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 1183 self.box.deinit(allocator); 1184 } 1185 1186 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1187 try self.box.build(allocator, constraint, root_focus); 1188 } 1189 1190 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1191 _ = self; 1192 _ = allocator; 1193 _ = key; 1194 _ = root_focus; 1195 } 1196 1197 pub fn clearGrid(self: *SectionLabel) void { 1198 self.box.clearGrid(); 1199 } 1200 1201 pub fn getGrid(self: SectionLabel) ?Grid { 1202 return self.box.getGrid(); 1203 } 1204 1205 pub fn getFocus(self: *SectionLabel) *Focus { 1206 return self.box.getFocus(); 1207 } 1208 }; 1209 1210 // a submit button, indented past its form's content. the web form POSTs 1211 // the button's form; terminal hosts detect presses through `buttonId`. 1212 pub const SubmitButton = struct { 1213 box: wgt.Box(Widget), 1214 1215 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 1216 return initLabeled(allocator, "submit"); 1217 } 1218 1219 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 1220 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1221 errdefer box.deinit(allocator); 1222 1223 { 1224 var gap = try wgt.Text.init(allocator, " "); 1225 errdefer gap.deinit(allocator); 1226 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1227 } 1228 1229 { 1230 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1231 errdefer button.deinit(allocator); 1232 button.getFocus().mode = .all; 1233 // the renderer distinguishes plain clickables from buttons that 1234 // should POST to a server route by this kind. 1235 button.getFocus().kind = .{ .custom = "submit" }; 1236 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1237 } 1238 1239 box.getFocus().child_id = box.children.keys()[1]; 1240 return .{ .box = box }; 1241 } 1242 1243 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 1244 self.box.deinit(allocator); 1245 } 1246 1247 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 1248 const button = &self.box.children.values()[1]; 1249 try button.widget.text_box.setContent(allocator, label); 1250 button.min_size = .{ .width = label.len + 2, .height = null }; 1251 } 1252 1253 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1254 try self.box.build(allocator, constraint, root_focus); 1255 } 1256 1257 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1258 _ = self; 1259 _ = allocator; 1260 _ = key; 1261 _ = root_focus; 1262 } 1263 1264 pub fn clearGrid(self: *SubmitButton) void { 1265 self.box.clearGrid(); 1266 } 1267 1268 pub fn getGrid(self: SubmitButton) ?Grid { 1269 return self.box.getGrid(); 1270 } 1271 1272 pub fn getFocus(self: *SubmitButton) *Focus { 1273 return self.box.getFocus(); 1274 } 1275 1276 // the button's focus id, for click hit-testing 1277 pub fn buttonId(self: *const SubmitButton) usize { 1278 return self.box.children.keys()[1]; 1279 } 1280 }; 1281 1282 pub const Center = struct { 1283 focus: *Focus, 1284 grid: ?Grid, 1285 child: *Widget, 1286 1287 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1288 const child = try allocator.create(Widget); 1289 errdefer allocator.destroy(child); 1290 child.* = child_widget; 1291 return .{ 1292 .focus = try Focus.create(allocator, .container), 1293 .grid = null, 1294 .child = child, 1295 }; 1296 } 1297 1298 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1299 self.focus.destroy(allocator); 1300 if (self.grid) |*grid| { 1301 grid.deinit(); 1302 self.grid = null; 1303 } 1304 self.child.deinit(allocator); 1305 allocator.destroy(self.child); 1306 } 1307 1308 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1309 self.clearGrid(); 1310 self.getFocus().clear(); 1311 1312 // build the child without forcing it to fill min_size; it sizes 1313 // itself to its content within the available max. 1314 try self.child.build(allocator, .{ 1315 .min_size = .{ .width = null, .height = null }, 1316 .max_size = constraint.max_size, 1317 }, root_focus); 1318 1319 const child_grid = self.child.getGrid() orelse return; 1320 1321 // prefer max when given; otherwise grow to max(min, child) so that 1322 // when only a min is set (e.g. the wasm path passes viewport rows 1323 // as min_height with no max), we can still vertically center while 1324 // letting taller content extend past the min. 1325 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; 1326 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; 1327 if (width == 0 or height == 0) return; 1328 1329 const offset_x: usize = (width -| child_grid.size.width) / 2; 1330 const offset_y: usize = (height -| child_grid.size.height) / 2; 1331 1332 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1333 errdefer grid.deinit(); 1334 try grid.drawGrid(child_grid, offset_x, offset_y); 1335 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1336 self.getFocus().child_id = self.child.getFocus().id; 1337 1338 self.grid = grid; 1339 } 1340 1341 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1342 try self.child.input(allocator, key, root_focus); 1343 } 1344 1345 pub fn clearGrid(self: *Center) void { 1346 if (self.grid) |*grid| { 1347 grid.deinit(); 1348 self.grid = null; 1349 } 1350 self.child.clearGrid(); 1351 } 1352 1353 pub fn getGrid(self: Center) ?Grid { 1354 return self.grid; 1355 } 1356 1357 pub fn getFocus(self: *Center) *Focus { 1358 return self.focus; 1359 } 1360 }; 1361 1362 // renders truecolor ANSI art 1363 pub const AnsiArt = struct { 1364 focus: *Focus, 1365 grid: ?Grid, 1366 content: []const u8, 1367 1368 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1369 return .{ 1370 .focus = try Focus.create(allocator, .container), 1371 .grid = null, 1372 .content = content, 1373 }; 1374 } 1375 1376 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1377 self.focus.destroy(allocator); 1378 if (self.grid) |*grid| { 1379 grid.deinit(); 1380 self.grid = null; 1381 } 1382 } 1383 1384 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1385 _ = root_focus; 1386 self.clearGrid(); 1387 1388 // parse into rows of cells, then copy into a rectangular grid 1389 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1390 defer { 1391 for (rows.items) |*row| row.deinit(allocator); 1392 rows.deinit(allocator); 1393 } 1394 var row: std.ArrayList(Grid.Cell) = .empty; 1395 errdefer row.deinit(allocator); 1396 1397 var style: Grid.Style = .{}; 1398 var width: usize = 0; 1399 const content = self.content; 1400 var i: usize = 0; 1401 while (i < content.len) { 1402 const byte = content[i]; 1403 if (byte == '\n') { 1404 width = @max(width, row.items.len); 1405 try rows.append(allocator, row); 1406 row = .empty; 1407 i += 1; 1408 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1409 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1410 var j = i + 2; 1411 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1412 if (j >= content.len) { 1413 i = content.len; // malformed trailing escape; stop 1414 } else { 1415 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1416 i = j + 1; 1417 } 1418 } else { 1419 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1420 const end = @min(content.len, i + len); 1421 const rune = content[i..end]; 1422 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1423 // a foreground color does not make a space visible. only an 1424 // effective background makes it opaque; foreground state often 1425 // remains active through the padding at the end of a row. 1426 const effective_background = if (style.inverted) style.fg else style.bg; 1427 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1428 try row.append(allocator, .{ 1429 .rune = if (transparent) null else codepoint, 1430 .style = style, 1431 }); 1432 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1433 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1434 } 1435 i = end; 1436 } 1437 } 1438 // a trailing row with content but no closing newline 1439 if (row.items.len > 0) { 1440 width = @max(width, row.items.len); 1441 try rows.append(allocator, row); 1442 } else { 1443 row.deinit(allocator); 1444 } 1445 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1446 1447 const height = rows.items.len; 1448 if (width == 0 or height == 0) return; 1449 1450 const clamped_w = @min(width, constraint.max_size.width orelse width); 1451 const clamped_h = @min(height, constraint.max_size.height orelse height); 1452 1453 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1454 errdefer grid.deinit(); 1455 for (rows.items[0..clamped_h], 0..) |r, y| { 1456 const n = @min(r.items.len, clamped_w); 1457 for (r.items[0..n], 0..) |cell, x| { 1458 if (cell.continuation) continue; 1459 (try grid.cell(x, y)).style = cell.style; 1460 try grid.setRune(x, y, cell.rune); 1461 } 1462 } 1463 self.grid = grid; 1464 } 1465 1466 pub fn clearGrid(self: *AnsiArt) void { 1467 if (self.grid) |*grid| { 1468 grid.deinit(); 1469 self.grid = null; 1470 } 1471 } 1472 1473 pub fn getGrid(self: AnsiArt) ?Grid { 1474 return self.grid; 1475 } 1476 1477 pub fn getFocus(self: *AnsiArt) *Focus { 1478 return self.focus; 1479 } 1480 1481 fn applySgr(style: *Grid.Style, params: []const u8) void { 1482 var nums: [16]u32 = undefined; 1483 var n: usize = 0; 1484 var it = std.mem.splitScalar(u8, params, ';'); 1485 while (it.next()) |tok| { 1486 if (n >= nums.len) break; 1487 // an empty parameter (e.g. bare "\x1b[m") means 0 1488 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1489 n += 1; 1490 } 1491 if (n == 0) { 1492 style.* = .{}; 1493 return; 1494 } 1495 var i: usize = 0; 1496 while (i < n) : (i += 1) { 1497 switch (nums[i]) { 1498 0 => style.* = .{}, 1499 7 => style.inverted = true, 1500 27 => style.inverted = false, 1501 39 => style.fg = null, 1502 49 => style.bg = null, 1503 38, 48 => { 1504 // truecolor form: 38;2;r;g;b — anything else is ignored 1505 if (i + 4 < n and nums[i + 1] == 2) { 1506 const c = Grid.Color{ .rgb = .{ 1507 .r = @truncate(nums[i + 2]), 1508 .g = @truncate(nums[i + 3]), 1509 .b = @truncate(nums[i + 4]), 1510 } }; 1511 if (nums[i] == 38) style.fg = c else style.bg = c; 1512 i += 4; 1513 } 1514 }, 1515 else => {}, 1516 } 1517 } 1518 } 1519 }; 1520 1521 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1522 pub const AnsiBackground = struct { 1523 grid: ?Grid, 1524 child: *Widget, 1525 art: AnsiArt, 1526 session: *ui.Session, 1527 1528 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1529 var cw = child_widget; 1530 const child = allocator.create(Widget) catch |e| { 1531 cw.deinit(allocator); 1532 return e; 1533 }; 1534 child.* = cw; 1535 errdefer { 1536 child.deinit(allocator); 1537 allocator.destroy(child); 1538 } 1539 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1540 } 1541 1542 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1543 if (self.grid) |*grid| grid.deinit(); 1544 self.art.deinit(allocator); 1545 self.child.deinit(allocator); 1546 allocator.destroy(self.child); 1547 } 1548 1549 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1550 self.clearGrid(); 1551 1552 // make the wrapped page fill the whole available area 1553 try self.child.build(allocator, .{ 1554 .min_size = .{ 1555 .width = constraint.max_size.width orelse constraint.min_size.width, 1556 .height = constraint.max_size.height orelse constraint.min_size.height, 1557 }, 1558 .max_size = constraint.max_size, 1559 }, root_focus); 1560 1561 if (!self.session.data.enable_ansi) return; 1562 const foreground = self.child.getGrid() orelse return; 1563 self.art.content = self.session.data.ansi_art; 1564 try self.buildArt(allocator, foreground.size, root_focus); 1565 if (!self.session.is_terminal) return; 1566 const art_grid = self.art.getGrid() orelse return; 1567 self.grid = try artBehind(allocator, foreground, art_grid); 1568 } 1569 1570 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1571 try self.child.input(allocator, key, root_focus); 1572 } 1573 1574 pub fn clearGrid(self: *AnsiBackground) void { 1575 if (self.grid) |*grid| { 1576 grid.deinit(); 1577 self.grid = null; 1578 } 1579 self.child.clearGrid(); 1580 self.art.clearGrid(); 1581 } 1582 1583 pub fn getGrid(self: AnsiBackground) ?Grid { 1584 return self.grid orelse self.child.getGrid(); 1585 } 1586 1587 pub fn getFocus(self: *AnsiBackground) *Focus { 1588 return self.child.getFocus(); 1589 } 1590 1591 // a foreground cell counts as blank if it's empty or a bare space without styling 1592 fn cellIsBlank(cell: Grid.Cell) bool { 1593 if (cell.continuation) return false; 1594 if (cell.rune) |rune| { 1595 return rune == ' ' and 1596 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1597 } 1598 return true; 1599 } 1600 1601 // the art is dimmed to this fraction of its brightness so foreground text 1602 // stays legible over it 1603 const art_brightness = 35; // percent 1604 1605 // the art parser only emits truecolor, so a palette color never occurs 1606 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1607 const c = color orelse return null; 1608 return switch (c) { 1609 .rgb => |v| v, 1610 else => null, 1611 }; 1612 } 1613 1614 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1615 const v = artRgb(color) orelse return color; 1616 return .{ .rgb = .{ 1617 .r = @intCast(@as(u16, v.r) * art_brightness / 100), 1618 .g = @intCast(@as(u16, v.g) * art_brightness / 100), 1619 .b = @intCast(@as(u16, v.b) * art_brightness / 100), 1620 } }; 1621 } 1622 1623 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1624 try self.art.build(allocator, .{ 1625 .min_size = .{ .width = null, .height = null }, 1626 .max_size = .{ .width = size.width, .height = size.height }, 1627 }, root_focus); 1628 if (self.art.grid) |*grid| { 1629 for (grid.cells) |*cell| { 1630 cell.style.fg = dimColor(cell.style.fg); 1631 cell.style.bg = dimColor(cell.style.bg); 1632 } 1633 } 1634 } 1635 1636 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1637 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1638 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1639 // snap such channels clear of that range so the backdrop doesn't blink or 1640 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1641 // actually render truecolor. 1642 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1643 const v = artRgb(color) orelse return color; 1644 const snap = struct { 1645 fn f(c: u8) u8 { 1646 return if (c >= 1 and c <= 9) 10 else c; 1647 } 1648 }.f; 1649 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1650 } 1651 1652 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1653 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1654 dst.style.bg = background_maybe; 1655 // change unstyled text to contrast with the art behind it 1656 if (dst.style.fg == null) { 1657 if (artRgb(background_maybe)) |v| { 1658 // use near-black and near-white, which is easier on the eyes 1659 // while retaining high contrast 1660 const luminance = (@as(u32, v.r) * 299 + 1661 @as(u32, v.g) * 587 + 1662 @as(u32, v.b) * 114) / 1000; 1663 dst.style.fg = if (luminance >= 128) 1664 .{ .rgb = .{ .r = 16, .g = 16, .b = 16 } } 1665 else 1666 .{ .rgb = .{ .r = 240, .g = 240, .b = 240 } }; 1667 } 1668 } 1669 } 1670 1671 // composites ANSI art behind a foreground grid 1672 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1673 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1674 errdefer out.deinit(); 1675 1676 const anchor_x = foreground.size.width -| art_grid.size.width; 1677 for (0..art_grid.size.height) |y| { 1678 for (0..art_grid.size.width) |x| { 1679 const src = (try art_grid.cell(x, y)).*; 1680 if (src.rune == null) continue; 1681 const dst = try out.cell(anchor_x + x, y); 1682 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1683 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1684 // blank cells take the art; occupied cells take its background 1685 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1686 dst.style = src.style; 1687 dst.style.fg = sgrSafe(dst.style.fg); 1688 dst.style.bg = sgrSafe(dst.style.bg); 1689 try out.setRune(anchor_x + x, y, src.rune); 1690 } else { 1691 applyArtBackground(dst, src); 1692 if (next_dst) |next| applyArtBackground(next, src); 1693 } 1694 } 1695 } 1696 return out; 1697 } 1698 }; 1699 1700 pub const CopyableText = struct { 1701 pub const Choice = struct { 1702 selector: []const u8 = "", 1703 text: []const u8, 1704 copyable_text: ?[]const u8 = null, 1705 label: []const u8 = "", 1706 bottom_label: []const u8 = "", 1707 }; 1708 1709 box: wgt.Box(Widget), 1710 session: *ui.Session, 1711 choices: []Choice, 1712 selected: usize = 0, 1713 1714 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1715 if (choices.len == 0) return error.MissingCopyableText; 1716 1717 const owned_choices = try allocator.dupe(Choice, choices); 1718 errdefer allocator.free(owned_choices); 1719 1720 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1721 errdefer box.deinit(allocator); 1722 1723 if (choices.len > 1) for (choices) |choice| { 1724 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none }); 1725 errdefer selector_box.deinit(allocator); 1726 selector_box.getFocus().mode = .all; 1727 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 } }); 1728 }; 1729 1730 var text_input = try wgt.TextInput.init(allocator, .{ 1731 .border_style = .single, 1732 .round_corners = true, 1733 .label = choices[0].label, 1734 .bottom_label = choices[0].bottom_label, 1735 .read_only = true, 1736 .render_content = session.is_terminal, 1737 .visible_width = null, 1738 }); 1739 errdefer text_input.deinit(allocator); 1740 text_input.getFocus().mode = .all; 1741 try text_input.setContent(allocator, choices[0].text); 1742 box.getFocus().child_id = text_input.getFocus().id; 1743 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1744 1745 return .{ .box = box, .session = session, .choices = owned_choices }; 1746 } 1747 1748 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1749 const aa = session.page_arena.allocator(); 1750 var choices: [2]Choice = undefined; 1751 var count: usize = 0; 1752 switch (location) { 1753 .repo => |identity| { 1754 if (session.data.git_http_port) |port| { 1755 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1756 choices[count] = .{ 1757 .selector = "http", 1758 .text = url, 1759 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1760 .label = " clone ", 1761 }; 1762 count += 1; 1763 } 1764 if (session.data.git_ssh_port) |port| { 1765 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1766 choices[count] = .{ 1767 .selector = "ssh", 1768 .text = url, 1769 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1770 .label = " clone ", 1771 }; 1772 count += 1; 1773 } 1774 }, 1775 .fork => |f| if (session.data.git_ssh_port) |port| { 1776 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1777 choices[count] = .{ 1778 .text = url, 1779 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1780 .label = " clone ", 1781 }; 1782 count += 1; 1783 }, 1784 } 1785 if (count == 0) return null; 1786 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1787 } 1788 1789 pub fn minWidth(self: *const CopyableText) usize { 1790 var width = maxWidth(self.choices) + 2; 1791 if (self.choices.len > 1) { 1792 for (self.choices) |choice| width += choice.selector.len + 2; 1793 } 1794 return width; 1795 } 1796 1797 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1798 self.box.deinit(allocator); 1799 allocator.free(self.choices); 1800 } 1801 1802 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1803 self.clearGrid(); 1804 1805 if (self.choices.len > 1) { 1806 if (root_focus.grandchild_id) |id| { 1807 for (0..self.choices.len) |i| { 1808 if (id != self.selector(i).getFocus().id) continue; 1809 if (i != self.selected) try self.select(allocator, i); 1810 root_focus.setFocus(self.textInput().getFocus().id); 1811 break; 1812 } 1813 } 1814 1815 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1816 for (0..self.choices.len) |i| { 1817 self.selector(i).options.border_style = if (i == self.selected) 1818 (if (focused) .double else .single) 1819 else 1820 .hidden; 1821 } 1822 } 1823 self.textInput().options.border_style = .single; 1824 self.textInput().cursor = 0; 1825 self.textInput().scroll_offset = 0; 1826 try self.box.build(allocator, constraint, root_focus); 1827 1828 if (!self.session.is_terminal) { 1829 const aa = self.session.arena.allocator(); 1830 const id = self.textInput().getFocus().id; 1831 try self.session.text_inputs.put(aa, id, self.textInput()); 1832 } 1833 } 1834 1835 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1836 switch (key) { 1837 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1838 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1839 .enter => if (self.session.is_terminal) self.show(), 1840 .mouse => |mouse| { 1841 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1842 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1843 try self.select(allocator, i); 1844 root_focus.setFocus(self.textInput().getFocus().id); 1845 return; 1846 }; 1847 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1848 }, 1849 else => {}, 1850 } 1851 } 1852 1853 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1854 self.selected = selected; 1855 const choice = self.choices[selected]; 1856 const text_input = self.textInput(); 1857 text_input.options.label = choice.label; 1858 text_input.options.bottom_label = choice.bottom_label; 1859 try text_input.setContent(allocator, choice.text); 1860 } 1861 1862 fn show(self: *CopyableText) void { 1863 const choice = self.choices[self.selected]; 1864 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1865 } 1866 1867 fn textInput(self: *CopyableText) *wgt.TextInput { 1868 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1869 } 1870 1871 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1872 return &self.box.children.values()[index].widget.text_box; 1873 } 1874 1875 fn maxWidth(choices: []const Choice) usize { 1876 var width: usize = 0; 1877 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1878 return width; 1879 } 1880 1881 pub fn clearGrid(self: *CopyableText) void { 1882 self.box.clearGrid(); 1883 } 1884 1885 pub fn getGrid(self: CopyableText) ?Grid { 1886 return self.box.getGrid(); 1887 } 1888 1889 pub fn getFocus(self: *CopyableText) *Focus { 1890 return self.box.getFocus(); 1891 } 1892 }; 1893