╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1577 cell_height: usize = 3, 1578 }; 1579 1580 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 1581 return .{ 1582 .focus = try Focus.create(allocator, .container), 1583 .grid = null, 1584 .text_boxes = .empty, 1585 .arena = std.heap.ArenaAllocator.init(allocator), 1586 .last_cols = 1, 1587 .options = options, 1588 }; 1589 } 1590 1591 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 1592 self.focus.destroy(allocator); 1593 if (self.grid) |*grid| { 1594 grid.deinit(); 1595 self.grid = null; 1596 } 1597 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1598 self.text_boxes.deinit(allocator); 1599 self.arena.deinit(); 1600 } 1601 1602 // one flow item: its display text plus an optional link. a non-empty `link` 1603 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 1604 // the web renderer turns into an anchor. 1605 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1606 1607 // both the item text and the link are copied into the arena, so the caller's 1608 // slices needn't outlive this call. 1609 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 1610 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1611 self.text_boxes.clearAndFree(allocator); 1612 1613 // the old text boxes (and their borrowed content/links) are gone now, so 1614 // the arena that backed those strings can be reclaimed. 1615 _ = self.arena.reset(.retain_capacity); 1616 const aa = self.arena.allocator(); 1617 1618 self.focus.clear(); 1619 self.focus.child_id = null; 1620 1621 for (items) |item| { 1622 const line = try aa.dupe(u8, item.text); 1623 1624 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 1625 errdefer text_box.deinit(allocator); 1626 text_box.getFocus().focusable = true; 1627 1628 if (item.link.len > 0) { 1629 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1630 } 1631 1632 try self.text_boxes.append(allocator, text_box); 1633 } 1634 1635 if (self.text_boxes.items.len > 0) { 1636 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1637 } 1638 } 1639 1640 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1641 self.clearGrid(); 1642 self.focus.clear(); 1643 1644 const cell_width = self.options.cell_width; 1645 const max_width = constraint.max_size.width orelse cell_width; 1646 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1647 self.last_cols = cols; 1648 1649 const count = self.text_boxes.items.len; 1650 if (count == 0) return; 1651 const slot_height = self.options.cell_height + border_rows; 1652 if (slot_height == 0) return; 1653 1654 // build at the slot size so every text box fits a single grid cell 1655 for (self.text_boxes.items) |*tb| { 1656 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1657 try tb.build(allocator, .{ 1658 .min_size = .{ .width = cell_width, .height = null }, 1659 .max_size = .{ .width = cell_width, .height = slot_height }, 1660 }, root_focus); 1661 } 1662 1663 const rows = (count + cols - 1) / cols; 1664 const content_height = rows * slot_height; 1665 const total_width = cols * cell_width; 1666 if (total_width == 0 or content_height == 0) return; 1667 1668 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1669 errdefer grid.deinit(); 1670 1671 for (self.text_boxes.items, 0..) |*tb, i| { 1672 const tb_grid = tb.getGrid() orelse continue; 1673 const col = i % cols; 1674 const row = i / cols; 1675 const cell_x = col * cell_width; 1676 const cell_y = row * slot_height; 1677 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1678 try grid.drawGrid(tb_grid, cell_x, cell_y); 1679 } 1680 1681 self.grid = grid; 1682 } 1683 1684 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1685 _ = self; 1686 _ = allocator; 1687 _ = key; 1688 _ = root_focus; 1689 } 1690 1691 pub fn clearGrid(self: *FlowBox) void { 1692 if (self.grid) |*grid| { 1693 grid.deinit(); 1694 self.grid = null; 1695 } 1696 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1697 } 1698 1699 pub fn getGrid(self: FlowBox) ?Grid { 1700 return self.grid; 1701 } 1702 1703 pub fn getFocus(self: *FlowBox) *Focus { 1704 return self.focus; 1705 } 1706 1707 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1708 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1709 const slot_height = self.options.cell_height + border_rows; 1710 const col = index % self.last_cols; 1711 const row = index / self.last_cols; 1712 return .{ 1713 .x = @intCast(col * self.options.cell_width), 1714 .y = @intCast(row * slot_height), 1715 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1716 }; 1717 } 1718 1719 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1720 for (self.text_boxes.items, 0..) |tb, i| { 1721 if (tb.box.focus.id == focus_id) return i; 1722 } 1723 return null; 1724 } 1725 1726 pub const Scroll = struct { 1727 scroll: wgt.Scroll(Widget), 1728 1729 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1730 var layout_inner = try FlowBox.init(allocator, options); 1731 errdefer layout_inner.deinit(allocator); 1732 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1733 errdefer scroll.deinit(allocator); 1734 return .{ .scroll = scroll }; 1735 } 1736 1737 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1738 self.scroll.deinit(allocator); 1739 } 1740 1741 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1742 self.scroll.x = 0; 1743 self.scroll.y = 0; 1744 try self.inner().setItems(allocator, items); 1745 } 1746 1747 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1748 try self.scroll.build(allocator, constraint, root_focus); 1749 } 1750 1751 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1752 _ = allocator; 1753 const in = self.inner(); 1754 const child_id = in.focus.child_id orelse return; 1755 const current_index = in.indexOfFocusId(child_id) orelse return; 1756 const count = in.text_boxes.items.len; 1757 if (count == 0) return; 1758 const cols = in.last_cols; 1759 const slot_height = in.options.cell_height + FlowBox.border_rows; 1760 1761 var index = current_index; 1762 switch (key) { 1763 .arrow_up => index -|= cols, 1764 .arrow_down => if (index + cols < count) { 1765 index += cols; 1766 }, 1767 .arrow_left => index -|= 1, 1768 .arrow_right => if (index + 1 < count) { 1769 index += 1; 1770 }, 1771 .home => index = 0, 1772 .end => index = count - 1, 1773 .page_up => { 1774 if (self.scroll.grid) |grid| if (slot_height > 0) { 1775 const rows_per_page = grid.size.height / slot_height; 1776 index -|= rows_per_page * cols; 1777 }; 1778 }, 1779 .page_down => { 1780 if (self.scroll.grid) |grid| if (slot_height > 0) { 1781 const rows_per_page = grid.size.height / slot_height; 1782 index = @min(index + rows_per_page * cols, count - 1); 1783 }; 1784 }, 1785 // scroll wheel moves the focused cell by a full row so the 1786 // viewport (via scrollToRect) follows in row-sized steps, 1787 // matching how a scroll wheel feels in a grid view. 1788 .mouse => |mouse| switch (mouse.action) { 1789 .scroll => |dir| switch (dir) { 1790 .up => index -|= cols, 1791 .down => if (index + cols < count) { 1792 index += cols; 1793 }, 1794 }, 1795 else => {}, 1796 }, 1797 else => {}, 1798 } 1799 1800 if (index != current_index) { 1801 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1802 if (in.cellRect(index)) |rect| { 1803 self.scroll.scrollToRect(rect); 1804 } 1805 } 1806 } 1807 1808 pub fn clearGrid(self: *Scroll) void { 1809 self.scroll.clearGrid(); 1810 } 1811 1812 pub fn getGrid(self: Scroll) ?Grid { 1813 return self.scroll.getGrid(); 1814 } 1815 1816 pub fn getFocus(self: *Scroll) *Focus { 1817 return self.scroll.getFocus(); 1818 } 1819 1820 pub fn getSelectedIndex(self: Scroll) ?usize { 1821 const in = self.scroll.child.flow_box; 1822 const child_id = in.focus.child_id orelse return null; 1823 return in.indexOfFocusId(child_id); 1824 } 1825 1826 fn inner(self: *Scroll) *FlowBox { 1827 return &self.scroll.child.flow_box; 1828 } 1829 }; 1830 }; 1831 1832 // a left-to-right flow of variable-width focusable items that wraps at the 1833 // available width, like words in a paragraph. unlike FlowBox there is no cell 1834 // grid: each item is as wide as its text. selection movement is driven by the 1835 // owning view via indexOfFocusId/verticalNeighbor. 1836 pub const TagFlow = struct { 1837 focus: *Focus, 1838 grid: ?Grid, 1839 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1840 // last build's item rects (content space), for vertical navigation and 1841 // scroll-into-view. 1842 rects: std.ArrayList(layout.IRect), 1843 // backs the per-item strings the text boxes borrow. 1844 arena: std.heap.ArenaAllocator, 1845 1846 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1847 1848 pub fn init(allocator: std.mem.Allocator) !TagFlow { 1849 return .{ 1850 .focus = try Focus.create(allocator, .container), 1851 .grid = null, 1852 .text_boxes = .empty, 1853 .rects = .empty, 1854 .arena = std.heap.ArenaAllocator.init(allocator), 1855 }; 1856 } 1857 1858 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 1859 self.focus.destroy(allocator); 1860 if (self.grid) |*grid| { 1861 grid.deinit(); 1862 self.grid = null; 1863 } 1864 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1865 self.text_boxes.deinit(allocator); 1866 self.rects.deinit(allocator); 1867 self.arena.deinit(); 1868 } 1869 1870 // both the item text and the link are copied into the arena, so the caller's 1871 // slices needn't outlive this call. 1872 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 1873 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1874 self.text_boxes.clearAndFree(allocator); 1875 1876 _ = self.arena.reset(.retain_capacity); 1877 const aa = self.arena.allocator(); 1878 1879 self.focus.clear(); 1880 self.focus.child_id = null; 1881 1882 for (items) |item| { 1883 const line = try aa.dupe(u8, item.text); 1884 1885 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1886 errdefer text_box.deinit(allocator); 1887 text_box.getFocus().focusable = true; 1888 1889 if (item.link.len > 0) { 1890 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1891 } 1892 1893 try self.text_boxes.append(allocator, text_box); 1894 } 1895 1896 if (self.text_boxes.items.len > 0) { 1897 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1898 } 1899 } 1900 1901 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1902 self.clearGrid(); 1903 self.focus.clear(); 1904 1905 if (self.text_boxes.items.len == 0) return; 1906 const max_width = constraint.max_size.width orelse 80; 1907 if (max_width == 0) return; 1908 1909 // lay the items out left to right, wrapping to a new row when the next 1910 // one wouldn't fit. 1911 self.rects.clearRetainingCapacity(); 1912 var x: usize = 0; 1913 var y: usize = 0; 1914 var row_height: usize = 0; 1915 var total_width: usize = 0; 1916 for (self.text_boxes.items) |*tb| { 1917 try tb.build(allocator, .{ 1918 .min_size = .{ .width = null, .height = null }, 1919 .max_size = .{ .width = max_width, .height = null }, 1920 }, root_focus); 1921 const tb_grid_maybe = tb.getGrid(); 1922 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 1923 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 1924 if (x > 0 and x + w > max_width) { 1925 x = 0; 1926 y += row_height; 1927 row_height = 0; 1928 } 1929 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 1930 if (h > row_height) row_height = h; 1931 x += w; 1932 if (x > total_width) total_width = x; 1933 } 1934 1935 const total_height = y + row_height; 1936 if (total_width == 0 or total_height == 0) return; 1937 1938 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 1939 errdefer grid.deinit(); 1940 1941 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 1942 const tb_grid = tb.getGrid() orelse continue; 1943 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 1944 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 1945 } 1946 1947 self.grid = grid; 1948 } 1949 1950 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1951 _ = self; 1952 _ = allocator; 1953 _ = key; 1954 _ = root_focus; 1955 } 1956 1957 pub fn clearGrid(self: *TagFlow) void { 1958 if (self.grid) |*grid| { 1959 grid.deinit(); 1960 self.grid = null; 1961 } 1962 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1963 } 1964 1965 pub fn getGrid(self: TagFlow) ?Grid { 1966 return self.grid; 1967 } 1968 1969 pub fn getFocus(self: *TagFlow) *Focus { 1970 return self.focus; 1971 } 1972 1973 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 1974 for (self.text_boxes.items, 0..) |tb, i| { 1975 if (tb.box.focus.id == focus_id) return i; 1976 } 1977 return null; 1978 } 1979 1980 // the first item of the row below (when `downward`) or above, or null at 1981 // the flow's edge. items are laid out in order, so y is nondecreasing. 1982 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 1983 const rects = self.rects.items; 1984 if (index >= rects.len) return null; 1985 const cur_y = rects[index].y; 1986 if (downward) { 1987 for (rects[index + 1 ..], index + 1..) |r, i| { 1988 if (r.y > cur_y) return i; 1989 } 1990 return null; 1991 } 1992 // walk the earlier rows; the last one visited is the row above. 1993 var prev_first: ?usize = null; 1994 var i: usize = 0; 1995 while (i < rects.len and rects[i].y < cur_y) { 1996 prev_first = i; 1997 const y = rects[i].y; 1998 while (i < rects.len and rects[i].y == y) i += 1; 1999 } 2000 return prev_first; 2001 } 2002 }; 2003 2004 // an invisible widget that fills the horizontal space granted by its parent. 2005 // used inside Box(horiz) with a min_size so the box reserves space for the 2006 // children that follow, pushing them to the right. 2007 pub const Spacer = struct { 2008 focus: *Focus, 2009 grid: ?Grid, 2010 2011 pub fn init(allocator: std.mem.Allocator) !Spacer { 2012 return .{ 2013 .focus = try Focus.create(allocator, .container), 2014 .grid = null, 2015 }; 2016 } 2017 2018 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 2019 self.focus.destroy(allocator); 2020 if (self.grid) |*grid| { 2021 grid.deinit(); 2022 self.grid = null; 2023 } 2024 } 2025 2026 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2027 _ = root_focus; 2028 self.clearGrid(); 2029 const width = constraint.max_size.width orelse return; 2030 if (width == 0) return; 2031 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2032 } 2033 2034 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2035 _ = self; 2036 _ = allocator; 2037 _ = key; 2038 _ = root_focus; 2039 } 2040 2041 pub fn clearGrid(self: *Spacer) void { 2042 if (self.grid) |*grid| { 2043 grid.deinit(); 2044 self.grid = null; 2045 } 2046 } 2047 2048 pub fn getGrid(self: Spacer) ?Grid { 2049 return self.grid; 2050 } 2051 2052 pub fn getFocus(self: *Spacer) *Focus { 2053 return self.focus; 2054 } 2055 }; 2056 2057 // a one-row, non-focusable status bar showing the current page's url 2058 pub const Footer = struct { 2059 focus: *Focus, 2060 grid: ?Grid, 2061 session: *Session, 2062 arena: std.heap.ArenaAllocator, 2063 2064 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 2065 return .{ 2066 .focus = try Focus.create(allocator, .container), 2067 .grid = null, 2068 .session = session, 2069 .arena = std.heap.ArenaAllocator.init(allocator), 2070 }; 2071 } 2072 2073 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2074 self.focus.destroy(allocator); 2075 if (self.grid) |*grid| grid.deinit(); 2076 self.arena.deinit(); 2077 } 2078 2079 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2080 _ = root_focus; 2081 self.clearGrid(); 2082 const width = constraint.max_size.width orelse return; 2083 if (width == 0) return; 2084 2085 _ = self.arena.reset(.retain_capacity); 2086 const aa = self.arena.allocator(); 2087 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2088 const text = if (self.session.web_port) |port| 2089 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 2090 else 2091 path; 2092 2093 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2094 errdefer grid.deinit(); 2095 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2096 var i: usize = 0; 2097 while (utf8.nextCodepointSlice()) |ch| { 2098 if (i == width) break; 2099 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2100 i += 1; 2101 } 2102 self.grid = grid; 2103 } 2104 2105 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2106 _ = self; 2107 _ = allocator; 2108 _ = key; 2109 _ = root_focus; 2110 } 2111 2112 pub fn clearGrid(self: *Footer) void { 2113 if (self.grid) |*grid| { 2114 grid.deinit(); 2115 self.grid = null; 2116 } 2117 } 2118 2119 pub fn getGrid(self: Footer) ?Grid { 2120 return self.grid; 2121 } 2122 2123 pub fn getFocus(self: *Footer) *Focus { 2124 return self.focus; 2125 } 2126 }; 2127 2128 // a single-child wrapper that builds the child at its natural size and 2129 // positions its grid in the middle of the area granted by the parent 2130 // a bordered label, indented past the content it precedes 2131 pub const SectionLabel = struct { 2132 box: wgt.Box(Widget), 2133 2134 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 2135 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2136 errdefer box.deinit(allocator); 2137 2138 { 2139 var gap = try wgt.Text(Widget).init(allocator, " "); 2140 errdefer gap.deinit(allocator); 2141 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2142 } 2143 2144 { 2145 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2146 errdefer tb.deinit(allocator); 2147 tb.getFocus().focusable = true; 2148 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2149 } 2150 2151 box.getFocus().child_id = box.children.keys()[1]; 2152 return .{ .box = box }; 2153 } 2154 2155 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2156 self.box.deinit(allocator); 2157 } 2158 2159 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2160 try self.box.build(allocator, constraint, root_focus); 2161 } 2162 2163 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2164 _ = self; 2165 _ = allocator; 2166 _ = key; 2167 _ = root_focus; 2168 } 2169 2170 pub fn clearGrid(self: *SectionLabel) void { 2171 self.box.clearGrid(); 2172 } 2173 2174 pub fn getGrid(self: SectionLabel) ?Grid { 2175 return self.box.getGrid(); 2176 } 2177 2178 pub fn getFocus(self: *SectionLabel) *Focus { 2179 return self.box.getFocus(); 2180 } 2181 }; 2182 2183 // a submit button, indented past its form's content. the web overlay POSTs 2184 // the button's form; terminal hosts detect presses through `buttonId`. 2185 pub const SubmitButton = struct { 2186 box: wgt.Box(Widget), 2187 2188 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2189 return initLabeled(allocator, "submit"); 2190 } 2191 2192 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 2193 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2194 errdefer box.deinit(allocator); 2195 2196 { 2197 var gap = try wgt.Text(Widget).init(allocator, " "); 2198 errdefer gap.deinit(allocator); 2199 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2200 } 2201 2202 { 2203 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2204 errdefer button.deinit(allocator); 2205 button.getFocus().focusable = true; 2206 // the renderer distinguishes plain clickables from buttons that 2207 // should POST to a server route by this kind. 2208 button.getFocus().kind = .{ .custom = "submit" }; 2209 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2210 } 2211 2212 box.getFocus().child_id = box.children.keys()[1]; 2213 return .{ .box = box }; 2214 } 2215 2216 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2217 self.box.deinit(allocator); 2218 } 2219 2220 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2221 try self.box.build(allocator, constraint, root_focus); 2222 } 2223 2224 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2225 _ = self; 2226 _ = allocator; 2227 _ = key; 2228 _ = root_focus; 2229 } 2230 2231 pub fn clearGrid(self: *SubmitButton) void { 2232 self.box.clearGrid(); 2233 } 2234 2235 pub fn getGrid(self: SubmitButton) ?Grid { 2236 return self.box.getGrid(); 2237 } 2238 2239 pub fn getFocus(self: *SubmitButton) *Focus { 2240 return self.box.getFocus(); 2241 } 2242 2243 // the button's focus id, for click hit-testing 2244 pub fn buttonId(self: *const SubmitButton) usize { 2245 return self.box.children.keys()[1]; 2246 } 2247 }; 2248 2249 pub const Center = struct { 2250 focus: *Focus, 2251 grid: ?Grid, 2252 child: *Widget, 2253 2254 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2255 const child = try allocator.create(Widget); 2256 errdefer allocator.destroy(child); 2257 child.* = child_widget; 2258 return .{ 2259 .focus = try Focus.create(allocator, .container), 2260 .grid = null, 2261 .child = child, 2262 }; 2263 } 2264 2265 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2266 self.focus.destroy(allocator); 2267 if (self.grid) |*grid| { 2268 grid.deinit(); 2269 self.grid = null; 2270 } 2271 self.child.deinit(allocator); 2272 allocator.destroy(self.child); 2273 } 2274 2275 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2276 self.clearGrid(); 2277 self.getFocus().clear(); 2278 2279 // build the child without forcing it to fill min_size; it sizes 2280 // itself to its content within the available max. 2281 try self.child.build(allocator, .{ 2282 .min_size = .{ .width = null, .height = null }, 2283 .max_size = constraint.max_size, 2284 }, root_focus); 2285 2286 const child_grid = self.child.getGrid() orelse return; 2287 2288 // prefer max when given; otherwise grow to max(min, child) so that 2289 // when only a min is set (e.g. the wasm path passes viewport rows 2290 // as min_height with no max), we can still vertically center while 2291 // letting taller content extend past the min. 2292 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; 2293 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; 2294 if (width == 0 or height == 0) return; 2295 2296 const offset_x: usize = (width -| child_grid.size.width) / 2; 2297 const offset_y: usize = (height -| child_grid.size.height) / 2; 2298 2299 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2300 errdefer grid.deinit(); 2301 try grid.drawGrid(child_grid, offset_x, offset_y); 2302 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2303 self.getFocus().child_id = self.child.getFocus().id; 2304 2305 self.grid = grid; 2306 } 2307 2308 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2309 try self.child.input(allocator, key, root_focus); 2310 } 2311 2312 pub fn clearGrid(self: *Center) void { 2313 if (self.grid) |*grid| { 2314 grid.deinit(); 2315 self.grid = null; 2316 } 2317 self.child.clearGrid(); 2318 } 2319 2320 pub fn getGrid(self: Center) ?Grid { 2321 return self.grid; 2322 } 2323 2324 pub fn getFocus(self: *Center) *Focus { 2325 return self.focus; 2326 } 2327 }; 2328 2329 // renders truecolor ANSI art 2330 pub const AnsiArt = struct { 2331 focus: *Focus, 2332 grid: ?Grid, 2333 content: []const u8, 2334 2335 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2336 return .{ 2337 .focus = try Focus.create(allocator, .container), 2338 .grid = null, 2339 .content = content, 2340 }; 2341 } 2342 2343 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2344 self.focus.destroy(allocator); 2345 if (self.grid) |*grid| { 2346 grid.deinit(); 2347 self.grid = null; 2348 } 2349 } 2350 2351 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2352 _ = root_focus; 2353 self.clearGrid(); 2354 2355 // parse into rows of cells, then copy into a rectangular grid 2356 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2357 defer { 2358 for (rows.items) |*row| row.deinit(allocator); 2359 rows.deinit(allocator); 2360 } 2361 var row: std.ArrayList(Grid.Cell) = .empty; 2362 errdefer row.deinit(allocator); 2363 2364 var style: Grid.Style = .{}; 2365 var width: usize = 0; 2366 const content = self.content; 2367 var i: usize = 0; 2368 while (i < content.len) { 2369 const byte = content[i]; 2370 if (byte == '\n') { 2371 width = @max(width, row.items.len); 2372 try rows.append(allocator, row); 2373 row = .empty; 2374 i += 1; 2375 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2376 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2377 var j = i + 2; 2378 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2379 if (j >= content.len) { 2380 i = content.len; // malformed trailing escape; stop 2381 } else { 2382 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2383 i = j + 1; 2384 } 2385 } else { 2386 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2387 const end = @min(content.len, i + len); 2388 const rune = content[i..end]; 2389 const transparent = end - i == 1 and rune[0] == ' ' and 2390 style.fg == null and style.bg == null and !style.inverted; 2391 try row.append(allocator, .{ 2392 .rune = if (transparent) null else rune, 2393 .style = style, 2394 }); 2395 i = end; 2396 } 2397 } 2398 // a trailing row with content but no closing newline 2399 if (row.items.len > 0) { 2400 width = @max(width, row.items.len); 2401 try rows.append(allocator, row); 2402 } else { 2403 row.deinit(allocator); 2404 } 2405 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2406 2407 const height = rows.items.len; 2408 if (width == 0 or height == 0) return; 2409 2410 const clamped_w = @min(width, constraint.max_size.width orelse width); 2411 const clamped_h = @min(height, constraint.max_size.height orelse height); 2412 2413 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2414 errdefer grid.deinit(); 2415 for (rows.items[0..clamped_h], 0..) |r, y| { 2416 const n = @min(r.items.len, clamped_w); 2417 for (r.items[0..n], 0..) |cell, x| { 2418 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2419 } 2420 } 2421 self.grid = grid; 2422 } 2423 2424 pub fn clearGrid(self: *AnsiArt) void { 2425 if (self.grid) |*grid| { 2426 grid.deinit(); 2427 self.grid = null; 2428 } 2429 } 2430 2431 pub fn getGrid(self: AnsiArt) ?Grid { 2432 return self.grid; 2433 } 2434 2435 pub fn getFocus(self: *AnsiArt) *Focus { 2436 return self.focus; 2437 } 2438 2439 fn applySgr(style: *Grid.Style, params: []const u8) void { 2440 var nums: [16]u32 = undefined; 2441 var n: usize = 0; 2442 var it = std.mem.splitScalar(u8, params, ';'); 2443 while (it.next()) |tok| { 2444 if (n >= nums.len) break; 2445 // an empty parameter (e.g. bare "\x1b[m") means 0 2446 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2447 n += 1; 2448 } 2449 if (n == 0) { 2450 style.* = .{}; 2451 return; 2452 } 2453 var i: usize = 0; 2454 while (i < n) : (i += 1) { 2455 switch (nums[i]) { 2456 0 => style.* = .{}, 2457 7 => style.inverted = true, 2458 27 => style.inverted = false, 2459 39 => style.fg = null, 2460 49 => style.bg = null, 2461 38, 48 => { 2462 // truecolor form: 38;2;r;g;b — anything else is ignored 2463 if (i + 4 < n and nums[i + 1] == 2) { 2464 const c = Grid.Color{ 2465 .r = @truncate(nums[i + 2]), 2466 .g = @truncate(nums[i + 3]), 2467 .b = @truncate(nums[i + 4]), 2468 }; 2469 if (nums[i] == 38) style.fg = c else style.bg = c; 2470 i += 4; 2471 } 2472 }, 2473 else => {}, 2474 } 2475 } 2476 } 2477 }; 2478 2479 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2480 pub const AnsiBackground = struct { 2481 grid: ?Grid, 2482 child: *Widget, 2483 art: AnsiArt, 2484 session: *Session, 2485 2486 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2487 var cw = child_widget; 2488 const child = allocator.create(Widget) catch |e| { 2489 cw.deinit(allocator); 2490 return e; 2491 }; 2492 child.* = cw; 2493 errdefer { 2494 child.deinit(allocator); 2495 allocator.destroy(child); 2496 } 2497 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2498 } 2499 2500 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2501 if (self.grid) |*grid| grid.deinit(); 2502 self.art.deinit(allocator); 2503 self.child.deinit(allocator); 2504 allocator.destroy(self.child); 2505 } 2506 2507 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2508 self.clearGrid(); 2509 2510 // make the wrapped page fill the whole available area 2511 try self.child.build(allocator, .{ 2512 .min_size = .{ 2513 .width = constraint.max_size.width orelse constraint.min_size.width, 2514 .height = constraint.max_size.height orelse constraint.min_size.height, 2515 }, 2516 .max_size = constraint.max_size, 2517 }, root_focus); 2518 2519 if (self.session.data.enable_ansi) { 2520 if (self.child.getGrid()) |fg| { 2521 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2522 } 2523 } 2524 } 2525 2526 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2527 try self.child.input(allocator, key, root_focus); 2528 } 2529 2530 pub fn clearGrid(self: *AnsiBackground) void { 2531 if (self.grid) |*grid| { 2532 grid.deinit(); 2533 self.grid = null; 2534 } 2535 self.child.clearGrid(); 2536 self.art.clearGrid(); 2537 } 2538 2539 pub fn getGrid(self: AnsiBackground) ?Grid { 2540 return self.grid orelse self.child.getGrid(); 2541 } 2542 2543 pub fn getFocus(self: *AnsiBackground) *Focus { 2544 return self.child.getFocus(); 2545 } 2546 2547 // which top corner the art hugs. 2548 const ArtAnchor = enum { top_left, top_right }; 2549 2550 // a foreground cell counts as blank if it's empty or a bare space without styling 2551 fn cellIsBlank(cell: Grid.Cell) bool { 2552 if (cell.rune) |rune| { 2553 return std.mem.eql(u8, rune, " ") and 2554 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2555 } 2556 return true; 2557 } 2558 2559 // the art is dimmed to this fraction of its brightness so foreground text 2560 // stays legible over it 2561 const art_brightness = 45; // percent 2562 2563 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2564 const c = color orelse return null; 2565 return .{ 2566 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2567 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2568 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2569 }; 2570 } 2571 2572 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2573 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2574 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2575 // snap such channels clear of that range so the backdrop doesn't blink or 2576 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2577 // actually render truecolor. 2578 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2579 const c = color orelse return null; 2580 const snap = struct { 2581 fn f(v: u8) u8 { 2582 return if (v >= 1 and v <= 9) 10 else v; 2583 } 2584 }.f; 2585 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2586 } 2587 2588 // composites ANSI art behind a foreground grid 2589 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2590 art.clearGrid(); 2591 try art.build(allocator, .{ 2592 .min_size = .{ .width = null, .height = null }, 2593 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2594 }, root_focus); 2595 2596 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2597 errdefer out.deinit(); 2598 2599 if (art.getGrid()) |art_grid| { 2600 const anchor_x = switch (anchor) { 2601 .top_left => 0, 2602 .top_right => foreground.size.width -| art_grid.size.width, 2603 }; 2604 for (0..art_grid.size.height) |y| { 2605 for (0..art_grid.size.width) |x| { 2606 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2607 if (src.rune == null) continue; 2608 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2609 const dst = &out.cells.items[idx]; 2610 // composite the dimmed art behind every cell so it shows 2611 // through the whole UI: a blank cell becomes the art, while a 2612 // cell with a glyph keeps the glyph but takes the dimmed art as 2613 // its background (replacing whatever background it had, which 2614 // would otherwise obscure the art). 2615 if (cellIsBlank(dst.*)) { 2616 dst.* = src; 2617 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2618 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2619 } else { 2620 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2621 dst.style.bg = background_maybe; 2622 // change the color of text overlaying ANSI art to ensure 2623 // it is readable. don't do this in the web UI, since we 2624 // have direct control of the theme there (also, inner 2625 // scroll panes allow text to move without a re-render 2626 // by the TUI, which would break this). also don't do this 2627 // if the text is explicitly styled. 2628 if (is_terminal and dst.style.fg == null) { 2629 if (background_maybe) |background| { 2630 // use near-black and near-white, which is easier on the eyes 2631 // while retaining high contrast 2632 const luminance = (@as(u32, background.r) * 299 + 2633 @as(u32, background.g) * 587 + 2634 @as(u32, background.b) * 114) / 1000; 2635 dst.style.fg = if (luminance >= 128) 2636 .{ .r = 16, .g = 16, .b = 16 } 2637 else 2638 .{ .r = 240, .g = 240, .b = 240 }; 2639 } 2640 } 2641 } 2642 } 2643 } 2644 } 2645 return out; 2646 } 2647 }; 2648