╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1673 while (i < n) : (i += 1) { 1674 switch (nums[i]) { 1675 0 => style.* = .{}, 1676 7 => style.inverted = true, 1677 27 => style.inverted = false, 1678 39 => style.fg = null, 1679 49 => style.bg = null, 1680 38, 48 => { 1681 // truecolor form: 38;2;r;g;b — anything else is ignored 1682 if (i + 4 < n and nums[i + 1] == 2) { 1683 const c = Grid.Color{ .rgb = .{ 1684 .r = @truncate(nums[i + 2]), 1685 .g = @truncate(nums[i + 3]), 1686 .b = @truncate(nums[i + 4]), 1687 } }; 1688 if (nums[i] == 38) style.fg = c else style.bg = c; 1689 i += 4; 1690 } 1691 }, 1692 else => {}, 1693 } 1694 } 1695 } 1696 }; 1697 1698 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1699 pub const AnsiBackground = struct { 1700 grid: ?Grid, 1701 child: *Widget, 1702 art: AnsiArt, 1703 session: *ui.Session, 1704 1705 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1706 var cw = child_widget; 1707 const child = allocator.create(Widget) catch |e| { 1708 cw.deinit(allocator); 1709 return e; 1710 }; 1711 child.* = cw; 1712 errdefer { 1713 child.deinit(allocator); 1714 allocator.destroy(child); 1715 } 1716 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1717 } 1718 1719 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1720 if (self.grid) |*grid| grid.deinit(); 1721 self.art.deinit(allocator); 1722 self.child.deinit(allocator); 1723 allocator.destroy(self.child); 1724 } 1725 1726 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1727 self.clearGrid(); 1728 1729 // make the wrapped page fill the whole available area 1730 try self.child.build(allocator, .{ 1731 .min_size = .{ 1732 .width = constraint.max_size.width orelse constraint.min_size.width, 1733 .height = constraint.max_size.height orelse constraint.min_size.height, 1734 }, 1735 .max_size = constraint.max_size, 1736 }, root_focus); 1737 1738 const foreground = self.child.getGrid() orelse return; 1739 if (!self.session.color) return; 1740 // terminals show the art only once they report a background to fade it toward 1741 const backdrop: ?Grid.Color.Rgb = if (self.session.is_terminal) self.session.terminal_background orelse return else null; 1742 self.art.content = self.session.data.ansi_art; 1743 try self.art.build(allocator, .{ 1744 .min_size = .{ .width = null, .height = null }, 1745 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1746 }, root_focus); 1747 // the web fades the art with css opacity instead 1748 const art_backdrop = backdrop orelse return; 1749 if (self.art.grid) |*art_grid| { 1750 for (art_grid.cells) |*cell| { 1751 cell.style.fg = fadeColor(cell.style.fg, art_backdrop); 1752 cell.style.bg = fadeColor(cell.style.bg, art_backdrop); 1753 } 1754 self.grid = try artBehind(allocator, foreground, art_grid.*); 1755 } 1756 } 1757 1758 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1759 try self.child.input(allocator, key, root_focus); 1760 } 1761 1762 pub fn clearGrid(self: *AnsiBackground) void { 1763 if (self.grid) |*grid| { 1764 grid.deinit(); 1765 self.grid = null; 1766 } 1767 self.child.clearGrid(); 1768 self.art.clearGrid(); 1769 } 1770 1771 pub fn getGrid(self: AnsiBackground) ?Grid { 1772 return self.grid orelse self.child.getGrid(); 1773 } 1774 1775 pub fn getFocus(self: *AnsiBackground) *Focus { 1776 return self.child.getFocus(); 1777 } 1778 1779 // a foreground cell counts as blank if it's empty or a bare space without styling 1780 fn cellIsBlank(cell: Grid.Cell) bool { 1781 if (cell.continuation) return false; 1782 if (cell.rune) |rune| { 1783 return rune == ' ' and 1784 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1785 } 1786 return true; 1787 } 1788 1789 // the art is faded toward the background at this opacity so foreground 1790 // text stays legible over it 1791 pub const art_opacity = 25; // percent 1792 1793 // the art parser only emits truecolor, so a palette color never occurs 1794 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1795 const c = color orelse return null; 1796 return switch (c) { 1797 .rgb => |v| v, 1798 else => null, 1799 }; 1800 } 1801 1802 fn fadeColor(color: ?Grid.Color, backdrop: Grid.Color.Rgb) ?Grid.Color { 1803 const v = artRgb(color) orelse return color; 1804 const blend = struct { 1805 fn f(c: u8, b: u8) u8 { 1806 return @intCast((@as(u16, c) * art_opacity + @as(u16, b) * (100 - art_opacity)) / 100); 1807 } 1808 }.f; 1809 return .{ .rgb = .{ .r = blend(v.r, backdrop.r), .g = blend(v.g, backdrop.g), .b = blend(v.b, backdrop.b) } }; 1810 } 1811 1812 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1813 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1814 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1815 // snap such channels clear of that range so the backdrop doesn't blink or 1816 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1817 // actually render truecolor. 1818 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1819 const v = artRgb(color) orelse return color; 1820 const snap = struct { 1821 fn f(c: u8) u8 { 1822 return if (c >= 1 and c <= 9) 10 else c; 1823 } 1824 }.f; 1825 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1826 } 1827 1828 // composites ANSI art behind a foreground grid 1829 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1830 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1831 errdefer out.deinit(); 1832 1833 const anchor_x = foreground.size.width -| art_grid.size.width; 1834 for (0..art_grid.size.height) |y| { 1835 for (0..art_grid.size.width) |x| { 1836 const src = (try art_grid.cell(x, y)).*; 1837 if (src.rune == null) continue; 1838 const dst = try out.cell(anchor_x + x, y); 1839 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1840 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1841 // blank cells take the art; occupied cells take its background 1842 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1843 dst.style = src.style; 1844 dst.style.fg = sgrSafe(dst.style.fg); 1845 dst.style.bg = sgrSafe(dst.style.bg); 1846 try out.setRune(anchor_x + x, y, src.rune); 1847 } else { 1848 const art_bg = sgrSafe(src.style.bg orelse src.style.fg); 1849 dst.style.bg = art_bg; 1850 if (next_dst) |next| next.style.bg = art_bg; 1851 } 1852 } 1853 } 1854 return out; 1855 } 1856 }; 1857 1858 pub const CopyableText = struct { 1859 pub const Choice = struct { 1860 selector: []const u8 = "", 1861 text: []const u8, 1862 copyable_text: ?[]const u8 = null, 1863 label: []const u8 = "", 1864 bottom_label: []const u8 = "", 1865 }; 1866 1867 box: wgt.Box(Widget), 1868 session: *ui.Session, 1869 choices: []Choice, 1870 selected: usize = 0, 1871 1872 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 1873 if (choices.len == 0) return error.MissingCopyableText; 1874 1875 const owned_choices = try allocator.dupe(Choice, choices); 1876 errdefer allocator.free(owned_choices); 1877 1878 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1879 errdefer box.deinit(allocator); 1880 1881 if (choices.len > 1) for (choices) |choice| { 1882 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none }); 1883 errdefer selector_box.deinit(allocator); 1884 selector_box.getFocus().mode = .all; 1885 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 } }); 1886 }; 1887 1888 var text_input = try wgt.TextInput.init(allocator, .{ 1889 .border_style = .single, 1890 .round_corners = true, 1891 .label = choices[initial].label, 1892 .bottom_label = choices[initial].bottom_label, 1893 .read_only = true, 1894 .render_content = session.is_terminal, 1895 .visible_width = null, 1896 }); 1897 errdefer text_input.deinit(allocator); 1898 text_input.getFocus().mode = .all; 1899 try text_input.setContent(allocator, choices[initial].text); 1900 box.getFocus().child_id = text_input.getFocus().id; 1901 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1902 1903 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 1904 } 1905 1906 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 1907 const aa = session.page_arena.allocator(); 1908 var choices: [2]Choice = undefined; 1909 var count: usize = 0; 1910 var ssh_index: ?usize = null; 1911 switch (handle.location) { 1912 .repo => |identity| { 1913 if (session.data.git_http_port) |port| { 1914 const url = try aa.print("http://localhost:{d}/{s}", .{ port, identity }); 1915 choices[count] = .{ 1916 .selector = "http", 1917 .text = url, 1918 .copyable_text = try aa.print("git clone {s}", .{url}), 1919 .label = " clone ", 1920 }; 1921 count += 1; 1922 } 1923 if (session.data.git_ssh_port) |port| { 1924 ssh_index = count; 1925 const url = try aa.print("ssh://localhost:{d}/{s}", .{ port, identity }); 1926 choices[count] = .{ 1927 .selector = "ssh", 1928 .text = url, 1929 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1930 .label = " clone ", 1931 }; 1932 count += 1; 1933 } 1934 }, 1935 .fork => |f| if (session.data.git_ssh_port) |port| { 1936 const url = try aa.print("ssh://localhost:{d}/{s}+{s}", .{ port, f.identity, f.id }); 1937 choices[count] = .{ 1938 .text = url, 1939 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1940 .label = " clone ", 1941 }; 1942 count += 1; 1943 }, 1944 } 1945 if (count == 0) return null; 1946 // a viewer who can push starts on the ssh url 1947 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 1948 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 1949 } 1950 1951 pub fn minWidth(self: *const CopyableText) usize { 1952 var width = maxWidth(self.choices) + 2; 1953 if (self.choices.len > 1) { 1954 for (self.choices) |choice| width += choice.selector.len + 2; 1955 } 1956 return width; 1957 } 1958 1959 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1960 self.box.deinit(allocator); 1961 allocator.free(self.choices); 1962 } 1963 1964 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1965 self.clearGrid(); 1966 1967 if (self.choices.len > 1) { 1968 if (root_focus.grandchild_id) |id| { 1969 for (0..self.choices.len) |i| { 1970 if (id != self.selector(i).getFocus().id) continue; 1971 if (i != self.selected) try self.select(allocator, i); 1972 root_focus.setFocus(self.textInput().getFocus().id); 1973 break; 1974 } 1975 } 1976 1977 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1978 for (0..self.choices.len) |i| { 1979 self.selector(i).options.border_style = if (i == self.selected) 1980 (if (focused) .double else .single) 1981 else 1982 .hidden; 1983 } 1984 } 1985 self.textInput().options.border_style = .single; 1986 self.textInput().cursor = 0; 1987 self.textInput().scroll_offset = 0; 1988 try self.box.build(allocator, constraint, root_focus); 1989 1990 if (!self.session.is_terminal) { 1991 const aa = self.session.arena.allocator(); 1992 const id = self.textInput().getFocus().id; 1993 try self.session.text_inputs.put(aa, id, self.textInput()); 1994 } 1995 } 1996 1997 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1998 switch (key) { 1999 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2000 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2001 .enter => if (self.session.is_terminal) self.show(), 2002 .mouse => |mouse| { 2003 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2004 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2005 try self.select(allocator, i); 2006 root_focus.setFocus(self.textInput().getFocus().id); 2007 return; 2008 }; 2009 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2010 }, 2011 else => {}, 2012 } 2013 } 2014 2015 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2016 self.selected = selected; 2017 const choice = self.choices[selected]; 2018 const text_input = self.textInput(); 2019 text_input.options.label = choice.label; 2020 text_input.options.bottom_label = choice.bottom_label; 2021 try text_input.setContent(allocator, choice.text); 2022 } 2023 2024 fn show(self: *CopyableText) void { 2025 const choice = self.choices[self.selected]; 2026 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2027 } 2028 2029 fn textInput(self: *CopyableText) *wgt.TextInput { 2030 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2031 } 2032 2033 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2034 return &self.box.children.values()[index].widget.text_box; 2035 } 2036 2037 fn maxWidth(choices: []const Choice) usize { 2038 var width: usize = 0; 2039 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2040 return width; 2041 } 2042 2043 pub fn clearGrid(self: *CopyableText) void { 2044 self.box.clearGrid(); 2045 } 2046 2047 pub fn getGrid(self: CopyableText) ?Grid { 2048 return self.box.getGrid(); 2049 } 2050 2051 pub fn getFocus(self: *CopyableText) *Focus { 2052 return self.box.getFocus(); 2053 } 2054 }; 2055