╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1186 } 1187 } 1188 } 1189 1190 // composites ANSI art behind a foreground grid 1191 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1192 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1193 errdefer out.deinit(); 1194 1195 const anchor_x = foreground.size.width -| art_grid.size.width; 1196 for (0..art_grid.size.height) |y| { 1197 for (0..art_grid.size.width) |x| { 1198 const src = (try art_grid.cell(x, y)).*; 1199 if (src.rune == null) continue; 1200 const dst = try out.cell(anchor_x + x, y); 1201 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1202 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1203 // blank cells take the art; occupied cells take its background 1204 if (cellIsBlank(dst.*) and (next_dst == null or cellIsBlank(next_dst.?.*))) { 1205 dst.style = src.style; 1206 dst.style.fg = sgrSafe(dst.style.fg); 1207 dst.style.bg = sgrSafe(dst.style.bg); 1208 try out.setRune(anchor_x + x, y, src.rune); 1209 } else { 1210 applyArtBackground(dst, src); 1211 if (next_dst) |next| applyArtBackground(next, src); 1212 } 1213 } 1214 } 1215 return out; 1216 } 1217 }; 1218 1219 pub const CopyableText = struct { 1220 pub const Choice = struct { 1221 selector: []const u8 = "", 1222 text: []const u8, 1223 copyable_text: ?[]const u8 = null, 1224 label: []const u8 = "", 1225 bottom_label: []const u8 = "", 1226 }; 1227 1228 box: wgt.Box(Widget), 1229 session: *ui.Session, 1230 choices: []Choice, 1231 selected: usize = 0, 1232 1233 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1234 if (choices.len == 0) return error.MissingCopyableText; 1235 1236 const owned_choices = try allocator.dupe(Choice, choices); 1237 errdefer allocator.free(owned_choices); 1238 1239 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1240 errdefer box.deinit(allocator); 1241 1242 if (choices.len > 1) for (choices) |choice| { 1243 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .wrap_kind = .none }); 1244 errdefer selector_box.deinit(allocator); 1245 selector_box.getFocus().focusable = true; 1246 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 } }); 1247 }; 1248 1249 var text_input = try wgt.TextInput.init(allocator, .{ 1250 .border_style = .single, 1251 .label = choices[0].label, 1252 .bottom_label = choices[0].bottom_label, 1253 .read_only = true, 1254 .render_content = session.is_terminal, 1255 .visible_width = null, 1256 }); 1257 errdefer text_input.deinit(allocator); 1258 text_input.getFocus().focusable = true; 1259 try text_input.setContent(allocator, choices[0].text); 1260 box.getFocus().child_id = text_input.getFocus().id; 1261 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1262 1263 return .{ .box = box, .session = session, .choices = owned_choices }; 1264 } 1265 1266 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1267 const aa = session.page_arena.allocator(); 1268 var choices: [2]Choice = undefined; 1269 var count: usize = 0; 1270 switch (location) { 1271 .repo => |identity| { 1272 if (session.data.git_http_port) |port| { 1273 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1274 choices[count] = .{ 1275 .selector = "http", 1276 .text = url, 1277 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1278 .label = " clone ", 1279 }; 1280 count += 1; 1281 } 1282 if (session.data.git_ssh_port) |port| { 1283 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1284 choices[count] = .{ 1285 .selector = "ssh", 1286 .text = url, 1287 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1288 .label = " clone ", 1289 }; 1290 count += 1; 1291 } 1292 }, 1293 .fork => |f| if (f.target_branch.len != 0) if (session.data.git_ssh_port) |port| { 1294 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}/patch:{s}/branch:{s}", .{ port, f.identity, f.id, try ui.urlEncodeRef(aa, f.target_branch) }); 1295 choices[count] = .{ 1296 .text = url, 1297 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1298 .label = " clone ", 1299 }; 1300 count += 1; 1301 }, 1302 } 1303 if (count == 0) return null; 1304 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1305 } 1306 1307 pub fn minWidth(self: *const CopyableText) usize { 1308 var width = maxWidth(self.choices) + 2; 1309 if (self.choices.len > 1) { 1310 for (self.choices) |choice| width += choice.selector.len + 2; 1311 } 1312 return width; 1313 } 1314 1315 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1316 self.box.deinit(allocator); 1317 allocator.free(self.choices); 1318 } 1319 1320 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1321 self.clearGrid(); 1322 1323 if (self.choices.len > 1) { 1324 if (root_focus.grandchild_id) |id| { 1325 for (0..self.choices.len) |i| { 1326 if (id != self.selector(i).getFocus().id) continue; 1327 if (i != self.selected) try self.select(allocator, i); 1328 root_focus.setFocus(self.textInput().getFocus().id); 1329 break; 1330 } 1331 } 1332 1333 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1334 for (0..self.choices.len) |i| { 1335 self.selector(i).options.border_style = if (i == self.selected) 1336 (if (focused) .double else .single) 1337 else 1338 .hidden; 1339 } 1340 } 1341 self.textInput().options.border_style = .single; 1342 self.textInput().cursor = 0; 1343 self.textInput().scroll_offset = 0; 1344 try self.box.build(allocator, constraint, root_focus); 1345 1346 if (!self.session.is_terminal) { 1347 const aa = self.session.arena.allocator(); 1348 const id = self.textInput().getFocus().id; 1349 try self.session.text_inputs.put(aa, id, self.textInput()); 1350 } 1351 } 1352 1353 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1354 switch (key) { 1355 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1356 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1357 .enter => if (self.session.is_terminal) self.show(), 1358 .mouse => |mouse| { 1359 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1360 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1361 try self.select(allocator, i); 1362 root_focus.setFocus(self.textInput().getFocus().id); 1363 return; 1364 }; 1365 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1366 }, 1367 else => {}, 1368 } 1369 } 1370 1371 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1372 self.selected = selected; 1373 const choice = self.choices[selected]; 1374 const text_input = self.textInput(); 1375 text_input.options.label = choice.label; 1376 text_input.options.bottom_label = choice.bottom_label; 1377 try text_input.setContent(allocator, choice.text); 1378 } 1379 1380 fn show(self: *CopyableText) void { 1381 const choice = self.choices[self.selected]; 1382 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1383 } 1384 1385 fn textInput(self: *CopyableText) *wgt.TextInput { 1386 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1387 } 1388 1389 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1390 return &self.box.children.values()[index].widget.text_box; 1391 } 1392 1393 fn maxWidth(choices: []const Choice) usize { 1394 var width: usize = 0; 1395 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1396 return width; 1397 } 1398 1399 pub fn clearGrid(self: *CopyableText) void { 1400 self.box.clearGrid(); 1401 } 1402 1403 pub fn getGrid(self: CopyableText) ?Grid { 1404 return self.box.getGrid(); 1405 } 1406 1407 pub fn getFocus(self: *CopyableText) *Focus { 1408 return self.box.getFocus(); 1409 } 1410 }; 1411