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

@ -6,6 +6,7 @@ mod file;
mod params;
mod project;
mod settings;
pub mod theme;
use crate::{EngineError, Registry};
@ -30,5 +31,7 @@ pub fn register_builtins(registry: &mut Registry) -> Result<(), EngineError> {
registry.register(Box::new(settings::Get))?;
registry.register(Box::new(settings::Set))?;
registry.register(Box::new(settings::Reset))?;
registry.register(Box::new(theme::List))?;
registry.register(Box::new(theme::SystemPreference))?;
Ok(())
}

View file

@ -5,6 +5,7 @@ use serde::Deserialize;
use serde_json::{Map, Value, json};
use crate::cmd::params::parse;
use crate::cmd::theme;
use crate::settings::{self, DEFS, SetError};
use crate::{Command, CommandSpec, EngineError, Event, Outcome, State};
@ -96,11 +97,12 @@ impl Command for Set {
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
let id = self.spec().id;
let p: SetParams = parse(id, params)?;
let before = theme::resolved(state);
let changed = state
.settings
.set(&p.key, p.value)
.map_err(|e| set_error(id, e))?;
finish(state, &p.key, changed)
finish(state, &p.key, changed, before)
}
}
@ -129,8 +131,9 @@ impl Command for Reset {
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
let id = self.spec().id;
let p: ResetParams = parse(id, params)?;
let before = theme::resolved(state);
let changed = state.settings.reset(&p.key).map_err(|e| set_error(id, e))?;
finish(state, &p.key, changed)
finish(state, &p.key, changed, before)
}
}
@ -144,12 +147,19 @@ fn set_error(command: &str, error: SetError) -> EngineError {
}
}
/// Emits `settingsChanged` if the value changed and returns `{ value }`.
fn finish(state: &mut State, key: &str, changed: bool) -> Result<Outcome, EngineError> {
/// Emits `settingsChanged` if the value changed, then `themeChanged` if the
/// resolved theme is no longer `before`, and returns `{ value }`.
fn finish(
state: &mut State,
key: &str,
changed: bool,
before: crate::theme::Theme,
) -> Result<Outcome, EngineError> {
if changed {
state.emit(Event::SettingsChanged {
keys: vec![key.to_string()],
});
theme::emit_if_changed(state, before);
}
let value = state
.settings

View file

@ -0,0 +1,102 @@
//! Theme commands: `theme.list` and `theme.systemPreference`. Switching
//! themes goes through `settings.set appearance.theme`; whenever the
//! resolved theme changes, `themeChanged` carries its tokens.
use serde::Deserialize;
use serde_json::{Value, json};
use crate::cmd::params::parse;
use crate::theme::{self, BUILTIN, Theme};
use crate::{Command, CommandSpec, EngineError, Event, Outcome, State};
/// The setting that selects the theme.
pub const SETTING: &str = "appearance.theme";
/// The theme the current settings and OS preference resolve to.
pub fn resolved(state: &State) -> Theme {
let setting = state.settings.get(SETTING);
let id = setting
.as_ref()
.and_then(Value::as_str)
.unwrap_or(theme::SYSTEM);
theme::resolve(id, state.system_dark)
}
/// The `themeChanged` event for a theme.
pub fn changed_event(theme: &Theme) -> Event {
Event::ThemeChanged {
id: theme.id.to_string(),
dark: theme.dark,
tokens: theme::token_map(theme),
}
}
/// Emits `themeChanged` if the resolved theme is no longer `before`.
pub fn emit_if_changed(state: &mut State, before: Theme) {
let now = resolved(state);
if now != before {
state.emit(changed_event(&now));
}
}
/// `theme.list`: the built-in themes.
pub struct List;
impl Command for List {
fn spec(&self) -> &CommandSpec {
&CommandSpec {
id: "theme.list",
label: "List Themes",
menu_path: None,
default_shortcut: None,
params: "none; returns [{ id, label, dark }], label is a message-catalog key",
}
}
fn run(&self, _state: &mut State, _params: Value) -> Result<Outcome, EngineError> {
let list: Vec<_> = BUILTIN
.iter()
.map(|t| json!({ "id": t.id, "label": t.label_key, "dark": t.dark }))
.collect();
Ok(Outcome::value(Value::Array(list)))
}
}
/// Params of `theme.systemPreference`.
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct PreferenceParams {
/// Whether the OS prefers a dark theme.
dark: bool,
}
/// `theme.systemPreference`: the frontend reports the OS colour preference;
/// in "system" mode a change of it switches the theme.
pub struct SystemPreference;
impl Command for SystemPreference {
fn spec(&self) -> &CommandSpec {
&CommandSpec {
id: "theme.systemPreference",
label: "Report System Theme",
menu_path: None,
default_shortcut: None,
params: "{ dark: bool }; returns the resolved theme { id, dark, tokens }; emits themeChanged when it changes",
}
}
fn run(&self, state: &mut State, params: Value) -> Result<Outcome, EngineError> {
let p: PreferenceParams = parse(self.spec().id, params)?;
let before = resolved(state);
state.system_dark = Some(p.dark);
emit_if_changed(state, before);
let now = resolved(state);
let value = serde_json::to_value(changed_event(&now))
.map_err(|e| EngineError::Internal(e.to_string()))?;
let Value::Object(mut fields) = value else {
return Err(EngineError::Internal("theme is not an object".into()));
};
fields.remove("kind");
Ok(Outcome::value(Value::Object(fields)))
}
}