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