| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
use std::collections::BTreeMap; |
| 18 |
use std::sync::LazyLock; |
| 19 |
|
| 20 |
use makeover::{ThemeMeta, embedded_themes, intent_css_vars, parse_theme_str, resolve}; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
pub const DEFAULT_THEME_ID: &str = "makenotwork"; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
pub struct ThemeEntry { |
| 29 |
pub meta: ThemeMeta, |
| 30 |
|
| 31 |
pub css: String, |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
static REGISTRY: LazyLock<BTreeMap<String, ThemeEntry>> = LazyLock::new(|| { |
| 36 |
let mut map = BTreeMap::new(); |
| 37 |
for (id, content) in embedded_themes() { |
| 38 |
match parse_theme_str(id, content, false) { |
| 39 |
Ok(theme) => { |
| 40 |
let tokens = resolve(&theme); |
| 41 |
let css = intent_css_vars(&tokens); |
| 42 |
map.insert( |
| 43 |
id.to_string(), |
| 44 |
ThemeEntry { |
| 45 |
meta: tokens.meta, |
| 46 |
css, |
| 47 |
}, |
| 48 |
); |
| 49 |
} |
| 50 |
Err(e) => { |
| 51 |
|
| 52 |
|
| 53 |
tracing::error!(theme = id, error = %e, "skipping unparseable bundled theme"); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
map |
| 58 |
}); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
pub fn is_valid_theme(id: &str) -> bool { |
| 63 |
REGISTRY.contains_key(id) |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
pub fn theme_css(id: Option<&str>) -> &'static str { |
| 73 |
let chosen = id |
| 74 |
.filter(|i| REGISTRY.contains_key(*i)) |
| 75 |
.unwrap_or(DEFAULT_THEME_ID); |
| 76 |
REGISTRY |
| 77 |
.get(chosen) |
| 78 |
.or_else(|| REGISTRY.get(DEFAULT_THEME_ID)) |
| 79 |
.map_or("", |e| e.css.as_str()) |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
pub fn list_themes() -> Vec<&'static ThemeMeta> { |
| 85 |
let mut themes: Vec<&ThemeMeta> = REGISTRY.values().map(|e| &e.meta).collect(); |
| 86 |
themes.sort_by(|a, b| a.name.cmp(&b.name)); |
| 87 |
themes |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
pub fn normalize_theme_id(raw: Option<&str>) -> Result<Option<String>, String> { |
| 96 |
match raw.map(str::trim) { |
| 97 |
None | Some("") => Ok(None), |
| 98 |
Some(id) if is_valid_theme(id) => Ok(Some(id.to_string())), |
| 99 |
Some(id) => Err(id.to_string()), |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
#[derive(Debug, Clone)] |
| 106 |
pub struct ThemeOption { |
| 107 |
pub id: String, |
| 108 |
pub name: String, |
| 109 |
pub selected: bool, |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
pub fn theme_options(selected: Option<&str>) -> Vec<ThemeOption> { |
| 117 |
let current = selected |
| 118 |
.filter(|i| is_valid_theme(i)) |
| 119 |
.unwrap_or(DEFAULT_THEME_ID); |
| 120 |
list_themes() |
| 121 |
.into_iter() |
| 122 |
.map(|m| ThemeOption { |
| 123 |
id: m.id.clone(), |
| 124 |
name: m.name.clone(), |
| 125 |
selected: m.id == current, |
| 126 |
}) |
| 127 |
.collect() |
| 128 |
} |
| 129 |
|
| 130 |
#[cfg(test)] |
| 131 |
mod tests { |
| 132 |
use super::*; |
| 133 |
|
| 134 |
#[test] |
| 135 |
fn default_theme_is_bundled() { |
| 136 |
assert!( |
| 137 |
is_valid_theme(DEFAULT_THEME_ID), |
| 138 |
"makenotwork.toml must be bundled" |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
#[test] |
| 143 |
fn default_css_carries_intent_tokens() { |
| 144 |
let css = theme_css(None); |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
assert!(css.contains("--surface-page: #ddd6c9;")); |
| 152 |
assert!(css.contains("--content: #3d3530;")); |
| 153 |
assert!(css.contains("--action: #6c5ce7;")); |
| 154 |
assert!(css.contains("--border: #b3ab97;")); |
| 155 |
|
| 156 |
assert!(css.contains("--action-hover: ")); |
| 157 |
assert!(css.contains("--overlay: rgba(")); |
| 158 |
} |
| 159 |
|
| 160 |
#[test] |
| 161 |
fn unknown_id_falls_back_to_default() { |
| 162 |
assert_eq!(theme_css(Some("no-such-theme")), theme_css(None)); |
| 163 |
assert_eq!(theme_css(Some("../etc/passwd")), theme_css(None)); |
| 164 |
} |
| 165 |
|
| 166 |
#[test] |
| 167 |
fn known_alternate_theme_differs_from_default() { |
| 168 |
|
| 169 |
assert!(is_valid_theme("nord")); |
| 170 |
assert_ne!(theme_css(Some("nord")), theme_css(None)); |
| 171 |
} |
| 172 |
|
| 173 |
#[test] |
| 174 |
fn picker_list_is_nonempty_and_includes_default() { |
| 175 |
let themes = list_themes(); |
| 176 |
assert!(themes.len() > 1); |
| 177 |
assert!(themes.iter().any(|m| m.id == DEFAULT_THEME_ID)); |
| 178 |
} |
| 179 |
} |
| 180 |
|