1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! The `Theme` struct — loads a stylesheet and resolves styles for widgets.
use std::cell::RefCell;
use std::collections::HashMap;
use crate::theme::color::ThemeColor;
use crate::theme::css::ast::{CssValue, PseudoClass, Stylesheet};
use crate::theme::css::parser::{ParseError, parse_stylesheet};
use crate::theme::resolve::computed::ComputedStyle;
/// A loaded theme. Constructed from an `.icss` source string.
///
/// Provides methods to resolve iced widget styles from CSS class lists.
pub struct Theme {
stylesheet: Stylesheet,
/// Index: class name → indices into `stylesheet.rules` that mention this class.
class_index: HashMap<String, Vec<usize>>,
/// Cache: (sorted classes key, pseudo) → ComputedStyle.
cache: RefCell<HashMap<CacheKey, ComputedStyle>>,
/// Default font for `theme.text(...)` helper when a class sets font-weight.
/// Keeps family/stretch/style stable while only overriding weight.
/// Defaults to `iced::Font::DEFAULT`; set via [`Theme::with_default_font`].
default_font: iced::Font,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct CacheKey {
/// Sorted, joined class names.
classes: String,
pseudo: Option<PseudoClass>,
}
impl Theme {
/// Parse an `.icss` source string and build the theme.
pub fn load(source: &str) -> Result<Self, ParseError> {
let stylesheet = parse_stylesheet(source)?;
let class_index = build_class_index(&stylesheet);
Ok(Self {
stylesheet,
class_index,
cache: RefCell::new(HashMap::new()),
default_font: iced::Font::DEFAULT,
})
}
/// Set the default font used by `theme.text(...)` when a class sets
/// `font-weight`. The helper constructs an override as
/// `iced::Font { weight: <parsed>, ..default_font }`, so family/stretch/
/// style stay stable while only weight changes.
///
/// Call this once at app boot after theme load, passing the same font you
/// configured in `iced::application(...).default_font(...)`.
pub fn with_default_font(mut self, font: iced::Font) -> Self {
self.default_font = font;
self
}
/// Read the default font used for font-weight overrides in `theme.text`.
pub fn default_font(&self) -> iced::Font {
self.default_font
}
/// Access the raw stylesheet.
pub fn stylesheet(&self) -> &Stylesheet {
&self.stylesheet
}
/// Resolve a `ComputedStyle` for an element with the given classes and pseudo-state.
///
/// This is the core resolution algorithm:
/// 1. Find all rules whose selector classes are a subset of `classes`
/// 2. Filter by pseudo-class match
/// 3. Sort by specificity, then source order
/// 4. Merge declarations (later/more-specific wins)
/// 5. Resolve `var()` references
pub fn resolve(&self, classes: &[&str], pseudo: Option<PseudoClass>) -> ComputedStyle {
// Check cache.
let mut sorted_classes: Vec<&str> = classes.to_vec();
sorted_classes.sort();
let key = CacheKey {
classes: sorted_classes.join(","),
pseudo,
};
if let Some(cached) = self.cache.borrow().get(&key) {
return cached.clone();
}
// Collect candidate rule indices via the class index.
let mut candidate_indices: Vec<usize> = Vec::new();
for class in &sorted_classes {
if let Some(indices) = self.class_index.get(*class) {
for &idx in indices {
if !candidate_indices.contains(&idx) {
candidate_indices.push(idx);
}
}
}
}
// Filter: selector classes must be a subset of element classes,
// and pseudo must match.
let mut matching: Vec<&crate::theme::css::ast::Rule> = candidate_indices
.iter()
.filter_map(|&idx| {
let rule = &self.stylesheet.rules[idx];
if rule.selector.matches(classes, pseudo) {
Some(rule)
} else {
None
}
})
.collect();
// Also include base rules (no pseudo) when resolving with a pseudo-state.
// Base rules form the foundation; pseudo rules override.
if pseudo.is_some() {
let base_matching: Vec<&crate::theme::css::ast::Rule> = candidate_indices
.iter()
.filter_map(|&idx| {
let rule = &self.stylesheet.rules[idx];
if rule.selector.pseudo.is_none() && rule.selector.matches(classes, None) {
Some(rule)
} else {
None
}
})
.collect();
// Combine: base rules first, then pseudo-specific rules.
let mut combined = base_matching;
combined.extend(matching);
matching = combined;
}
// Sort by specificity (ascending), then source order.
matching.sort_by(|a, b| {
let spec_a = a.selector.specificity();
let spec_b = b.selector.specificity();
spec_a
.cmp(&spec_b)
.then(a.source_order.cmp(&b.source_order))
});
// Merge declarations — later wins.
let mut computed = ComputedStyle::new();
for rule in &matching {
for decl in &rule.declarations {
let resolved = self.resolve_value(&decl.value);
computed.set(decl.property.clone(), resolved);
}
}
// Cache and return.
self.cache.borrow_mut().insert(key, computed.clone());
computed
}
/// Resolve a `CssValue`, substituting `var()` references.
fn resolve_value(&self, value: &CssValue) -> CssValue {
match value {
CssValue::Var { name, fallback } => {
if let Some(resolved) = self.stylesheet.custom_properties.get(name) {
self.resolve_value(resolved)
} else if let Some(fb) = fallback {
self.resolve_value(fb)
} else {
tracing::warn!("unresolved CSS variable: {name}");
CssValue::Keyword("unset".into())
}
}
// Raw values may contain unresolved var() references (e.g. shadow
// colors like `0 2px 8px var(--shadow)`). Substitute and re-parse.
CssValue::Raw(raw) if raw.contains("var(") || raw.contains("var (") => {
let substituted = self.substitute_vars_in_raw(raw);
crate::theme::css::tokenize_and_parse(&substituted)
}
other => other.clone(),
}
}
/// Text-level var() substitution for raw CSS values.
fn substitute_vars_in_raw(&self, raw: &str) -> String {
let mut result = raw.to_string();
// Iteratively substitute var(--name) references.
// The tokenizer may insert spaces: "var ( --name )" so we handle both forms.
while let Some(start) = result.find("var(").or_else(|| result.find("var (")) {
// Skip past "var" + optional space + "("
let paren_pos = result[start..].find('(').unwrap() + start;
let after = &result[paren_pos + 1..];
if let Some(end) = after.find(')') {
let var_name = after[..end].trim();
let replacement = self
.stylesheet
.custom_properties
.get(var_name)
.map(|v| v.to_css_string())
.unwrap_or_else(|| "transparent".into());
result = format!(
"{}{}{}",
&result[..start],
replacement,
&result[paren_pos + 1 + end + 1..]
);
} else {
break;
}
}
result
}
/// Resolve with base state (no pseudo) + a specific pseudo layered on top.
///
/// This is the typical pattern for widget style closures:
/// compute base, then override with pseudo-specific properties.
pub fn resolve_with_pseudo(
&self,
classes: &[&str],
pseudo: Option<PseudoClass>,
) -> ComputedStyle {
if pseudo.is_none() {
return self.resolve(classes, None);
}
// The resolve() method already handles layering base + pseudo.
self.resolve(classes, pseudo)
}
/// Look up a CSS custom property (e.g. `"primary-s0"`) and return as iced Color.
/// Accepts with or without `--` prefix.
pub fn color_var(&self, var_name: &str) -> Option<iced::Color> {
let key = if var_name.starts_with("--") {
var_name.to_string()
} else {
format!("--{var_name}")
};
let value = self.stylesheet.custom_properties.get(&key)?;
match value {
CssValue::Color(c) => Some(c.to_iced()),
CssValue::Raw(s) => ThemeColor::from_hex(s).map(|c| c.to_iced()),
_ => None,
}
}
/// Look up a CSS custom property (e.g. `"space-400"`) and return as f32 length.
/// Accepts with or without `--` prefix.
pub fn length_var(&self, var_name: &str) -> Option<f32> {
let key = if var_name.starts_with("--") {
var_name.to_string()
} else {
format!("--{var_name}")
};
let value = self.stylesheet.custom_properties.get(&key)?;
match value {
CssValue::Length(v) | CssValue::Number(v) => Some(*v),
CssValue::Raw(s) => s.trim_end_matches("px").parse().ok(),
_ => None,
}
}
/// Helper: resolve a color from computed style. Does NOT apply opacity.
pub fn resolve_color(computed: &ComputedStyle, property: &str) -> Option<ThemeColor> {
computed.color(property)
}
}
/// Build an index from class names to rule indices.
fn build_class_index(stylesheet: &Stylesheet) -> HashMap<String, Vec<usize>> {
let mut index: HashMap<String, Vec<usize>> = HashMap::new();
for (i, rule) in stylesheet.rules.iter().enumerate() {
for class in &rule.selector.classes {
index.entry(class.clone()).or_default().push(i);
}
}
index
}
#[cfg(test)]
mod tests {
use super::*;
const TEST_CSS: &str = r#"
:root {
--primary: #0f3460;
--primary-light: #1a4a80;
}
.button {
color: #ffffff;
border-radius: 8px;
}
.primary {
background-color: var(--primary);
}
.primary:hover {
background-color: var(--primary-light);
}
.button.primary {
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
}
.small {
border-radius: 4px;
}
"#;
#[test]
fn resolve_single_class() {
let theme = Theme::load(TEST_CSS).unwrap();
let style = theme.resolve(&["button"], None);
assert!(style.color("color").is_some());
assert_eq!(style.length("border-radius"), Some(8.0));
}
#[test]
fn resolve_multi_class() {
let theme = Theme::load(TEST_CSS).unwrap();
let style = theme.resolve(&["button", "primary"], None);
// Should have color from .button, background-color from .primary,
// and box-shadow from .button.primary.
assert!(style.color("color").is_some());
assert!(style.color("background-color").is_some());
assert!(style.shadow("box-shadow").is_some());
}
#[test]
fn resolve_pseudo_layers_on_base() {
let theme = Theme::load(TEST_CSS).unwrap();
let style = theme.resolve(&["button", "primary"], Some(PseudoClass::Hover));
// hover should override background-color from :hover rule,
// but keep other base properties.
let bg = style.color("background-color").unwrap();
// --primary-light = #1a4a80 → r ≈ 0.102
assert!(bg.r > 0.09 && bg.r < 0.12);
}
#[test]
fn resolve_composable_override() {
let theme = Theme::load(TEST_CSS).unwrap();
let style = theme.resolve(&["button", "primary", "small"], None);
// .small has border-radius: 4px which should override .button's 8px
// because .small comes later in source order with same specificity.
assert_eq!(style.length("border-radius"), Some(4.0));
}
#[test]
fn resolve_unmatched_class() {
let theme = Theme::load(TEST_CSS).unwrap();
let style = theme.resolve(&["nonexistent"], None);
assert!(style.color("color").is_none());
}
#[test]
fn var_resolution() {
let theme = Theme::load(TEST_CSS).unwrap();
let style = theme.resolve(&["primary"], None);
let bg = style.color("background-color").unwrap();
// --primary = #0f3460 → r ≈ 0.059
assert!(bg.r > 0.05 && bg.r < 0.07);
}
#[test]
fn caching_works() {
let theme = Theme::load(TEST_CSS).unwrap();
let s1 = theme.resolve(&["button"], None);
let s2 = theme.resolve(&["button"], None);
// Should be identical (from cache).
assert_eq!(s1.length("border-radius"), s2.length("border-radius"));
}
}