╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1514 1515 if (self.text_boxes.items.len > 0) { 1516 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1517 } 1518 } 1519 1520 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1521 self.clearGrid(); 1522 self.focus.clear(); 1523 1524 const cell_width = self.options.cell_width; 1525 const max_width = constraint.max_size.width orelse cell_width; 1526 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1527 self.last_cols = cols; 1528 1529 const count = self.text_boxes.items.len; 1530 if (count == 0) return; 1531 const slot_height = self.options.cell_height + border_rows; 1532 if (slot_height == 0) return; 1533 1534 // build at the slot size so every text box fits a single grid cell 1535 for (self.text_boxes.items) |*tb| { 1536 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1537 try tb.build(allocator, .{ 1538 .min_size = .{ .width = cell_width, .height = null }, 1539 .max_size = .{ .width = cell_width, .height = slot_height }, 1540 }, root_focus); 1541 } 1542 1543 const rows = (count + cols - 1) / cols; 1544 const content_height = rows * slot_height; 1545 const total_width = cols * cell_width; 1546 if (total_width == 0 or content_height == 0) return; 1547 1548 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1549 errdefer grid.deinit(); 1550 1551 for (self.text_boxes.items, 0..) |*tb, i| { 1552 const tb_grid = tb.getGrid() orelse continue; 1553 const col = i % cols; 1554 const row = i / cols; 1555 const cell_x = col * cell_width; 1556 const cell_y = row * slot_height; 1557 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1558 try grid.drawGrid(tb_grid, cell_x, cell_y); 1559 } 1560 1561 self.grid = grid; 1562 } 1563 1564 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1565 _ = self; 1566 _ = allocator; 1567 _ = key; 1568 _ = root_focus; 1569 } 1570 1571 pub fn clearGrid(self: *FlowBox) void { 1572 if (self.grid) |*grid| { 1573 grid.deinit(); 1574 self.grid = null; 1575 } 1576 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1577 } 1578 1579 pub fn getGrid(self: FlowBox) ?Grid { 1580 return self.grid; 1581 } 1582 1583 pub fn getFocus(self: *FlowBox) *Focus { 1584 return self.focus; 1585 } 1586 1587 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1588 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1589 const slot_height = self.options.cell_height + border_rows; 1590 const col = index % self.last_cols; 1591 const row = index / self.last_cols; 1592 return .{ 1593 .x = @intCast(col * self.options.cell_width), 1594 .y = @intCast(row * slot_height), 1595 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1596 }; 1597 } 1598 1599 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1600 for (self.text_boxes.items, 0..) |tb, i| { 1601 if (tb.box.focus.id == focus_id) return i; 1602 } 1603 return null; 1604 } 1605 1606 pub const Scroll = struct { 1607 scroll: wgt.Scroll(Widget), 1608 1609 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1610 var layout_inner = try FlowBox.init(allocator, options); 1611 errdefer layout_inner.deinit(allocator); 1612 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1613 errdefer scroll.deinit(allocator); 1614 return .{ .scroll = scroll }; 1615 } 1616 1617 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1618 self.scroll.deinit(allocator); 1619 } 1620 1621 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1622 self.scroll.x = 0; 1623 self.scroll.y = 0; 1624 try self.inner().setItems(allocator, items); 1625 } 1626 1627 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1628 try self.scroll.build(allocator, constraint, root_focus); 1629 } 1630 1631 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1632 _ = allocator; 1633 const in = self.inner(); 1634 const child_id = in.focus.child_id orelse return; 1635 const current_index = in.indexOfFocusId(child_id) orelse return; 1636 const count = in.text_boxes.items.len; 1637 if (count == 0) return; 1638 const cols = in.last_cols; 1639 const slot_height = in.options.cell_height + FlowBox.border_rows; 1640 1641 var index = current_index; 1642 switch (key) { 1643 .arrow_up => index -|= cols, 1644 .arrow_down => if (index + cols < count) { 1645 index += cols; 1646 }, 1647 .arrow_left => index -|= 1, 1648 .arrow_right => if (index + 1 < count) { 1649 index += 1; 1650 }, 1651 .home => index = 0, 1652 .end => index = count - 1, 1653 .page_up => { 1654 if (self.scroll.grid) |grid| if (slot_height > 0) { 1655 const rows_per_page = grid.size.height / slot_height; 1656 index -|= rows_per_page * cols; 1657 }; 1658 }, 1659 .page_down => { 1660 if (self.scroll.grid) |grid| if (slot_height > 0) { 1661 const rows_per_page = grid.size.height / slot_height; 1662 index = @min(index + rows_per_page * cols, count - 1); 1663 }; 1664 }, 1665 // scroll wheel moves the focused cell by a full row so the 1666 // viewport (via scrollToRect) follows in row-sized steps, 1667 // matching how a scroll wheel feels in a grid view. 1668 .mouse => |mouse| switch (mouse.action) { 1669 .scroll => |dir| switch (dir) { 1670 .up => index -|= cols, 1671 .down => if (index + cols < count) { 1672 index += cols; 1673 }, 1674 }, 1675 else => {}, 1676 }, 1677 else => {}, 1678 } 1679 1680 if (index != current_index) { 1681 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1682 if (in.cellRect(index)) |rect| { 1683 self.scroll.scrollToRect(rect); 1684 } 1685 } 1686 } 1687 1688 pub fn clearGrid(self: *Scroll) void { 1689 self.scroll.clearGrid(); 1690 } 1691 1692 pub fn getGrid(self: Scroll) ?Grid { 1693 return self.scroll.getGrid(); 1694 } 1695 1696 pub fn getFocus(self: *Scroll) *Focus { 1697 return self.scroll.getFocus(); 1698 } 1699 1700 pub fn getSelectedIndex(self: Scroll) ?usize { 1701 const in = self.scroll.child.flow_box; 1702 const child_id = in.focus.child_id orelse return null; 1703 return in.indexOfFocusId(child_id); 1704 } 1705 1706 fn inner(self: *Scroll) *FlowBox { 1707 return &self.scroll.child.flow_box; 1708 } 1709 }; 1710 }; 1711 1712 // a left-to-right flow of variable-width focusable items that wraps at the 1713 // available width, like words in a paragraph. unlike FlowBox there is no cell 1714 // grid: each item is as wide as its text. selection movement is driven by the 1715 // owning view via indexOfFocusId/verticalNeighbor. 1716 pub const TagFlow = struct { 1717 focus: *Focus, 1718 grid: ?Grid, 1719 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1720 // last build's item rects (content space), for vertical navigation and 1721 // scroll-into-view. 1722 rects: std.ArrayList(layout.IRect), 1723 // backs the per-item strings the text boxes borrow. 1724 arena: std.heap.ArenaAllocator, 1725 1726 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1727 1728 pub fn init(allocator: std.mem.Allocator) !TagFlow { 1729 return .{ 1730 .focus = try Focus.create(allocator, .container), 1731 .grid = null, 1732 .text_boxes = .empty, 1733 .rects = .empty, 1734 .arena = std.heap.ArenaAllocator.init(allocator), 1735 }; 1736 } 1737 1738 pub fn deinit(self: *TagFlow, allocator: std.mem.Allocator) void { 1739 self.focus.destroy(allocator); 1740 if (self.grid) |*grid| { 1741 grid.deinit(); 1742 self.grid = null; 1743 } 1744 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1745 self.text_boxes.deinit(allocator); 1746 self.rects.deinit(allocator); 1747 self.arena.deinit(); 1748 } 1749 1750 // both the item text and the link are copied into the arena, so the caller's 1751 // slices needn't outlive this call. 1752 pub fn setItems(self: *TagFlow, allocator: std.mem.Allocator, items: []const Item) !void { 1753 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1754 self.text_boxes.clearAndFree(allocator); 1755 1756 _ = self.arena.reset(.retain_capacity); 1757 const aa = self.arena.allocator(); 1758 1759 self.focus.clear(); 1760 self.focus.child_id = null; 1761 1762 for (items) |item| { 1763 const line = try aa.dupe(u8, item.text); 1764 1765 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1766 errdefer text_box.deinit(allocator); 1767 text_box.getFocus().focusable = true; 1768 1769 if (item.link.len > 0) { 1770 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1771 } 1772 1773 try self.text_boxes.append(allocator, text_box); 1774 } 1775 1776 if (self.text_boxes.items.len > 0) { 1777 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1778 } 1779 } 1780 1781 pub fn build(self: *TagFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1782 self.clearGrid(); 1783 self.focus.clear(); 1784 1785 if (self.text_boxes.items.len == 0) return; 1786 const max_width = constraint.max_size.width orelse 80; 1787 if (max_width == 0) return; 1788 1789 // lay the items out left to right, wrapping to a new row when the next 1790 // one wouldn't fit. 1791 self.rects.clearRetainingCapacity(); 1792 var x: usize = 0; 1793 var y: usize = 0; 1794 var row_height: usize = 0; 1795 var total_width: usize = 0; 1796 for (self.text_boxes.items) |*tb| { 1797 try tb.build(allocator, .{ 1798 .min_size = .{ .width = null, .height = null }, 1799 .max_size = .{ .width = max_width, .height = null }, 1800 }, root_focus); 1801 const tb_grid_maybe = tb.getGrid(); 1802 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 1803 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 1804 if (x > 0 and x + w > max_width) { 1805 x = 0; 1806 y += row_height; 1807 row_height = 0; 1808 } 1809 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 1810 if (h > row_height) row_height = h; 1811 x += w; 1812 if (x > total_width) total_width = x; 1813 } 1814 1815 const total_height = y + row_height; 1816 if (total_width == 0 or total_height == 0) return; 1817 1818 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 1819 errdefer grid.deinit(); 1820 1821 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 1822 const tb_grid = tb.getGrid() orelse continue; 1823 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 1824 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 1825 } 1826 1827 self.grid = grid; 1828 } 1829 1830 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1831 _ = self; 1832 _ = allocator; 1833 _ = key; 1834 _ = root_focus; 1835 } 1836 1837 pub fn clearGrid(self: *TagFlow) void { 1838 if (self.grid) |*grid| { 1839 grid.deinit(); 1840 self.grid = null; 1841 } 1842 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1843 } 1844 1845 pub fn getGrid(self: TagFlow) ?Grid { 1846 return self.grid; 1847 } 1848 1849 pub fn getFocus(self: *TagFlow) *Focus { 1850 return self.focus; 1851 } 1852 1853 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 1854 for (self.text_boxes.items, 0..) |tb, i| { 1855 if (tb.box.focus.id == focus_id) return i; 1856 } 1857 return null; 1858 } 1859 1860 // the first item of the row below (when `downward`) or above, or null at 1861 // the flow's edge. items are laid out in order, so y is nondecreasing. 1862 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 1863 const rects = self.rects.items; 1864 if (index >= rects.len) return null; 1865 const cur_y = rects[index].y; 1866 if (downward) { 1867 for (rects[index + 1 ..], index + 1..) |r, i| { 1868 if (r.y > cur_y) return i; 1869 } 1870 return null; 1871 } 1872 // walk the earlier rows; the last one visited is the row above. 1873 var prev_first: ?usize = null; 1874 var i: usize = 0; 1875 while (i < rects.len and rects[i].y < cur_y) { 1876 prev_first = i; 1877 const y = rects[i].y; 1878 while (i < rects.len and rects[i].y == y) i += 1; 1879 } 1880 return prev_first; 1881 } 1882 }; 1883 1884 // an invisible widget that fills the horizontal space granted by its parent. 1885 // used inside Box(horiz) with a min_size so the box reserves space for the 1886 // children that follow, pushing them to the right. 1887 pub const Spacer = struct { 1888 focus: *Focus, 1889 grid: ?Grid, 1890 1891 pub fn init(allocator: std.mem.Allocator) !Spacer { 1892 return .{ 1893 .focus = try Focus.create(allocator, .container), 1894 .grid = null, 1895 }; 1896 } 1897 1898 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1899 self.focus.destroy(allocator); 1900 if (self.grid) |*grid| { 1901 grid.deinit(); 1902 self.grid = null; 1903 } 1904 } 1905 1906 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1907 _ = root_focus; 1908 self.clearGrid(); 1909 const width = constraint.max_size.width orelse return; 1910 if (width == 0) return; 1911 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1912 } 1913 1914 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1915 _ = self; 1916 _ = allocator; 1917 _ = key; 1918 _ = root_focus; 1919 } 1920 1921 pub fn clearGrid(self: *Spacer) void { 1922 if (self.grid) |*grid| { 1923 grid.deinit(); 1924 self.grid = null; 1925 } 1926 } 1927 1928 pub fn getGrid(self: Spacer) ?Grid { 1929 return self.grid; 1930 } 1931 1932 pub fn getFocus(self: *Spacer) *Focus { 1933 return self.focus; 1934 } 1935 }; 1936 1937 // a one-row, non-focusable status bar showing the current page's url 1938 pub const Footer = struct { 1939 focus: *Focus, 1940 grid: ?Grid, 1941 session: *Session, 1942 arena: std.heap.ArenaAllocator, 1943 1944 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1945 return .{ 1946 .focus = try Focus.create(allocator, .container), 1947 .grid = null, 1948 .session = session, 1949 .arena = std.heap.ArenaAllocator.init(allocator), 1950 }; 1951 } 1952 1953 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1954 self.focus.destroy(allocator); 1955 if (self.grid) |*grid| grid.deinit(); 1956 self.arena.deinit(); 1957 } 1958 1959 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1960 _ = root_focus; 1961 self.clearGrid(); 1962 const width = constraint.max_size.width orelse return; 1963 if (width == 0) return; 1964 1965 _ = self.arena.reset(.retain_capacity); 1966 const aa = self.arena.allocator(); 1967 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1968 const text = if (self.session.web_port) |port| 1969 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1970 else 1971 path; 1972 1973 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1974 errdefer grid.deinit(); 1975 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1976 var i: usize = 0; 1977 while (utf8.nextCodepointSlice()) |ch| { 1978 if (i == width) break; 1979 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1980 i += 1; 1981 } 1982 self.grid = grid; 1983 } 1984 1985 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1986 _ = self; 1987 _ = allocator; 1988 _ = key; 1989 _ = root_focus; 1990 } 1991 1992 pub fn clearGrid(self: *Footer) void { 1993 if (self.grid) |*grid| { 1994 grid.deinit(); 1995 self.grid = null; 1996 } 1997 } 1998 1999 pub fn getGrid(self: Footer) ?Grid { 2000 return self.grid; 2001 } 2002 2003 pub fn getFocus(self: *Footer) *Focus { 2004 return self.focus; 2005 } 2006 }; 2007 2008 // a single-child wrapper that builds the child at its natural size and 2009 // positions its grid in the middle of the area granted by the parent 2010 pub const Center = struct { 2011 focus: *Focus, 2012 grid: ?Grid, 2013 child: *Widget, 2014 2015 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2016 const child = try allocator.create(Widget); 2017 errdefer allocator.destroy(child); 2018 child.* = child_widget; 2019 return .{ 2020 .focus = try Focus.create(allocator, .container), 2021 .grid = null, 2022 .child = child, 2023 }; 2024 } 2025 2026 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2027 self.focus.destroy(allocator); 2028 if (self.grid) |*grid| { 2029 grid.deinit(); 2030 self.grid = null; 2031 } 2032 self.child.deinit(allocator); 2033 allocator.destroy(self.child); 2034 } 2035 2036 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2037 self.clearGrid(); 2038 self.getFocus().clear(); 2039 2040 // build the child without forcing it to fill min_size; it sizes 2041 // itself to its content within the available max. 2042 try self.child.build(allocator, .{ 2043 .min_size = .{ .width = null, .height = null }, 2044 .max_size = constraint.max_size, 2045 }, root_focus); 2046 2047 const child_grid = self.child.getGrid() orelse return; 2048 2049 // prefer max when given; otherwise grow to max(min, child) so that 2050 // when only a min is set (e.g. the wasm path passes viewport rows 2051 // as min_height with no max), we can still vertically center while 2052 // letting taller content extend past the min. 2053 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; 2054 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; 2055 if (width == 0 or height == 0) return; 2056 2057 const offset_x: usize = (width -| child_grid.size.width) / 2; 2058 const offset_y: usize = (height -| child_grid.size.height) / 2; 2059 2060 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2061 errdefer grid.deinit(); 2062 try grid.drawGrid(child_grid, offset_x, offset_y); 2063 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2064 self.getFocus().child_id = self.child.getFocus().id; 2065 2066 self.grid = grid; 2067 } 2068 2069 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2070 try self.child.input(allocator, key, root_focus); 2071 } 2072 2073 pub fn clearGrid(self: *Center) void { 2074 if (self.grid) |*grid| { 2075 grid.deinit(); 2076 self.grid = null; 2077 } 2078 self.child.clearGrid(); 2079 } 2080 2081 pub fn getGrid(self: Center) ?Grid { 2082 return self.grid; 2083 } 2084 2085 pub fn getFocus(self: *Center) *Focus { 2086 return self.focus; 2087 } 2088 }; 2089 2090 // renders truecolor ANSI art 2091 pub const AnsiArt = struct { 2092 focus: *Focus, 2093 grid: ?Grid, 2094 content: []const u8, 2095 2096 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2097 return .{ 2098 .focus = try Focus.create(allocator, .container), 2099 .grid = null, 2100 .content = content, 2101 }; 2102 } 2103 2104 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2105 self.focus.destroy(allocator); 2106 if (self.grid) |*grid| { 2107 grid.deinit(); 2108 self.grid = null; 2109 } 2110 } 2111 2112 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2113 _ = root_focus; 2114 self.clearGrid(); 2115 2116 // parse into rows of cells, then copy into a rectangular grid 2117 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2118 defer { 2119 for (rows.items) |*row| row.deinit(allocator); 2120 rows.deinit(allocator); 2121 } 2122 var row: std.ArrayList(Grid.Cell) = .empty; 2123 errdefer row.deinit(allocator); 2124 2125 var style: Grid.Style = .{}; 2126 var width: usize = 0; 2127 const content = self.content; 2128 var i: usize = 0; 2129 while (i < content.len) { 2130 const byte = content[i]; 2131 if (byte == '\n') { 2132 width = @max(width, row.items.len); 2133 try rows.append(allocator, row); 2134 row = .empty; 2135 i += 1; 2136 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2137 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2138 var j = i + 2; 2139 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2140 if (j >= content.len) { 2141 i = content.len; // malformed trailing escape; stop 2142 } else { 2143 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2144 i = j + 1; 2145 } 2146 } else { 2147 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2148 const end = @min(content.len, i + len); 2149 const rune = content[i..end]; 2150 const transparent = end - i == 1 and rune[0] == ' ' and 2151 style.fg == null and style.bg == null and !style.inverted; 2152 try row.append(allocator, .{ 2153 .rune = if (transparent) null else rune, 2154 .style = style, 2155 }); 2156 i = end; 2157 } 2158 } 2159 // a trailing row with content but no closing newline 2160 if (row.items.len > 0) { 2161 width = @max(width, row.items.len); 2162 try rows.append(allocator, row); 2163 } else { 2164 row.deinit(allocator); 2165 } 2166 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2167 2168 const height = rows.items.len; 2169 if (width == 0 or height == 0) return; 2170 2171 const clamped_w = @min(width, constraint.max_size.width orelse width); 2172 const clamped_h = @min(height, constraint.max_size.height orelse height); 2173 2174 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2175 errdefer grid.deinit(); 2176 for (rows.items[0..clamped_h], 0..) |r, y| { 2177 const n = @min(r.items.len, clamped_w); 2178 for (r.items[0..n], 0..) |cell, x| { 2179 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2180 } 2181 } 2182 self.grid = grid; 2183 } 2184 2185 pub fn clearGrid(self: *AnsiArt) void { 2186 if (self.grid) |*grid| { 2187 grid.deinit(); 2188 self.grid = null; 2189 } 2190 } 2191 2192 pub fn getGrid(self: AnsiArt) ?Grid { 2193 return self.grid; 2194 } 2195 2196 pub fn getFocus(self: *AnsiArt) *Focus { 2197 return self.focus; 2198 } 2199 2200 fn applySgr(style: *Grid.Style, params: []const u8) void { 2201 var nums: [16]u32 = undefined; 2202 var n: usize = 0; 2203 var it = std.mem.splitScalar(u8, params, ';'); 2204 while (it.next()) |tok| { 2205 if (n >= nums.len) break; 2206 // an empty parameter (e.g. bare "\x1b[m") means 0 2207 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2208 n += 1; 2209 } 2210 if (n == 0) { 2211 style.* = .{}; 2212 return; 2213 } 2214 var i: usize = 0; 2215 while (i < n) : (i += 1) { 2216 switch (nums[i]) { 2217 0 => style.* = .{}, 2218 7 => style.inverted = true, 2219 27 => style.inverted = false, 2220 39 => style.fg = null, 2221 49 => style.bg = null, 2222 38, 48 => { 2223 // truecolor form: 38;2;r;g;b — anything else is ignored 2224 if (i + 4 < n and nums[i + 1] == 2) { 2225 const c = Grid.Color{ 2226 .r = @truncate(nums[i + 2]), 2227 .g = @truncate(nums[i + 3]), 2228 .b = @truncate(nums[i + 4]), 2229 }; 2230 if (nums[i] == 38) style.fg = c else style.bg = c; 2231 i += 4; 2232 } 2233 }, 2234 else => {}, 2235 } 2236 } 2237 } 2238 }; 2239 2240 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2241 pub const AnsiBackground = struct { 2242 grid: ?Grid, 2243 child: *Widget, 2244 art: AnsiArt, 2245 session: *Session, 2246 2247 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2248 var cw = child_widget; 2249 const child = allocator.create(Widget) catch |e| { 2250 cw.deinit(allocator); 2251 return e; 2252 }; 2253 child.* = cw; 2254 errdefer { 2255 child.deinit(allocator); 2256 allocator.destroy(child); 2257 } 2258 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2259 } 2260 2261 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2262 if (self.grid) |*grid| grid.deinit(); 2263 self.art.deinit(allocator); 2264 self.child.deinit(allocator); 2265 allocator.destroy(self.child); 2266 } 2267 2268 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2269 self.clearGrid(); 2270 2271 // make the wrapped page fill the whole available area 2272 try self.child.build(allocator, .{ 2273 .min_size = .{ 2274 .width = constraint.max_size.width orelse constraint.min_size.width, 2275 .height = constraint.max_size.height orelse constraint.min_size.height, 2276 }, 2277 .max_size = constraint.max_size, 2278 }, root_focus); 2279 2280 if (self.session.data.enable_ansi) { 2281 if (self.child.getGrid()) |fg| { 2282 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2283 } 2284 } 2285 } 2286 2287 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2288 try self.child.input(allocator, key, root_focus); 2289 } 2290 2291 pub fn clearGrid(self: *AnsiBackground) void { 2292 if (self.grid) |*grid| { 2293 grid.deinit(); 2294 self.grid = null; 2295 } 2296 self.child.clearGrid(); 2297 self.art.clearGrid(); 2298 } 2299 2300 pub fn getGrid(self: AnsiBackground) ?Grid { 2301 return self.grid orelse self.child.getGrid(); 2302 } 2303 2304 pub fn getFocus(self: *AnsiBackground) *Focus { 2305 return self.child.getFocus(); 2306 } 2307 2308 // which top corner the art hugs. 2309 const ArtAnchor = enum { top_left, top_right }; 2310 2311 // a foreground cell counts as blank if it's empty or a bare space without styling 2312 fn cellIsBlank(cell: Grid.Cell) bool { 2313 if (cell.rune) |rune| { 2314 return std.mem.eql(u8, rune, " ") and 2315 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2316 } 2317 return true; 2318 } 2319 2320 // the art is dimmed to this fraction of its brightness so foreground text 2321 // stays legible over it 2322 const art_brightness = 45; // percent 2323 2324 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2325 const c = color orelse return null; 2326 return .{ 2327 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2328 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2329 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2330 }; 2331 } 2332 2333 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2334 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2335 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2336 // snap such channels clear of that range so the backdrop doesn't blink or 2337 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2338 // actually render truecolor. 2339 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2340 const c = color orelse return null; 2341 const snap = struct { 2342 fn f(v: u8) u8 { 2343 return if (v >= 1 and v <= 9) 10 else v; 2344 } 2345 }.f; 2346 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2347 } 2348 2349 // composites ANSI art behind a foreground grid 2350 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2351 art.clearGrid(); 2352 try art.build(allocator, .{ 2353 .min_size = .{ .width = null, .height = null }, 2354 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2355 }, root_focus); 2356 2357 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2358 errdefer out.deinit(); 2359 2360 if (art.getGrid()) |art_grid| { 2361 const anchor_x = switch (anchor) { 2362 .top_left => 0, 2363 .top_right => foreground.size.width -| art_grid.size.width, 2364 }; 2365 for (0..art_grid.size.height) |y| { 2366 for (0..art_grid.size.width) |x| { 2367 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2368 if (src.rune == null) continue; 2369 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2370 const dst = &out.cells.items[idx]; 2371 // composite the dimmed art behind every cell so it shows 2372 // through the whole UI: a blank cell becomes the art, while a 2373 // cell with a glyph keeps the glyph but takes the dimmed art as 2374 // its background (replacing whatever background it had, which 2375 // would otherwise obscure the art). 2376 if (cellIsBlank(dst.*)) { 2377 dst.* = src; 2378 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2379 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2380 } else { 2381 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2382 } 2383 } 2384 } 2385 } 2386 return out; 2387 } 2388 }; 2389