M1.10: light theme, live theme switching, editor tokens

- theme::LIGHT next to DARK with the 10 new tokens (editor selection,
  active line, cursor, gutter, match, font size, tab active/modified,
  notice); light fg-muted, accent and error darkened for WCAG AA
- theme.list, theme.systemPreference, themeChanged event when the
  resolved theme changes (settings.set/reset appearance.theme, or the
  OS preference in system mode)
- desktop init script and window background from the stored setting;
  system mode uses a prefers-color-scheme media query for the first frame
- ui/src/theme.ts applies tokens to :root and color-scheme live; editor,
  tabs and notice drop their var() fallbacks
- Vitest WCAG AA contrast check against a Rust-maintained snapshot,
  DOM tests, headless control-channel test
- docs: architecture Themes, parity rows, ui-parity, gaps, roadmaps

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-16 15:49:23 +02:00
parent 1ab448de4d
commit cde6e119c6
29 changed files with 1192 additions and 72 deletions

109
ui/src/theme.ts Normal file
View file

@ -0,0 +1,109 @@
/**
* Live theme switching. The desktop shell injects the stored theme's tokens
* before first paint; afterwards the engine sends `themeChanged` with every
* token, and {@link applyTheme} writes them onto `:root` and sets
* `color-scheme`, so a switch needs no reload. In "system" mode the engine
* resolves the theme from the OS preference that {@link startTheme} reports
* with `theme.systemPreference` whenever it changes.
*/
import { onEngineEvent, run } from "./ipc";
/** A resolved theme as the engine sends it. */
export interface ResolvedTheme {
/** Theme id, e.g. `lutra-light`. */
id: string;
/** Whether the theme is dark. */
dark: boolean;
/** Every token as `--lt-<name>` → CSS value. */
tokens: Record<string, string>;
}
/** Media query of the OS dark preference. */
export const DARK_QUERY = "(prefers-color-scheme: dark)";
/**
* Reads a resolved theme from untrusted data (an event or a command result).
* Tokens without the `--lt-` prefix or with a non-string value are dropped.
*
* @param value - the data.
* @returns the theme, or null if `id` or `dark` is missing.
*/
export function toResolvedTheme(value: unknown): ResolvedTheme | null {
if (typeof value !== "object" || value === null) {
return null;
}
const { id, dark, tokens } = value as Record<string, unknown>;
if (typeof id !== "string" || typeof dark !== "boolean") {
return null;
}
const clean: Record<string, string> = {};
if (typeof tokens === "object" && tokens !== null) {
for (const [name, token] of Object.entries(tokens)) {
if (name.startsWith("--lt-") && typeof token === "string") {
clean[name] = token;
}
}
}
return { id, dark, tokens: clean };
}
/**
* Applies a theme: writes its tokens onto the root element's inline style
* (which wins over the injected `<style id="lt-theme">`), sets
* `color-scheme` and records the id in `data-theme`.
*
* @param theme - the theme.
* @param root - the element to theme; the document element by default.
*/
export function applyTheme(theme: ResolvedTheme, root: HTMLElement = document.documentElement): void {
for (const [name, value] of Object.entries(theme.tokens)) {
root.style.setProperty(name, value);
}
root.style.colorScheme = theme.dark ? "dark" : "light";
root.dataset.theme = theme.id;
}
/**
* Reports the OS preference to the engine and applies the theme it
* resolves. A failed call keeps the current theme.
*
* @param dark - whether the OS prefers a dark theme.
*/
export async function reportSystemPreference(dark: boolean): Promise<void> {
const result = await run("theme.systemPreference", { dark });
if (!result.ok) {
return;
}
const theme = toResolvedTheme(result.value);
if (theme) {
applyTheme(theme);
}
}
/**
* Starts following the theme: applies every `themeChanged` event and
* reports the OS preference now and whenever it changes. Outside a browser
* with `matchMedia` the OS preference counts as dark.
*
* @returns a function that stops following.
*/
export function startTheme(): () => void {
const unsubscribe = onEngineEvent((event) => {
if (event.kind === "themeChanged") {
const theme = toResolvedTheme(event);
if (theme) {
applyTheme(theme);
}
}
});
const query = typeof window.matchMedia === "function" ? window.matchMedia(DARK_QUERY) : null;
const onChange = (event: MediaQueryListEvent): void => {
void reportSystemPreference(event.matches);
};
query?.addEventListener("change", onChange);
void reportSystemPreference(query?.matches ?? true);
return () => {
unsubscribe();
query?.removeEventListener("change", onChange);
};
}