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