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