╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1676 for (rows.items) |*row| row.deinit(allocator); 1677 rows.deinit(allocator); 1678 } 1679 var row: std.ArrayList(Grid.Cell) = .empty; 1680 errdefer row.deinit(allocator); 1681 1682 var style: Grid.Style = .{}; 1683 var width: usize = 0; 1684 const content = self.content; 1685 var i: usize = 0; 1686 while (i < content.len) { 1687 const byte = content[i]; 1688 if (byte == '\n') { 1689 width = @max(width, row.items.len); 1690 try rows.append(allocator, row); 1691 row = .empty; 1692 i += 1; 1693 } else if (byte == '\r') { 1694 // git may checkout the ansi art with crlf line endings on windows 1695 i += 1; 1696 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1697 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1698 var j = i + 2; 1699 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1700 if (j >= content.len) { 1701 i = content.len; // malformed trailing escape; stop 1702 } else { 1703 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1704 i = j + 1; 1705 } 1706 } else { 1707 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1708 const end = @min(content.len, i + len); 1709 const rune = content[i..end]; 1710 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1711 // a foreground color does not make a space visible. only an 1712 // effective background makes it opaque; foreground state often 1713 // remains active through the padding at the end of a row. 1714 const effective_background = if (style.inverted) style.fg else style.bg; 1715 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1716 try row.append(allocator, .{ 1717 .rune = if (transparent) null else codepoint, 1718 .style = style, 1719 }); 1720 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1721 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1722 } 1723 i = end; 1724 } 1725 } 1726 // a trailing row with content but no closing newline 1727 if (row.items.len > 0) { 1728 width = @max(width, row.items.len); 1729 try rows.append(allocator, row); 1730 } else { 1731 row.deinit(allocator); 1732 } 1733 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1734 1735 const height = rows.items.len; 1736 if (width == 0 or height == 0) return; 1737 1738 const clamped_w = @min(width, constraint.max_size.width orelse width); 1739 const clamped_h = @min(height, constraint.max_size.height orelse height); 1740 1741 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1742 errdefer grid.deinit(); 1743 for (rows.items[0..clamped_h], 0..) |r, y| { 1744 const n = @min(r.items.len, clamped_w); 1745 for (r.items[0..n], 0..) |cell, x| { 1746 if (cell.continuation) continue; 1747 (try grid.cell(x, y)).style = cell.style; 1748 try grid.setRune(x, y, cell.rune); 1749 } 1750 } 1751 self.grid = grid; 1752 } 1753 1754 pub fn clearGrid(self: *AnsiArt) void { 1755 if (self.grid) |*grid| { 1756 grid.deinit(); 1757 self.grid = null; 1758 } 1759 } 1760 1761 pub fn getGrid(self: AnsiArt) ?Grid { 1762 return self.grid; 1763 } 1764 1765 pub fn getFocus(self: *AnsiArt) *Focus { 1766 return self.focus; 1767 } 1768 1769 fn applySgr(style: *Grid.Style, params: []const u8) void { 1770 var nums: [16]u32 = undefined; 1771 var n: usize = 0; 1772 var it = std.mem.splitScalar(u8, params, ';'); 1773 while (it.next()) |tok| { 1774 if (n >= nums.len) break; 1775 // an empty parameter (e.g. bare "\x1b[m") means 0 1776 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1777 n += 1; 1778 } 1779 if (n == 0) { 1780 style.* = .{}; 1781 return; 1782 } 1783 var i: usize = 0; 1784 while (i < n) : (i += 1) { 1785 switch (nums[i]) { 1786 0 => style.* = .{}, 1787 7 => style.inverted = true, 1788 27 => style.inverted = false, 1789 39 => style.fg = null, 1790 49 => style.bg = null, 1791 38, 48 => { 1792 // truecolor form: 38;2;r;g;b — anything else is ignored 1793 if (i + 4 < n and nums[i + 1] == 2) { 1794 const c = Grid.Color{ .rgb = .{ 1795 .r = @truncate(nums[i + 2]), 1796 .g = @truncate(nums[i + 3]), 1797 .b = @truncate(nums[i + 4]), 1798 } }; 1799 if (nums[i] == 38) style.fg = c else style.bg = c; 1800 i += 4; 1801 } 1802 }, 1803 else => {}, 1804 } 1805 } 1806 } 1807 }; 1808 1809 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1810 pub const AnsiBackground = struct { 1811 grid: ?Grid, 1812 child: *Widget, 1813 art: AnsiArt, 1814 session: *ui.Session, 1815 1816 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1817 var cw = child_widget; 1818 const child = allocator.create(Widget) catch |e| { 1819 cw.deinit(allocator); 1820 return e; 1821 }; 1822 child.* = cw; 1823 errdefer { 1824 child.deinit(allocator); 1825 allocator.destroy(child); 1826 } 1827 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1828 } 1829 1830 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1831 if (self.grid) |*grid| grid.deinit(); 1832 self.art.deinit(allocator); 1833 self.child.deinit(allocator); 1834 allocator.destroy(self.child); 1835 } 1836 1837 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1838 self.clearGrid(); 1839 1840 // make the wrapped page fill the whole available area 1841 try self.child.build(allocator, .{ 1842 .min_size = .{ 1843 .width = constraint.max_size.width orelse constraint.min_size.width, 1844 .height = constraint.max_size.height orelse constraint.min_size.height, 1845 }, 1846 .max_size = constraint.max_size, 1847 }, root_focus); 1848 1849 const foreground = self.child.getGrid() orelse return; 1850 if (!self.session.color) return; 1851 // terminals show the art only once they report a background to fade it toward 1852 const backdrop: ?Grid.Color.Rgb = if (self.session.is_terminal) self.session.terminal_background orelse return else null; 1853 self.art.content = self.session.data.ansi_art; 1854 try self.art.build(allocator, .{ 1855 .min_size = .{ .width = null, .height = null }, 1856 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1857 }, root_focus); 1858 // the web fades the art with css opacity instead 1859 const art_backdrop = backdrop orelse return; 1860 if (self.art.grid) |*art_grid| { 1861 for (art_grid.cells) |*cell| { 1862 cell.style.fg = fadeColor(cell.style.fg, art_backdrop); 1863 cell.style.bg = fadeColor(cell.style.bg, art_backdrop); 1864 } 1865 self.grid = try artBehind(allocator, foreground, art_grid.*); 1866 } 1867 } 1868 1869 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1870 try self.child.input(allocator, key, root_focus); 1871 } 1872 1873 pub fn clearGrid(self: *AnsiBackground) void { 1874 if (self.grid) |*grid| { 1875 grid.deinit(); 1876 self.grid = null; 1877 } 1878 self.child.clearGrid(); 1879 self.art.clearGrid(); 1880 } 1881 1882 pub fn getGrid(self: AnsiBackground) ?Grid { 1883 return self.grid orelse self.child.getGrid(); 1884 } 1885 1886 pub fn getFocus(self: *AnsiBackground) *Focus { 1887 return self.child.getFocus(); 1888 } 1889 1890 // a foreground cell counts as blank if it's empty or a bare space without styling 1891 fn cellIsBlank(cell: Grid.Cell) bool { 1892 if (cell.continuation) return false; 1893 if (cell.rune) |rune| { 1894 return rune == ' ' and 1895 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1896 } 1897 return true; 1898 } 1899 1900 // the art is faded toward the background at this opacity so foreground 1901 // text stays legible over it 1902 pub const art_opacity = 25; // percent 1903 1904 // the art parser only emits truecolor, so a palette color never occurs 1905 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1906 const c = color orelse return null; 1907 return switch (c) { 1908 .rgb => |v| v, 1909 else => null, 1910 }; 1911 } 1912 1913 fn fadeColor(color: ?Grid.Color, backdrop: Grid.Color.Rgb) ?Grid.Color { 1914 const v = artRgb(color) orelse return color; 1915 const blend = struct { 1916 fn f(c: u8, b: u8) u8 { 1917 return @intCast((@as(u16, c) * art_opacity + @as(u16, b) * (100 - art_opacity)) / 100); 1918 } 1919 }.f; 1920 return .{ .rgb = .{ .r = blend(v.r, backdrop.r), .g = blend(v.g, backdrop.g), .b = blend(v.b, backdrop.b) } }; 1921 } 1922 1923 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1924 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1925 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1926 // snap such channels clear of that range so the backdrop doesn't blink or 1927 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1928 // actually render truecolor. 1929 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1930 const v = artRgb(color) orelse return color; 1931 const snap = struct { 1932 fn f(c: u8) u8 { 1933 return if (c >= 1 and c <= 9) 10 else c; 1934 } 1935 }.f; 1936 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1937 } 1938 1939 // composites ANSI art behind a foreground grid 1940 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1941 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1942 errdefer out.deinit(); 1943 1944 const anchor_x = foreground.size.width -| art_grid.size.width; 1945 for (0..art_grid.size.height) |y| { 1946 for (0..art_grid.size.width) |x| { 1947 const src = (try art_grid.cell(x, y)).*; 1948 if (src.rune == null) continue; 1949 const dst = try out.cell(anchor_x + x, y); 1950 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1951 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1952 // blank cells take the art; occupied cells take its background 1953 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1954 dst.style = src.style; 1955 dst.style.fg = sgrSafe(dst.style.fg); 1956 dst.style.bg = sgrSafe(dst.style.bg); 1957 try out.setRune(anchor_x + x, y, src.rune); 1958 } else { 1959 const art_bg = sgrSafe(src.style.bg orelse src.style.fg); 1960 dst.style.bg = art_bg; 1961 if (next_dst) |next| next.style.bg = art_bg; 1962 } 1963 } 1964 } 1965 return out; 1966 } 1967 }; 1968 1969 pub const CopyableText = struct { 1970 pub const Choice = struct { 1971 selector: []const u8 = "", 1972 text: []const u8, 1973 copyable_text: ?[]const u8 = null, 1974 label: []const u8 = "", 1975 bottom_label: []const u8 = "", 1976 }; 1977 1978 box: wgt.Box(Widget), 1979 session: *ui.Session, 1980 choices: []Choice, 1981 selected: usize = 0, 1982 1983 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 1984 if (choices.len == 0) return error.MissingCopyableText; 1985 1986 const owned_choices = try allocator.dupe(Choice, choices); 1987 errdefer allocator.free(owned_choices); 1988 1989 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1990 errdefer box.deinit(allocator); 1991 1992 if (choices.len > 1) for (choices) |choice| { 1993 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 1994 errdefer selector_box.deinit(allocator); 1995 selector_box.getFocus().mode = .all; 1996 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 } }); 1997 }; 1998 1999 var text_input = try wgt.TextInput.init(allocator, .{ 2000 .border = .single, 2001 .round_corners = true, 2002 .top_label = .{ .text = choices[initial].label }, 2003 .bottom_label = .{ .text = choices[initial].bottom_label }, 2004 .read_only = true, 2005 .render_content = session.is_terminal, 2006 .visible_width = null, 2007 }); 2008 errdefer text_input.deinit(allocator); 2009 text_input.getFocus().mode = .all; 2010 try text_input.setContent(allocator, choices[initial].text); 2011 box.getFocus().child_id = text_input.getFocus().id; 2012 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 2013 2014 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 2015 } 2016 2017 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 2018 const aa = session.page_arena.allocator(); 2019 var choices: [2]Choice = undefined; 2020 var count: usize = 0; 2021 var ssh_index: ?usize = null; 2022 switch (handle.location) { 2023 .repo => |identity| { 2024 if (session.data.git_http_port) |port| { 2025 const url = try aa.print("http://localhost:{d}/{s}", .{ port, identity }); 2026 choices[count] = .{ 2027 .selector = "http", 2028 .text = url, 2029 .copyable_text = try aa.print("git clone {s}", .{url}), 2030 .label = " clone ", 2031 }; 2032 count += 1; 2033 } 2034 if (session.data.git_ssh_port) |port| { 2035 ssh_index = count; 2036 const url = try aa.print("ssh://localhost:{d}/{s}", .{ port, identity }); 2037 choices[count] = .{ 2038 .selector = "ssh", 2039 .text = url, 2040 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2041 .label = " clone ", 2042 }; 2043 count += 1; 2044 } 2045 }, 2046 .fork => |f| if (session.data.git_ssh_port) |port| { 2047 const url = try aa.print("ssh://localhost:{d}/{s}+{s}", .{ port, f.identity, f.id }); 2048 choices[count] = .{ 2049 .text = url, 2050 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2051 .label = " clone ", 2052 }; 2053 count += 1; 2054 }, 2055 } 2056 if (count == 0) return null; 2057 // a viewer who can push starts on the ssh url 2058 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 2059 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 2060 } 2061 2062 pub fn minWidth(self: *const CopyableText) usize { 2063 var width = maxWidth(self.choices) + 2; 2064 if (self.choices.len > 1) { 2065 for (self.choices) |choice| width += choice.selector.len + 2; 2066 } 2067 return width; 2068 } 2069 2070 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 2071 self.box.deinit(allocator); 2072 allocator.free(self.choices); 2073 } 2074 2075 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2076 self.clearGrid(); 2077 2078 if (self.choices.len > 1) { 2079 if (root_focus.grandchild_id) |id| { 2080 for (0..self.choices.len) |i| { 2081 if (id != self.selector(i).getFocus().id) continue; 2082 if (i != self.selected) try self.select(allocator, i); 2083 root_focus.setFocus(self.textInput().getFocus().id); 2084 break; 2085 } 2086 } 2087 2088 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2089 for (0..self.choices.len) |i| { 2090 self.selector(i).options.border = if (i == self.selected) 2091 (if (focused) .double else .single) 2092 else 2093 .hidden; 2094 } 2095 } 2096 self.textInput().options.border = .single; 2097 self.textInput().cursor = 0; 2098 self.textInput().scroll_offset = 0; 2099 try self.box.build(allocator, constraint, root_focus); 2100 2101 if (!self.session.is_terminal) { 2102 const aa = self.session.arena.allocator(); 2103 const id = self.textInput().getFocus().id; 2104 try self.session.text_inputs.put(aa, id, self.textInput()); 2105 } 2106 } 2107 2108 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2109 switch (key) { 2110 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2111 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2112 .enter => if (self.session.is_terminal) self.show(), 2113 .mouse => |mouse| { 2114 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2115 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2116 try self.select(allocator, i); 2117 root_focus.setFocus(self.textInput().getFocus().id); 2118 return; 2119 }; 2120 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2121 }, 2122 else => {}, 2123 } 2124 } 2125 2126 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2127 self.selected = selected; 2128 const choice = self.choices[selected]; 2129 const text_input = self.textInput(); 2130 text_input.options.top_label.text = choice.label; 2131 text_input.options.bottom_label.text = choice.bottom_label; 2132 try text_input.setContent(allocator, choice.text); 2133 } 2134 2135 fn show(self: *CopyableText) void { 2136 const choice = self.choices[self.selected]; 2137 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2138 } 2139 2140 fn textInput(self: *CopyableText) *wgt.TextInput { 2141 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2142 } 2143 2144 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2145 return &self.box.children.values()[index].widget.text_box; 2146 } 2147 2148 fn maxWidth(choices: []const Choice) usize { 2149 var width: usize = 0; 2150 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2151 return width; 2152 } 2153 2154 pub fn clearGrid(self: *CopyableText) void { 2155 self.box.clearGrid(); 2156 } 2157 2158 pub fn getGrid(self: CopyableText) ?Grid { 2159 return self.box.getGrid(); 2160 } 2161 2162 pub fn getFocus(self: *CopyableText) *Focus { 2163 return self.box.getFocus(); 2164 } 2165 }; 2166