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