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