╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1384 self.clearGrid(); 1385 1386 // parse into rows of cells, then copy into a rectangular grid 1387 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1388 defer { 1389 for (rows.items) |*row| row.deinit(allocator); 1390 rows.deinit(allocator); 1391 } 1392 var row: std.ArrayList(Grid.Cell) = .empty; 1393 errdefer row.deinit(allocator); 1394 1395 var style: Grid.Style = .{}; 1396 var width: usize = 0; 1397 const content = self.content; 1398 var i: usize = 0; 1399 while (i < content.len) { 1400 const byte = content[i]; 1401 if (byte == '\n') { 1402 width = @max(width, row.items.len); 1403 try rows.append(allocator, row); 1404 row = .empty; 1405 i += 1; 1406 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1407 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1408 var j = i + 2; 1409 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1410 if (j >= content.len) { 1411 i = content.len; // malformed trailing escape; stop 1412 } else { 1413 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1414 i = j + 1; 1415 } 1416 } else { 1417 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1418 const end = @min(content.len, i + len); 1419 const rune = content[i..end]; 1420 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1421 // a foreground color does not make a space visible. only an 1422 // effective background makes it opaque; foreground state often 1423 // remains active through the padding at the end of a row. 1424 const effective_background = if (style.inverted) style.fg else style.bg; 1425 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1426 try row.append(allocator, .{ 1427 .rune = if (transparent) null else codepoint, 1428 .style = style, 1429 }); 1430 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1431 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1432 } 1433 i = end; 1434 } 1435 } 1436 // a trailing row with content but no closing newline 1437 if (row.items.len > 0) { 1438 width = @max(width, row.items.len); 1439 try rows.append(allocator, row); 1440 } else { 1441 row.deinit(allocator); 1442 } 1443 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1444 1445 const height = rows.items.len; 1446 if (width == 0 or height == 0) return; 1447 1448 const clamped_w = @min(width, constraint.max_size.width orelse width); 1449 const clamped_h = @min(height, constraint.max_size.height orelse height); 1450 1451 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1452 errdefer grid.deinit(); 1453 for (rows.items[0..clamped_h], 0..) |r, y| { 1454 const n = @min(r.items.len, clamped_w); 1455 for (r.items[0..n], 0..) |cell, x| { 1456 if (cell.continuation) continue; 1457 (try grid.cell(x, y)).style = cell.style; 1458 try grid.setRune(x, y, cell.rune); 1459 } 1460 } 1461 self.grid = grid; 1462 } 1463 1464 pub fn clearGrid(self: *AnsiArt) void { 1465 if (self.grid) |*grid| { 1466 grid.deinit(); 1467 self.grid = null; 1468 } 1469 } 1470 1471 pub fn getGrid(self: AnsiArt) ?Grid { 1472 return self.grid; 1473 } 1474 1475 pub fn getFocus(self: *AnsiArt) *Focus { 1476 return self.focus; 1477 } 1478 1479 fn applySgr(style: *Grid.Style, params: []const u8) void { 1480 var nums: [16]u32 = undefined; 1481 var n: usize = 0; 1482 var it = std.mem.splitScalar(u8, params, ';'); 1483 while (it.next()) |tok| { 1484 if (n >= nums.len) break; 1485 // an empty parameter (e.g. bare "\x1b[m") means 0 1486 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1487 n += 1; 1488 } 1489 if (n == 0) { 1490 style.* = .{}; 1491 return; 1492 } 1493 var i: usize = 0; 1494 while (i < n) : (i += 1) { 1495 switch (nums[i]) { 1496 0 => style.* = .{}, 1497 7 => style.inverted = true, 1498 27 => style.inverted = false, 1499 39 => style.fg = null, 1500 49 => style.bg = null, 1501 38, 48 => { 1502 // truecolor form: 38;2;r;g;b — anything else is ignored 1503 if (i + 4 < n and nums[i + 1] == 2) { 1504 const c = Grid.Color{ .rgb = .{ 1505 .r = @truncate(nums[i + 2]), 1506 .g = @truncate(nums[i + 3]), 1507 .b = @truncate(nums[i + 4]), 1508 } }; 1509 if (nums[i] == 38) style.fg = c else style.bg = c; 1510 i += 4; 1511 } 1512 }, 1513 else => {}, 1514 } 1515 } 1516 } 1517 }; 1518 1519 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1520 pub const AnsiBackground = struct { 1521 grid: ?Grid, 1522 child: *Widget, 1523 art: AnsiArt, 1524 session: *ui.Session, 1525 1526 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1527 var cw = child_widget; 1528 const child = allocator.create(Widget) catch |e| { 1529 cw.deinit(allocator); 1530 return e; 1531 }; 1532 child.* = cw; 1533 errdefer { 1534 child.deinit(allocator); 1535 allocator.destroy(child); 1536 } 1537 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1538 } 1539 1540 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1541 if (self.grid) |*grid| grid.deinit(); 1542 self.art.deinit(allocator); 1543 self.child.deinit(allocator); 1544 allocator.destroy(self.child); 1545 } 1546 1547 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1548 self.clearGrid(); 1549 1550 // make the wrapped page fill the whole available area 1551 try self.child.build(allocator, .{ 1552 .min_size = .{ 1553 .width = constraint.max_size.width orelse constraint.min_size.width, 1554 .height = constraint.max_size.height orelse constraint.min_size.height, 1555 }, 1556 .max_size = constraint.max_size, 1557 }, root_focus); 1558 1559 if (!self.session.data.enable_ansi) return; 1560 const foreground = self.child.getGrid() orelse return; 1561 self.art.content = self.session.data.ansi_art; 1562 try self.buildArt(allocator, foreground.size, root_focus); 1563 if (!self.session.is_terminal) return; 1564 const art_grid = self.art.getGrid() orelse return; 1565 self.grid = try artBehind(allocator, foreground, art_grid); 1566 } 1567 1568 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1569 try self.child.input(allocator, key, root_focus); 1570 } 1571 1572 pub fn clearGrid(self: *AnsiBackground) void { 1573 if (self.grid) |*grid| { 1574 grid.deinit(); 1575 self.grid = null; 1576 } 1577 self.child.clearGrid(); 1578 self.art.clearGrid(); 1579 } 1580 1581 pub fn getGrid(self: AnsiBackground) ?Grid { 1582 return self.grid orelse self.child.getGrid(); 1583 } 1584 1585 pub fn getFocus(self: *AnsiBackground) *Focus { 1586 return self.child.getFocus(); 1587 } 1588 1589 // a foreground cell counts as blank if it's empty or a bare space without styling 1590 fn cellIsBlank(cell: Grid.Cell) bool { 1591 if (cell.continuation) return false; 1592 if (cell.rune) |rune| { 1593 return rune == ' ' and 1594 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1595 } 1596 return true; 1597 } 1598 1599 // the art is dimmed to this fraction of its brightness so foreground text 1600 // stays legible over it 1601 const art_brightness = 35; // percent 1602 1603 // the art parser only emits truecolor, so a palette color never occurs 1604 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1605 const c = color orelse return null; 1606 return switch (c) { 1607 .rgb => |v| v, 1608 else => null, 1609 }; 1610 } 1611 1612 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1613 const v = artRgb(color) orelse return color; 1614 return .{ .rgb = .{ 1615 .r = @intCast(@as(u16, v.r) * art_brightness / 100), 1616 .g = @intCast(@as(u16, v.g) * art_brightness / 100), 1617 .b = @intCast(@as(u16, v.b) * art_brightness / 100), 1618 } }; 1619 } 1620 1621 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1622 try self.art.build(allocator, .{ 1623 .min_size = .{ .width = null, .height = null }, 1624 .max_size = .{ .width = size.width, .height = size.height }, 1625 }, root_focus); 1626 if (self.art.grid) |*grid| { 1627 for (grid.cells) |*cell| { 1628 cell.style.fg = dimColor(cell.style.fg); 1629 cell.style.bg = dimColor(cell.style.bg); 1630 } 1631 } 1632 } 1633 1634 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1635 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1636 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1637 // snap such channels clear of that range so the backdrop doesn't blink or 1638 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1639 // actually render truecolor. 1640 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1641 const v = artRgb(color) orelse return color; 1642 const snap = struct { 1643 fn f(c: u8) u8 { 1644 return if (c >= 1 and c <= 9) 10 else c; 1645 } 1646 }.f; 1647 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1648 } 1649 1650 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1651 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1652 dst.style.bg = background_maybe; 1653 // change unstyled text to contrast with the art behind it 1654 if (dst.style.fg == null) { 1655 if (artRgb(background_maybe)) |v| { 1656 // use near-black and near-white, which is easier on the eyes 1657 // while retaining high contrast 1658 const luminance = (@as(u32, v.r) * 299 + 1659 @as(u32, v.g) * 587 + 1660 @as(u32, v.b) * 114) / 1000; 1661 dst.style.fg = if (luminance >= 128) 1662 .{ .rgb = .{ .r = 16, .g = 16, .b = 16 } } 1663 else 1664 .{ .rgb = .{ .r = 240, .g = 240, .b = 240 } }; 1665 } 1666 } 1667 } 1668 1669 // composites ANSI art behind a foreground grid 1670 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1671 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1672 errdefer out.deinit(); 1673 1674 const anchor_x = foreground.size.width -| art_grid.size.width; 1675 for (0..art_grid.size.height) |y| { 1676 for (0..art_grid.size.width) |x| { 1677 const src = (try art_grid.cell(x, y)).*; 1678 if (src.rune == null) continue; 1679 const dst = try out.cell(anchor_x + x, y); 1680 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1681 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1682 // blank cells take the art; occupied cells take its background 1683 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1684 dst.style = src.style; 1685 dst.style.fg = sgrSafe(dst.style.fg); 1686 dst.style.bg = sgrSafe(dst.style.bg); 1687 try out.setRune(anchor_x + x, y, src.rune); 1688 } else { 1689 applyArtBackground(dst, src); 1690 if (next_dst) |next| applyArtBackground(next, src); 1691 } 1692 } 1693 } 1694 return out; 1695 } 1696 }; 1697 1698 pub const CopyableText = struct { 1699 pub const Choice = struct { 1700 selector: []const u8 = "", 1701 text: []const u8, 1702 copyable_text: ?[]const u8 = null, 1703 label: []const u8 = "", 1704 bottom_label: []const u8 = "", 1705 }; 1706 1707 box: wgt.Box(Widget), 1708 session: *ui.Session, 1709 choices: []Choice, 1710 selected: usize = 0, 1711 1712 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1713 if (choices.len == 0) return error.MissingCopyableText; 1714 1715 const owned_choices = try allocator.dupe(Choice, choices); 1716 errdefer allocator.free(owned_choices); 1717 1718 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1719 errdefer box.deinit(allocator); 1720 1721 if (choices.len > 1) for (choices) |choice| { 1722 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none }); 1723 errdefer selector_box.deinit(allocator); 1724 selector_box.getFocus().mode = .all; 1725 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 } }); 1726 }; 1727 1728 var text_input = try wgt.TextInput.init(allocator, .{ 1729 .border_style = .single, 1730 .round_corners = true, 1731 .label = choices[0].label, 1732 .bottom_label = choices[0].bottom_label, 1733 .read_only = true, 1734 .render_content = session.is_terminal, 1735 .visible_width = null, 1736 }); 1737 errdefer text_input.deinit(allocator); 1738 text_input.getFocus().mode = .all; 1739 try text_input.setContent(allocator, choices[0].text); 1740 box.getFocus().child_id = text_input.getFocus().id; 1741 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1742 1743 return .{ .box = box, .session = session, .choices = owned_choices }; 1744 } 1745 1746 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1747 const aa = session.page_arena.allocator(); 1748 var choices: [2]Choice = undefined; 1749 var count: usize = 0; 1750 switch (location) { 1751 .repo => |identity| { 1752 if (session.data.git_http_port) |port| { 1753 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1754 choices[count] = .{ 1755 .selector = "http", 1756 .text = url, 1757 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1758 .label = " clone ", 1759 }; 1760 count += 1; 1761 } 1762 if (session.data.git_ssh_port) |port| { 1763 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1764 choices[count] = .{ 1765 .selector = "ssh", 1766 .text = url, 1767 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1768 .label = " clone ", 1769 }; 1770 count += 1; 1771 } 1772 }, 1773 .fork => |f| if (session.data.git_ssh_port) |port| { 1774 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1775 choices[count] = .{ 1776 .text = url, 1777 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1778 .label = " clone ", 1779 }; 1780 count += 1; 1781 }, 1782 } 1783 if (count == 0) return null; 1784 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1785 } 1786 1787 pub fn minWidth(self: *const CopyableText) usize { 1788 var width = maxWidth(self.choices) + 2; 1789 if (self.choices.len > 1) { 1790 for (self.choices) |choice| width += choice.selector.len + 2; 1791 } 1792 return width; 1793 } 1794 1795 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1796 self.box.deinit(allocator); 1797 allocator.free(self.choices); 1798 } 1799 1800 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1801 self.clearGrid(); 1802 1803 if (self.choices.len > 1) { 1804 if (root_focus.grandchild_id) |id| { 1805 for (0..self.choices.len) |i| { 1806 if (id != self.selector(i).getFocus().id) continue; 1807 if (i != self.selected) try self.select(allocator, i); 1808 root_focus.setFocus(self.textInput().getFocus().id); 1809 break; 1810 } 1811 } 1812 1813 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1814 for (0..self.choices.len) |i| { 1815 self.selector(i).options.border_style = if (i == self.selected) 1816 (if (focused) .double else .single) 1817 else 1818 .hidden; 1819 } 1820 } 1821 self.textInput().options.border_style = .single; 1822 self.textInput().cursor = 0; 1823 self.textInput().scroll_offset = 0; 1824 try self.box.build(allocator, constraint, root_focus); 1825 1826 if (!self.session.is_terminal) { 1827 const aa = self.session.arena.allocator(); 1828 const id = self.textInput().getFocus().id; 1829 try self.session.text_inputs.put(aa, id, self.textInput()); 1830 } 1831 } 1832 1833 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1834 switch (key) { 1835 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1836 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1837 .enter => if (self.session.is_terminal) self.show(), 1838 .mouse => |mouse| { 1839 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1840 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1841 try self.select(allocator, i); 1842 root_focus.setFocus(self.textInput().getFocus().id); 1843 return; 1844 }; 1845 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1846 }, 1847 else => {}, 1848 } 1849 } 1850 1851 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1852 self.selected = selected; 1853 const choice = self.choices[selected]; 1854 const text_input = self.textInput(); 1855 text_input.options.label = choice.label; 1856 text_input.options.bottom_label = choice.bottom_label; 1857 try text_input.setContent(allocator, choice.text); 1858 } 1859 1860 fn show(self: *CopyableText) void { 1861 const choice = self.choices[self.selected]; 1862 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1863 } 1864 1865 fn textInput(self: *CopyableText) *wgt.TextInput { 1866 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1867 } 1868 1869 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1870 return &self.box.children.values()[index].widget.text_box; 1871 } 1872 1873 fn maxWidth(choices: []const Choice) usize { 1874 var width: usize = 0; 1875 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1876 return width; 1877 } 1878 1879 pub fn clearGrid(self: *CopyableText) void { 1880 self.box.clearGrid(); 1881 } 1882 1883 pub fn getGrid(self: CopyableText) ?Grid { 1884 return self.box.getGrid(); 1885 } 1886 1887 pub fn getFocus(self: *CopyableText) *Focus { 1888 return self.box.getFocus(); 1889 } 1890 }; 1891