T1: four more themes, theme picker, theme file import, editor font settings

Plum Night, River, Paper and Reed (designed by Amy, approved by Daniel)
join the brand pair. theme.list/current/select/import, a picker with
live preview (view.selectTheme) and "Import Theme…" (view.importTheme);
imported themes are validated and stored in <data_dir>/themes. Editor
font family and line height settings apply live. Docs: docs/themes.md.

Gate: fmt, clippy and the rest pass up to cargo test, where only the
unrelated lutra-terminal PTY test a_full_buffer_stops_the_shell timed
out under load; to be re-run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-11-02 19:47:58 +01:00
parent 872b5cb258
commit 086bc8a925
26 changed files with 2881 additions and 100 deletions

View file

@ -1,6 +1,7 @@
//! Theme commands and `themeChanged`: switching through `settings.set`, the
//! "system" mode following the reported OS preference, and the stored theme
//! surviving a restart.
//! "system" mode following the reported OS preference, the stored theme
//! surviving a restart, `theme.select`, `theme.current` and importing theme
//! files (C9 T1).
#![allow(
clippy::unwrap_used,
@ -45,13 +46,156 @@ fn report(engine: &mut Engine, dark: bool) -> (Value, Vec<(String, bool)>) {
fn list_names_the_builtin_themes_by_catalog_key() {
let mut e = Engine::new().unwrap();
let list = e.execute("theme.list", Value::Null).unwrap().value;
let rows: Vec<_> = list
.as_array()
.unwrap()
.iter()
.map(|t| {
(
t["id"].as_str().unwrap(),
t["label"].as_str().unwrap(),
t["dark"].as_bool().unwrap(),
)
})
.collect();
assert_eq!(
list,
json!([
{"id": "lutra-dark", "label": "theme.lutraDark", "dark": true},
{"id": "lutra-light", "label": "theme.lutraLight", "dark": false},
])
rows,
[
("lutra-dark", "theme.lutraDark", true),
("lutra-light", "theme.lutraLight", false),
("lutra-plum-night", "theme.lutraPlumNight", true),
("lutra-river", "theme.lutraRiver", true),
("lutra-paper", "theme.lutraPaper", false),
("lutra-reed", "theme.lutraReed", false),
]
);
assert_eq!(list[3]["tokens"], json!(theme::token_map(&theme::RIVER)));
assert_eq!(list[3]["builtin"], true);
assert_eq!(list[3]["name"], Value::Null);
}
/// A valid theme file with the colours of `base`.
fn theme_file(base: &theme::Theme, id: &str, name: &str) -> String {
let tokens: serde_json::Map<_, _> = base
.tokens
.iter()
.map(|(n, v)| (format!("--lt-{n}"), Value::from(*v)))
.collect();
json!({"lutraTheme": 1, "id": id, "name": name, "dark": base.dark, "tokens": tokens})
.to_string()
}
#[test]
fn select_and_current_switch_live() {
let mut e = Engine::new().unwrap();
let out = e
.execute("theme.select", json!({"id": "lutra-paper"}))
.unwrap();
assert_eq!(out.value["id"], "lutra-paper");
assert_eq!(out.value["tokens"]["--lt-bg"], "#f6f5f1");
assert_eq!(themes(&out.events), [("lutra-paper".into(), false)]);
assert!(matches!(
out.events.first(),
Some(Event::SettingsChanged { keys }) if keys == &["appearance.theme"]
));
let current = e.execute("theme.current", Value::Null).unwrap().value;
assert_eq!(current["id"], "lutra-paper");
assert_eq!(current["tokens"], json!(theme::token_map(&theme::PAPER)));
let again = e
.execute("theme.select", json!({"id": "lutra-paper"}))
.unwrap();
assert!(again.events.is_empty());
for bad in [json!({"id": "nope"}), json!({}), json!({"id": "x", "y": 1})] {
let err = e.execute("theme.select", bad).unwrap_err();
assert_eq!(err.code(), "invalid_params");
}
let err = e
.execute(
"settings.set",
json!({"key": "appearance.theme", "value": "not-imported"}),
)
.unwrap_err();
assert!(err.to_string().contains("unknown theme"), "{err}");
}
#[test]
fn imported_themes_are_selected_stored_and_reloaded() {
let dir = TempDir::new().unwrap();
let config = EngineConfig {
config_dir: dir.path().join("config"),
data_dir: dir.path().join("data"),
};
let file = dir.path().join("dusk.json");
std::fs::write(&file, theme_file(&theme::RIVER, "dusk", "Dusk")).unwrap();
let mut e = Engine::with_config(config.clone()).unwrap();
let out = e.execute("theme.import", json!({"path": file})).unwrap();
assert_eq!(
out.value,
json!({"id": "dusk", "name": "Dusk", "dark": true, "replaced": false})
);
assert_eq!(themes(&out.events), [("dusk".into(), true)]);
let stored = dir.path().join("data/themes/dusk.json");
assert!(stored.is_file());
let list = e.execute("settings.list", Value::Null).unwrap().value;
assert_eq!(list[0]["enumValues"].as_array().unwrap().len(), 8);
assert_eq!(list[0]["enumValues"][6], "dusk");
assert_eq!(list[0]["valueNames"], json!({"dusk": "Dusk"}));
std::fs::write(&file, theme_file(&theme::REED, "dusk", "Dusk 2")).unwrap();
let out = e
.execute("theme.import", json!({"path": file, "select": false}))
.unwrap();
assert_eq!(out.value["replaced"], true);
assert_eq!(themes(&out.events), [("dusk".into(), false)]);
drop(e);
std::fs::write(dir.path().join("data/themes/broken.json"), "{oops").unwrap();
let mut e = Engine::with_config(config).unwrap();
let events = e.take_events();
assert!(
events.iter().any(|ev| matches!(
ev,
Event::Notification { message: Some(m), .. } if m.contains("broken.json") && m.contains("not valid JSON")
)),
"{events:?}"
);
let current = e.execute("theme.current", Value::Null).unwrap().value;
assert_eq!(current["id"], "dusk");
assert_eq!(current["tokens"]["--lt-bg"], "#e9e7d8");
let list = e.execute("theme.list", Value::Null).unwrap().value;
assert_eq!(list[6]["name"], "Dusk 2");
assert_eq!(list[6]["builtin"], false);
}
#[test]
fn a_broken_theme_file_is_refused_with_the_reason() {
let dir = TempDir::new().unwrap();
let mut e = Engine::new().unwrap();
let missing = e
.execute(
"theme.import",
json!({"path": dir.path().join("none.json")}),
)
.unwrap_err();
assert_eq!(missing.code(), "failed");
assert!(missing.to_string().contains("cannot read it"), "{missing}");
let file = dir.path().join("bad.json");
std::fs::write(&file, r##"{"lutraTheme": 1, "id": "bad", "name": "Bad", "dark": true, "tokens": {"bg": "#000"}}"##).unwrap();
let err = e
.execute("theme.import", json!({"path": file}))
.unwrap_err();
assert!(
err.to_string()
.contains("the token \"bg\" must be a colour"),
"{err}"
);
std::fs::write(&file, "x".repeat(300 * 1024)).unwrap();
let err = e
.execute("theme.import", json!({"path": file}))
.unwrap_err();
assert!(err.to_string().contains("larger than 256 KiB"), "{err}");
let list = e.execute("theme.list", Value::Null).unwrap().value;
assert_eq!(list.as_array().unwrap().len(), 6);
}
#[test]