// SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // The top bar's popovers, macOS menu-bar extras: plain panels (style.css // .popover) under the bar's right side, all on BarPopover. // - The Control Center (#tb-cc), opened by the bar's last item (TopBar.tsx // #tb-status, two switches). Top to bottom: // - the machine's state, quietly: a dot in its colour (running green; // starting, suspending or held amber; suspended grey; halted dim; error // red), the state's name (store label) and the guest's uptime; // - quick settings, as small round icon buttons (the word is the tooltip; // pressed ones stronger), only what changes at once: Full screen (the // page; where the browser has it, or iPhone's hint), Wallpaper (Random: // the next photo, the popover stays; any other wallpaper: Settings, // Desktop), Display (while a program has the display open: shows the // Display window, or minimizes it). Boot options (network, guest GL, // sound, memory…) apply on reboot, so they stay in Settings; // - Settings…, System…, the kernel log, the keyboard shortcuts and About // unkernel (with their keys, as in the menus; #tb-cc-settings, // #tb-cc-system, #tb-cc-log, #tb-cc-keys, #tb-cc-about); // - power: Suspend (Resume once suspended), Reboot ("Reboot to apply" // while Settings' options differ from this boot's; asks first, // ui.confirmReboot()), and while in safe mode with a distro, Leave safe // mode (asks first). The items keep the old power menu's ids // (#tb-suspend, #tb-resume, #tb-reboot). // - The applets' panels (#tb--panel: applets/Applet.tsx AppletPanel, // one module per applet), opened by their items in the bar. // Behaviour, all (the opener, TopBar.tsx, keeps which is open and how it // opened): a press anywhere but on it or the bar's controls that open them // closes it; Esc closes it wherever the focus is (a mouse open leaves the // focus in the terminal: presses on its buttons never take it); Tab, // Shift-Tab and the arrow keys move through its buttons (wrapping), Home and // End. Opened from the keyboard its first button takes the focus. Picking an // item or Display closes it; the other tiles keep it. Each carries // data-tb-pop, which the bar (TopBar.tsx) and focus.ts (BARS) look for. // Portaled to at the menus' layer (--z-menu, over the toast); under // sm (phones) a sheet across the page under the bar, else its width (CC_W, // an applet's own), its right edge under its anchor's, kept EDGE px inside // the viewport; taller than the room below the bar, it scrolls. import { useEffect, useReducer, useRef, type KeyboardEvent, type ReactNode } from "react"; import { createPortal } from "react-dom"; import { nextRandom, setWallpaper, useDesktop } from "../desktop.ts"; import { DISPLAY_WIN, offerFullscreen, toggleDisplay, toggleFullscreen, useFullscreen } from "../display.ts"; import { fmtUptime } from "../format.ts"; import { useOutsidePress } from "../hooks.ts"; import { PREFIX_SHORT } from "../keys.ts"; import { KW } from "../kw.ts"; import { useStats } from "../stats.ts"; import { isRunning, phase, useStore, type Phase } from "../store.ts"; import { openSection, openSettings, SETTINGS, SYSTEM } from "../sysapps.ts"; import { openItem } from "../winmenu.tsx"; import { isVisible, useWm } from "../wm.ts"; import { openAbout } from "./About.tsx"; import { DisplayIcon, FullscreenIcon, LayersIcon, LogIcon, PauseIcon, PlayIcon, RebootIcon, ShuffleIcon, Triangle } from "./icons.tsx"; import { menuKeys } from "./KeyHint.tsx"; import type { MenuEntry, MenuItem } from "./Menu.tsx"; /** How it closed: Esc, a press elsewhere, an item picked. */ export type CcClose = "key" | "away" | "pick"; const EDGE = 8, GAP = 6, CC_W = 300; /** The state dot's colour by phase. */ const DOT: Record = { running: "var(--color-ok)", starting: "var(--color-warn)", suspending: "var(--color-warn)", held: "var(--color-warn)", suspended: "var(--color-muted)", halted: "var(--color-off)", error: "var(--color-err)", }; /** A quick setting: a small round icon button (its word is the tooltip and the label); `on` draws it pressed. */ function Tile({ id, label, title, icon, on, busy, onClick }: { id: string; label: string; title: string; icon: ReactNode; on?: boolean; busy?: boolean; onClick: () => void; }) { return ( ); } /** A row as in the menus (Menu.tsx): icon, label, its key on the right. */ function Item({ it, onPick }: { it: MenuItem; onPick: (it: MenuItem) => void }) { return ( ); } export const Sep = () =>
; /** What the opener passes to a popover. */ export interface PopoverProps { /** The bar's control under which it opens (its rect is read on each render: it moves with the bar). */ anchorId: string; /** Opened from the keyboard: its first button takes the focus. */ kb: boolean; /** Presses on these do not close it (the bar's controls that open it: their click toggles it). */ inside: (t: EventTarget | null) => boolean; onClose: (how: CcClose) => void; } /** * The popover itself: placement, closing, the keys; `children` gets pick(), which closes it and runs the item. * Opened from the keyboard, its `[data-autofocus]` button (else its first) takes the focus. Also the applets' panels (applets/Applet.tsx). */ export function BarPopover({ id, label, width, anchorId, kb, inside, onClose, children }: PopoverProps & { id: string; label: string; width: number; children: (pick: (it: MenuItem) => void) => ReactNode; }) { const box = useRef(null); const close = useRef(onClose); close.current = onClose; const [, relayout] = useReducer((n: number) => n + 1, 0); useOutsidePress((t) => !!box.current?.contains(t as Node) || inside(t), () => close.current("away")); const buttons = () => [...(box.current?.querySelectorAll("button:not(:disabled)") ?? [])]; // Opened (or kept, after a hover opened it) from the keyboard: its first button takes the focus. useEffect(() => { if (kb) (box.current?.querySelector("button[data-autofocus]:not(:disabled)") ?? buttons()[0])?.focus({ preventScroll: true }); }, [kb]); useEffect(() => { // Esc closes it wherever the focus is (opened with the mouse, the terminal keeps it); the guest does not get that Esc. const key = (e: globalThis.KeyboardEvent) => { if (e.key !== "Escape" || e.ctrlKey || e.altKey || e.metaKey || e.shiftKey) return; e.preventDefault(); e.stopImmediatePropagation(); close.current("key"); }; addEventListener("keydown", key, true); addEventListener("resize", relayout); return () => { removeEventListener("keydown", key, true); removeEventListener("resize", relayout); }; }, []); const onKey = (e: KeyboardEvent) => { e.stopPropagation(); const list = buttons(), i = list.indexOf(document.activeElement as HTMLButtonElement); const go = (n: number) => list[(n + list.length) % list.length]?.focus({ preventScroll: false }); switch (e.key) { case "ArrowDown": case "ArrowRight": go(i + 1); break; case "ArrowUp": case "ArrowLeft": go(i < 0 ? -1 : i - 1); break; case "Tab": go(e.shiftKey ? (i < 0 ? -1 : i - 1) : i + 1); break; case "Home": go(0); break; case "End": go(-1); break; default: return; } e.preventDefault(); }; const pick = (it: MenuItem) => { close.current("pick"); it.run(); }; const a = document.getElementById(anchorId)?.getBoundingClientRect(); const bar = document.getElementById("topbar")?.getBoundingClientRect(); const top = (a && a.height > 0 ? a.bottom : bar?.bottom ?? 32) + GAP; const sheet = innerWidth < 640; const right = a && a.width > 0 ? a.right : innerWidth - EDGE; const left = Math.max(EDGE, Math.min(right - width, innerWidth - EDGE - width)); const place = sheet ? { left: EDGE, right: EDGE, top } : { left, top, width }; return createPortal( , document.body, ); } /** The Control Center (#tb-cc): the state, quick settings, About and the system apps, power. */ export function ControlCenter(props: PopoverProps) { const ui = KW.ui!; const cfg = KW.config; const st = useStats("lite"); const label = useStore((s) => s.label); const ph = useStore(phase); const running = useStore(isRunning); const suspended = useStore((s) => s.suspended); useStore((s) => s.options); // "Reboot to apply" follows the options const displayOn = useStore((s) => s.display.attached); const displayShown = useWm((s) => { const w = s.windows.find((x) => x.id === DISPLAY_WIN); return !!w && isVisible(w, s); }); const fs = useFullscreen(); const random = useDesktop((s) => s.prefs.wallpaper.kind === "random"); const rolling = useDesktop((s) => s.pending === "random"); const up = ph === "running" || ph === "held" || ph === "suspending" ? st.guest?.raw.uptime ?? null : null; const tiles = (pick: (it: MenuItem) => void): ReactNode[] => { const out: ReactNode[] = []; if (offerFullscreen()) out.push(} onClick={() => void toggleFullscreen().then((msg) => { if (msg) ui.toast(msg); })} />); // Always the shuffle: another random photo, which also turns Random on when a wallpaper was pinned (Settings, Desktop pins one again). out.push(} onClick={() => void (random ? nextRandom() : setWallpaper({ kind: "random" }))} />); if (displayOn) out.push(} onClick={() => pick({ label: "Display", run: toggleDisplay })} />); return out; }; const apps: MenuItem[] = [ { ...openItem(SETTINGS, openSettings)!, id: "tb-cc-settings" }, { ...openItem(SYSTEM, () => openSection("status"))!, id: "tb-cc-system" }, { id: "tb-cc-log", label: "Kernel log", icon: , keys: menuKeys("log"), run: () => openSection("log") }, { id: "tb-cc-keys", label: "Keyboard shortcuts", icon: {PREFIX_SHORT}, keys: menuKeys("keys"), run: () => openSection("keys") }, { id: "tb-cc-about", label: "About unkernel", icon: , keys: menuKeys("about"), run: openAbout }, ]; const changed = ui.optionsChanged(); const power: MenuEntry[] = [ ...(suspended ? [{ id: "tb-resume", label: "Resume", icon: , run: ui.resume }] : running && KW.suspend ? [{ id: "tb-suspend", label: "Suspend", icon: , keys: menuKeys("suspend"), run: () => ui.suspend() }] : []), { id: "tb-reboot", label: changed ? "Reboot to apply" : "Reboot", icon: , keys: menuKeys("reboot"), run: () => void ui.confirmReboot() }, ...(cfg.root === "initramfs" && cfg.distro ? [{ id: "tb-safe-leave", label: "Leave safe mode", icon: , run: () => void ui.safeModeToggle() }] : []), ]; return ( {(pick) => ( <>

{tiles(pick)}
{apps.map((it) => )} {power.map((it) => it && )} )}
); }