AI media harness for planning, generating, and editing locally
TypeScript
/* Light / dark appearance.
*
* Which theme this computer shows is a property of THIS computer and of the
* person sitting at it — not of a project. It therefore lives in localStorage
* next to the engine paths (see settings.ts for why machine-level settings
* never go into slopus.json), and a project folder copied here from another
* machine does not bring somebody else's appearance with it.
*
* THREE choices, not two:
*
* "system" follow the computer, LIVE: data-theme is the OS's theme, and
* main.tsx re-applies it whenever the OS flips (watchSystemTheme)
* "light" <html data-theme="light">
* "dark" <html data-theme="dark">
*
* The attribute always names the RESOLVED theme, never the choice. That is
* what lets tokens.css write its light palette once, under
* :root[data-theme="light"], instead of twice (a media-query copy for
* "system" plus the attribute copy) kept identical by hand. The choice itself
* lives in localStorage; read it with loadTheme(), never from the attribute.
*/
import { setWindowTheme } from "./nativeShell";
export type ThemeChoice = "system" | "light" | "dark";
/** What actually gets painted once the choice is resolved against the OS. */
export type ResolvedTheme = "light" | "dark";
export const THEME_KEY = "slopus.theme.v1";
const LEGACY_THEME_KEY = "polstudio.theme.v1";
export const THEME_CHOICES: readonly ThemeChoice[] = ["system", "light", "dark"];
/* The two grounds, duplicated from tokens.css (--bg). They are needed BEFORE
any stylesheet exists — public/theme-boot.js paints the document with one
of them so the window never flashes the other theme — so this is the one
place a raw colour is allowed outside tokens.css. Keep them in step with
`--bg`; the test in theme.test.ts reads tokens.css and fails if they drift.
Under Mica (Windows 11) neither is painted: the ground is the backdrop
itself, see `hasMicaBackdrop` below and src/styles/window.css. */
export const THEME_BACKGROUND: Record<ResolvedTheme, string> = {
dark: "#080a0f",
light: "#eef1f6",
};
const isChoice = (value: unknown): value is ThemeChoice =>
value === "system" || value === "light" || value === "dark";
/** Anything unrecognised — a hand-edited entry, a value from a future version
* — reads as "system" rather than taking the whole app down. */
export function loadTheme(): ThemeChoice {
try {
const current = localStorage.getItem(THEME_KEY);
const value = current ?? localStorage.getItem(LEGACY_THEME_KEY);
if (!current && value) localStorage.setItem(THEME_KEY, value);
return isChoice(value) ? value : "system";
} catch {
return "system";
}
}
export function saveTheme(choice: ThemeChoice): void {
try {
localStorage.setItem(THEME_KEY, choice);
} catch {
/* A full or disabled localStorage costs the preference next launch, not
the session: applyTheme has already repainted. */
}
}
/** What the computer itself is set to, right now. */
export function systemTheme(): ResolvedTheme {
return typeof matchMedia === "function" && matchMedia("(prefers-color-scheme: light)").matches
? "light"
: "dark";
}
export function resolveTheme(choice: ThemeChoice): ResolvedTheme {
return choice === "system" ? systemTheme() : choice;
}
/** The global src-tauri/src/native_shell.rs sets from an initialization
* script — before any page script — when the window has Mica behind it. */
export const BACKDROP_GLOBAL = "__SLOPUS_BACKDROP__";
/** True when the window is transparent with Mica behind it. Read from the
* attribute theme-boot.js writes, or straight from the native global when
* that script never ran (a test renderer, a stripped index.html), in which
* case the attribute is written here. */
export function hasMicaBackdrop(): boolean {
const root = document.documentElement;
if (root.getAttribute("data-backdrop") === "mica") return true;
const native = (window as unknown as Record<string, unknown>)[BACKDROP_GLOBAL] === "mica";
if (native) root.setAttribute("data-backdrop", "mica");
return native;
}
/** Writes the resolved theme onto <html> and repaints the document ground.
*
* The ground is set inline as well as by CSS because theme-boot.js sets it
* that way before the stylesheet has loaded; leaving a stale inline colour
* behind would out-specify the stylesheet and strand the old theme's
* background under the new theme's panels. Under Mica the inline ground is
* `transparent`, for the same reason in reverse: any colour there would
* cover the backdrop.
*
* It also hands the choice to the native window, whose theme tints Mica and
* the system menus and message boxes. */
export function applyTheme(choice: ThemeChoice): ResolvedTheme {
const root = document.documentElement;
const resolved = resolveTheme(choice);
root.setAttribute("data-theme", resolved);
root.style.backgroundColor = hasMicaBackdrop() ? "transparent" : THEME_BACKGROUND[resolved];
void setWindowTheme(choice);
return resolved;
}
/** Calls back whenever the COMPUTER's setting changes. Returns an unsubscribe.
* Only "system" needs it: the attribute (and with it the whole palette) and
* the inline ground colour above have to be rewritten, and the settings
* screen has to keep saying which one the computer is currently on. */
export function watchSystemTheme(onChange: (theme: ResolvedTheme) => void): () => void {
if (typeof matchMedia !== "function") return () => {};
const query = matchMedia("(prefers-color-scheme: light)");
const handler = () => onChange(query.matches ? "light" : "dark");
/* Safari below 14 only has the deprecated form; a desktop webview that lacks
addEventListener would otherwise throw on startup. */
if (typeof query.addEventListener === "function") {
query.addEventListener("change", handler);
return () => query.removeEventListener("change", handler);
}
query.addListener(handler);
return () => query.removeListener(handler);
}