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

View file

@ -2,18 +2,28 @@
/**
* Root component: mounts the main window layout with the editor area; the
* slogan shows while no tab is open. Loads product information from the
* engine on mount.
* engine on mount and starts following the theme (see `theme.ts`).
*/
import { onMounted } from "vue";
import { onMounted, onUnmounted } from "vue";
import EditorArea from "./components/editor/EditorArea.vue";
import MainLayout from "./components/layout/MainLayout.vue";
import { useAppStore } from "./stores/app";
import { startTheme } from "./theme";
const app = useAppStore();
/** Stops following the theme; set while mounted. */
let stopTheme: (() => void) | null = null;
onMounted(() => {
stopTheme = startTheme();
void app.loadAbout();
});
onUnmounted(() => {
stopTheme?.();
stopTheme = null;
});
</script>
<template>

View file

@ -139,7 +139,7 @@ onBeforeUnmount(() => {
padding: 4px 12px;
flex-shrink: 0;
color: var(--lt-fg);
background: var(--lt-notice-bg, color-mix(in srgb, var(--lt-accent) 15%, var(--lt-bg)));
background: var(--lt-notice-bg);
border-bottom: 1px solid var(--lt-border);
}

View file

@ -118,7 +118,7 @@ function onKeydown(event: KeyboardEvent): void {
.tabs__tab--active {
color: var(--lt-fg);
background: var(--lt-tab-active-bg, var(--lt-editor-bg));
background: var(--lt-tab-active-bg);
border-bottom-color: var(--lt-accent);
}
@ -131,7 +131,7 @@ function onKeydown(event: KeyboardEvent): void {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--lt-tab-modified, var(--lt-accent));
background: var(--lt-tab-modified);
}
.tabs__close {

View file

@ -1,14 +1,10 @@
/**
* CodeMirror extensions of the Lutra editor: the basic setup (line numbers,
* history, selection, brackets, search) without a language (PHP comes in
* M2), and a theme that takes every colour from `--lt-*` tokens.
*
* Tokens the theme does not define yet are read with a fallback derived from
* existing tokens (`var(--lt-editor-selection-bg, color-mix(…))`), so that a
* token added to the Rust theme later takes effect without code changes.
* Requested from design: `--lt-editor-selection-bg`,
* `--lt-editor-active-line-bg`, `--lt-editor-cursor`, `--lt-editor-gutter-bg`,
* `--lt-editor-gutter-fg`, `--lt-editor-match-bg`, `--lt-font-size-editor`.
* M2), and a theme that takes every colour from `--lt-*` tokens, including
* the editor tokens (`--lt-editor-selection-bg`, `--lt-editor-active-line-bg`,
* `--lt-editor-cursor`, `--lt-editor-gutter-bg`, `--lt-editor-gutter-fg`,
* `--lt-editor-match-bg`, `--lt-font-size-editor`).
*/
import { EditorView } from "@codemirror/view";
import type { Extension } from "@codemirror/state";
@ -20,7 +16,7 @@ export const lutraEditorTheme: Extension = EditorView.theme({
height: "100%",
color: "var(--lt-fg)",
backgroundColor: "var(--lt-editor-bg)",
fontSize: "var(--lt-font-size-editor, var(--lt-font-size-ui))",
fontSize: "var(--lt-font-size-editor)",
},
"&.cm-focused": {
outline: "none",
@ -30,37 +26,32 @@ export const lutraEditorTheme: Extension = EditorView.theme({
lineHeight: "1.5",
},
".cm-content": {
caretColor: "var(--lt-editor-cursor, var(--lt-accent))",
caretColor: "var(--lt-editor-cursor)",
},
".cm-cursor, .cm-dropCursor": {
borderLeftColor: "var(--lt-editor-cursor, var(--lt-accent))",
borderLeftColor: "var(--lt-editor-cursor)",
},
"&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection":
{
backgroundColor:
"var(--lt-editor-selection-bg, color-mix(in srgb, var(--lt-accent) 30%, transparent))",
backgroundColor: "var(--lt-editor-selection-bg)",
},
".cm-activeLine": {
backgroundColor:
"var(--lt-editor-active-line-bg, color-mix(in srgb, var(--lt-fg) 5%, transparent))",
backgroundColor: "var(--lt-editor-active-line-bg)",
},
".cm-selectionMatch, .cm-searchMatch": {
backgroundColor:
"var(--lt-editor-match-bg, color-mix(in srgb, var(--lt-accent) 18%, transparent))",
backgroundColor: "var(--lt-editor-match-bg)",
},
"&.cm-focused .cm-matchingBracket": {
backgroundColor:
"var(--lt-editor-match-bg, color-mix(in srgb, var(--lt-accent) 18%, transparent))",
backgroundColor: "var(--lt-editor-match-bg)",
outline: "1px solid var(--lt-border)",
},
".cm-gutters": {
backgroundColor: "var(--lt-editor-gutter-bg, var(--lt-editor-bg))",
color: "var(--lt-editor-gutter-fg, var(--lt-fg-muted))",
backgroundColor: "var(--lt-editor-gutter-bg)",
color: "var(--lt-editor-gutter-fg)",
borderRight: "1px solid var(--lt-border)",
},
".cm-activeLineGutter": {
backgroundColor:
"var(--lt-editor-active-line-bg, color-mix(in srgb, var(--lt-fg) 5%, transparent))",
backgroundColor: "var(--lt-editor-active-line-bg)",
color: "var(--lt-fg)",
},
".cm-panels": {

View file

@ -18,5 +18,7 @@
"project.empty.noFolder": "No folder is open.",
"project.openFolder": "Open Folder…",
"project.toolWindow.title": "Project",
"project.tree.loading": "Loading…"
"project.tree.loading": "Loading…",
"theme.lutraDark": "Lutra Dark",
"theme.lutraLight": "Lutra Light"
}

View file

@ -25,7 +25,8 @@ export type EngineEventPayload =
| { kind: "dirtyChanged"; id: number; dirty: boolean }
| { kind: "bufferClosed"; id: number }
| { kind: "fileChangedOnDisk"; path: string; id: number; removed: boolean }
| { kind: "watchFailed"; message: string };
| { kind: "watchFailed"; message: string }
| { kind: "themeChanged"; id: string; dark: boolean; tokens: Record<string, string> };
/** Callback receiving engine events. */
export type EngineEventHandler = (event: EngineEventPayload) => void;

View file

@ -1,6 +1,7 @@
/**
* Base layout of the Lutra window. Colours and fonts come only from the
* theme tokens (`--lt-*`) that the desktop shell injects before first paint.
* theme tokens (`--lt-*`) that the desktop shell injects before first paint
* and `theme.ts` replaces on `:root` when the theme changes.
*/
html,
body,

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);
};
}