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