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