//! 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-`). Frontend components use only `var(--lt-…)`. The palettes //! are Lutra's own: warm otter browns with a copper accent. Text and //! background pairs meet WCAG AA in both themes; `ui/tests/theme` checks it //! against the snapshot `ui/tests/theme/themes.json`, which the test //! `ui_snapshot_is_current` keeps in step with this file. //! //! The setting `appearance.theme` holds a theme id or [`SYSTEM`]. "System" //! follows the OS: the frontend reports the OS preference and //! [`resolve`] picks the theme. use std::collections::BTreeMap; /// 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, /// Message-catalog key of the display name. pub label_key: &'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)], } /// The `appearance.theme` value that follows the OS preference. pub const SYSTEM: &str = "system"; /// 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"), ("font-size-editor", "14px"), ]; /// Syntax token names (`--lt-syntax-`) every theme defines. The /// editor's highlighter reads only these; each reaches 4.5:1 on the editor /// background (`ui/tests/theme/contrast.test.ts`). The colours are Lutra's /// own, derived from the theme's copper accent and warm neutrals. pub const SYNTAX_TOKENS: &[&str] = &[ "keyword", "string", "number", "comment", "variable", "function", "type", "constant", "tag", "attribute", "operator", ]; /// The default dark theme. pub const DARK: Theme = Theme { id: "lutra-dark", label_key: "theme.lutraDark", 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"), ("editor-selection-bg", "#513f2c"), ("editor-active-line-bg", "#232221"), ("editor-cursor", "#d39a5c"), ("editor-gutter-bg", "#191817"), ("editor-gutter-fg", "#9a928a"), ("editor-match-bg", "#3a2f23"), ("tab-active-bg", "#191817"), ("tab-modified", "#d39a5c"), ("notice-bg", "#3a3025"), ("syntax-keyword", "#d39a5c"), ("syntax-string", "#a9bd7e"), ("syntax-number", "#e3a1a8"), ("syntax-comment", "#958d84"), ("syntax-variable", "#d8bba6"), ("syntax-function", "#e6c46f"), ("syntax-type", "#7fbac6"), ("syntax-constant", "#c4a0d6"), ("syntax-tag", "#e08b70"), ("syntax-attribute", "#cdb87e"), ("syntax-operator", "#b8afa6"), ], }; /// The light theme. pub const LIGHT: Theme = Theme { id: "lutra-light", label_key: "theme.lutraLight", dark: false, tokens: &[ ("bg", "#f4f1ec"), ("editor-bg", "#fbfaf8"), ("fg", "#2a2622"), ("fg-muted", "#6b625a"), ("border", "#ddd6cd"), ("accent", "#965a22"), ("error", "#b23a29"), ("statusbar-bg", "#ece7e0"), ("statusbar-fg", "#5a5149"), ("editor-selection-bg", "#e5d7c9"), ("editor-active-line-bg", "#f1efed"), ("editor-cursor", "#965a22"), ("editor-gutter-bg", "#fbfaf8"), ("editor-gutter-fg", "#6b625a"), ("editor-match-bg", "#ece2d8"), ("tab-active-bg", "#fbfaf8"), ("tab-modified", "#965a22"), ("notice-bg", "#e9dfd4"), ("syntax-keyword", "#965a22"), ("syntax-string", "#4e6a24"), ("syntax-number", "#a03a4e"), ("syntax-comment", "#6e665e"), ("syntax-variable", "#7f4a34"), ("syntax-function", "#7a5808"), ("syntax-type", "#24697a"), ("syntax-constant", "#73479a"), ("syntax-tag", "#a5432a"), ("syntax-attribute", "#695d1a"), ("syntax-operator", "#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 { BUILTIN.iter().copied().find(|t| t.id == id) } /// The theme for an `appearance.theme` value. /// /// # Parameters /// - `setting`: a theme id or [`SYSTEM`]. /// - `system_dark`: the OS preference; `None` while the frontend has not /// reported it, which counts as dark. /// /// Returns [`DARK`] for an unknown id. pub fn resolve(setting: &str, system_dark: Option) -> Theme { if setting == SYSTEM { return if system_dark.unwrap_or(true) { DARK } else { LIGHT }; } by_id(setting).unwrap_or(DARK) } /// Every token of a theme, shared ones included, as `--lt-` → value. pub fn token_map(theme: &Theme) -> BTreeMap { COMMON .iter() .chain(theme.tokens) .map(|(name, value)| (format!("--lt-{name}"), (*value).to_string())) .collect() } /// 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 } /// The CSS for an `appearance.theme` value: the theme's variables, or for /// [`SYSTEM`] the dark theme with the light one inside a /// `prefers-color-scheme: light` media query, so the first frame follows the /// OS before the frontend has started. pub fn css_for_setting(setting: &str) -> String { if setting == SYSTEM { return format!( "{}\n@media (prefers-color-scheme: light) {{\n{}\n}}", css_variables(&DARK), css_variables(&LIGHT) ); } css_variables(&resolve(setting, None)) } /// JavaScript that installs the CSS for an `appearance.theme` value as a /// `