M0.6: command registry, guard, theme tokens, app.about and app.quit

Command trait with static spec, enabled check and run; Registry rejects
duplicate ids; Engine::execute runs every command inside the guard, which
turns panics into EngineError::Internal and keeps the engine usable.
Effects (Quit) are applied by the host. Theme tokens for Lutra Dark and
Lutra Light render as --lt-* CSS variables and a webview init script.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-14 06:40:15 +02:00
parent 50bcc95006
commit b382f9fae7
12 changed files with 758 additions and 1 deletions

129
crates/engine/src/theme.rs Normal file
View file

@ -0,0 +1,129 @@
//! Theme tokens: the single source of every colour and font in the UI.
//!
//! Themes are defined here in Rust and rendered as CSS custom properties
//! (`--lt-<token>`). Frontend components use only `var(--lt-…)`. The palettes
//! are Lutra's own: warm otter browns with a copper accent.
/// A named set of token values.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Theme {
/// Stable id used in settings, e.g. `lutra-dark`.
pub id: &'static str,
/// Display name.
pub name: &'static str,
/// Whether the theme is dark, for the window chrome and `color-scheme`.
pub dark: bool,
/// Token names (without the `--lt-` prefix) and CSS values.
pub tokens: &'static [(&'static str, &'static str)],
}
/// Tokens shared by every theme (fonts and sizes).
const COMMON: &[(&str, &str)] = &[
(
"font-ui",
"system-ui, -apple-system, \"Segoe UI\", Roboto, Ubuntu, sans-serif",
),
(
"font-mono",
"ui-monospace, \"Cascadia Code\", Menlo, Consolas, monospace",
),
("font-size-ui", "13px"),
("font-size-small", "12px"),
];
/// The default dark theme.
pub const DARK: Theme = Theme {
id: "lutra-dark",
name: "Lutra Dark",
dark: true,
tokens: &[
("bg", "#1f1d1b"),
("editor-bg", "#191817"),
("fg", "#e6e1da"),
("fg-muted", "#9a928a"),
("border", "#34302c"),
("accent", "#d39a5c"),
("error", "#ef7b6c"),
("statusbar-bg", "#24211f"),
("statusbar-fg", "#b8afa6"),
],
};
/// The light theme.
pub const LIGHT: Theme = Theme {
id: "lutra-light",
name: "Lutra Light",
dark: false,
tokens: &[
("bg", "#f4f1ec"),
("editor-bg", "#fbfaf8"),
("fg", "#2a2622"),
("fg-muted", "#7a7168"),
("border", "#ddd6cd"),
("accent", "#a8662a"),
("error", "#c2412f"),
("statusbar-bg", "#ece7e0"),
("statusbar-fg", "#5a5149"),
],
};
/// Every built-in theme.
pub const BUILTIN: &[Theme] = &[DARK, LIGHT];
/// Looks a built-in theme up by id.
pub fn by_id(id: &str) -> Option<Theme> {
BUILTIN.iter().copied().find(|t| t.id == id)
}
/// Renders a theme as a `:root { … }` rule of CSS custom properties.
pub fn css_variables(theme: &Theme) -> String {
let mut css = String::from(":root {\n");
css.push_str(&format!(
" color-scheme: {};\n",
if theme.dark { "dark" } else { "light" }
));
for (name, value) in COMMON.iter().chain(theme.tokens) {
css.push_str(&format!(" --lt-{name}: {value};\n"));
}
css.push('}');
css
}
/// JavaScript that installs the theme's CSS as a `<style id="lt-theme">`
/// element as soon as the document has a `<head>`, for use as a webview
/// initialization script.
pub fn init_script(theme: &Theme) -> String {
let css = serde_json::Value::String(css_variables(theme)).to_string();
format!(
"(function(){{var css={css};function apply(){{var s=document.getElementById('lt-theme');if(!s){{s=document.createElement('style');s.id='lt-theme';document.head.appendChild(s);}}s.textContent=css;}}if(document.head){{apply();}}else{{document.addEventListener('DOMContentLoaded',apply);}}}})();"
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn themes_define_the_same_tokens() {
let names = |t: &Theme| t.tokens.iter().map(|(n, _)| *n).collect::<Vec<_>>();
assert_eq!(names(&DARK), names(&LIGHT));
}
#[test]
fn renders_css_variables() {
let css = css_variables(&DARK);
assert!(css.starts_with(":root {"));
assert!(css.contains(" --lt-bg: #1f1d1b;\n"));
assert!(css.contains(" --lt-font-size-ui: 13px;\n"));
assert!(css.contains("color-scheme: dark;"));
}
#[test]
fn init_script_embeds_escaped_css() {
let js = init_script(&LIGHT);
assert!(js.contains("\\\"Segoe UI\\\""));
assert!(js.contains("lt-theme"));
assert_eq!(by_id("lutra-light"), Some(LIGHT));
assert_eq!(by_id("nope"), None);
}
}