╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1813 if (cell.rune) |rune| { 1814 return rune == ' ' and 1815 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1816 } 1817 return true; 1818 } 1819 1820 // the art is faded toward the background at this opacity so foreground 1821 // text stays legible over it 1822 pub const art_opacity = 25; // percent 1823 1824 // the art parser only emits truecolor, so a palette color never occurs 1825 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1826 const c = color orelse return null; 1827 return switch (c) { 1828 .rgb => |v| v, 1829 else => null, 1830 }; 1831 } 1832 1833 fn fadeColor(color: ?Grid.Color, backdrop: Grid.Color.Rgb) ?Grid.Color { 1834 const v = artRgb(color) orelse return color; 1835 const blend = struct { 1836 fn f(c: u8, b: u8) u8 { 1837 return @intCast((@as(u16, c) * art_opacity + @as(u16, b) * (100 - art_opacity)) / 100); 1838 } 1839 }.f; 1840 return .{ .rgb = .{ .r = blend(v.r, backdrop.r), .g = blend(v.g, backdrop.g), .b = blend(v.b, backdrop.b) } }; 1841 } 1842 1843 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1844 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1845 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1846 // snap such channels clear of that range so the backdrop doesn't blink or 1847 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1848 // actually render truecolor. 1849 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1850 const v = artRgb(color) orelse return color; 1851 const snap = struct { 1852 fn f(c: u8) u8 { 1853 return if (c >= 1 and c <= 9) 10 else c; 1854 } 1855 }.f; 1856 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1857 } 1858 1859 // composites ANSI art behind a foreground grid 1860 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1861 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1862 errdefer out.deinit(); 1863 1864 const anchor_x = foreground.size.width -| art_grid.size.width; 1865 for (0..art_grid.size.height) |y| { 1866 for (0..art_grid.size.width) |x| { 1867 const src = (try art_grid.cell(x, y)).*; 1868 if (src.rune == null) continue; 1869 const dst = try out.cell(anchor_x + x, y); 1870 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1871 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1872 // blank cells take the art; occupied cells take its background 1873 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1874 dst.style = src.style; 1875 dst.style.fg = sgrSafe(dst.style.fg); 1876 dst.style.bg = sgrSafe(dst.style.bg); 1877 try out.setRune(anchor_x + x, y, src.rune); 1878 } else { 1879 const art_bg = sgrSafe(src.style.bg orelse src.style.fg); 1880 dst.style.bg = art_bg; 1881 if (next_dst) |next| next.style.bg = art_bg; 1882 } 1883 } 1884 } 1885 return out; 1886 } 1887 }; 1888 1889 pub const CopyableText = struct { 1890 pub const Choice = struct { 1891 selector: []const u8 = "", 1892 text: []const u8, 1893 copyable_text: ?[]const u8 = null, 1894 label: []const u8 = "", 1895 bottom_label: []const u8 = "", 1896 }; 1897 1898 box: wgt.Box(Widget), 1899 session: *ui.Session, 1900 choices: []Choice, 1901 selected: usize = 0, 1902 1903 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 1904 if (choices.len == 0) return error.MissingCopyableText; 1905 1906 const owned_choices = try allocator.dupe(Choice, choices); 1907 errdefer allocator.free(owned_choices); 1908 1909 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1910 errdefer box.deinit(allocator); 1911 1912 if (choices.len > 1) for (choices) |choice| { 1913 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 1914 errdefer selector_box.deinit(allocator); 1915 selector_box.getFocus().mode = .all; 1916 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 } }); 1917 }; 1918 1919 var text_input = try wgt.TextInput.init(allocator, .{ 1920 .border = .single, 1921 .round_corners = true, 1922 .top_label = .{ .text = choices[initial].label }, 1923 .bottom_label = .{ .text = choices[initial].bottom_label }, 1924 .read_only = true, 1925 .render_content = session.is_terminal, 1926 .visible_width = null, 1927 }); 1928 errdefer text_input.deinit(allocator); 1929 text_input.getFocus().mode = .all; 1930 try text_input.setContent(allocator, choices[initial].text); 1931 box.getFocus().child_id = text_input.getFocus().id; 1932 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1933 1934 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 1935 } 1936 1937 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 1938 const aa = session.page_arena.allocator(); 1939 var choices: [2]Choice = undefined; 1940 var count: usize = 0; 1941 var ssh_index: ?usize = null; 1942 switch (handle.location) { 1943 .repo => |identity| { 1944 if (session.data.git_http_port) |port| { 1945 const url = try aa.print("http://localhost:{d}/{s}", .{ port, identity }); 1946 choices[count] = .{ 1947 .selector = "http", 1948 .text = url, 1949 .copyable_text = try aa.print("git clone {s}", .{url}), 1950 .label = " clone ", 1951 }; 1952 count += 1; 1953 } 1954 if (session.data.git_ssh_port) |port| { 1955 ssh_index = count; 1956 const url = try aa.print("ssh://localhost:{d}/{s}", .{ port, identity }); 1957 choices[count] = .{ 1958 .selector = "ssh", 1959 .text = url, 1960 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1961 .label = " clone ", 1962 }; 1963 count += 1; 1964 } 1965 }, 1966 .fork => |f| if (session.data.git_ssh_port) |port| { 1967 const url = try aa.print("ssh://localhost:{d}/{s}+{s}", .{ port, f.identity, f.id }); 1968 choices[count] = .{ 1969 .text = url, 1970 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1971 .label = " clone ", 1972 }; 1973 count += 1; 1974 }, 1975 } 1976 if (count == 0) return null; 1977 // a viewer who can push starts on the ssh url 1978 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 1979 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 1980 } 1981 1982 pub fn minWidth(self: *const CopyableText) usize { 1983 var width = maxWidth(self.choices) + 2; 1984 if (self.choices.len > 1) { 1985 for (self.choices) |choice| width += choice.selector.len + 2; 1986 } 1987 return width; 1988 } 1989 1990 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1991 self.box.deinit(allocator); 1992 allocator.free(self.choices); 1993 } 1994 1995 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1996 self.clearGrid(); 1997 1998 if (self.choices.len > 1) { 1999 if (root_focus.grandchild_id) |id| { 2000 for (0..self.choices.len) |i| { 2001 if (id != self.selector(i).getFocus().id) continue; 2002 if (i != self.selected) try self.select(allocator, i); 2003 root_focus.setFocus(self.textInput().getFocus().id); 2004 break; 2005 } 2006 } 2007 2008 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2009 for (0..self.choices.len) |i| { 2010 self.selector(i).options.border = if (i == self.selected) 2011 (if (focused) .double else .single) 2012 else 2013 .hidden; 2014 } 2015 } 2016 self.textInput().options.border = .single; 2017 self.textInput().cursor = 0; 2018 self.textInput().scroll_offset = 0; 2019 try self.box.build(allocator, constraint, root_focus); 2020 2021 if (!self.session.is_terminal) { 2022 const aa = self.session.arena.allocator(); 2023 const id = self.textInput().getFocus().id; 2024 try self.session.text_inputs.put(aa, id, self.textInput()); 2025 } 2026 } 2027 2028 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2029 switch (key) { 2030 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2031 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2032 .enter => if (self.session.is_terminal) self.show(), 2033 .mouse => |mouse| { 2034 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2035 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2036 try self.select(allocator, i); 2037 root_focus.setFocus(self.textInput().getFocus().id); 2038 return; 2039 }; 2040 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2041 }, 2042 else => {}, 2043 } 2044 } 2045 2046 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2047 self.selected = selected; 2048 const choice = self.choices[selected]; 2049 const text_input = self.textInput(); 2050 text_input.options.top_label.text = choice.label; 2051 text_input.options.bottom_label.text = choice.bottom_label; 2052 try text_input.setContent(allocator, choice.text); 2053 } 2054 2055 fn show(self: *CopyableText) void { 2056 const choice = self.choices[self.selected]; 2057 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2058 } 2059 2060 fn textInput(self: *CopyableText) *wgt.TextInput { 2061 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2062 } 2063 2064 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2065 return &self.box.children.values()[index].widget.text_box; 2066 } 2067 2068 fn maxWidth(choices: []const Choice) usize { 2069 var width: usize = 0; 2070 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2071 return width; 2072 } 2073 2074 pub fn clearGrid(self: *CopyableText) void { 2075 self.box.clearGrid(); 2076 } 2077 2078 pub fn getGrid(self: CopyableText) ?Grid { 2079 return self.box.getGrid(); 2080 } 2081 2082 pub fn getFocus(self: *CopyableText) *Focus { 2083 return self.box.getFocus(); 2084 } 2085 }; 2086