341 lines
11 KiB
Rust
341 lines
11 KiB
Rust
//! 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. 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-<name>`) 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<Theme> {
|
|
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<bool>) -> 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-<name>` → value.
|
|
pub fn token_map(theme: &Theme) -> BTreeMap<String, String> {
|
|
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
|
|
/// `<style id="lt-theme">` element as soon as the document has a `<head>`,
|
|
/// for use as a webview initialization script.
|
|
pub fn init_script(setting: &str) -> String {
|
|
let css = serde_json::Value::String(css_for_setting(setting)).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::*;
|
|
|
|
/// The snapshot of every theme that the frontend's contrast test reads.
|
|
fn ui_snapshot() -> String {
|
|
let themes: Vec<_> = BUILTIN
|
|
.iter()
|
|
.map(|t| {
|
|
serde_json::json!({
|
|
"id": t.id,
|
|
"labelKey": t.label_key,
|
|
"dark": t.dark,
|
|
"tokens": token_map(t),
|
|
})
|
|
})
|
|
.collect();
|
|
let mut text = serde_json::to_string_pretty(&themes).unwrap();
|
|
text.push('\n');
|
|
text
|
|
}
|
|
|
|
#[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 themes_define_the_editor_tokens() {
|
|
for theme in BUILTIN {
|
|
let tokens = token_map(theme);
|
|
for name in [
|
|
"editor-selection-bg",
|
|
"editor-active-line-bg",
|
|
"editor-cursor",
|
|
"editor-gutter-bg",
|
|
"editor-gutter-fg",
|
|
"editor-match-bg",
|
|
"font-size-editor",
|
|
"tab-active-bg",
|
|
"tab-modified",
|
|
"notice-bg",
|
|
] {
|
|
assert!(
|
|
tokens.contains_key(&format!("--lt-{name}")),
|
|
"{} lacks {name}",
|
|
theme.id
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn themes_define_the_syntax_tokens() {
|
|
for theme in BUILTIN {
|
|
let tokens = token_map(theme);
|
|
for name in SYNTAX_TOKENS {
|
|
assert!(
|
|
tokens.contains_key(&format!("--lt-syntax-{name}")),
|
|
"{} lacks syntax-{name}",
|
|
theme.id
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn resolves_settings() {
|
|
assert_eq!(resolve("lutra-light", Some(true)), LIGHT);
|
|
assert_eq!(resolve("lutra-dark", Some(false)), DARK);
|
|
assert_eq!(resolve(SYSTEM, Some(false)), LIGHT);
|
|
assert_eq!(resolve(SYSTEM, Some(true)), DARK);
|
|
assert_eq!(resolve(SYSTEM, None), DARK);
|
|
assert_eq!(resolve("nope", Some(false)), DARK);
|
|
}
|
|
|
|
#[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 css_follows_the_setting() {
|
|
assert_eq!(css_for_setting("lutra-light"), css_variables(&LIGHT));
|
|
assert_eq!(css_for_setting("lutra-dark"), css_variables(&DARK));
|
|
let system = css_for_setting(SYSTEM);
|
|
assert!(system.starts_with(&css_variables(&DARK)));
|
|
assert!(system.contains("@media (prefers-color-scheme: light) {\n:root {"));
|
|
assert!(system.contains("color-scheme: light;"));
|
|
}
|
|
|
|
#[test]
|
|
fn init_script_embeds_escaped_css() {
|
|
let js = init_script("lutra-light");
|
|
assert!(js.contains("\\\"Segoe UI\\\""));
|
|
assert!(js.contains("lt-theme"));
|
|
assert!(js.contains("--lt-bg: #f4f1ec;"));
|
|
assert_eq!(by_id("lutra-light"), Some(LIGHT));
|
|
assert_eq!(by_id("nope"), None);
|
|
}
|
|
|
|
/// Set `LUTRA_UPDATE_SNAPSHOTS=1` to rewrite the snapshot after changing
|
|
/// a token.
|
|
#[test]
|
|
fn ui_snapshot_is_current() {
|
|
let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
.join("../../ui/tests/theme/themes.json");
|
|
let expected = ui_snapshot();
|
|
if std::env::var_os("LUTRA_UPDATE_SNAPSHOTS").is_some() {
|
|
std::fs::write(&path, &expected).unwrap();
|
|
}
|
|
let actual = std::fs::read_to_string(&path)
|
|
.unwrap_or_default()
|
|
.replace("\r\n", "\n");
|
|
assert!(
|
|
actual == expected,
|
|
"{} is out of date; run with LUTRA_UPDATE_SNAPSHOTS=1",
|
|
path.display()
|
|
);
|
|
}
|
|
}
|