╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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