Something went wrong. Try again.
An experimental port of NetBSD, FreeBSD, OpenBSD, and XNU to unkernel unkernel.dev
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163// SPDX-License-Identifier: MIT// Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>// The Display applet (#tb-display, its panel #tb-display-panel): the page's// theme, the guest's display and its GPU renderers (guest GL, Vulkan).// - The item: a monitor, always there from sm up (the theme makes it of use// without a GPU). While guest GL's or Vulkan's renderer is loaded (ready,// or lost) a bar beside it is filled to the share of the page's time the// two took together (Gpu.tsx gpuBar(); each is time on the page's// thread), red while one is lost or failed; the tooltip has the theme, the// guest display's mode and each renderer's figures.// - The panel (the full sampling rate while open), top to bottom: the// header (the pill: a lost or failed renderer in red, else the renderers'// share, else the display's mode while a program has it, else the theme// shown); the theme as a segmented control (#tb-display-theme-auto,// -dark, -light; radios: ui.setTheme() at once, kept as Settings, Desktop// keeps it; the panel stays); the guest display (/dev/fb0: its mode while// a program has it open, with Show or Minimize for the Display window,// #tb-display-window, as the Control Center's tile; else a line: nothing// has it open, or off this boot); then a section per renderer loading or// loaded (Gpu.tsx GlSection, VkSection: activity history, share of the// page's time, scanout, WebGL implementation or adapter, errors and// device losses in red), and for one off this boot or not loaded yet a// quiet line instead (#tb-display-gl, #tb-display-vk). Never called GPU// utilisation: neither WebGL nor WebGPU tells it.// - The header's Monitor opens System's Overview, Settings opens Settings'// Display and sound (guest GL, Vulkan, the display device); the footer's// Theme and wallpaper… (#tb-display-desktop) opens Settings' Desktop.import { KW } from "../../kw.ts";import { DISPLAY_WIN, toggleDisplay } from "../../display.ts";import { fmtPct } from "../../format.ts";import { useStats, type StatsState } from "../../stats.ts";import { useStore, type UiState } from "../../store.ts";import { openSection } from "../../sysapps.ts";import { isVisible, useWm } from "../../wm.ts";import type { PopoverProps } from "../ControlCenter.tsx";import type { MenuItem } from "../Menu.tsx";import { GPU_STATE, VK_STATE } from "../Glance.tsx";import { DisplayIcon } from "../icons.tsx";import { AppletButton, AppletNote, AppletPanel, AppletSection, Bars, ICON, type AppletItemProps, type AppletTone } from "./Applet.tsx";import { bad, GlSection, gpuBar, loaded, VkSection } from "./Gpu.tsx";
const ID = "display";
type Theme = UiState["theme"];const THEMES: { id: Theme; label: string }[] = [{ id: "auto", label: "Auto" }, { id: "dark", label: "Dark" }, { id: "light", label: "Light" }];const cap = (s: string) => s[0]!.toUpperCase() + s.slice(1);
/** The theme in words: "Dark", or "Auto (dark)" while it follows the system. */const themeWords = (pref: Theme, shown: "dark" | "light") => (pref === "auto" ? `Auto (${shown})` : cap(pref));
/** The Display window is there and shown (not minimized). */const useDisplayShown = () => useWm((s) => { const w = s.windows.find((x) => x.id === DISPLAY_WIN); return !!w && isVisible(w, s); });
export function DisplayItem(props: AppletItemProps) { const st = useStats("lite"); const theme = useStore((s) => s.theme), shown = useStore((s) => s.shownTheme); const dpy = useStore((s) => s.display); const g = gpuBar(st); const title = [ `Display: ${themeWords(theme, shown).toLowerCase()} theme`, dpy.attached && `Guest display: ${dpy.width}×${dpy.height}`, ...(g?.lines ?? []), ].filter(Boolean).join("\n"); return ( <AppletButton id={ID} title={title} tone={g?.err ? "err" : undefined} wide meter={!!g} {...props}> <DisplayIcon size={ICON} /> {g && <Bars values={[g.busy]} />} </AppletButton> );}
/** The theme as three segments (radios); a press applies it at once and keeps the panel open. */function ThemeChoice() { const theme = useStore((s) => s.theme), shown = useStore((s) => s.shownTheme); return ( <AppletSection title="Theme" aside={theme === "auto" ? `follows the system: ${shown}` : undefined}> <div id="tb-display-theme" role="radiogroup" aria-label="Theme" className="flex rounded-lg bg-ink/[0.07] p-0.5 font-sans"> {THEMES.map((t) => { const on = theme === t.id; return ( <button key={t.id} id={`tb-display-theme-${t.id}`} type="button" role="radio" aria-checked={on} data-autofocus={on ? "" : undefined} title={t.id === "auto" ? "Follow the system's light or dark setting" : `The ${t.id} theme`} onClick={() => KW.ui!.setTheme(t.id)} className={`h-7 min-w-0 flex-1 rounded-md text-[12px] transition-colors focus-visible:outline-none pointer-coarse:h-9 ${on ? "bg-ink/[0.14] font-medium text-fg" : "text-muted hover:bg-ink/[0.07] hover:text-fg focus-visible:bg-ink/[0.07] focus-visible:text-fg"}`}> {t.label} </button> ); })} </div> </AppletSection> );}
/** The guest's display: its mode and the window's Show or Minimize, else why there is none. */function GuestDisplay({ pick }: { pick: (it: MenuItem) => void }) { const dpy = useStore((s) => s.display); const win = useDisplayShown(); if (!KW.config.display) return <AppletSection title="Guest display"><AppletNote>Off this boot (Settings, Display and sound).</AppletNote></AppletSection>; if (!dpy.attached) return <AppletSection title="Guest display"><AppletNote>No program has the display open.</AppletNote></AppletSection>; return ( <AppletSection title="Guest display"> <div className="flex min-w-0 items-center gap-3 font-sans"> <span className="min-w-0 flex-1 truncate text-[12px] text-fg tabular-nums"> {dpy.width}×{dpy.height}<span className="text-faint">{win ? ", shown" : ", minimized"}</span> </span> <button id="tb-display-window" type="button" className="btn btn-sm flex-none pointer-coarse:h-9" title={win ? "Minimize the Display window (the program keeps running)" : "Show the Display window"} onClick={() => pick({ label: "Display", run: toggleDisplay })}>{win ? "Minimize" : "Show"}</button> </div> </AppletSection> );}
/** A renderer off this boot or not loaded yet, as one quiet line. */function RendererNote({ id, name, on, why, idle }: { id: string; name: string; on: boolean; why: string | null; idle: string | null }) { const text = !on ? `off this boot${why ? `: ${why}` : ""}` : idle ?? "comes with the system"; return <p id={id} className="truncate px-4 py-1 font-sans text-[11.5px] text-faint" title={`${name}: ${text}`}><span className="text-muted">{name}:</span> {text}</p>;}
/** The pill: a lost or failed renderer, else the renderers' share, else the display's mode, else the theme shown. */function pill(st: StatsState, attached: string | null, theme: string): { state: string; tone: AppletTone } { if (bad(st.gpu?.state) || bad(st.vk?.state)) return { state: bad(st.vk?.state) ? VK_STATE[st.vk!.state] : GPU_STATE[st.gpu!.state], tone: "err" }; const { busy } = loaded(st); if (busy != null) return { state: `${fmtPct(busy)} of the page`, tone: "pending" }; if (attached) return { state: attached, tone: "ok" }; return { state: theme, tone: "off" };}
export function DisplayPanel(props: PopoverProps) { const st = useStats(); const theme = useStore((s) => s.theme), shown = useStore((s) => s.shownTheme); const dpy = useStore((s) => s.display); const cfg = KW.config; const gl = st.gpu && st.gpu.state !== "idle" ? st.gpu : null; const vk = st.vk && st.vk.state !== "idle" ? st.vk : null; const p = pill(st, dpy.attached ? `${dpy.width}×${dpy.height}` : null, themeWords(theme, shown)); return ( <AppletPanel id={ID} title="Display" icon={<DisplayIcon size={16} />} {...p} width={360} monitor="status" settings="display" {...props} actions={(pick) => ( <button id="tb-display-desktop" type="button" className="btn btn-sm my-1 pointer-coarse:h-9" title="Settings, Desktop: the theme and the wallpaper" onClick={() => pick({ label: "Desktop", run: () => openSection("desktop") })}>Theme and wallpaper…</button> )}> {(pick) => ( <> <ThemeChoice /> <GuestDisplay pick={pick} /> {gl && <><div role="separator" className="border-t border-ink/10" /><GlSection st={st} gpu={gl} /></>} {vk && <><div role="separator" className="border-t border-ink/10" /><VkSection st={st} vk={vk} /></>} {(!gl || !vk) && ( <div className="flex flex-col pt-0.5 pb-1.5"> {!gl && <RendererNote id="tb-display-gl" name="Guest GL" on={cfg.gl} why={cfg.glWhy} idle={st.gpu ? "loads on the first GL program" : null} />} {!vk && <RendererNote id="tb-display-vk" name="Vulkan" on={cfg.vk} why={cfg.vkWhy} idle={st.vk ? "loads on the first Vulkan program" : null} />} </div> )} </> )} </AppletPanel> );}