╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1845 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1846 1847 const height = rows.items.len; 1848 if (width == 0 or height == 0) return; 1849 1850 const clamped_w = @min(width, constraint.max_size.width orelse width); 1851 const clamped_h = @min(height, constraint.max_size.height orelse height); 1852 1853 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1854 errdefer grid.deinit(); 1855 for (rows.items[0..clamped_h], 0..) |r, y| { 1856 const n = @min(r.items.len, clamped_w); 1857 for (r.items[0..n], 0..) |cell, x| { 1858 if (cell.continuation) continue; 1859 (try grid.cell(x, y)).style = cell.style; 1860 try grid.setRune(x, y, cell.rune); 1861 } 1862 } 1863 self.grid = grid; 1864 } 1865 1866 pub fn clearGrid(self: *AnsiArt) void { 1867 if (self.grid) |*grid| { 1868 grid.deinit(); 1869 self.grid = null; 1870 } 1871 } 1872 1873 pub fn getGrid(self: AnsiArt) ?Grid { 1874 return self.grid; 1875 } 1876 1877 pub fn getFocus(self: *AnsiArt) *Focus { 1878 return self.focus; 1879 } 1880 1881 fn applySgr(style: *Grid.Style, params: []const u8) void { 1882 var nums: [16]u32 = undefined; 1883 var n: usize = 0; 1884 var it = std.mem.splitScalar(u8, params, ';'); 1885 while (it.next()) |tok| { 1886 if (n >= nums.len) break; 1887 // an empty parameter (e.g. bare "\x1b[m") means 0 1888 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1889 n += 1; 1890 } 1891 if (n == 0) { 1892 style.* = .{}; 1893 return; 1894 } 1895 var i: usize = 0; 1896 while (i < n) : (i += 1) { 1897 switch (nums[i]) { 1898 0 => style.* = .{}, 1899 7 => style.inverted = true, 1900 27 => style.inverted = false, 1901 39 => style.fg = null, 1902 49 => style.bg = null, 1903 38, 48 => { 1904 // truecolor form: 38;2;r;g;b — anything else is ignored 1905 if (i + 4 < n and nums[i + 1] == 2) { 1906 const c = Grid.Color{ .rgb = .{ 1907 .r = @truncate(nums[i + 2]), 1908 .g = @truncate(nums[i + 3]), 1909 .b = @truncate(nums[i + 4]), 1910 } }; 1911 if (nums[i] == 38) style.fg = c else style.bg = c; 1912 i += 4; 1913 } 1914 }, 1915 else => {}, 1916 } 1917 } 1918 } 1919 }; 1920 1921 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1922 pub const AnsiBackground = struct { 1923 grid: ?Grid, 1924 child: *Widget, 1925 art: AnsiArt, 1926 session: *ui.Session, 1927 1928 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1929 var cw = child_widget; 1930 const child = allocator.create(Widget) catch |e| { 1931 cw.deinit(allocator); 1932 return e; 1933 }; 1934 child.* = cw; 1935 errdefer { 1936 child.deinit(allocator); 1937 allocator.destroy(child); 1938 } 1939 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1940 } 1941 1942 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1943 if (self.grid) |*grid| grid.deinit(); 1944 self.art.deinit(allocator); 1945 self.child.deinit(allocator); 1946 allocator.destroy(self.child); 1947 } 1948 1949 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1950 self.clearGrid(); 1951 1952 // make the wrapped page fill the whole available area 1953 try self.child.build(allocator, .{ 1954 .min_size = .{ 1955 .width = constraint.max_size.width orelse constraint.min_size.width, 1956 .height = constraint.max_size.height orelse constraint.min_size.height, 1957 }, 1958 .max_size = constraint.max_size, 1959 }, root_focus); 1960 1961 const foreground = self.child.getGrid() orelse return; 1962 if (!self.session.color) return; 1963 // terminals show the art only once they report a background to fade it toward 1964 const backdrop: ?Grid.Color.Rgb = if (self.session.is_terminal) self.session.terminal_background orelse return else null; 1965 self.art.content = self.session.data.ansi_art; 1966 try self.art.build(allocator, .{ 1967 .min_size = .{ .width = null, .height = null }, 1968 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1969 }, root_focus); 1970 // the web fades the art with css opacity instead 1971 const art_backdrop = backdrop orelse return; 1972 if (self.art.grid) |*art_grid| { 1973 for (art_grid.cells) |*cell| { 1974 cell.style.fg = fadeColor(cell.style.fg, art_backdrop); 1975 cell.style.bg = fadeColor(cell.style.bg, art_backdrop); 1976 } 1977 self.grid = try artBehind(allocator, foreground, art_grid.*); 1978 } 1979 } 1980 1981 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1982 try self.child.input(allocator, key, root_focus); 1983 } 1984 1985 pub fn clearGrid(self: *AnsiBackground) void { 1986 if (self.grid) |*grid| { 1987 grid.deinit(); 1988 self.grid = null; 1989 } 1990 self.child.clearGrid(); 1991 self.art.clearGrid(); 1992 } 1993 1994 pub fn getGrid(self: AnsiBackground) ?Grid { 1995 return self.grid orelse self.child.getGrid(); 1996 } 1997 1998 pub fn getFocus(self: *AnsiBackground) *Focus { 1999 return self.child.getFocus(); 2000 } 2001 2002 // a foreground cell counts as blank if it's empty or a bare space without styling 2003 fn cellIsBlank(cell: Grid.Cell) bool { 2004 if (cell.continuation) return false; 2005 if (cell.rune) |rune| { 2006 return rune == ' ' and 2007 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2008 } 2009 return true; 2010 } 2011 2012 // the art is faded toward the background at this opacity so foreground 2013 // text stays legible over it 2014 pub const art_opacity = 25; // percent 2015 2016 // the art parser only emits truecolor, so a palette color never occurs 2017 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 2018 const c = color orelse return null; 2019 return switch (c) { 2020 .rgb => |v| v, 2021 else => null, 2022 }; 2023 } 2024 2025 fn fadeColor(color: ?Grid.Color, backdrop: Grid.Color.Rgb) ?Grid.Color { 2026 const v = artRgb(color) orelse return color; 2027 const blend = struct { 2028 fn f(c: u8, b: u8) u8 { 2029 return @intCast((@as(u16, c) * art_opacity + @as(u16, b) * (100 - art_opacity)) / 100); 2030 } 2031 }.f; 2032 return .{ .rgb = .{ .r = blend(v.r, backdrop.r), .g = blend(v.g, backdrop.g), .b = blend(v.b, backdrop.b) } }; 2033 } 2034 2035 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2036 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2037 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2038 // snap such channels clear of that range so the backdrop doesn't blink or 2039 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2040 // actually render truecolor. 2041 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2042 const v = artRgb(color) orelse return color; 2043 const snap = struct { 2044 fn f(c: u8) u8 { 2045 return if (c >= 1 and c <= 9) 10 else c; 2046 } 2047 }.f; 2048 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 2049 } 2050 2051 // composites ANSI art behind a foreground grid 2052 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 2053 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2054 errdefer out.deinit(); 2055 2056 const anchor_x = foreground.size.width -| art_grid.size.width; 2057 for (0..art_grid.size.height) |y| { 2058 for (0..art_grid.size.width) |x| { 2059 const src = (try art_grid.cell(x, y)).*; 2060 if (src.rune == null) continue; 2061 const dst = try out.cell(anchor_x + x, y); 2062 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 2063 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 2064 // blank cells take the art; occupied cells take its background 2065 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 2066 dst.style = src.style; 2067 dst.style.fg = sgrSafe(dst.style.fg); 2068 dst.style.bg = sgrSafe(dst.style.bg); 2069 try out.setRune(anchor_x + x, y, src.rune); 2070 } else { 2071 const art_bg = sgrSafe(src.style.bg orelse src.style.fg); 2072 dst.style.bg = art_bg; 2073 if (next_dst) |next| next.style.bg = art_bg; 2074 } 2075 } 2076 } 2077 return out; 2078 } 2079 }; 2080 2081 pub const CopyableText = struct { 2082 pub const Choice = struct { 2083 selector: []const u8 = "", 2084 text: []const u8, 2085 copyable_text: ?[]const u8 = null, 2086 label: []const u8 = "", 2087 bottom_label: []const u8 = "", 2088 }; 2089 2090 box: wgt.Box(Widget), 2091 session: *ui.Session, 2092 choices: []Choice, 2093 selected: usize = 0, 2094 2095 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 2096 if (choices.len == 0) return error.MissingCopyableText; 2097 2098 const owned_choices = try allocator.dupe(Choice, choices); 2099 errdefer allocator.free(owned_choices); 2100 2101 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2102 errdefer box.deinit(allocator); 2103 2104 if (choices.len > 1) for (choices) |choice| { 2105 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 2106 errdefer selector_box.deinit(allocator); 2107 selector_box.getFocus().mode = .all; 2108 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 } }); 2109 }; 2110 2111 var text_input = try wgt.TextInput.init(allocator, .{ 2112 .border = .single, 2113 .round_corners = true, 2114 .top_label = .{ .text = choices[initial].label }, 2115 .bottom_label = .{ .text = choices[initial].bottom_label }, 2116 .read_only = true, 2117 .render_content = session.is_terminal, 2118 .visible_width = null, 2119 }); 2120 errdefer text_input.deinit(allocator); 2121 text_input.getFocus().mode = .all; 2122 try text_input.setContent(allocator, choices[initial].text); 2123 box.getFocus().child_id = text_input.getFocus().id; 2124 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 2125 2126 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 2127 } 2128 2129 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 2130 const aa = session.page_arena.allocator(); 2131 var choices: [2]Choice = undefined; 2132 var count: usize = 0; 2133 var ssh_index: ?usize = null; 2134 switch (handle.location) { 2135 .repo => |identity| { 2136 const http_url = if (handle.read_access == .public) session.data.git_http_url else null; 2137 if (http_url) |base| { 2138 const url = try aa.print("{s}/{s}", .{ base, identity }); 2139 choices[count] = .{ 2140 .selector = "http", 2141 .text = url, 2142 .copyable_text = try aa.print("git clone {s}", .{url}), 2143 .label = " clone ", 2144 }; 2145 count += 1; 2146 } 2147 if (session.data.git_ssh_url) |ssh_url| { 2148 ssh_index = count; 2149 const url = try aa.print("{s}/{s}", .{ ssh_url, identity }); 2150 choices[count] = .{ 2151 .selector = "ssh", 2152 .text = url, 2153 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2154 .label = " clone ", 2155 }; 2156 count += 1; 2157 } 2158 }, 2159 .fork => |f| if (session.data.git_ssh_url) |ssh_url| { 2160 const url = try aa.print("{s}/{s}+{s}", .{ ssh_url, f.identity, f.id }); 2161 choices[count] = .{ 2162 .text = url, 2163 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2164 .label = " clone ", 2165 }; 2166 count += 1; 2167 }, 2168 } 2169 if (count == 0) return null; 2170 // a viewer who can push starts on the ssh url 2171 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 2172 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 2173 } 2174 2175 pub fn minWidth(self: *const CopyableText) usize { 2176 var width = maxWidth(self.choices) + 2; 2177 if (self.choices.len > 1) { 2178 for (self.choices) |choice| width += choice.selector.len + 2; 2179 } 2180 return width; 2181 } 2182 2183 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 2184 self.box.deinit(allocator); 2185 allocator.free(self.choices); 2186 } 2187 2188 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2189 self.clearGrid(); 2190 2191 if (self.choices.len > 1) { 2192 if (root_focus.grandchild_id) |id| { 2193 for (0..self.choices.len) |i| { 2194 if (id != self.selector(i).getFocus().id) continue; 2195 if (i != self.selected) try self.select(allocator, i); 2196 root_focus.setFocus(self.textInput().getFocus().id); 2197 break; 2198 } 2199 } 2200 2201 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2202 for (0..self.choices.len) |i| { 2203 self.selector(i).options.border = if (i == self.selected) 2204 (if (focused) .double else .single) 2205 else 2206 .hidden; 2207 } 2208 } 2209 self.textInput().options.border = .single; 2210 self.textInput().cursor = 0; 2211 self.textInput().scroll_offset = 0; 2212 try self.box.build(allocator, constraint, root_focus); 2213 2214 if (!self.session.is_terminal) { 2215 const aa = self.session.arena.allocator(); 2216 const id = self.textInput().getFocus().id; 2217 try self.session.text_inputs.put(aa, id, self.textInput()); 2218 } 2219 } 2220 2221 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2222 switch (key) { 2223 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2224 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2225 .enter => if (self.session.is_terminal) self.show(), 2226 .mouse => |mouse| { 2227 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2228 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2229 try self.select(allocator, i); 2230 root_focus.setFocus(self.textInput().getFocus().id); 2231 return; 2232 }; 2233 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2234 }, 2235 else => {}, 2236 } 2237 } 2238 2239 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2240 self.selected = selected; 2241 const choice = self.choices[selected]; 2242 const text_input = self.textInput(); 2243 text_input.options.top_label.text = choice.label; 2244 text_input.options.bottom_label.text = choice.bottom_label; 2245 try text_input.setContent(allocator, choice.text); 2246 } 2247 2248 fn show(self: *CopyableText) void { 2249 const choice = self.choices[self.selected]; 2250 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2251 } 2252 2253 fn textInput(self: *CopyableText) *wgt.TextInput { 2254 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2255 } 2256 2257 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2258 return &self.box.children.values()[index].widget.text_box; 2259 } 2260 2261 fn maxWidth(choices: []const Choice) usize { 2262 var width: usize = 0; 2263 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2264 return width; 2265 } 2266 2267 pub fn clearGrid(self: *CopyableText) void { 2268 self.box.clearGrid(); 2269 } 2270 2271 pub fn getGrid(self: CopyableText) ?Grid { 2272 return self.box.getGrid(); 2273 } 2274 2275 pub fn getFocus(self: *CopyableText) *Focus { 2276 return self.box.getFocus(); 2277 } 2278 }; 2279