╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1473 if (self.data.is_local) { 1474 const src = self.local orelse return local_author_fallback; 1475 const io = self.io orelse return local_author_fallback; 1476 return try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 1477 } 1478 const user_id = self.data.user_id orelse return null; 1479 const moment = self.haxy_moment orelse return null; 1480 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.page_arena, user_id)) orelse return null; 1481 return .{ .name = user.event.name, .email = user.event.email }; 1482 } 1483 1484 // queue an action for the host to drain this frame. 1485 pub fn push(self: *Self, action: Action) !void { 1486 try self.pending.append(self.arena.allocator(), action); 1487 } 1488 1489 // apply a single action's in-memory effect only (no persistence). 1490 fn apply(self: *Self, action: Action) void { 1491 switch (action) { 1492 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 1493 } 1494 } 1495 1496 // drain the session's queued actions by applying them to the session. 1497 // used on the wasm path, which has no repo to persist to. 1498 pub fn applyPending(self: *Self) void { 1499 for (self.pending.items) |action| self.apply(action); 1500 self.pending.clearRetainingCapacity(); 1501 } 1502 1503 // drain the session's queued actions by applying them to the session and 1504 // writing them to the db 1505 pub fn applyAndWritePending( 1506 self: *Self, 1507 io: std.Io, 1508 allocator: std.mem.Allocator, 1509 repo: *rp.Repo(.xit, evt.admin_repo_opts), 1510 ) !void { 1511 defer self.pending.clearRetainingCapacity(); 1512 for (self.pending.items) |action| { 1513 self.apply(action); 1514 switch (action) { 1515 .toggle_ansi => if (self.data.user_id) |user_id| { 1516 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, user_id); 1517 }, 1518 } 1519 } 1520 } 1521 1522 // reload the moment from the admin repo and re-read user preferences 1523 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 1524 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 1525 self.haxy_moment = moment; 1526 1527 const user_id = self.data.user_id orelse return; 1528 var arena = std.heap.ArenaAllocator.init(allocator); 1529 defer arena.deinit(); 1530 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, user_id)) |user| { 1531 self.data.enable_ansi = user.event.enable_ansi; 1532 } 1533 } 1534 1535 // request a forward navigation to `route`; the host consumes next_page 1536 // (Nav.sync on the terminal, the wasm tick on the web). 1537 pub fn navigate(self: *Session, route: RoutablePage) !void { 1538 self.next_page = route; 1539 } 1540 }; 1541 1542 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 1543 var nav = try Nav.init(allocator, session); 1544 defer nav.deinit(allocator); 1545 1546 var terminal = try term.Terminal.init(io, allocator); 1547 var terminal_live = true; 1548 defer if (terminal_live) terminal.deinit(io); 1549 1550 // set term as active so it will be properly cooked 1551 // when a panic/segfault happens 1552 term.setActive(&terminal); 1553 defer term.setActive(null); 1554 1555 while (!terminal.shouldQuit()) { 1556 const grid_changed = try terminal.render(&nav.root); 1557 1558 // process any inputs. 1559 // 1560 // if the grid didn't change, then first do a blocking 1561 // read, so the thread will sleep until further input. 1562 // after that, all remaining reads are non-blocking so 1563 // we can process the rest of the queued inputs. 1564 // 1565 // if the grid *did* change, then only do non-blocking 1566 // reads. we do not want to sleep the thread because 1567 // there may be an animation that requires more looping. 1568 var blocking = !grid_changed; 1569 while (try terminal.readKey(io, blocking)) |key| { 1570 blocking = false; 1571 try inputKey(allocator, &nav.root, key, session); 1572 } 1573 1574 if (session.host_request) |request| { 1575 session.host_request = null; 1576 switch (request) { 1577 .show_copyable_text => |copyable_text| { 1578 term.setActive(null); 1579 terminal.deinit(io); 1580 terminal_live = false; 1581 showCopyableText(io, copyable_text) catch |show_err| { 1582 terminal = try term.Terminal.init(io, allocator); 1583 terminal_live = true; 1584 term.setActive(&terminal); 1585 return show_err; 1586 }; 1587 terminal = try term.Terminal.init(io, allocator); 1588 terminal_live = true; 1589 term.setActive(&terminal); 1590 }, 1591 .sync_events => { 1592 try nav.root.build(allocator, .{ 1593 .min_size = .{ .width = null, .height = null }, 1594 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 1595 }, nav.root.getFocus()); 1596 _ = try terminal.render(&nav.root); 1597 try Repo.Events.performSync(allocator, session); 1598 }, 1599 } 1600 } 1601 1602 // persist queued actions when there's a repo; local mode has none, 1603 // so just apply in-memory 1604 if (repo_maybe) |repo| { 1605 try session.applyAndWritePending(io, allocator, repo); 1606 // reload so the next navigation builds its page from the current moment 1607 try session.reloadMoment(allocator, repo); 1608 } else { 1609 session.applyPending(); 1610 } 1611 1612 // reconcile navigation: forward to a new page, or back on escape. 1613 try nav.sync(allocator, session); 1614 1615 // the quit button (on the quit tab) asks the host to tear down. 1616 if (session.quit_requested) terminal.requestQuit(); 1617 1618 try nav.root.build(allocator, .{ 1619 .min_size = .{ .width = null, .height = null }, 1620 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 1621 }, nav.root.getFocus()); 1622 } 1623 } 1624 1625 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 1626 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 1627 null 1628 else 1629 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 1630 defer if (tty) |file| file.close(io); 1631 1632 const input = if (tty) |file| file else std.Io.File.stdin(); 1633 const output = if (tty) |file| file else std.Io.File.stdout(); 1634 1635 var write_buf: [1024]u8 = undefined; 1636 var writer = output.writer(io, &write_buf); 1637 try writer.interface.print("\x1b[2J\x1b[Hcopy the following text and then press enter to go back:\r\n\r\n{s}\r\n", .{copyable_text}); 1638 try writer.interface.flush(); 1639 1640 var read_buf: [1]u8 = undefined; 1641 var reader = input.reader(io, &read_buf); 1642 while (true) { 1643 const byte = try reader.interface.takeByte(); 1644 if (byte == '\r' or byte == '\n') return; 1645 } 1646 } 1647 1648 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 1649 const root_focus = root.getFocus(); 1650 const focused_before = root_focus.grandchild_id; 1651 var activate_in_page_link = false; 1652 switch (key) { 1653 // request a navigation pop; the host's Nav.sync goes back a page, or 1654 // quits when there's no history left. 1655 .escape => { 1656 session.nav_back = true; 1657 return; 1658 }, 1659 .ctrl => |letter| switch (letter) { 1660 'r' => { 1661 session.refresh_requested = true; 1662 return; 1663 }, 1664 else => try root.input(allocator, key, root_focus), 1665 }, 1666 .enter => { 1667 if (root_focus.grandchild_id) |gid| { 1668 // follow a cross-page link 1669 if (crossPageLink(root_focus, gid, session.data)) |route| { 1670 return session.navigate(route); 1671 } 1672 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 1673 } 1674 activate_in_page_link = true; 1675 try root.input(allocator, key, root_focus); 1676 }, 1677 .mouse => |mouse| { 1678 if (mouse.action == .press and mouse.action.press == .left) { 1679 var clicked: ?usize = null; 1680 var iter = root_focus.children.iterator(); 1681 while (iter.next()) |entry| { 1682 const child = entry.value_ptr.*; 1683 if (!child.focus.focusable) continue; 1684 const r = child.rect; 1685 if (mouse.x >= r.x and mouse.y >= r.y and 1686 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 1687 { 1688 clicked = entry.key_ptr.*; 1689 break; 1690 } 1691 } 1692 if (clicked) |focus_id| { 1693 // follow a cross-page link 1694 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 1695 return session.navigate(route); 1696 } 1697 if (rawLink(root_focus, focus_id)) |url| { 1698 if (!mouse.ctrl) try requestRawLink(session, url); 1699 return; 1700 } 1701 root_focus.setFocus(focus_id); 1702 activate_in_page_link = true; 1703 } 1704 // forward the press into the widget tree so buttons (and any 1705 // future click-aware widgets) can react. widgets that don't 1706 // care about presses ignore it. 1707 try root.input(allocator, key, root_focus); 1708 } else { 1709 try root.input(allocator, key, root_focus); 1710 } 1711 }, 1712 else => try root.input(allocator, key, root_focus), 1713 } 1714 1715 const focused_after = root_focus.grandchild_id; 1716 if (!activate_in_page_link and focused_before == focused_after) return; 1717 const focus_id = focused_after orelse return; 1718 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 1719 } 1720 1721 // a link to bytes the server serves directly rather than to a page route, so 1722 // it never resolves to a RoutablePage 1723 pub const raw_link_prefix = "ax:"; 1724 1725 // a route already loaded into the current page; focusing it changes the url 1726 // without rebuilding the page 1727 pub const in_page_link_prefix = "ai:"; 1728 1729 // an in-page tab's base route, or the exact current route when it is selected 1730 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 1731 const target = if (selected) session.data.current_page else route; 1732 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 1733 } 1734 1735 // a button the web renderer covers with a file picker that posts the chosen 1736 // file to the url after the prefix 1737 pub const file_input_prefix = "file:"; 1738 1739 // a submit button that overrides its enclosing form's action 1740 pub const submit_action_prefix = "submit:"; 1741 1742 // the url an `ax:` link points at, or null for anything else. a host that 1743 // suppresses the anchor's own navigation needs this to follow the link. 1744 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 1745 const child = root_focus.children.get(focus_id) orelse return null; 1746 const custom = switch (child.focus.kind) { 1747 .custom => |c| c, 1748 else => return null, 1749 }; 1750 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 1751 return custom[raw_link_prefix.len..]; 1752 } 1753 1754 fn requestRawLink(session: *Session, url: []const u8) !void { 1755 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 1756 } 1757 1758 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 1759 return if (session.web_port) |port| 1760 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 1761 else 1762 path; 1763 } 1764 1765 // if the focus target at focus_id is an `a:` link that should navigate (a 1766 // different parent page, or a different files directory within the repo page), 1767 // return its route; otherwise null. lets a host turn a click / enter on such a 1768 // link into a navigation rather than a focus change. 1769 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 1770 const current = data.current_page; 1771 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 1772 // a link to a different parent page always navigates; within a page, a 1773 // files-directory / commits-page / list-window change navigates while tab 1774 // links stay in-page. 1775 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 1776 return null; 1777 } 1778 1779 // the route carried by an `ai:` focus target, or null for any other target 1780 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 1781 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 1782 } 1783 1784 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 1785 const child = root_focus.children.get(focus_id) orelse return null; 1786 const custom = switch (child.focus.kind) { 1787 .custom => |c| c, 1788 else => return null, 1789 }; 1790 if (!std.mem.startsWith(u8, custom, prefix)) return null; 1791 const path = custom[prefix.len..]; 1792 // local sessions build (and parse) links with the repo identity elided 1793 return if (data.is_local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 1794 } 1795 1796 // a display author, resolved against the admin db at read time 1797 pub const Author = union(enum) { 1798 unknown, 1799 email: []const u8, // an email no user matches 1800 user_name: []const u8, 1801 1802 // the commit author line's email, resolved like initFromEmail 1803 pub fn init( 1804 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1805 arena: *std.heap.ArenaAllocator, 1806 author_line: []const u8, 1807 ) !Author { 1808 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 1809 } 1810 1811 // from a record's stored author email: it resolves to the user currently 1812 // holding it, else stays the email. the result is allocated in `arena`, 1813 // so the email may borrow transient memory. 1814 pub fn initFromEmail( 1815 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1816 arena: *std.heap.ArenaAllocator, 1817 author_email: ?[]const u8, 1818 ) !Author { 1819 const email = author_email orelse return .unknown; 1820 if (haxy_moment) |moment| { 1821 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 1822 return .{ .user_name = user.event.name }; 1823 } 1824 } 1825 return .{ .email = try arena.allocator().dupe(u8, email) }; 1826 } 1827 }; 1828 1829 // the "a:" link to user `name`'s page. 1830 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 1831 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 1832 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 1833 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1834 } 1835 1836 // a focusable " author " box showing `author`, linking to their user page 1837 // when it is a known user 1838 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 1839 const text = switch (author) { 1840 .unknown => "", 1841 .email, .user_name => |t| t, 1842 }; 1843 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 1844 errdefer tb.deinit(allocator); 1845 tb.getFocus().focusable = true; 1846 switch (author) { 1847 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 1848 else => {}, 1849 } 1850 return tb; 1851 } 1852 1853 // native-TUI navigation 1854 pub const Nav = struct { 1855 root: Widget, 1856 route: RoutablePage, 1857 // backs the current page (its Page data and the page-scoped strings its 1858 // widgets build). owned here, swapped on each navigation. session.page_arena 1859 // tracks whichever of these is current so widgets allocate into it. 1860 arena: *std.heap.ArenaAllocator, 1861 history: std.ArrayList(Entry), 1862 1863 // each retained page keeps its own arena, freed when the page leaves the 1864 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 1865 // long-lived session from accumulating every page it ever visited. 1866 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 1867 1868 // cap on retained back-history so the chain can't grow memory without bound 1869 const max_history: usize = 16; 1870 1871 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 1872 const arena = try allocator.create(std.heap.ArenaAllocator); 1873 arena.* = std.heap.ArenaAllocator.init(allocator); 1874 errdefer { 1875 arena.deinit(); 1876 allocator.destroy(arena); 1877 } 1878 1879 session.page_arena = arena; 1880 const route = session.data.current_page; 1881 1882 const page = try arena.allocator().create(Page); 1883 page.* = try Page.init(arena, session, route); 1884 return .{ 1885 .root = try initRoot(allocator, page, session), 1886 .route = route, 1887 .arena = arena, 1888 .history = .empty, 1889 }; 1890 } 1891 1892 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 1893 self.root.deinit(allocator); 1894 freeArena(allocator, self.arena); 1895 for (self.history.items) |*entry| { 1896 entry.root.deinit(allocator); 1897 freeArena(allocator, entry.arena); 1898 } 1899 self.history.deinit(allocator); 1900 } 1901 1902 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 1903 arena.deinit(); 1904 allocator.destroy(arena); 1905 } 1906 1907 // reconcile the displayed root with the session's navigation state. a 1908 // forward nav (current_page moved to a different parent page) pushes the 1909 // current root and builds the new page in a fresh arena; a back request 1910 // frees the current page and restores the previous one. when escape is 1911 // pressed with no history left we switch to the quit tab instead of quitting. 1912 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 1913 // refresh: rebuild the current page in place from a current moment 1914 if (session.refresh_requested) { 1915 session.refresh_requested = false; 1916 if (session.haxy_moment != null or session.local != null) { 1917 const arena = try allocator.create(std.heap.ArenaAllocator); 1918 arena.* = std.heap.ArenaAllocator.init(allocator); 1919 errdefer freeArena(allocator, arena); 1920 1921 session.page_arena = arena; 1922 1923 const route = session.data.current_page; 1924 const page = try arena.allocator().create(Page); 1925 page.* = try Page.init(arena, session, route); 1926 const new_root = try initRoot(allocator, page, session); 1927 1928 self.root.deinit(allocator); 1929 freeArena(allocator, self.arena); 1930 self.root = new_root; 1931 self.route = route; 1932 self.arena = arena; 1933 } 1934 return; 1935 } 1936 1937 if (session.nav_back) { 1938 session.nav_back = false; 1939 if (self.history.pop()) |entry| { 1940 self.root.deinit(allocator); 1941 freeArena(allocator, self.arena); 1942 self.root = entry.root; 1943 self.route = entry.route; 1944 self.arena = entry.arena; 1945 session.page_arena = entry.arena; 1946 session.data.current_page = entry.route; 1947 chooseAnsiArtForNavigation(session); 1948 return; 1949 } 1950 // nothing to go back to; switch to the quit confirmation 1951 const root_focus = self.root.getFocus(); 1952 var iter = root_focus.children.iterator(); 1953 while (iter.next()) |entry| { 1954 switch (entry.value_ptr.focus.kind) { 1955 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 1956 root_focus.setFocus(entry.key_ptr.*); 1957 // a Stack only builds its selected child, so the quit 1958 // button isn't in the focus tree until a build runs with 1959 // the quit tab selected. build once, then send a 1960 // synthetic arrow_down to drop focus from the tab onto 1961 // the button so it's ready to confirm. 1962 try self.root.build(allocator, .{ 1963 .min_size = .{ .width = null, .height = 40 }, 1964 .max_size = .{ .width = 80, .height = null }, 1965 }, root_focus); 1966 try self.root.input(allocator, .arrow_down, root_focus); 1967 break; 1968 }, 1969 else => {}, 1970 } 1971 } 1972 return; 1973 } 1974 1975 // forward navigation: navigate() set next_page to the page to move to (a 1976 // cross-page link or tab change crossing pages) 1977 if (session.next_page) |route| { 1978 session.next_page = null; 1979 if (session.haxy_moment == null and session.local == null) return; 1980 // the page we navigated to becomes the current page 1981 session.data.current_page = route; 1982 1983 const arena = try allocator.create(std.heap.ArenaAllocator); 1984 arena.* = std.heap.ArenaAllocator.init(allocator); 1985 errdefer freeArena(allocator, arena); 1986 1987 session.page_arena = arena; 1988 1989 const page = try arena.allocator().create(Page); 1990 page.* = try Page.init(arena, session, route); 1991 const new_root = try initRoot(allocator, page, session); 1992 1993 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 1994 // drop the oldest entry (freeing its widget tree and arena) once over cap 1995 if (self.history.items.len > max_history) { 1996 var oldest = self.history.orderedRemove(0); 1997 oldest.root.deinit(allocator); 1998 freeArena(allocator, oldest.arena); 1999 } 2000 self.root = new_root; 2001 self.route = route; 2002 self.arena = arena; 2003 } 2004 } 2005 }; 2006 2007 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2008 const page_widget: Widget = switch (page.*) { 2009 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2010 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2011 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2012 }; 2013 2014 chooseAnsiArtForNavigation(session); 2015 2016 // on the TUI/SSH, the page sits above a one-row footer showing the url 2017 var root = if (session.is_terminal) blk: { 2018 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2019 errdefer box.deinit(allocator); 2020 const bg_id = bg_blk: { 2021 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2022 errdefer background.deinit(allocator); 2023 const id = background.getFocus().id; 2024 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2025 break :bg_blk id; 2026 }; 2027 { 2028 var footer = try widget.Footer.init(allocator, session); 2029 errdefer footer.deinit(allocator); 2030 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2031 } 2032 box.getFocus().child_id = bg_id; 2033 break :blk Widget{ .box = box }; 2034 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2035 errdefer root.deinit(allocator); 2036 2037 // input-owning views build their TextInputs in init, so reset their 2038 // registrations here 2039 session.text_inputs.clearRetainingCapacity(); 2040 2041 try root.build(allocator, .{ 2042 .min_size = .{ .width = null, .height = 40 }, 2043 .max_size = .{ .width = 100, .height = null }, 2044 }, root.getFocus()); 2045 2046 return root; 2047 } 2048 2049 // native and server-side sessions choose from the embedded collection. WASM 2050 // keeps the art serialized by the server instead. 2051 fn chooseAnsiArtForNavigation(session: *Session) void { 2052 if (comptime ansi_arts.len == 0) return; 2053 const io = session.io orelse return; 2054 var random: usize = undefined; 2055 io.random(std.mem.asBytes(&random)); 2056 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2057 } 2058