╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
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