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