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