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