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>
298 lines
9.9 KiB
Rust
298 lines
9.9 KiB
Rust
//! Theme commands and `themeChanged`: switching through `settings.set`, the
|
|
//! "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,
|
|
clippy::expect_used,
|
|
clippy::panic,
|
|
clippy::indexing_slicing
|
|
)]
|
|
|
|
use lutra_engine::{Engine, EngineConfig, Event, theme};
|
|
use serde_json::{Value, json};
|
|
use tempfile::TempDir;
|
|
|
|
fn themes(events: &[Event]) -> Vec<(String, bool)> {
|
|
events
|
|
.iter()
|
|
.filter_map(|e| match e {
|
|
Event::ThemeChanged { id, dark, .. } => Some((id.clone(), *dark)),
|
|
_ => None,
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn set_theme(engine: &mut Engine, value: &str) -> Vec<(String, bool)> {
|
|
let outcome = engine
|
|
.execute(
|
|
"settings.set",
|
|
json!({"key": "appearance.theme", "value": value}),
|
|
)
|
|
.unwrap();
|
|
themes(&outcome.events)
|
|
}
|
|
|
|
fn report(engine: &mut Engine, dark: bool) -> (Value, Vec<(String, bool)>) {
|
|
let outcome = engine
|
|
.execute("theme.systemPreference", json!({"dark": dark}))
|
|
.unwrap();
|
|
let events = themes(&outcome.events);
|
|
(outcome.value, events)
|
|
}
|
|
|
|
#[test]
|
|
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!(
|
|
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]
|
|
fn switching_the_setting_emits_the_new_tokens() {
|
|
let mut e = Engine::new().unwrap();
|
|
let outcome = e
|
|
.execute(
|
|
"settings.set",
|
|
json!({"key": "appearance.theme", "value": "lutra-light"}),
|
|
)
|
|
.unwrap();
|
|
let Some(Event::ThemeChanged { id, dark, tokens }) = outcome
|
|
.events
|
|
.iter()
|
|
.find(|e| matches!(e, Event::ThemeChanged { .. }))
|
|
else {
|
|
panic!("no themeChanged in {:?}", outcome.events);
|
|
};
|
|
assert_eq!(id, "lutra-light");
|
|
assert!(!dark);
|
|
assert_eq!(tokens, &theme::token_map(&theme::LIGHT));
|
|
assert_eq!(tokens["--lt-bg"], "#f3efe3");
|
|
assert_eq!(tokens["--lt-font-size-editor"], "14px");
|
|
assert!(matches!(
|
|
outcome.events.first(),
|
|
Some(Event::SettingsChanged { .. })
|
|
));
|
|
|
|
assert_eq!(set_theme(&mut e, "lutra-light"), []);
|
|
assert_eq!(
|
|
set_theme(&mut e, "lutra-dark"),
|
|
[("lutra-dark".into(), true)]
|
|
);
|
|
let reset = e
|
|
.execute("settings.reset", json!({"key": "appearance.theme"}))
|
|
.unwrap();
|
|
assert_eq!(themes(&reset.events), []);
|
|
let other = e
|
|
.execute(
|
|
"settings.set",
|
|
json!({"key": "editor.fontSize", "value": 18}),
|
|
)
|
|
.unwrap();
|
|
assert_eq!(themes(&other.events), []);
|
|
}
|
|
|
|
#[test]
|
|
fn system_mode_follows_the_reported_preference() {
|
|
let mut e = Engine::new().unwrap();
|
|
let (value, events) = report(&mut e, true);
|
|
assert_eq!(events, []);
|
|
assert_eq!(value["id"], "lutra-dark");
|
|
assert_eq!(value["dark"], true);
|
|
assert_eq!(value["tokens"]["--lt-bg"], "#1e1b1e");
|
|
assert!(value.get("kind").is_none());
|
|
|
|
let (value, events) = report(&mut e, false);
|
|
assert_eq!(events, [("lutra-light".into(), false)]);
|
|
assert_eq!(value["id"], "lutra-light");
|
|
assert_eq!(report(&mut e, false).1, []);
|
|
|
|
assert_eq!(
|
|
set_theme(&mut e, "lutra-dark"),
|
|
[("lutra-dark".into(), true)]
|
|
);
|
|
let (value, events) = report(&mut e, true);
|
|
assert_eq!(events, []);
|
|
assert_eq!(report(&mut e, false).1, []);
|
|
assert_eq!(value["id"], "lutra-dark");
|
|
|
|
assert_eq!(set_theme(&mut e, "system"), [("lutra-light".into(), false)]);
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_bad_preference_params() {
|
|
let mut e = Engine::new().unwrap();
|
|
for params in [
|
|
json!({}),
|
|
json!({"dark": "yes"}),
|
|
json!({"dark": true, "x": 1}),
|
|
] {
|
|
let err = e.execute("theme.systemPreference", params).unwrap_err();
|
|
assert_eq!(err.code(), "invalid_params");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn the_stored_theme_survives_a_restart() {
|
|
let dir = TempDir::new().unwrap();
|
|
let config = EngineConfig {
|
|
config_dir: dir.path().join("config"),
|
|
data_dir: dir.path().join("data"),
|
|
};
|
|
let mut e = Engine::with_config(config.clone()).unwrap();
|
|
set_theme(&mut e, "lutra-light");
|
|
drop(e);
|
|
let mut e = Engine::with_config(config).unwrap();
|
|
let (value, _) = report(&mut e, true);
|
|
assert_eq!(value["id"], "lutra-light");
|
|
}
|