╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1373 } 1374 1375 pub fn build(self: *SearchHeader, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1376 self.clearGrid(); 1377 try self.scroll.build(allocator, constraint, root_focus); 1378 } 1379 1380 pub fn input(self: *SearchHeader, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1381 const current = self.scroll.row().getFocus().child_id orelse return; 1382 if (self.searchBox()) |search_box| { 1383 if (current == search_box.getFocus().id) { 1384 // tab, and right-arrow past the last character, step to the 1385 // clone url beside the box 1386 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 1387 if (self.cloneUrl()) |clone_url| { 1388 root_focus.setFocus(clone_url.getFocus().id); 1389 return; 1390 } 1391 } 1392 return search_box.input(allocator, key, root_focus); 1393 } 1394 } 1395 const clone_url = self.cloneUrl() orelse return; 1396 if (key == .back_tab or (key == .arrow_left and clone_url.selected == 0)) { 1397 if (self.searchBox()) |search_box| { 1398 root_focus.setFocus(search_box.getFocus().id); 1399 return; 1400 } 1401 } 1402 try clone_url.input(allocator, key, root_focus); 1403 } 1404 1405 // the typed query when enter should submit it: the box exists and holds 1406 // focus. the caller owns the returned text. 1407 pub fn submittedText(self: *SearchHeader, allocator: std.mem.Allocator) !?[]u8 { 1408 const search_box = self.searchBox() orelse return null; 1409 if (self.scroll.row().getFocus().child_id != search_box.getFocus().id) return null; 1410 return try search_box.text(allocator); 1411 } 1412 1413 // enter the header, preferring the search box. 1414 pub fn focusHeader(self: *SearchHeader, root_focus: *Focus) bool { 1415 if (self.searchBox()) |search_box| { 1416 root_focus.setFocus(search_box.getFocus().id); 1417 return true; 1418 } 1419 const clone_url = self.cloneUrl() orelse return false; 1420 root_focus.setFocus(clone_url.getFocus().id); 1421 return true; 1422 } 1423 1424 pub fn hasFocusable(self: *SearchHeader) bool { 1425 return self.cloneUrl() != null or self.searchBox() != null; 1426 } 1427 1428 fn cloneUrl(self: *SearchHeader) ?*CopyableText { 1429 for (self.scroll.row().children.values()) |*child| switch (child.widget) { 1430 .copyable_text => |*copyable_text| return copyable_text, 1431 else => {}, 1432 }; 1433 return null; 1434 } 1435 1436 fn searchBox(self: *SearchHeader) ?*SearchBox { 1437 for (self.scroll.row().children.values()) |*child| switch (child.widget) { 1438 .search_box => |*search_box| return search_box, 1439 else => {}, 1440 }; 1441 return null; 1442 } 1443 1444 pub fn clearGrid(self: *SearchHeader) void { 1445 self.scroll.clearGrid(); 1446 } 1447 1448 pub fn getGrid(self: SearchHeader) ?Grid { 1449 return self.scroll.getGrid(); 1450 } 1451 1452 pub fn getFocus(self: *SearchHeader) *Focus { 1453 return self.scroll.getFocus(); 1454 } 1455 }; 1456 1457 // a one-row, non-focusable status bar showing the current page's url 1458 pub const Footer = struct { 1459 focus: *Focus, 1460 grid: ?Grid, 1461 session: *ui.Session, 1462 arena: std.heap.ArenaAllocator, 1463 url: []const u8 = "", 1464 url_width: usize = 0, 1465 1466 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 1467 return .{ 1468 .focus = try Focus.create(allocator, .container), 1469 .grid = null, 1470 .session = session, 1471 .arena = std.heap.ArenaAllocator.init(allocator), 1472 }; 1473 } 1474 1475 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1476 self.focus.destroy(allocator); 1477 if (self.grid) |*grid| grid.deinit(); 1478 self.arena.deinit(); 1479 } 1480 1481 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1482 _ = root_focus; 1483 self.clearGrid(); 1484 const width = constraint.max_size.width orelse return; 1485 if (width == 0) return; 1486 1487 _ = self.arena.reset(.retain_capacity); 1488 self.url = ""; 1489 self.url_width = 0; 1490 const aa = self.arena.allocator(); 1491 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1492 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 1493 self.url = text; 1494 1495 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1496 errdefer grid.deinit(); 1497 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1498 var i: usize = 0; 1499 while (utf8.nextCodepoint()) |ch| { 1500 const rune_width = xitui.width.cellWidth(ch); 1501 if (i + rune_width > width) break; 1502 // a terminal hyperlink to the page 1503 (try grid.cell(i, 0)).link = text; 1504 try grid.setRune(i, 0, ch); 1505 i += rune_width; 1506 } 1507 self.url_width = i; 1508 self.grid = grid; 1509 } 1510 1511 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1512 _ = allocator; 1513 switch (key) { 1514 .mouse => |mouse| { 1515 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 1516 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 1517 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 1518 }, 1519 else => {}, 1520 } 1521 } 1522 1523 pub fn clearGrid(self: *Footer) void { 1524 if (self.grid) |*grid| { 1525 grid.deinit(); 1526 self.grid = null; 1527 } 1528 } 1529 1530 pub fn getGrid(self: Footer) ?Grid { 1531 return self.grid; 1532 } 1533 1534 pub fn getFocus(self: *Footer) *Focus { 1535 return self.focus; 1536 } 1537 }; 1538 1539 // a single-child wrapper that builds the child at its natural size and 1540 // positions its grid in the middle of the area granted by the parent 1541 // a bordered label, indented past the content it precedes 1542 pub const SectionLabel = struct { 1543 box: wgt.Box(Widget), 1544 1545 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 1546 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1547 errdefer box.deinit(allocator); 1548 1549 { 1550 var gap = try wgt.Text.init(allocator, " "); 1551 errdefer gap.deinit(allocator); 1552 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1553 } 1554 1555 { 1556 var tb = try wgt.TextBox.init(allocator, content, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 1557 errdefer tb.deinit(allocator); 1558 tb.getFocus().mode = .all; 1559 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1560 } 1561 1562 box.getFocus().child_id = box.children.keys()[1]; 1563 return .{ .box = box }; 1564 } 1565 1566 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 1567 self.box.deinit(allocator); 1568 } 1569 1570 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1571 try self.box.build(allocator, constraint, root_focus); 1572 } 1573 1574 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1575 _ = self; 1576 _ = allocator; 1577 _ = key; 1578 _ = root_focus; 1579 } 1580 1581 pub fn clearGrid(self: *SectionLabel) void { 1582 self.box.clearGrid(); 1583 } 1584 1585 pub fn getGrid(self: SectionLabel) ?Grid { 1586 return self.box.getGrid(); 1587 } 1588 1589 pub fn getFocus(self: *SectionLabel) *Focus { 1590 return self.box.getFocus(); 1591 } 1592 }; 1593 1594 // a submit button, indented past its form's content. the web form POSTs 1595 // the button's form; terminal hosts detect presses through `buttonId`. 1596 pub const SubmitButton = struct { 1597 box: wgt.Box(Widget), 1598 1599 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 1600 return initLabeled(allocator, "submit"); 1601 } 1602 1603 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 1604 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1605 errdefer box.deinit(allocator); 1606 1607 { 1608 var gap = try wgt.Text.init(allocator, " "); 1609 errdefer gap.deinit(allocator); 1610 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1611 } 1612 1613 { 1614 var button = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 1615 errdefer button.deinit(allocator); 1616 button.getFocus().mode = .all; 1617 // the renderer distinguishes plain clickables from buttons that 1618 // should POST to a server route by this kind. 1619 button.getFocus().kind = .{ .custom = "submit" }; 1620 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1621 } 1622 1623 box.getFocus().child_id = box.children.keys()[1]; 1624 return .{ .box = box }; 1625 } 1626 1627 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 1628 self.box.deinit(allocator); 1629 } 1630 1631 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 1632 const button = &self.box.children.values()[1]; 1633 try button.widget.text_box.setContent(allocator, label); 1634 button.min_size = .{ .width = label.len + 2, .height = null }; 1635 } 1636 1637 pub fn setBorder(self: *SubmitButton, border: wgt.Border) void { 1638 self.box.children.values()[1].widget.text_box.options.border = border; 1639 } 1640 1641 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1642 try self.box.build(allocator, constraint, root_focus); 1643 } 1644 1645 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1646 _ = self; 1647 _ = allocator; 1648 _ = key; 1649 _ = root_focus; 1650 } 1651 1652 pub fn clearGrid(self: *SubmitButton) void { 1653 self.box.clearGrid(); 1654 } 1655 1656 pub fn getGrid(self: SubmitButton) ?Grid { 1657 return self.box.getGrid(); 1658 } 1659 1660 pub fn getFocus(self: *SubmitButton) *Focus { 1661 return self.box.getFocus(); 1662 } 1663 1664 // the button's focus id, for click hit-testing 1665 pub fn buttonId(self: *const SubmitButton) usize { 1666 return self.box.children.keys()[1]; 1667 } 1668 }; 1669 1670 // keep `rect`, in a centered widget's space, in view within its scroll 1671 pub fn scrollToCenteredRect(scroll: *wgt.Scroll(Widget), rect: layout.IRect) void { 1672 const center = &scroll.child.center; 1673 const center_grid = center.getGrid() orelse return; 1674 const child_grid = center.child.getGrid() orelse return; 1675 const offset_y: isize = @intCast((center_grid.size.height -| child_grid.size.height) / 2); 1676 scroll.scrollToRect(.{ .x = rect.x, .y = rect.y + offset_y, .size = rect.size }); 1677 } 1678 1679 pub const Center = struct { 1680 focus: *Focus, 1681 grid: ?Grid, 1682 child: *Widget, 1683 1684 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1685 const child = try allocator.create(Widget); 1686 errdefer allocator.destroy(child); 1687 child.* = child_widget; 1688 return .{ 1689 .focus = try Focus.create(allocator, .container), 1690 .grid = null, 1691 .child = child, 1692 }; 1693 } 1694 1695 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1696 self.focus.destroy(allocator); 1697 if (self.grid) |*grid| { 1698 grid.deinit(); 1699 self.grid = null; 1700 } 1701 self.child.deinit(allocator); 1702 allocator.destroy(self.child); 1703 } 1704 1705 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1706 self.clearGrid(); 1707 self.getFocus().clear(); 1708 1709 // build the child without forcing it to fill min_size; it sizes 1710 // itself to its content within the available max. 1711 try self.child.build(allocator, .{ 1712 .min_size = .{ .width = null, .height = null }, 1713 .max_size = constraint.max_size, 1714 }, root_focus); 1715 1716 const child_grid = self.child.getGrid() orelse return; 1717 1718 // prefer max when given; otherwise grow to max(min, child) so that 1719 // when only a min is set (e.g. the wasm path passes viewport rows 1720 // as min_height with no max), we can still vertically center while 1721 // letting taller content extend past the min. 1722 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; 1723 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; 1724 if (width == 0 or height == 0) return; 1725 1726 const offset_x: usize = (width -| child_grid.size.width) / 2; 1727 const offset_y: usize = (height -| child_grid.size.height) / 2; 1728 1729 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1730 errdefer grid.deinit(); 1731 try grid.drawGrid(child_grid, offset_x, offset_y); 1732 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1733 self.getFocus().child_id = self.child.getFocus().id; 1734 1735 self.grid = grid; 1736 } 1737 1738 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1739 try self.child.input(allocator, key, root_focus); 1740 } 1741 1742 pub fn clearGrid(self: *Center) void { 1743 if (self.grid) |*grid| { 1744 grid.deinit(); 1745 self.grid = null; 1746 } 1747 self.child.clearGrid(); 1748 } 1749 1750 pub fn getGrid(self: Center) ?Grid { 1751 return self.grid; 1752 } 1753 1754 pub fn getFocus(self: *Center) *Focus { 1755 return self.focus; 1756 } 1757 }; 1758 1759 // renders truecolor ANSI art 1760 pub const AnsiArt = struct { 1761 focus: *Focus, 1762 grid: ?Grid, 1763 content: []const u8, 1764 1765 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1766 return .{ 1767 .focus = try Focus.create(allocator, .container), 1768 .grid = null, 1769 .content = content, 1770 }; 1771 } 1772 1773 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1774 self.focus.destroy(allocator); 1775 if (self.grid) |*grid| { 1776 grid.deinit(); 1777 self.grid = null; 1778 } 1779 } 1780 1781 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1782 _ = root_focus; 1783 self.clearGrid(); 1784 1785 // parse into rows of cells, then copy into a rectangular grid 1786 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1787 defer { 1788 for (rows.items) |*row| row.deinit(allocator); 1789 rows.deinit(allocator); 1790 } 1791 var row: std.ArrayList(Grid.Cell) = .empty; 1792 errdefer row.deinit(allocator); 1793 1794 var style: Grid.Style = .{}; 1795 var width: usize = 0; 1796 const content = self.content; 1797 var i: usize = 0; 1798 while (i < content.len) { 1799 const byte = content[i]; 1800 if (byte == '\n') { 1801 width = @max(width, row.items.len); 1802 try rows.append(allocator, row); 1803 row = .empty; 1804 i += 1; 1805 } else if (byte == '\r') { 1806 // git may checkout the ansi art with crlf line endings on windows 1807 i += 1; 1808 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1809 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1810 var j = i + 2; 1811 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1812 if (j >= content.len) { 1813 i = content.len; // malformed trailing escape; stop 1814 } else { 1815 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1816 i = j + 1; 1817 } 1818 } else { 1819 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1820 const end = @min(content.len, i + len); 1821 const rune = content[i..end]; 1822 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1823 // a foreground color does not make a space visible. only an 1824 // effective background makes it opaque; foreground state often 1825 // remains active through the padding at the end of a row. 1826 const effective_background = if (style.inverted) style.fg else style.bg; 1827 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1828 try row.append(allocator, .{ 1829 .rune = if (transparent) null else codepoint, 1830 .style = style, 1831 }); 1832 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1833 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1834 } 1835 i = end; 1836 } 1837 } 1838 // a trailing row with content but no closing newline 1839 if (row.items.len > 0) { 1840 width = @max(width, row.items.len); 1841 try rows.append(allocator, row); 1842 } else { 1843 row.deinit(allocator); 1844 } 1845 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1846 1847 const height = rows.items.len; 1848 if (width == 0 or height == 0) return; 1849 1850 const clamped_w = @min(width, constraint.max_size.width orelse width); 1851 const clamped_h = @min(height, constraint.max_size.height orelse height); 1852 1853 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1854 errdefer grid.deinit(); 1855 for (rows.items[0..clamped_h], 0..) |r, y| { 1856 const n = @min(r.items.len, clamped_w); 1857 for (r.items[0..n], 0..) |cell, x| { 1858 if (cell.continuation) continue; 1859 (try grid.cell(x, y)).style = cell.style; 1860 try grid.setRune(x, y, cell.rune); 1861 } 1862 } 1863 self.grid = grid; 1864 } 1865 1866 pub fn clearGrid(self: *AnsiArt) void { 1867 if (self.grid) |*grid| { 1868 grid.deinit(); 1869 self.grid = null; 1870 } 1871 } 1872 1873 pub fn getGrid(self: AnsiArt) ?Grid { 1874 return self.grid; 1875 } 1876 1877 pub fn getFocus(self: *AnsiArt) *Focus { 1878 return self.focus; 1879 } 1880 1881 fn applySgr(style: *Grid.Style, params: []const u8) void { 1882 var nums: [16]u32 = undefined; 1883 var n: usize = 0; 1884 var it = std.mem.splitScalar(u8, params, ';'); 1885 while (it.next()) |tok| { 1886 if (n >= nums.len) break; 1887 // an empty parameter (e.g. bare "\x1b[m") means 0 1888 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1889 n += 1; 1890 } 1891 if (n == 0) { 1892 style.* = .{}; 1893 return; 1894 } 1895 var i: usize = 0; 1896 while (i < n) : (i += 1) { 1897 switch (nums[i]) { 1898 0 => style.* = .{}, 1899 7 => style.inverted = true, 1900 27 => style.inverted = false, 1901 39 => style.fg = null, 1902 49 => style.bg = null, 1903 38, 48 => { 1904 // truecolor form: 38;2;r;g;b — anything else is ignored 1905 if (i + 4 < n and nums[i + 1] == 2) { 1906 const c = Grid.Color{ .rgb = .{ 1907 .r = @truncate(nums[i + 2]), 1908 .g = @truncate(nums[i + 3]), 1909 .b = @truncate(nums[i + 4]), 1910 } }; 1911 if (nums[i] == 38) style.fg = c else style.bg = c; 1912 i += 4; 1913 } 1914 }, 1915 else => {}, 1916 } 1917 } 1918 } 1919 }; 1920 1921 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1922 pub const AnsiBackground = struct { 1923 grid: ?Grid, 1924 child: *Widget, 1925 art: AnsiArt, 1926 session: *ui.Session, 1927 1928 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1929 var cw = child_widget; 1930 const child = allocator.create(Widget) catch |e| { 1931 cw.deinit(allocator); 1932 return e; 1933 }; 1934 child.* = cw; 1935 errdefer { 1936 child.deinit(allocator); 1937 allocator.destroy(child); 1938 } 1939 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1940 } 1941 1942 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1943 if (self.grid) |*grid| grid.deinit(); 1944 self.art.deinit(allocator); 1945 self.child.deinit(allocator); 1946 allocator.destroy(self.child); 1947 } 1948 1949 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1950 self.clearGrid(); 1951 1952 // make the wrapped page fill the whole available area 1953 try self.child.build(allocator, .{ 1954 .min_size = .{ 1955 .width = constraint.max_size.width orelse constraint.min_size.width, 1956 .height = constraint.max_size.height orelse constraint.min_size.height, 1957 }, 1958 .max_size = constraint.max_size, 1959 }, root_focus); 1960 1961 const foreground = self.child.getGrid() orelse return; 1962 if (!self.session.color) return; 1963 // terminals show the art only once they report a background to fade it toward 1964 const backdrop: ?Grid.Color.Rgb = if (self.session.is_terminal) self.session.terminal_background orelse return else null; 1965 self.art.content = self.session.data.ansi_art; 1966 try self.art.build(allocator, .{ 1967 .min_size = .{ .width = null, .height = null }, 1968 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1969 }, root_focus); 1970 // the web fades the art with css opacity instead 1971 const art_backdrop = backdrop orelse return; 1972 if (self.art.grid) |*art_grid| { 1973 for (art_grid.cells) |*cell| { 1974 cell.style.fg = fadeColor(cell.style.fg, art_backdrop); 1975 cell.style.bg = fadeColor(cell.style.bg, art_backdrop); 1976 } 1977 self.grid = try artBehind(allocator, foreground, art_grid.*); 1978 } 1979 } 1980 1981 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1982 try self.child.input(allocator, key, root_focus); 1983 } 1984 1985 pub fn clearGrid(self: *AnsiBackground) void { 1986 if (self.grid) |*grid| { 1987 grid.deinit(); 1988 self.grid = null; 1989 } 1990 self.child.clearGrid(); 1991 self.art.clearGrid(); 1992 } 1993 1994 pub fn getGrid(self: AnsiBackground) ?Grid { 1995 return self.grid orelse self.child.getGrid(); 1996 } 1997 1998 pub fn getFocus(self: *AnsiBackground) *Focus { 1999 return self.child.getFocus(); 2000 } 2001 2002 // a foreground cell counts as blank if it's empty or a bare space without styling 2003 fn cellIsBlank(cell: Grid.Cell) bool { 2004 if (cell.continuation) return false; 2005 if (cell.rune) |rune| { 2006 return rune == ' ' and 2007 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2008 } 2009 return true; 2010 } 2011 2012 // the art is faded toward the background at this opacity so foreground 2013 // text stays legible over it 2014 pub const art_opacity = 25; // percent 2015 2016 // the art parser only emits truecolor, so a palette color never occurs 2017 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 2018 const c = color orelse return null; 2019 return switch (c) { 2020 .rgb => |v| v, 2021 else => null, 2022 }; 2023 } 2024 2025 fn fadeColor(color: ?Grid.Color, backdrop: Grid.Color.Rgb) ?Grid.Color { 2026 const v = artRgb(color) orelse return color; 2027 const blend = struct { 2028 fn f(c: u8, b: u8) u8 { 2029 return @intCast((@as(u16, c) * art_opacity + @as(u16, b) * (100 - art_opacity)) / 100); 2030 } 2031 }.f; 2032 return .{ .rgb = .{ .r = blend(v.r, backdrop.r), .g = blend(v.g, backdrop.g), .b = blend(v.b, backdrop.b) } }; 2033 } 2034 2035 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2036 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2037 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2038 // snap such channels clear of that range so the backdrop doesn't blink or 2039 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2040 // actually render truecolor. 2041 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2042 const v = artRgb(color) orelse return color; 2043 const snap = struct { 2044 fn f(c: u8) u8 { 2045 return if (c >= 1 and c <= 9) 10 else c; 2046 } 2047 }.f; 2048 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 2049 } 2050 2051 // composites ANSI art behind a foreground grid 2052 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 2053 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2054 errdefer out.deinit(); 2055 2056 const anchor_x = foreground.size.width -| art_grid.size.width; 2057 for (0..art_grid.size.height) |y| { 2058 for (0..art_grid.size.width) |x| { 2059 const src = (try art_grid.cell(x, y)).*; 2060 if (src.rune == null) continue; 2061 const dst = try out.cell(anchor_x + x, y); 2062 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 2063 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 2064 // blank cells take the art; occupied cells take its background 2065 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 2066 dst.style = src.style; 2067 dst.style.fg = sgrSafe(dst.style.fg); 2068 dst.style.bg = sgrSafe(dst.style.bg); 2069 try out.setRune(anchor_x + x, y, src.rune); 2070 } else { 2071 const art_bg = sgrSafe(src.style.bg orelse src.style.fg); 2072 dst.style.bg = art_bg; 2073 if (next_dst) |next| next.style.bg = art_bg; 2074 } 2075 } 2076 } 2077 return out; 2078 } 2079 }; 2080 2081 pub const CopyableText = struct { 2082 pub const Choice = struct { 2083 selector: []const u8 = "", 2084 text: []const u8, 2085 copyable_text: ?[]const u8 = null, 2086 label: []const u8 = "", 2087 bottom_label: []const u8 = "", 2088 }; 2089 2090 box: wgt.Box(Widget), 2091 session: *ui.Session, 2092 choices: []Choice, 2093 selected: usize = 0, 2094 2095 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 2096 if (choices.len == 0) return error.MissingCopyableText; 2097 2098 const owned_choices = try allocator.dupe(Choice, choices); 2099 errdefer allocator.free(owned_choices); 2100 2101 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 2102 errdefer box.deinit(allocator); 2103 2104 if (choices.len > 1) for (choices) |choice| { 2105 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 2106 errdefer selector_box.deinit(allocator); 2107 selector_box.getFocus().mode = .all; 2108 try box.children.put(allocator, selector_box.getFocus().id, .{ .widget = .{ .text_box = selector_box }, .rect = null, .min_size = .{ .width = choice.selector.len + 2, .height = 3 } }); 2109 }; 2110 2111 var text_input = try wgt.TextInput.init(allocator, .{ 2112 .border = .single, 2113 .round_corners = true, 2114 .top_label = .{ .text = choices[initial].label }, 2115 .bottom_label = .{ .text = choices[initial].bottom_label }, 2116 .read_only = true, 2117 .render_content = session.is_terminal, 2118 .visible_width = null, 2119 }); 2120 errdefer text_input.deinit(allocator); 2121 text_input.getFocus().mode = .all; 2122 try text_input.setContent(allocator, choices[initial].text); 2123 box.getFocus().child_id = text_input.getFocus().id; 2124 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 2125 2126 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 2127 } 2128 2129 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 2130 const aa = session.page_arena.allocator(); 2131 var choices: [2]Choice = undefined; 2132 var count: usize = 0; 2133 var ssh_index: ?usize = null; 2134 switch (handle.location) { 2135 .repo => |identity| { 2136 const http_url = if (handle.read_access == .public) session.data.git_http_url else null; 2137 if (http_url) |base| { 2138 const url = try aa.print("{s}/{s}", .{ base, identity }); 2139 choices[count] = .{ 2140 .selector = "http", 2141 .text = url, 2142 .copyable_text = try aa.print("git clone {s}", .{url}), 2143 .label = " clone ", 2144 }; 2145 count += 1; 2146 } 2147 if (session.data.git_ssh_url) |ssh_url| { 2148 ssh_index = count; 2149 const url = try aa.print("{s}/{s}", .{ ssh_url, identity }); 2150 choices[count] = .{ 2151 .selector = "ssh", 2152 .text = url, 2153 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2154 .label = " clone ", 2155 }; 2156 count += 1; 2157 } 2158 }, 2159 .fork => |f| if (session.data.git_ssh_url) |ssh_url| { 2160 const url = try aa.print("{s}/{s}+{s}", .{ ssh_url, f.identity, f.id }); 2161 choices[count] = .{ 2162 .text = url, 2163 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2164 .label = " clone ", 2165 }; 2166 count += 1; 2167 }, 2168 } 2169 if (count == 0) return null; 2170 // a viewer who can push starts on the ssh url 2171 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 2172 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 2173 } 2174 2175 pub fn minWidth(self: *const CopyableText) usize { 2176 var width = maxWidth(self.choices) + 2; 2177 if (self.choices.len > 1) { 2178 for (self.choices) |choice| width += choice.selector.len + 2; 2179 } 2180 return width; 2181 } 2182 2183 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 2184 self.box.deinit(allocator); 2185 allocator.free(self.choices); 2186 } 2187 2188 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2189 self.clearGrid(); 2190 2191 if (self.choices.len > 1) { 2192 if (root_focus.grandchild_id) |id| { 2193 for (0..self.choices.len) |i| { 2194 if (id != self.selector(i).getFocus().id) continue; 2195 if (i != self.selected) try self.select(allocator, i); 2196 root_focus.setFocus(self.textInput().getFocus().id); 2197 break; 2198 } 2199 } 2200 2201 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2202 for (0..self.choices.len) |i| { 2203 self.selector(i).options.border = if (i == self.selected) 2204 (if (focused) .double else .single) 2205 else 2206 .hidden; 2207 } 2208 } 2209 self.textInput().options.border = .single; 2210 self.textInput().cursor = 0; 2211 self.textInput().scroll_offset = 0; 2212 try self.box.build(allocator, constraint, root_focus); 2213 2214 if (!self.session.is_terminal) { 2215 const aa = self.session.arena.allocator(); 2216 const id = self.textInput().getFocus().id; 2217 try self.session.text_inputs.put(aa, id, self.textInput()); 2218 } 2219 } 2220 2221 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2222 switch (key) { 2223 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2224 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2225 .enter => if (self.session.is_terminal) self.show(), 2226 .mouse => |mouse| { 2227 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2228 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2229 try self.select(allocator, i); 2230 root_focus.setFocus(self.textInput().getFocus().id); 2231 return; 2232 }; 2233 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2234 }, 2235 else => {}, 2236 } 2237 } 2238 2239 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2240 self.selected = selected; 2241 const choice = self.choices[selected]; 2242 const text_input = self.textInput(); 2243 text_input.options.top_label.text = choice.label; 2244 text_input.options.bottom_label.text = choice.bottom_label; 2245 try text_input.setContent(allocator, choice.text); 2246 } 2247 2248 fn show(self: *CopyableText) void { 2249 const choice = self.choices[self.selected]; 2250 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2251 } 2252 2253 fn textInput(self: *CopyableText) *wgt.TextInput { 2254 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2255 } 2256 2257 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2258 return &self.box.children.values()[index].widget.text_box; 2259 } 2260 2261 fn maxWidth(choices: []const Choice) usize { 2262 var width: usize = 0; 2263 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2264 return width; 2265 } 2266 2267 pub fn clearGrid(self: *CopyableText) void { 2268 self.box.clearGrid(); 2269 } 2270 2271 pub fn getGrid(self: CopyableText) ?Grid { 2272 return self.box.getGrid(); 2273 } 2274 2275 pub fn getFocus(self: *CopyableText) *Focus { 2276 return self.box.getFocus(); 2277 } 2278 }; 2279