// SPDX-License-Identifier: MIT // Copyright (C) 2026 Pooya Parsa // 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 ( {g && } ); } /** 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 (
{THEMES.map((t) => { const on = theme === t.id; return ( ); })}
); } /** 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 Off this boot (Settings, Display and sound).; if (!dpy.attached) return No program has the display open.; return (
{dpy.width}×{dpy.height}{win ? ", shown" : ", minimized"}
); } /** 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

{name}: {text}

; } /** 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 ( } {...p} width={360} monitor="status" settings="display" {...props} actions={(pick) => ( )}> {(pick) => ( <> {gl && <>
} {vk && <>
} {(!gl || !vk) && (
{!gl && } {!vk && }
)} )} ); }