╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1241 // blank cells take the art; occupied cells take its background 1242 if (cellIsBlank(dst.*) and (next_dst == null or cellIsBlank(next_dst.?.*))) { 1243 dst.style = src.style; 1244 dst.style.fg = sgrSafe(dst.style.fg); 1245 dst.style.bg = sgrSafe(dst.style.bg); 1246 try out.setRune(anchor_x + x, y, src.rune); 1247 } else { 1248 applyArtBackground(dst, src); 1249 if (next_dst) |next| applyArtBackground(next, src); 1250 } 1251 } 1252 } 1253 return out; 1254 } 1255 }; 1256 1257 pub const CopyableText = struct { 1258 pub const Choice = struct { 1259 selector: []const u8 = "", 1260 text: []const u8, 1261 copyable_text: ?[]const u8 = null, 1262 label: []const u8 = "", 1263 bottom_label: []const u8 = "", 1264 }; 1265 1266 box: wgt.Box(Widget), 1267 session: *ui.Session, 1268 choices: []Choice, 1269 selected: usize = 0, 1270 1271 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1272 if (choices.len == 0) return error.MissingCopyableText; 1273 1274 const owned_choices = try allocator.dupe(Choice, choices); 1275 errdefer allocator.free(owned_choices); 1276 1277 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1278 errdefer box.deinit(allocator); 1279 1280 if (choices.len > 1) for (choices) |choice| { 1281 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .wrap_kind = .none }); 1282 errdefer selector_box.deinit(allocator); 1283 selector_box.getFocus().focusable = true; 1284 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 } }); 1285 }; 1286 1287 var text_input = try wgt.TextInput.init(allocator, .{ 1288 .border_style = .single, 1289 .label = choices[0].label, 1290 .bottom_label = choices[0].bottom_label, 1291 .read_only = true, 1292 .render_content = session.is_terminal, 1293 .visible_width = null, 1294 }); 1295 errdefer text_input.deinit(allocator); 1296 text_input.getFocus().focusable = true; 1297 try text_input.setContent(allocator, choices[0].text); 1298 box.getFocus().child_id = text_input.getFocus().id; 1299 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1300 1301 return .{ .box = box, .session = session, .choices = owned_choices }; 1302 } 1303 1304 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1305 const aa = session.page_arena.allocator(); 1306 var choices: [2]Choice = undefined; 1307 var count: usize = 0; 1308 switch (location) { 1309 .repo => |identity| { 1310 if (session.data.git_http_port) |port| { 1311 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1312 choices[count] = .{ 1313 .selector = "http", 1314 .text = url, 1315 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1316 .label = " clone ", 1317 }; 1318 count += 1; 1319 } 1320 if (session.data.git_ssh_port) |port| { 1321 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1322 choices[count] = .{ 1323 .selector = "ssh", 1324 .text = url, 1325 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1326 .label = " clone ", 1327 }; 1328 count += 1; 1329 } 1330 }, 1331 .fork => |f| if (session.data.git_ssh_port) |port| { 1332 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1333 choices[count] = .{ 1334 .text = url, 1335 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1336 .label = " clone ", 1337 }; 1338 count += 1; 1339 }, 1340 } 1341 if (count == 0) return null; 1342 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1343 } 1344 1345 pub fn minWidth(self: *const CopyableText) usize { 1346 var width = maxWidth(self.choices) + 2; 1347 if (self.choices.len > 1) { 1348 for (self.choices) |choice| width += choice.selector.len + 2; 1349 } 1350 return width; 1351 } 1352 1353 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1354 self.box.deinit(allocator); 1355 allocator.free(self.choices); 1356 } 1357 1358 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1359 self.clearGrid(); 1360 1361 if (self.choices.len > 1) { 1362 if (root_focus.grandchild_id) |id| { 1363 for (0..self.choices.len) |i| { 1364 if (id != self.selector(i).getFocus().id) continue; 1365 if (i != self.selected) try self.select(allocator, i); 1366 root_focus.setFocus(self.textInput().getFocus().id); 1367 break; 1368 } 1369 } 1370 1371 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1372 for (0..self.choices.len) |i| { 1373 self.selector(i).options.border_style = if (i == self.selected) 1374 (if (focused) .double else .single) 1375 else 1376 .hidden; 1377 } 1378 } 1379 self.textInput().options.border_style = .single; 1380 self.textInput().cursor = 0; 1381 self.textInput().scroll_offset = 0; 1382 try self.box.build(allocator, constraint, root_focus); 1383 1384 if (!self.session.is_terminal) { 1385 const aa = self.session.arena.allocator(); 1386 const id = self.textInput().getFocus().id; 1387 try self.session.text_inputs.put(aa, id, self.textInput()); 1388 } 1389 } 1390 1391 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1392 switch (key) { 1393 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1394 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1395 .enter => if (self.session.is_terminal) self.show(), 1396 .mouse => |mouse| { 1397 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1398 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1399 try self.select(allocator, i); 1400 root_focus.setFocus(self.textInput().getFocus().id); 1401 return; 1402 }; 1403 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1404 }, 1405 else => {}, 1406 } 1407 } 1408 1409 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1410 self.selected = selected; 1411 const choice = self.choices[selected]; 1412 const text_input = self.textInput(); 1413 text_input.options.label = choice.label; 1414 text_input.options.bottom_label = choice.bottom_label; 1415 try text_input.setContent(allocator, choice.text); 1416 } 1417 1418 fn show(self: *CopyableText) void { 1419 const choice = self.choices[self.selected]; 1420 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1421 } 1422 1423 fn textInput(self: *CopyableText) *wgt.TextInput { 1424 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1425 } 1426 1427 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1428 return &self.box.children.values()[index].widget.text_box; 1429 } 1430 1431 fn maxWidth(choices: []const Choice) usize { 1432 var width: usize = 0; 1433 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1434 return width; 1435 } 1436 1437 pub fn clearGrid(self: *CopyableText) void { 1438 self.box.clearGrid(); 1439 } 1440 1441 pub fn getGrid(self: CopyableText) ?Grid { 1442 return self.box.getGrid(); 1443 } 1444 1445 pub fn getFocus(self: *CopyableText) *Focus { 1446 return self.box.getFocus(); 1447 } 1448 }; 1449