Skip to main content

native_theme/model/
resolved.rs

1// Resolved (non-optional) theme types produced after theme resolution.
2//
3// These types mirror their Option-based counterparts in defaults.rs, font.rs,
4// icon_sizes.rs, and mod.rs (ThemeMode), but with all fields
5// guaranteed populated. Produced by validate() after resolve().
6
7use super::border::ResolvedBorderSpec;
8use super::font::ResolvedFontSpec;
9use crate::Rgba;
10
11// --- ResolvedIconSizes ---
12
13/// Fully resolved per-context icon sizes where every context is guaranteed populated.
14#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
15pub struct ResolvedIconSizes {
16    /// Icon size for toolbar buttons.
17    pub toolbar: f32,
18    /// Small icon size for inline use.
19    pub small: f32,
20    /// Large icon size for menus/lists.
21    pub large: f32,
22    /// Icon size for dialog buttons.
23    pub dialog: f32,
24    /// Icon size for panel headers.
25    pub panel: f32,
26}
27
28// --- ResolvedTextScaleEntry ---
29
30/// A single resolved text scale entry with guaranteed size, weight, and line height.
31///
32/// Phase 93-01 (G1): no `Default` derive. Constructed from a populated
33/// `TextScaleEntry` during resolution; the missing-field sentinel is
34/// written inline in
35/// `crate::resolve::validate_helpers::require_text_scale_entry` (private).
36#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
37pub struct ResolvedTextScaleEntry {
38    /// Font size in logical pixels (converted from points during resolution
39    /// if `font_dpi` was set).
40    pub size: f32,
41    /// CSS font weight (100-900).
42    pub weight: u16,
43    /// Line height in logical pixels. Computed as `defaults.line_height * size`
44    /// when not explicitly set. Explicit values from presets are converted from
45    /// points to pixels along with sizes when `font_dpi` is set.
46    pub line_height: f32,
47}
48
49// --- ResolvedTextScale ---
50
51/// A fully resolved text scale with all four typographic roles populated.
52#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
53pub struct ResolvedTextScale {
54    /// Caption / small label text.
55    pub caption: ResolvedTextScaleEntry,
56    /// Section heading text.
57    pub section_heading: ResolvedTextScaleEntry,
58    /// Dialog title text.
59    pub dialog_title: ResolvedTextScaleEntry,
60    /// Large display / hero text.
61    pub display: ResolvedTextScaleEntry,
62}
63
64// --- ResolvedDefaults ---
65
66/// Fully resolved global theme defaults where every field is guaranteed populated.
67///
68/// Mirrors [`crate::model::ThemeDefaults`] but with concrete (non-Option) types.
69/// Produced by the resolution/validation pipeline.
70#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
71pub struct ResolvedDefaults {
72    // ---- Base font ----
73    /// Primary UI font.
74    pub font: ResolvedFontSpec,
75    /// Line height multiplier.
76    pub line_height: f32,
77    /// Monospace font for code/terminal content.
78    pub mono_font: ResolvedFontSpec,
79
80    // ---- Base colors ----
81    /// Main window/surface background color.
82    pub background_color: Rgba,
83    /// Default text color.
84    pub text_color: Rgba,
85    /// Accent/brand color for interactive elements.
86    pub accent_color: Rgba,
87    /// Text color used on accent-colored backgrounds.
88    pub accent_text_color: Rgba,
89    /// Elevated surface color.
90    pub surface_color: Rgba,
91    /// Secondary/subdued text color.
92    pub muted_color: Rgba,
93    /// Drop shadow color.
94    pub shadow_color: Rgba,
95    /// Hyperlink text color.
96    pub link_color: Rgba,
97    /// Selection highlight background.
98    pub selection_background: Rgba,
99    /// Text color over selection highlight.
100    pub selection_text_color: Rgba,
101    /// Selection background when window is unfocused.
102    pub selection_inactive_background: Rgba,
103    /// Text selection background (inline text highlight).
104    pub text_selection_background: Rgba,
105    /// Text selection color (inline text highlight).
106    pub text_selection_color: Rgba,
107    /// Text color for disabled controls.
108    pub disabled_text_color: Rgba,
109
110    // ---- Status colors ----
111    /// Danger/error color.
112    pub danger_color: Rgba,
113    /// Text color on danger-colored backgrounds.
114    pub danger_text_color: Rgba,
115    /// Warning color.
116    pub warning_color: Rgba,
117    /// Text color on warning-colored backgrounds.
118    pub warning_text_color: Rgba,
119    /// Success/confirmation color.
120    pub success_color: Rgba,
121    /// Text color on success-colored backgrounds.
122    pub success_text_color: Rgba,
123    /// Informational color.
124    pub info_color: Rgba,
125    /// Text color on info-colored backgrounds.
126    pub info_text_color: Rgba,
127
128    // ---- Global geometry ----
129    /// Border sub-struct (color, corner_radius, line_width, etc.).
130    pub border: ResolvedBorderSpec,
131    /// Opacity for disabled controls.
132    pub disabled_opacity: f32,
133
134    // ---- Focus ring ----
135    /// Focus indicator outline color.
136    pub focus_ring_color: Rgba,
137    /// Focus indicator outline width.
138    pub focus_ring_width: f32,
139    /// Gap between element edge and focus indicator.
140    pub focus_ring_offset: f32,
141
142    // ---- Icon sizes ----
143    /// Per-context icon sizes.
144    pub icon_sizes: ResolvedIconSizes,
145}
146
147// --- ResolvedTheme ---
148
149/// A fully resolved theme where every field is guaranteed populated.
150///
151/// Produced by `validate()` after `resolve()`. Consumed by toolkit connectors.
152/// Mirrors [`crate::model::ThemeMode`] but with concrete (non-Option) types
153/// for all 25 per-widget structs plus defaults and text scale.
154#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
155pub struct ResolvedTheme {
156    /// Global defaults.
157    pub defaults: ResolvedDefaults,
158    /// Per-role text scale.
159    pub text_scale: ResolvedTextScale,
160
161    // ---- Per-widget resolved structs ----
162    /// Window chrome.
163    pub window: super::widgets::ResolvedWindowTheme,
164    /// Push button.
165    pub button: super::widgets::ResolvedButtonTheme,
166    /// Text input.
167    pub input: super::widgets::ResolvedInputTheme,
168    /// Checkbox / radio button.
169    pub checkbox: super::widgets::ResolvedCheckboxTheme,
170    /// Popup / context menu.
171    pub menu: super::widgets::ResolvedMenuTheme,
172    /// Tooltip.
173    pub tooltip: super::widgets::ResolvedTooltipTheme,
174    /// Scrollbar.
175    pub scrollbar: super::widgets::ResolvedScrollbarTheme,
176    /// Slider.
177    pub slider: super::widgets::ResolvedSliderTheme,
178    /// Progress bar.
179    pub progress_bar: super::widgets::ResolvedProgressBarTheme,
180    /// Tab bar.
181    pub tab: super::widgets::ResolvedTabTheme,
182    /// Sidebar panel.
183    pub sidebar: super::widgets::ResolvedSidebarTheme,
184    /// Toolbar.
185    pub toolbar: super::widgets::ResolvedToolbarTheme,
186    /// Status bar.
187    pub status_bar: super::widgets::ResolvedStatusBarTheme,
188    /// List / table.
189    pub list: super::widgets::ResolvedListTheme,
190    /// Popover / dropdown.
191    pub popover: super::widgets::ResolvedPopoverTheme,
192    /// Splitter handle.
193    pub splitter: super::widgets::ResolvedSplitterTheme,
194    /// Separator line.
195    pub separator: super::widgets::ResolvedSeparatorTheme,
196    /// Toggle switch.
197    pub switch: super::widgets::ResolvedSwitchTheme,
198    /// Dialog.
199    pub dialog: super::widgets::ResolvedDialogTheme,
200    /// Spinner / progress ring.
201    pub spinner: super::widgets::ResolvedSpinnerTheme,
202    /// ComboBox / dropdown trigger.
203    pub combo_box: super::widgets::ResolvedComboBoxTheme,
204    /// Segmented control.
205    pub segmented_control: super::widgets::ResolvedSegmentedControlTheme,
206    /// Card / container.
207    pub card: super::widgets::ResolvedCardTheme,
208    /// Expander / disclosure.
209    pub expander: super::widgets::ResolvedExpanderTheme,
210    /// Hyperlink.
211    pub link: super::widgets::ResolvedLinkTheme,
212}
213
214// --- Resolved ---
215
216/// A theme fully resolved for a specific color mode.
217///
218/// Returned by [`Theme::resolve`](super::Theme::resolve). Bundles everything
219/// a UI framework needs to render a window: the picked variant's resolved
220/// visual properties, the theme's icon set, and the icon theme name — each
221/// already passed through its precedence chain.
222///
223/// # Icon theme precedence
224///
225/// The [`icon_theme`](Self::icon_theme) field is filled by checking, in order:
226///
227/// 1. The picked variant's `defaults.icon_theme` (per-mode override, e.g.
228///    KDE Plasma's `"breeze"` light / `"breeze-dark"` dark).
229/// 2. The parent [`Theme::icon_theme`](super::Theme::icon_theme) (shared
230///    across light and dark).
231/// 3. [`system_icon_theme()`](super::system_icon_theme) (runtime detect).
232///
233/// The first value that is `Some` wins.
234/// [`icon_theme_explicit`](Self::icon_theme_explicit) reports whether the
235/// value came from the TOML (tiers 1 or 2) or from runtime detection
236/// (tier 3) — useful for UI code that labels the theme-author's choice as
237/// `default (X)`.
238///
239/// # Examples
240///
241/// ```
242/// use native_theme::theme::{ColorMode, Theme};
243///
244/// let theme = Theme::preset("material")?;
245/// let r = theme.resolve(ColorMode::Light)?;
246/// assert_eq!(r.icon_theme.as_ref(), "material");
247/// assert!(r.icon_theme_explicit);
248/// # Ok::<(), native_theme::error::Error>(())
249/// ```
250#[derive(Clone, Debug)]
251pub struct Resolved {
252    /// The picked variant's fully-resolved visual properties.
253    pub variant: ResolvedTheme,
254    /// The theme's icon set. Falls back to
255    /// [`system_icon_set()`](super::system_icon_set) when the TOML omits it.
256    pub icon_set: super::IconSet,
257    /// The icon theme name, resolved through the three-tier precedence
258    /// documented on the struct.
259    pub icon_theme: std::borrow::Cow<'static, str>,
260    /// `true` if [`icon_theme`](Self::icon_theme) came from the TOML (tiers 1
261    /// or 2); `false` if it was filled from runtime system detection (tier 3).
262    pub icon_theme_explicit: bool,
263}
264
265#[cfg(test)]
266#[allow(
267    clippy::unwrap_used,
268    clippy::expect_used,
269    clippy::bool_assert_comparison
270)]
271mod tests {
272    use super::*;
273    use crate::Rgba;
274    use crate::model::ResolvedFontSpec;
275    use crate::model::border::ResolvedBorderSpec;
276    use crate::model::font::FontStyle;
277
278    fn sample_font() -> ResolvedFontSpec {
279        ResolvedFontSpec {
280            family: "Inter".into(),
281            size: 14.0,
282            weight: 400,
283            style: FontStyle::Normal,
284            color: Rgba::rgb(128, 128, 128),
285        }
286    }
287
288    fn sample_border() -> ResolvedBorderSpec {
289        ResolvedBorderSpec {
290            color: Rgba::rgb(200, 200, 200),
291            corner_radius: 4.0,
292            corner_radius_lg: 8.0,
293            line_width: 1.0,
294            opacity: 0.15,
295            shadow_enabled: true,
296            padding_horizontal: 0.0,
297            padding_vertical: 0.0,
298        }
299    }
300
301    fn sample_icon_sizes() -> ResolvedIconSizes {
302        ResolvedIconSizes {
303            toolbar: 24.0,
304            small: 16.0,
305            large: 32.0,
306            dialog: 22.0,
307            panel: 20.0,
308        }
309    }
310
311    fn sample_text_scale_entry() -> ResolvedTextScaleEntry {
312        ResolvedTextScaleEntry {
313            size: 12.0,
314            weight: 400,
315            line_height: 1.4,
316        }
317    }
318
319    fn sample_defaults() -> ResolvedDefaults {
320        let c = Rgba::rgb(128, 128, 128);
321        ResolvedDefaults {
322            font: sample_font(),
323            line_height: 1.4,
324            mono_font: ResolvedFontSpec {
325                family: "JetBrains Mono".into(),
326                size: 12.0,
327                weight: 400,
328                style: FontStyle::Normal,
329                color: Rgba::rgb(128, 128, 128),
330            },
331            background_color: c,
332            text_color: c,
333            accent_color: c,
334            accent_text_color: c,
335            surface_color: c,
336            muted_color: c,
337            shadow_color: c,
338            link_color: c,
339            selection_background: c,
340            selection_text_color: c,
341            selection_inactive_background: c,
342            text_selection_background: c,
343            text_selection_color: c,
344            disabled_text_color: c,
345            danger_color: c,
346            danger_text_color: c,
347            warning_color: c,
348            warning_text_color: c,
349            success_color: c,
350            success_text_color: c,
351            info_color: c,
352            info_text_color: c,
353            border: sample_border(),
354            disabled_opacity: 0.5,
355            focus_ring_color: c,
356            focus_ring_width: 2.0,
357            focus_ring_offset: 1.0,
358            icon_sizes: sample_icon_sizes(),
359        }
360    }
361
362    // --- ResolvedIconSizes tests ---
363
364    #[test]
365    fn resolved_icon_sizes_has_5_concrete_fields() {
366        let i = sample_icon_sizes();
367        assert_eq!(i.toolbar, 24.0);
368        assert_eq!(i.small, 16.0);
369        assert_eq!(i.large, 32.0);
370        assert_eq!(i.dialog, 22.0);
371        assert_eq!(i.panel, 20.0);
372    }
373
374    #[test]
375    fn resolved_icon_sizes_derives_clone_debug_partialeq() {
376        let i = sample_icon_sizes();
377        let i2 = i.clone();
378        assert_eq!(i, i2);
379        let dbg = format!("{i:?}");
380        assert!(dbg.contains("ResolvedIconSizes"));
381    }
382
383    // --- ResolvedTextScaleEntry tests ---
384
385    #[test]
386    fn resolved_text_scale_entry_has_3_concrete_fields() {
387        let e = sample_text_scale_entry();
388        assert_eq!(e.size, 12.0);
389        assert_eq!(e.weight, 400);
390        assert_eq!(e.line_height, 1.4);
391    }
392
393    #[test]
394    fn resolved_text_scale_entry_derives_clone_debug_partialeq() {
395        let e = sample_text_scale_entry();
396        let e2 = e.clone();
397        assert_eq!(e, e2);
398        let dbg = format!("{e:?}");
399        assert!(dbg.contains("ResolvedTextScaleEntry"));
400    }
401
402    // --- ResolvedTextScale tests ---
403
404    #[test]
405    fn resolved_text_scale_has_4_entries() {
406        let ts = ResolvedTextScale {
407            caption: ResolvedTextScaleEntry {
408                size: 11.0,
409                weight: 400,
410                line_height: 1.3,
411            },
412            section_heading: ResolvedTextScaleEntry {
413                size: 14.0,
414                weight: 600,
415                line_height: 1.4,
416            },
417            dialog_title: ResolvedTextScaleEntry {
418                size: 16.0,
419                weight: 700,
420                line_height: 1.2,
421            },
422            display: ResolvedTextScaleEntry {
423                size: 24.0,
424                weight: 300,
425                line_height: 1.1,
426            },
427        };
428        assert_eq!(ts.caption.size, 11.0);
429        assert_eq!(ts.section_heading.weight, 600);
430        assert_eq!(ts.dialog_title.size, 16.0);
431        assert_eq!(ts.display.weight, 300);
432    }
433
434    #[test]
435    fn resolved_text_scale_derives_clone_debug_partialeq() {
436        let e = sample_text_scale_entry();
437        let ts = ResolvedTextScale {
438            caption: e.clone(),
439            section_heading: e.clone(),
440            dialog_title: e.clone(),
441            display: e,
442        };
443        let ts2 = ts.clone();
444        assert_eq!(ts, ts2);
445        let dbg = format!("{ts:?}");
446        assert!(dbg.contains("ResolvedTextScale"));
447    }
448
449    // --- ResolvedDefaults tests ---
450
451    #[test]
452    fn resolved_defaults_all_fields_concrete() {
453        let d = sample_defaults();
454        // Fonts
455        assert_eq!(d.font.family.as_ref(), "Inter");
456        assert_eq!(d.mono_font.family.as_ref(), "JetBrains Mono");
457        assert_eq!(d.line_height, 1.4);
458        // Some colors
459        assert_eq!(d.background_color, Rgba::rgb(128, 128, 128));
460        assert_eq!(d.accent_color, Rgba::rgb(128, 128, 128));
461        // Geometry (border sub-struct)
462        assert_eq!(d.border.corner_radius, 4.0);
463        assert_eq!(d.border.shadow_enabled, true);
464        // Focus ring
465        assert_eq!(d.focus_ring_width, 2.0);
466        // Icon sizes
467        assert_eq!(d.icon_sizes.toolbar, 24.0);
468    }
469
470    #[test]
471    fn resolved_defaults_derives_clone_debug_partialeq() {
472        let d = sample_defaults();
473        let d2 = d.clone();
474        assert_eq!(d, d2);
475        let dbg = format!("{d:?}");
476        assert!(dbg.contains("ResolvedDefaults"));
477    }
478
479    // --- ResolvedTheme tests ---
480    // NOTE: These tests construct ResolvedTheme with all 25 widget structs.
481    // The widget Resolved* types will have new field names after Task 2,
482    // but for now they reference the old names -- Plan 02 (resolve.rs) will
483    // update all consumers. These tests are intentionally commented out until
484    // the full atomic commit is assembled.
485    //
486    // The structural tests for ResolvedDefaults above verify the defaults
487    // rename is correct.
488
489    // --- Behavioral tests (issue 2d) ---
490    // These tests call into resolve() and presets, which will break until
491    // Plans 02-04 update all consumers. They are kept for reference but
492    // will not compile until the atomic commit is complete.
493}