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