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

@ -0,0 +1,134 @@
//! Theme switching over a real TCP socket: `settings.set appearance.theme`
//! and `theme.systemPreference` stream `themeChanged` with the theme's
//! tokens to `events.subscribe` clients, the way the desktop window gets
//! them.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::indexing_slicing,
clippy::panic
)]
use std::io::{BufRead, BufReader, Write};
use std::net::TcpStream;
use std::sync::Arc;
use std::time::Duration;
use lutra_engine::theme;
use lutra_testkit::EngineBackend;
use serde_json::{Value, json};
/// A client that collects event lines and returns responses.
struct Client {
reader: BufReader<TcpStream>,
writer: TcpStream,
events: Vec<Value>,
}
impl Client {
fn connect(addr: std::net::SocketAddr) -> Self {
let stream = TcpStream::connect(addr).unwrap();
stream
.set_read_timeout(Some(Duration::from_secs(5)))
.unwrap();
Self {
writer: stream.try_clone().unwrap(),
reader: BufReader::new(stream),
events: Vec::new(),
}
}
fn line(&mut self) -> Value {
let mut line = String::new();
self.reader.read_line(&mut line).expect("line within 5 s");
serde_json::from_str(&line).unwrap()
}
/// Sends a request and returns its response, collecting event lines that
/// arrive before it.
fn call(&mut self, request: Value) -> Value {
self.writer
.write_all(format!("{request}\n").as_bytes())
.unwrap();
loop {
let line = self.line();
if line.get("event").is_some() {
self.events.push(line["event"].clone());
} else {
return line;
}
}
}
/// Waits for the next `themeChanged` event (events arrive from a
/// subscriber queue, possibly after the response) and drops the events
/// before it.
fn next_theme(&mut self) -> Value {
loop {
if let Some(at) = self.events.iter().position(|e| e["kind"] == "themeChanged") {
let theme = self.events.remove(at);
self.events.drain(..at);
return theme;
}
let line = self.line();
if line.get("event").is_some() {
self.events.push(line["event"].clone());
}
}
}
}
fn execute(command: &str, params: Value) -> Value {
json!({"id": 2, "method": "engine.execute", "params": {"command": command, "params": params}})
}
fn tokens(theme: &theme::Theme) -> Value {
serde_json::to_value(theme::token_map(theme)).unwrap()
}
#[test]
fn switching_streams_theme_changed_with_tokens() {
let dir = tempfile::tempdir().unwrap();
let backend = Arc::new(EngineBackend::with_config(lutra_testkit::engine_config(
dir.path(),
)));
let server = lutra_control::start(0, backend.clone()).unwrap();
let mut client = Client::connect(server.local_addr());
client.call(json!({"id": 1, "method": "events.subscribe"}));
let r = client.call(execute("theme.list", Value::Null));
assert_eq!(r["result"][1]["id"], "lutra-light", "{r}");
let r = client.call(execute(
"settings.set",
json!({"key": "appearance.theme", "value": "lutra-light"}),
));
assert_eq!(r["result"]["value"], "lutra-light", "{r}");
let changed = client.next_theme();
assert_eq!(changed["id"], "lutra-light");
assert_eq!(changed["dark"], false);
assert_eq!(changed["tokens"], tokens(&theme::LIGHT));
client.call(execute(
"settings.set",
json!({"key": "appearance.theme", "value": "lutra-dark"}),
));
let changed = client.next_theme();
assert_eq!(changed["id"], "lutra-dark");
assert_eq!(changed["dark"], true);
assert_eq!(changed["tokens"], tokens(&theme::DARK));
client.call(execute(
"settings.set",
json!({"key": "appearance.theme", "value": "system"}),
));
let r = client.call(execute("theme.systemPreference", json!({"dark": false})));
assert_eq!(r["result"]["id"], "lutra-light", "{r}");
let changed = client.next_theme();
assert_eq!(
changed["id"], "lutra-light",
"switching from dark to system on an unreported (dark) OS must not emit"
);
assert_eq!(changed["tokens"]["--lt-editor-selection-bg"], "#e5d7c9");
}