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