╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1424 1425 var index = current_index; 1426 switch (key) { 1427 .arrow_up => index -|= cols, 1428 .arrow_down => if (index + cols < count) { 1429 index += cols; 1430 }, 1431 .arrow_left => index -|= 1, 1432 .arrow_right => if (index + 1 < count) { 1433 index += 1; 1434 }, 1435 .home => index = 0, 1436 .end => index = count - 1, 1437 .page_up => { 1438 if (self.scroll.grid) |grid| if (slot_height > 0) { 1439 const rows_per_page = grid.size.height / slot_height; 1440 index -|= rows_per_page * cols; 1441 }; 1442 }, 1443 .page_down => { 1444 if (self.scroll.grid) |grid| if (slot_height > 0) { 1445 const rows_per_page = grid.size.height / slot_height; 1446 index = @min(index + rows_per_page * cols, count - 1); 1447 }; 1448 }, 1449 // scroll wheel moves the focused cell by a full row so the 1450 // viewport (via scrollToRect) follows in row-sized steps, 1451 // matching how a scroll wheel feels in a grid view. 1452 .mouse => |mouse| switch (mouse.action) { 1453 .scroll => |dir| switch (dir) { 1454 .up => index -|= cols, 1455 .down => if (index + cols < count) { 1456 index += cols; 1457 }, 1458 }, 1459 else => {}, 1460 }, 1461 else => {}, 1462 } 1463 1464 if (index != current_index) { 1465 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1466 if (in.cellRect(index)) |rect| { 1467 self.scroll.scrollToRect(rect); 1468 } 1469 } 1470 } 1471 1472 pub fn clearGrid(self: *Scroll) void { 1473 self.scroll.clearGrid(); 1474 } 1475 1476 pub fn getGrid(self: Scroll) ?Grid { 1477 return self.scroll.getGrid(); 1478 } 1479 1480 pub fn getFocus(self: *Scroll) *Focus { 1481 return self.scroll.getFocus(); 1482 } 1483 1484 pub fn getSelectedIndex(self: Scroll) ?usize { 1485 const in = self.scroll.child.flow_box; 1486 const child_id = in.focus.child_id orelse return null; 1487 return in.indexOfFocusId(child_id); 1488 } 1489 1490 fn inner(self: *Scroll) *FlowBox { 1491 return &self.scroll.child.flow_box; 1492 } 1493 }; 1494 }; 1495 1496 // a left-to-right flow of variable-width focusable items that wraps at the 1497 // available width, like words in a paragraph. unlike FlowBox there is no cell 1498 // grid: each item is as wide as its text. selection movement is driven by the 1499 // owning view via indexOfFocusId/verticalNeighbor. 1500 pub const TagFlow = struct { 1501 focus: *Focus, 1502 grid: ?Grid, 1503 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1504 // last build's item rects (content space), for vertical navigation and 1505 // scroll-into-view. 1506 rects: std.ArrayList(layout.IRect), 1507 // backs the per-item strings the text boxes borrow. 1508 arena: std.heap.ArenaAllocator, 1509 // whether the owning view considers this flow selected; only then does the 1510 // selected item show its border. 1511 selected: bool, 1512 1513 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1514 1515 pub fn init(allocator: std.mem.Allocator) !TagFlow { 1516 return .{ 1517 .focus = try Focus.create(allocator, .container), 1518 .grid = null, 1519 .text_boxes = .empty, 1520 .rects = .empty, 1521 .arena = std.heap.ArenaAllocator.init(allocator), 1522 .selected = true, 1523 }; 1524 } 1525 1526 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 1527 self.focus.destroy(allocator); 1528 if (self.grid) |*grid| { 1529 grid.deinit(); 1530 self.grid = null; 1531 } 1532 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1533 self.text_boxes.deinit(allocator); 1534 self.rects.deinit(allocator); 1535 self.arena.deinit(); 1536 } 1537 1538 // both the item text and the link are copied into the arena, so the caller's 1539 // slices needn't outlive this call. 1540 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 1541 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1542 self.text_boxes.clearAndFree(allocator); 1543 1544 _ = self.arena.reset(.retain_capacity); 1545 const aa = self.arena.allocator(); 1546 1547 self.focus.clear(); 1548 self.focus.child_id = null; 1549 1550 for (items) |item| { 1551 const line = try aa.dupe(u8, item.text); 1552 1553 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 1554 errdefer text_box.deinit(allocator); 1555 text_box.getFocus().focusable = true; 1556 1557 if (item.link.len > 0) { 1558 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1559 } 1560 1561 try self.text_boxes.append(allocator, text_box); 1562 } 1563 1564 if (self.text_boxes.items.len > 0) { 1565 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1566 } 1567 } 1568 1569 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1570 self.clearGrid(); 1571 self.focus.clear(); 1572 1573 if (self.text_boxes.items.len == 0) return; 1574 const max_width = constraint.max_size.width orelse 80; 1575 if (max_width == 0) return; 1576 1577 // lay the items out left to right, wrapping to a new row when the next 1578 // one wouldn't fit. 1579 self.rects.clearRetainingCapacity(); 1580 var x: usize = 0; 1581 var y: usize = 0; 1582 var row_height: usize = 0; 1583 var total_width: usize = 0; 1584 for (self.text_boxes.items) |*tb| { 1585 tb.options.border_style = if (self.selected and self.focus.child_id == tb.getFocus().id) .single else .hidden; 1586 try tb.build(allocator, .{ 1587 .min_size = .{ .width = null, .height = null }, 1588 .max_size = .{ .width = max_width, .height = null }, 1589 }, root_focus); 1590 const tb_grid_maybe = tb.getGrid(); 1591 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 1592 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 1593 if (x > 0 and x + w > max_width) { 1594 x = 0; 1595 y += row_height; 1596 row_height = 0; 1597 } 1598 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 1599 if (h > row_height) row_height = h; 1600 x += w; 1601 if (x > total_width) total_width = x; 1602 } 1603 1604 const total_height = y + row_height; 1605 if (total_width == 0 or total_height == 0) return; 1606 1607 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 1608 errdefer grid.deinit(); 1609 1610 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 1611 const tb_grid = tb.getGrid() orelse continue; 1612 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 1613 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 1614 } 1615 1616 self.grid = grid; 1617 } 1618 1619 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1620 _ = self; 1621 _ = allocator; 1622 _ = key; 1623 _ = root_focus; 1624 } 1625 1626 pub fn clearGrid(self: *TagFlow) void { 1627 if (self.grid) |*grid| { 1628 grid.deinit(); 1629 self.grid = null; 1630 } 1631 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1632 } 1633 1634 pub fn getGrid(self: TagFlow) ?Grid { 1635 return self.grid; 1636 } 1637 1638 pub fn getFocus(self: *TagFlow) *Focus { 1639 return self.focus; 1640 } 1641 1642 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 1643 for (self.text_boxes.items, 0..) |tb, i| { 1644 if (tb.box.focus.id == focus_id) return i; 1645 } 1646 return null; 1647 } 1648 1649 // the first item of the row below (when `downward`) or above, or null at 1650 // the flow's edge. items are laid out in order, so y is nondecreasing. 1651 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 1652 const rects = self.rects.items; 1653 if (index >= rects.len) return null; 1654 const cur_y = rects[index].y; 1655 if (downward) { 1656 for (rects[index + 1 ..], index + 1..) |r, i| { 1657 if (r.y > cur_y) return i; 1658 } 1659 return null; 1660 } 1661 // walk the earlier rows; the last one visited is the row above. 1662 var prev_first: ?usize = null; 1663 var i: usize = 0; 1664 while (i < rects.len and rects[i].y < cur_y) { 1665 prev_first = i; 1666 const y = rects[i].y; 1667 while (i < rects.len and rects[i].y == y) i += 1; 1668 } 1669 return prev_first; 1670 } 1671 }; 1672 1673 // an invisible widget that fills the horizontal space granted by its parent. 1674 // used inside Box(horiz) with a min_size so the box reserves space for the 1675 // children that follow, pushing them to the right. 1676 pub const Spacer = struct { 1677 focus: *Focus, 1678 grid: ?Grid, 1679 1680 pub fn init(allocator: std.mem.Allocator) !Spacer { 1681 return .{ 1682 .focus = try Focus.create(allocator, .container), 1683 .grid = null, 1684 }; 1685 } 1686 1687 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1688 self.focus.destroy(allocator); 1689 if (self.grid) |*grid| { 1690 grid.deinit(); 1691 self.grid = null; 1692 } 1693 } 1694 1695 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1696 _ = root_focus; 1697 self.clearGrid(); 1698 const width = constraint.max_size.width orelse return; 1699 if (width == 0) return; 1700 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1701 } 1702 1703 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1704 _ = self; 1705 _ = allocator; 1706 _ = key; 1707 _ = root_focus; 1708 } 1709 1710 pub fn clearGrid(self: *Spacer) void { 1711 if (self.grid) |*grid| { 1712 grid.deinit(); 1713 self.grid = null; 1714 } 1715 } 1716 1717 pub fn getGrid(self: Spacer) ?Grid { 1718 return self.grid; 1719 } 1720 1721 pub fn getFocus(self: *Spacer) *Focus { 1722 return self.focus; 1723 } 1724 }; 1725 1726 // a one-row, non-focusable status bar showing the current page's url 1727 pub const Footer = struct { 1728 focus: *Focus, 1729 grid: ?Grid, 1730 session: *Session, 1731 arena: std.heap.ArenaAllocator, 1732 1733 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1734 return .{ 1735 .focus = try Focus.create(allocator, .container), 1736 .grid = null, 1737 .session = session, 1738 .arena = std.heap.ArenaAllocator.init(allocator), 1739 }; 1740 } 1741 1742 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1743 self.focus.destroy(allocator); 1744 if (self.grid) |*grid| grid.deinit(); 1745 self.arena.deinit(); 1746 } 1747 1748 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1749 _ = root_focus; 1750 self.clearGrid(); 1751 const width = constraint.max_size.width orelse return; 1752 if (width == 0) return; 1753 1754 _ = self.arena.reset(.retain_capacity); 1755 const aa = self.arena.allocator(); 1756 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1757 const text = if (self.session.web_port) |port| 1758 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1759 else 1760 path; 1761 1762 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1763 errdefer grid.deinit(); 1764 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1765 var i: usize = 0; 1766 while (utf8.nextCodepointSlice()) |ch| { 1767 if (i == width) break; 1768 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1769 i += 1; 1770 } 1771 self.grid = grid; 1772 } 1773 1774 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1775 _ = self; 1776 _ = allocator; 1777 _ = key; 1778 _ = root_focus; 1779 } 1780 1781 pub fn clearGrid(self: *Footer) void { 1782 if (self.grid) |*grid| { 1783 grid.deinit(); 1784 self.grid = null; 1785 } 1786 } 1787 1788 pub fn getGrid(self: Footer) ?Grid { 1789 return self.grid; 1790 } 1791 1792 pub fn getFocus(self: *Footer) *Focus { 1793 return self.focus; 1794 } 1795 }; 1796 1797 // a single-child wrapper that builds the child at its natural size and 1798 // positions its grid in the middle of the area granted by the parent 1799 pub const Center = struct { 1800 focus: *Focus, 1801 grid: ?Grid, 1802 child: *Widget, 1803 1804 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1805 const child = try allocator.create(Widget); 1806 errdefer allocator.destroy(child); 1807 child.* = child_widget; 1808 return .{ 1809 .focus = try Focus.create(allocator, .container), 1810 .grid = null, 1811 .child = child, 1812 }; 1813 } 1814 1815 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1816 self.focus.destroy(allocator); 1817 if (self.grid) |*grid| { 1818 grid.deinit(); 1819 self.grid = null; 1820 } 1821 self.child.deinit(allocator); 1822 allocator.destroy(self.child); 1823 } 1824 1825 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1826 self.clearGrid(); 1827 self.getFocus().clear(); 1828 1829 // build the child without forcing it to fill min_size; it sizes 1830 // itself to its content within the available max. 1831 try self.child.build(allocator, .{ 1832 .min_size = .{ .width = null, .height = null }, 1833 .max_size = constraint.max_size, 1834 }, root_focus); 1835 1836 const child_grid = self.child.getGrid() orelse return; 1837 1838 // prefer max when given; otherwise grow to max(min, child) so that 1839 // when only a min is set (e.g. the wasm path passes viewport rows 1840 // as min_height with no max), we can still vertically center while 1841 // letting taller content extend past the min. 1842 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 1843 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 1844 if (width == 0 or height == 0) return; 1845 1846 const offset_x: usize = (width -| child_grid.size.width) / 2; 1847 const offset_y: usize = (height -| child_grid.size.height) / 2; 1848 1849 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1850 errdefer grid.deinit(); 1851 try grid.drawGrid(child_grid, offset_x, offset_y); 1852 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1853 self.getFocus().child_id = self.child.getFocus().id; 1854 1855 self.grid = grid; 1856 } 1857 1858 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1859 try self.child.input(allocator, key, root_focus); 1860 } 1861 1862 pub fn clearGrid(self: *Center) void { 1863 if (self.grid) |*grid| { 1864 grid.deinit(); 1865 self.grid = null; 1866 } 1867 self.child.clearGrid(); 1868 } 1869 1870 pub fn getGrid(self: Center) ?Grid { 1871 return self.grid; 1872 } 1873 1874 pub fn getFocus(self: *Center) *Focus { 1875 return self.focus; 1876 } 1877 }; 1878 1879 // renders truecolor ANSI art 1880 pub const AnsiArt = struct { 1881 focus: *Focus, 1882 grid: ?Grid, 1883 content: []const u8, 1884 1885 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1886 return .{ 1887 .focus = try Focus.create(allocator, .container), 1888 .grid = null, 1889 .content = content, 1890 }; 1891 } 1892 1893 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1894 self.focus.destroy(allocator); 1895 if (self.grid) |*grid| { 1896 grid.deinit(); 1897 self.grid = null; 1898 } 1899 } 1900 1901 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1902 _ = root_focus; 1903 self.clearGrid(); 1904 1905 // parse into rows of cells, then copy into a rectangular grid 1906 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1907 defer { 1908 for (rows.items) |*row| row.deinit(allocator); 1909 rows.deinit(allocator); 1910 } 1911 var row: std.ArrayList(Grid.Cell) = .empty; 1912 errdefer row.deinit(allocator); 1913 1914 var style: Grid.Style = .{}; 1915 var width: usize = 0; 1916 const content = self.content; 1917 var i: usize = 0; 1918 while (i < content.len) { 1919 const byte = content[i]; 1920 if (byte == '\n') { 1921 width = @max(width, row.items.len); 1922 try rows.append(allocator, row); 1923 row = .empty; 1924 i += 1; 1925 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1926 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1927 var j = i + 2; 1928 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1929 if (j >= content.len) { 1930 i = content.len; // malformed trailing escape; stop 1931 } else { 1932 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1933 i = j + 1; 1934 } 1935 } else { 1936 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1937 const end = @min(content.len, i + len); 1938 const rune = content[i..end]; 1939 const transparent = end - i == 1 and rune[0] == ' ' and 1940 style.fg == null and style.bg == null and !style.inverted; 1941 try row.append(allocator, .{ 1942 .rune = if (transparent) null else rune, 1943 .style = style, 1944 }); 1945 i = end; 1946 } 1947 } 1948 // a trailing row with content but no closing newline 1949 if (row.items.len > 0) { 1950 width = @max(width, row.items.len); 1951 try rows.append(allocator, row); 1952 } else { 1953 row.deinit(allocator); 1954 } 1955 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1956 1957 const height = rows.items.len; 1958 if (width == 0 or height == 0) return; 1959 1960 const clamped_w = @min(width, constraint.max_size.width orelse width); 1961 const clamped_h = @min(height, constraint.max_size.height orelse height); 1962 1963 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1964 errdefer grid.deinit(); 1965 for (rows.items[0..clamped_h], 0..) |r, y| { 1966 const n = @min(r.items.len, clamped_w); 1967 for (r.items[0..n], 0..) |cell, x| { 1968 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1969 } 1970 } 1971 self.grid = grid; 1972 } 1973 1974 pub fn clearGrid(self: *AnsiArt) void { 1975 if (self.grid) |*grid| { 1976 grid.deinit(); 1977 self.grid = null; 1978 } 1979 } 1980 1981 pub fn getGrid(self: AnsiArt) ?Grid { 1982 return self.grid; 1983 } 1984 1985 pub fn getFocus(self: *AnsiArt) *Focus { 1986 return self.focus; 1987 } 1988 1989 fn applySgr(style: *Grid.Style, params: []const u8) void { 1990 var nums: [16]u32 = undefined; 1991 var n: usize = 0; 1992 var it = std.mem.splitScalar(u8, params, ';'); 1993 while (it.next()) |tok| { 1994 if (n >= nums.len) break; 1995 // an empty parameter (e.g. bare "\x1b[m") means 0 1996 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1997 n += 1; 1998 } 1999 if (n == 0) { 2000 style.* = .{}; 2001 return; 2002 } 2003 var i: usize = 0; 2004 while (i < n) : (i += 1) { 2005 switch (nums[i]) { 2006 0 => style.* = .{}, 2007 7 => style.inverted = true, 2008 27 => style.inverted = false, 2009 39 => style.fg = null, 2010 49 => style.bg = null, 2011 38, 48 => { 2012 // truecolor form: 38;2;r;g;b — anything else is ignored 2013 if (i + 4 < n and nums[i + 1] == 2) { 2014 const c = Grid.Color{ 2015 .r = @truncate(nums[i + 2]), 2016 .g = @truncate(nums[i + 3]), 2017 .b = @truncate(nums[i + 4]), 2018 }; 2019 if (nums[i] == 38) style.fg = c else style.bg = c; 2020 i += 4; 2021 } 2022 }, 2023 else => {}, 2024 } 2025 } 2026 } 2027 }; 2028 2029 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2030 pub const AnsiBackground = struct { 2031 grid: ?Grid, 2032 child: *Widget, 2033 art: AnsiArt, 2034 session: *Session, 2035 2036 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2037 var cw = child_widget; 2038 const child = allocator.create(Widget) catch |e| { 2039 cw.deinit(allocator); 2040 return e; 2041 }; 2042 child.* = cw; 2043 errdefer { 2044 child.deinit(allocator); 2045 allocator.destroy(child); 2046 } 2047 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2048 } 2049 2050 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2051 if (self.grid) |*grid| grid.deinit(); 2052 self.art.deinit(allocator); 2053 self.child.deinit(allocator); 2054 allocator.destroy(self.child); 2055 } 2056 2057 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2058 self.clearGrid(); 2059 2060 // make the wrapped page fill the whole available area 2061 try self.child.build(allocator, .{ 2062 .min_size = .{ 2063 .width = constraint.max_size.width orelse constraint.min_size.width, 2064 .height = constraint.max_size.height orelse constraint.min_size.height, 2065 }, 2066 .max_size = constraint.max_size, 2067 }, root_focus); 2068 2069 if (self.session.data.enable_ansi) { 2070 if (self.child.getGrid()) |fg| { 2071 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2072 } 2073 } 2074 } 2075 2076 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2077 try self.child.input(allocator, key, root_focus); 2078 } 2079 2080 pub fn clearGrid(self: *AnsiBackground) void { 2081 if (self.grid) |*grid| { 2082 grid.deinit(); 2083 self.grid = null; 2084 } 2085 self.child.clearGrid(); 2086 self.art.clearGrid(); 2087 } 2088 2089 pub fn getGrid(self: AnsiBackground) ?Grid { 2090 return self.grid orelse self.child.getGrid(); 2091 } 2092 2093 pub fn getFocus(self: *AnsiBackground) *Focus { 2094 return self.child.getFocus(); 2095 } 2096 2097 // which top corner the art hugs. 2098 const ArtAnchor = enum { top_left, top_right }; 2099 2100 // a foreground cell counts as blank if it's empty or a bare space without styling 2101 fn cellIsBlank(cell: Grid.Cell) bool { 2102 if (cell.rune) |rune| { 2103 return std.mem.eql(u8, rune, " ") and 2104 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2105 } 2106 return true; 2107 } 2108 2109 // the art is dimmed to this fraction of its brightness so foreground text 2110 // stays legible over it 2111 const art_brightness = 45; // percent 2112 2113 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2114 const c = color orelse return null; 2115 return .{ 2116 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2117 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2118 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2119 }; 2120 } 2121 2122 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2123 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2124 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2125 // snap such channels clear of that range so the backdrop doesn't blink or 2126 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2127 // actually render truecolor. 2128 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2129 const c = color orelse return null; 2130 const snap = struct { 2131 fn f(v: u8) u8 { 2132 return if (v >= 1 and v <= 9) 10 else v; 2133 } 2134 }.f; 2135 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2136 } 2137 2138 // composites ANSI art behind a foreground grid 2139 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2140 art.clearGrid(); 2141 try art.build(allocator, .{ 2142 .min_size = .{ .width = null, .height = null }, 2143 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2144 }, root_focus); 2145 2146 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2147 errdefer out.deinit(); 2148 2149 if (art.getGrid()) |art_grid| { 2150 const anchor_x = switch (anchor) { 2151 .top_left => 0, 2152 .top_right => foreground.size.width -| art_grid.size.width, 2153 }; 2154 for (0..art_grid.size.height) |y| { 2155 for (0..art_grid.size.width) |x| { 2156 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2157 if (src.rune == null) continue; 2158 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2159 const dst = &out.cells.items[idx]; 2160 // composite the dimmed art behind every cell so it shows 2161 // through the whole UI: a blank cell becomes the art, while a 2162 // cell with a glyph keeps the glyph but takes the dimmed art as 2163 // its background (replacing whatever background it had, which 2164 // would otherwise obscure the art). 2165 if (cellIsBlank(dst.*)) { 2166 dst.* = src; 2167 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2168 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2169 } else { 2170 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2171 } 2172 } 2173 } 2174 } 2175 return out; 2176 } 2177 }; 2178