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