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