Each command module keeps its own COMMAND_IDS next to its commands; the engine listing test checks their union against the registry, so a missing or extra command still fails without one shared list. The menu and palette tests derive their expectation from the listing and check grouping and ordering rules; the catalog's runtime keys are scanned from the sources. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
107 lines
3.5 KiB
Rust
107 lines
3.5 KiB
Rust
//! 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)))
|
|
}
|
|
}
|
|
|
|
/// The ids of the commands this module registers, checked against the
|
|
/// registry by the engine's listing test. Add a new command's id here.
|
|
#[cfg(test)]
|
|
pub(crate) const COMMAND_IDS: &[&str] = &["theme.list", "theme.systemPreference"];
|