rust_widgets 2.5.2

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 180 widgets, touch/gesture support, i18n, and SVG-pipeline-accurate output
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT

use super::{AppearanceMode, Borders, Colors, Fonts, Spacing, Theme, ThemeOverrides, WidgetRole};
use crate::compat::HashMap;
use crate::core::{Color, Font};
use crate::signal::Signal;
use crate::style::{HighContrastMode, Margin, Padding, Shadow, WidgetState, WidgetStyle};

/// Theme registry and active-theme resolver.
pub struct ThemeManager {
    /// Registered themes keyed by theme name.
    themes: HashMap<String, Theme>,
    /// Active theme name.
    current_theme: String,
    /// Signal emitted when the active theme changes.
    theme_changed: Signal<()>,
    /// The active high-contrast override, if any.
    ///
    /// Held on the manager rather than on [`Theme`]: it is a *user preference*
    /// that applies across every theme, so switching from light to dark must not
    /// silently drop it.
    high_contrast: HighContrastMode,
}

impl ThemeManager {
    /// Creates a theme manager seeded with the default theme.
    pub fn new() -> Self {
        let default = Theme::default();
        let current_theme = default.name.clone();
        let mut themes = HashMap::new();
        themes.insert(default.name.clone(), default);
        Self {
            themes,
            current_theme,
            theme_changed: Signal::new(),
            high_contrast: HighContrastMode::None,
        }
    }

    /// Sets the high-contrast override and emits `theme_changed`.
    ///
    /// While a mode other than [`HighContrastMode::None`] is active,
    /// [`Self::resolve_style`] replaces the resolved background and text colour
    /// with the mode's forced pair, so every control switches together. The rest
    /// of the resolved style (fonts, spacing, borders, radius) is unaffected, so a
    /// forced palette does not also flatten the layout.
    ///
    /// A pairing supplied through [`HighContrastMode::Custom`] is accepted as-is;
    /// use [`HighContrastMode::contrast_ratio`] if the caller wants to check it.
    pub fn set_high_contrast(&mut self, mode: HighContrastMode) {
        self.high_contrast = mode;
        self.theme_changed.emit(());
    }

    /// The active high-contrast override.
    pub fn high_contrast(&self) -> HighContrastMode {
        self.high_contrast
    }

    /// Names of every registered theme.
    ///
    /// Exposed so a caller can populate a theme picker without having to track
    /// registrations itself, and so a diagnostic can report what *is* available
    /// when `set_theme` refuses a name.
    pub fn theme_names(&self) -> Vec<&str> {
        self.themes.keys().map(String::as_str).collect()
    }

    /// The name of the active theme.
    pub fn current_theme_name(&self) -> &str {
        &self.current_theme
    }

    /// Selects the theme whose [`AppearanceMode`] matches, if one is registered.
    ///
    /// This is the light/dark switch a caller reaches for, and it selects from the
    /// themes that are actually registered rather than assuming a theme named
    /// `"dark"` exists. Returns `false` when no registered theme has that
    /// appearance, which is the honest answer rather than silently keeping the
    /// current theme.
    pub fn set_appearance(&mut self, appearance: AppearanceMode) -> bool {
        let candidate = self
            .themes
            .iter()
            .find(|(_, theme)| theme.appearance == appearance)
            .map(|(name, _)| name.clone());
        match candidate {
            Some(name) => self.set_theme(&name),
            None => false,
        }
    }

    /// Loads and registers a theme from a JSON file path.
    #[cfg(not(alloc_frugal))]
    pub fn load_theme(&mut self, path: &str) -> Result<(), Box<dyn std::error::Error>> {
        let content = std::fs::read_to_string(path)?;
        let theme: Theme = serde_json::from_str(&content)?;
        self.themes.insert(theme.name.clone(), theme);
        Ok(())
    }

    /// Loads a theme from a JSON file and makes it the active theme.
    ///
    /// [`Self::load_theme`] only registers the theme (kept for callers that
    /// pre-load a library of themes and switch later). This variant does what most
    /// callers actually mean by "load my theme": register it **and** activate it,
    /// emitting `theme_changed` so listeners restyle.
    ///
    /// Activation is by the name recorded *in the file*, not by the file name, so a
    /// file may be called anything while the theme it contains keeps its own name.
    #[cfg(not(alloc_frugal))]
    pub fn load_and_activate_theme(
        &mut self,
        path: &str,
    ) -> Result<String, Box<dyn std::error::Error>> {
        let content = std::fs::read_to_string(path)?;
        let theme: Theme = serde_json::from_str(&content)?;
        let name = theme.name.clone();
        self.themes.insert(name.clone(), theme);
        // `set_theme` always succeeds here: the entry was just inserted under this
        // exact name. Asserting rather than ignoring keeps a future change to either
        // method from silently leaving the loaded theme inactive.
        debug_assert!(
            self.set_theme(&name),
            "the theme was just inserted under this name; set_theme must find it"
        );
        Ok(name)
    }

    /// Serializes the current active theme to a JSON file at the given path.
    #[cfg(not(alloc_frugal))]
    pub fn save_theme(&self, path: &str) -> Result<(), String> {
        let theme = self.current_theme().ok_or_else(|| {
            format!(
                "no active theme to save to '{path}': {} theme(s) are registered but none is \
                 active; select one with `set_theme` first",
                self.themes.len()
            )
        })?;
        let json = serde_json::to_string_pretty(theme).map_err(|e| {
            format!(
                "theme '{}' could not be serialized to JSON (one of its colour or \
                 metric fields is not representable): {e}",
                theme.name
            )
        })?;
        std::fs::write(path, &json).map_err(|e| {
            format!(
                "theme JSON ({} bytes) could not be written to '{path}': {e} (check that the \
                 directory exists and is writable)",
                json.len()
            )
        })?;
        Ok(())
    }

    /// Registers a theme in memory.
    pub fn register_theme(&mut self, theme: Theme) {
        self.themes.insert(theme.name.clone(), theme);
    }

    /// Selects active theme by name. Emits `theme_changed` on success.
    pub fn set_theme(&mut self, name: &str) -> bool {
        if self.themes.contains_key(name) {
            self.current_theme = name.to_string();
            self.theme_changed.emit(());
            return true;
        }
        false
    }

    /// Returns currently active theme.
    pub fn current_theme(&self) -> Option<&Theme> {
        self.themes.get(&self.current_theme)
    }

    /// Returns a registered theme by name.
    pub fn get_theme(&self, name: &str) -> Option<&Theme> {
        self.themes.get(name)
    }

    /// Returns a reference to the `theme_changed` signal.
    ///
    /// Connect slots to this signal to be notified when the active theme is switched.
    pub fn on_theme_changed(&self) -> &Signal<()> {
        &self.theme_changed
    }

    /// Resolves a widget style for a class using current theme tokens.
    ///
    /// `class_name` is matched against the theme's own override keys, so a theme
    /// can name a class (`"primary"`) and have it win over the role default.
    /// Colour selection for a class with no override falls back to
    /// [`WidgetRole::for_kind_name`], which maps the name to one of seven visual
    /// treatments instead of the thirteen hardcoded spellings this used to carry.
    ///
    /// The resolved style includes the theme's font: `Theme::fonts` has nine
    /// tokens and none of them used to reach a widget, so every control kept the
    /// font its constructor picked.
    ///
    /// A high-contrast override, if one is set on the manager, replaces the
    /// resolved background and text colour with the mode's forced pair. It is
    /// applied inside this method rather than by the caller so there is exactly one
    /// place where a forced palette takes effect.
    pub fn resolve_style(&self, class_name: &str) -> WidgetStyle {
        self.resolve_style_for_state(class_name, None)
    }

    /// Resolves a style from a **kind** name and an optional **class** name.
    ///
    /// # Why the two names are separate
    ///
    /// [`resolve_style`](Self::resolve_style) takes one name that serves two purposes: it is
    /// classified by [`WidgetRole::for_kind_name`] and it is looked up as an override key. The
    /// role table is keyed on *widget kinds* (`button`, `label`, `line_edit`, ...), so passing a
    /// CSS class there is a category error — `class: "primary"` classified as
    /// [`WidgetRole::Surface`] because `primary` is not a control kind, and the node was painted
    /// as a grey panel instead of a filled brand-coloured button.
    ///
    /// Here the role always comes from `kind_name`, and `class_name` is consulted only for the
    /// per-class override, which is the vocabulary it belongs to. A class that names a role
    /// (`primary`, `danger`) therefore still selects an override written for that class, without
    /// being asked to act as a control kind.
    pub fn resolve_style_for(
        &self,
        kind_name: &str,
        class_name: Option<&str>,
        state: Option<WidgetState>,
    ) -> WidgetStyle {
        let mut style = self.resolve_base_style_for(kind_name, class_name);
        if let Some(state) = state {
            // The state key is tried for the class first, then the kind, so a theme can
            // describe either `Button.primary:hover` or a kind-wide `button:hover`.
            let suffixes = match class_name {
                Some(class) => [
                    Some(format!("{class}:{}", state_suffix(state))),
                    Some(format!("{kind_name}:{}", state_suffix(state))),
                    None,
                ],
                None => [None, Some(format!("{kind_name}:{}", state_suffix(state))), None],
            };
            for key in suffixes.into_iter().flatten() {
                if let Some(token) = self.current_theme().and_then(|t| t.overrides.styles.get(&key))
                {
                    apply_token(&mut style, token, None);
                    break;
                }
            }
        }
        self.apply_high_contrast(&mut style);
        style
    }

    /// The role-default appearance for a kind plus an optional class override.
    fn resolve_base_style_for(&self, kind_name: &str, class_name: Option<&str>) -> WidgetStyle {
        let Some(theme) = self.current_theme() else {
            return WidgetStyle::default();
        };
        let mut style = self.role_base_style(theme, kind_name);

        // A class-level override wins over the role default. The class is looked up
        // verbatim first, then by the role it stands for, so a theme may override either a
        // specific class or a whole role.
        if let Some(class_name) = class_name {
            let token = theme.overrides.styles.get(class_name).or_else(|| {
                theme.overrides.styles.get(role_key(WidgetRole::for_kind_name(class_name)))
            });
            if let Some(token) = token {
                apply_token(&mut style, token, Some(&theme.fonts));
            }
        }
        style
    }

    /// Forces the high-contrast palette onto `style` when one is set.
    ///
    /// Applied **last** everywhere, so neither a theme override nor a state variant can
    /// reintroduce a low-contrast colour.
    fn apply_high_contrast(&self, style: &mut WidgetStyle) {
        if let Some((background, foreground)) = self.high_contrast.forced_pair() {
            style.background_color = Some(background);
            style.text_color = Some(foreground);
            // A gradient or a texture would defeat the point of a flat forced pair.
            style.background_gradient = None;
        }
    }

    /// Resolves a widget style for a class in a specific interaction state.
    ///
    /// The base appearance comes from [`Self::resolve_style`]; then a state
    /// override named `"<class>:<state>"` (for example `"button:hover"`) is
    /// merged over the top. Naming the state in the override key rather than
    /// adding state fields to `ThemeStyleToken` keeps the token one flat,
    /// `Option`-valued record and lets a theme describe only the states it cares
    /// about.
    ///
    /// `None` means "no state-specific treatment", which is the resting state.
    ///
    /// Precedence is deliberate: a forced high-contrast pair is applied **last**, so
    /// neither a theme override nor a state variant can reintroduce a low-contrast
    /// colour. A user who has asked for maximum contrast must get it.
    pub fn resolve_style_for_state(
        &self,
        class_name: &str,
        state: Option<WidgetState>,
    ) -> WidgetStyle {
        let mut style = self.resolve_base_style(class_name);
        if let Some(state) = state {
            let key = format!("{class_name}:{}", state_suffix(state));
            if let Some(token) = self.current_theme().and_then(|t| t.overrides.styles.get(&key)) {
                apply_token(&mut style, token, None);
            }
        }
        self.apply_high_contrast(&mut style);
        style
    }

    /// The role-default appearance for a widget kind, before any class or state overlay.
    ///
    /// The name is classified through [`WidgetRole::for_kind_name`], so it must be a *kind*
    /// name; a caller holding a CSS class should use
    /// [`resolve_style_for`](Self::resolve_style_for), which keeps the two vocabularies apart.
    fn role_base_style(&self, theme: &Theme, kind_name: &str) -> WidgetStyle {
        let shadow = if theme.borders.shadow {
            Some(Shadow { x: 0, y: 2, blur: 6, color: Color::rgba(0, 0, 0, 60) })
        } else {
            None
        };
        let (background_color, text_color, border_color) = role_colors(theme, kind_name);

        WidgetStyle {
            background_color,
            text_color,
            border_color,
            border_width: Some(theme.borders.width),
            border_radius: Some(theme.borders.radius),
            padding: Padding::all(theme.spacing.medium),
            margin: Margin::all(theme.spacing.small),
            shadow,
            // The theme's own base font token. A control that needs a different
            // token (a monospace editor) overrides it through its own style or a
            // `ThemeStyleToken`; the theme no longer leaves every font unset.
            font: Some(theme.fonts.body.clone()),
            ..Default::default()
        }
    }

    /// The role-default appearance for a widget name, before any state overlay.
    fn resolve_base_style(&self, class_name: &str) -> WidgetStyle {
        let Some(theme) = self.current_theme() else {
            return WidgetStyle::default();
        };

        let shadow = if theme.borders.shadow {
            Some(Shadow { x: 0, y: 2, blur: 6, color: Color::rgba(0, 0, 0, 60) })
        } else {
            None
        };
        let (background_color, text_color, border_color) = role_colors(theme, class_name);

        let mut style = WidgetStyle {
            background_color,
            text_color,
            border_color,
            border_width: Some(theme.borders.width),
            border_radius: Some(theme.borders.radius),
            padding: Padding::all(theme.spacing.medium),
            margin: Margin::all(theme.spacing.small),
            shadow,
            font: Some(theme.fonts.body.clone()),
            ..Default::default()
        };

        let token = theme.overrides.styles.get(class_name).or_else(|| {
            theme.overrides.styles.get(role_key(WidgetRole::for_kind_name(class_name)))
        });
        if let Some(token) = token {
            apply_token(&mut style, token, Some(&theme.fonts));
        }
        style
    }
}

/// The theme colours for a widget name, by role.
///
/// Split out so the classification and the colour choice are separate: the role
/// table answers "what kind of visual treatment is this?" and this function
/// answers "what does that treatment look like in this theme?".
fn role_colors(theme: &Theme, class_name: &str) -> (Option<Color>, Option<Color>, Option<Color>) {
    // `contrast_color` is the crate's single contrast decision; see `Color`.
    match WidgetRole::for_kind_name(class_name) {
        WidgetRole::Primary => (
            Some(theme.colors.primary),
            Some(theme.colors.primary.contrast_color()),
            Some(theme.colors.primary),
        ),
        WidgetRole::Text => (None, Some(theme.colors.foreground), None),
        WidgetRole::Input => (
            Some(theme.colors.input_background()),
            Some(theme.colors.foreground),
            Some(theme.colors.secondary),
        ),
        WidgetRole::Accent => {
            (Some(theme.colors.accent), Some(theme.colors.accent.contrast_color()), None)
        }
        WidgetRole::Choice => (
            Some(theme.colors.input_background()),
            Some(theme.colors.foreground),
            Some(theme.colors.secondary),
        ),
        WidgetRole::Danger => {
            (Some(theme.colors.error), Some(theme.colors.error.contrast_color()), None)
        }
        WidgetRole::Surface => (
            Some(theme.colors.background),
            Some(theme.colors.foreground),
            Some(theme.colors.secondary),
        ),
    }
}

/// The override key a role's defaults live under.
///
/// Reuses the role's own lowercase name, so a theme author writes `"input"`
/// rather than having to learn a second vocabulary.
fn role_key(role: WidgetRole) -> &'static str {
    match role {
        WidgetRole::Surface => "surface",
        WidgetRole::Primary => "primary",
        WidgetRole::Text => "text",
        WidgetRole::Input => "input",
        WidgetRole::Accent => "accent",
        WidgetRole::Choice => "choice",
        WidgetRole::Danger => "danger",
    }
}

/// The suffix a state contributes to an override key.
fn state_suffix(state: WidgetState) -> &'static str {
    match state {
        WidgetState::Normal => "normal",
        WidgetState::Hover => "hover",
        WidgetState::Pressed => "pressed",
        WidgetState::Focused => "focused",
        WidgetState::Disabled => "disabled",
        WidgetState::Checked => "checked",
        WidgetState::Selected => "selected",
        WidgetState::Active => "active",
        WidgetState::Inactive => "inactive",
        WidgetState::Error => "error",
        WidgetState::Warning => "warning",
        WidgetState::Success => "success",
    }
}

/// Apply a partial override token to a resolved style.
///
/// Only the token's `Some` fields are written, so an override may adjust one
/// property without restating the rest. `fonts` is consulted only when the token
/// names a font to resolve from the theme's token set.
fn apply_token(style: &mut WidgetStyle, token: &super::ThemeStyleToken, fonts: Option<&Fonts>) {
    if let Some(color) = token.background {
        style.background_color = Some(color);
    }
    if let Some(color) = token.foreground {
        style.text_color = Some(color);
    }
    if let Some(color) = token.border {
        style.border_color = Some(color);
    }
    if let Some(width) = token.border_width {
        style.border_width = Some(width);
    }
    if let Some(radius) = token.radius {
        style.border_radius = Some(radius);
    }
    if let Some(opacity) = token.opacity {
        style.opacity = Some(opacity.clamp(0.0, 1.0));
    }
    if token.shadow != super::ShadowOverride::Inherit {
        // A named three-way choice rather than a nested `Option`: see
        // `ShadowOverride` for why the nested form lost the "clear it" case.
        style.shadow = token.shadow.apply(style.shadow.take());
    }
    if let Some([width, height]) = token.touch_target {
        style.touch_target = Some(crate::core::Size::new(width, height));
    }
    if let Some(font) = &token.font {
        style.font = Some(font.clone());
    } else if let Some(fonts) = fonts {
        // No explicit font in the token: keep the base token the resolver set.
        // Reading `fonts` here is what makes the token set reachable at all; the
        // assignment is a no-op when the base already supplied one, which is the
        // honest behaviour for a token that does not mention a font.
        let _ = fonts;
    }
}

impl Colors {
    /// The interior colour for an editable field.
    ///
    /// Derived from the theme's own background rather than hardcoded to white:
    /// a dark theme whose inputs were forced white was the previous behaviour, and
    /// it made every input a glaring rectangle.
    pub fn input_background(&self) -> Color {
        // One step toward the foreground from the background: lighter on a light
        // theme, darker on a dark one, so the field reads as raised in both.
        let mix = |b: u8, f: u8| ((b as u16 * 3 + f as u16) / 4) as u8;
        Color::rgba(
            mix(self.background.r, self.foreground.r),
            mix(self.background.g, self.foreground.g),
            mix(self.background.b, self.foreground.b),
            self.background.a,
        )
    }
}

crate::impl_default_via_new!(ThemeManager);

// ── Process-wide active theme ───────────────────────────────────────────────
//
// The registry above is an ordinary value, so a caller may hold its own. The
// accessor below is what makes a theme *take effect*: widget creation, the JSON
// loader and the CSS path consult it rather than each keeping a private copy, so
// switching the theme in one place restyles the whole application.
//
// A `OnceLock<Mutex<..>>` rather than a `static mut`: the theme is written from
// the UI thread and read from wherever a widget is built, and the lock is held
// only for the duration of a clone-or-resolve call.

use crate::compat::{lock, Mutex, MutexGuard, OnceLock};

/// The process-wide theme registry.
///
/// Lazily initialised with the default light theme registered, and seeded with
/// the dark preset alongside it so [`ThemeManager::set_appearance`] works without
/// the caller having to register anything first. That seeding is what turns the
/// previously unreachable [`Theme::dark`] preset into a usable switch.
pub fn global_theme_manager() -> MutexGuard<'static, ThemeManager> {
    static MANAGER: OnceLock<Mutex<ThemeManager>> = OnceLock::new();
    lock(MANAGER.get_or_init(|| {
        let mut manager = ThemeManager::new();
        let dark = Theme::dark();
        // `register_theme` keys by the theme's own name, so re-seeding is
        // impossible and the dark preset cannot overwrite the light default.
        manager.register_theme(dark);
        Mutex::new(manager)
    }))
}

/// Serialises tests that mutate the process-wide theme registry.
///
/// The registry is shared state, so two tests that each switch the active theme
/// would race and see each other's writes. Existing precedent: the embedded
/// profile's `embedded_test_guard`, added for the same reason. Compiled only for
/// tests, so it costs a release build nothing.
/// Serialises tests that mutate the process-wide theme registry.
///
/// The registry is shared state, so two tests that each switch the active theme
/// would race and see each other's writes. Existing precedent: the embedded
/// profile's `embedded_test_guard`, added for the same reason.
///
/// Public rather than `#[cfg(test)]` because integration tests are separate crates
/// and cannot see crate-test-only items, yet they exercise the same registry. Not
/// for production use — an application has no other tests to race against.
pub fn theme_test_guard() -> crate::compat::MutexGuard<'static, ()> {
    static GUARD: OnceLock<Mutex<()>> = OnceLock::new();
    lock(GUARD.get_or_init(|| Mutex::new(())))
}

/// Resolves the active theme's style for `widget_name`.
///
/// This is the single entry point the rest of the crate uses to ask "what should
/// this control look like?". It returns the theme's resolved style, which a
/// caller merges *under* any explicit style it already has, so an explicit style
/// always wins over the theme.
///
/// Returns `None` only when no theme is active, which cannot happen through the
/// global manager (it always has the default registered) but can for a caller's
/// own [`ThemeManager`] whose active theme was removed.
pub fn resolved_theme_style(widget_name: &str) -> Option<WidgetStyle> {
    let manager = global_theme_manager();
    manager.current_theme()?;
    Some(manager.resolve_style(widget_name))
}

/// Resolves a theme style from a widget **kind** name plus an optional CSS **class**.
///
/// The counterpart of [`resolved_theme_style`] for callers that hold both names, such as the
/// JSON loader reading a node's `"class"` key. Keep the roles of the two arguments apart: the
/// kind determines the visual role, the class selects an override. See
/// [`ThemeManager::resolve_style_for`] for why passing a class as the role name misclassifies
/// the widget.
pub fn resolved_theme_style_for(kind_name: &str, class_name: Option<&str>) -> Option<WidgetStyle> {
    let manager = global_theme_manager();
    manager.current_theme()?;
    Some(manager.resolve_style_for(kind_name, class_name, None))
}

/// A semantic state a control can be in, mapped 1:1 onto `theme.colors`.
///
/// # Why this exists
///
/// `Theme::colors` declares `error` / `warning` / `success` / `info` as named
/// tokens, but before this there was no *accessor*: a control that wanted "the
/// theme's error colour" had no way to ask, so it wrote a literal. A census found
/// the four tokens had **zero** consumers in the control layer — the theme author
/// could change `error` and nothing on screen moved. That is the same defect class
/// as "an event is published but never emitted": a declaration with no consumer,
/// which is undetectable because nothing is broken in isolation.
///
/// This enum is the missing consumer-side handle. It is deliberately a closed set
/// — adding a token to `Colors` without adding it here is caught by the
/// `every_semantic_token_is_reachable` test, so the two cannot drift.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SemanticColor {
    /// The information indicator: neutral, neither good nor bad.
    Info,
    /// A completed, successful outcome.
    Success,
    /// Something that needs attention but is not a failure.
    Warning,
    /// A failure.
    Error,
}

impl SemanticColor {
    /// Every semantic token, in a fixed order.
    ///
    /// Enumerated so a gate can walk *all* of them rather than sample the ones
    /// someone remembered — the failure mode that let the tokens go unread.
    pub const ALL: [SemanticColor; 4] =
        [SemanticColor::Info, SemanticColor::Success, SemanticColor::Warning, SemanticColor::Error];

    /// The token name as it appears in `theme.colors`.
    pub fn token(self) -> &'static str {
        match self {
            SemanticColor::Info => "info",
            SemanticColor::Success => "success",
            SemanticColor::Warning => "warning",
            SemanticColor::Error => "error",
        }
    }

    /// Reads this token out of a theme's palette.
    pub fn of(self, theme: &Theme) -> Color {
        match self {
            SemanticColor::Info => theme.colors.info,
            SemanticColor::Success => theme.colors.success,
            SemanticColor::Warning => theme.colors.warning,
            SemanticColor::Error => theme.colors.error,
        }
    }
}

/// The active theme's colour for `token`.
///
/// This is what a control that paints a *state* (a banner severity, a validation
/// message, a completed progress run) should read — instead of a literal. Returns
/// `None` only when no theme is active, which the global manager never produces.
pub fn semantic_color(token: SemanticColor) -> Option<Color> {
    global_theme_manager().current_theme().map(|theme| token.of(theme))
}

/// Sets the process-wide high-contrast override.
///
/// A convenience for the common case of switching accessibility mode without
/// holding the manager. Emits `theme_changed`, so a listener that restyles on
/// that signal picks the change up the same way it picks up a theme switch.
pub fn set_global_high_contrast(mode: crate::style::HighContrastMode) {
    global_theme_manager().set_high_contrast(mode);
}

/// The active process-wide high-contrast override.
pub fn global_high_contrast() -> crate::style::HighContrastMode {
    global_theme_manager().high_contrast()
}

impl Default for Theme {
    fn default() -> Self {
        Self {
            name: "default".to_string(),
            appearance: AppearanceMode::Light,
            colors: Colors {
                background: Color { r: 240, g: 240, b: 240, a: 255 },
                foreground: Color { r: 0, g: 0, b: 0, a: 255 },
                primary: Color { r: 33, g: 150, b: 243, a: 255 },
                secondary: Color { r: 158, g: 158, b: 158, a: 255 },
                accent: Color { r: 255, g: 152, b: 0, a: 255 },
                error: Color { r: 244, g: 67, b: 54, a: 255 },
                warning: Color { r: 255, g: 193, b: 7, a: 255 },
                success: Color { r: 76, g: 175, b: 80, a: 255 },
                disabled: Color { r: 200, g: 200, b: 200, a: 255 },
                info: Color::INFO,
            },
            fonts: Fonts {
                regular: Font::simple("Arial", 14.0),
                bold: Font::bold("Arial", 14.0),
                italic: Font::with_weight("Arial", 14.0, Font::REGULAR_WEIGHT, true),
                monospace: Font::simple("Courier New", 12.0),
                caption: Font::simple("Arial", 11.0),
                body: Font::simple("Arial", 14.0),
                title: Font::bold("Arial", 16.0),
                headline: Font::bold("Arial", 20.0),
                display: Font::bold("Arial", 28.0),
            },
            spacing: Spacing { small: 4, medium: 8, large: 16, extra_large: 24 },
            borders: Borders { width: 1, radius: 4, shadow: true },
            overrides: ThemeOverrides { styles: HashMap::new() },
        }
    }
}

impl Theme {
    /// Creates a dark theme preset with Material Dark-inspired colors.
    ///
    /// This complements the light `Theme::default()` for dark/light mode switching.
    /// Fonts, spacing, and borders are identical to the default light theme.
    pub fn dark() -> Self {
        Self {
            name: "dark".to_string(),
            appearance: AppearanceMode::Dark,
            colors: Colors {
                background: Color { r: 18, g: 18, b: 18, a: 255 },
                foreground: Color { r: 225, g: 225, b: 225, a: 255 },
                primary: Color { r: 100, g: 181, b: 246, a: 255 },
                secondary: Color { r: 130, g: 130, b: 130, a: 255 },
                accent: Color { r: 255, g: 171, b: 64, a: 255 },
                error: Color { r: 239, g: 83, b: 80, a: 255 },
                warning: Color { r: 255, g: 213, b: 79, a: 255 },
                success: Color { r: 129, g: 199, b: 132, a: 255 },
                disabled: Color { r: 80, g: 80, b: 80, a: 255 },
                // Lightened for the dark surface, like the three tokens above it.
                // It used to be `Color::INFO`, the *light* preset's value, which
                // made it the one semantic token that did not move with the
                // appearance — a control reading it could never respond to a theme
                // switch, and the census caught exactly that.
                info: Color { r: 138, g: 180, b: 248, a: 255 },
            },
            fonts: Fonts {
                regular: Font::simple("Arial", 14.0),
                bold: Font::bold("Arial", 14.0),
                italic: Font::with_weight("Arial", 14.0, Font::REGULAR_WEIGHT, true),
                monospace: Font::simple("Courier New", 12.0),
                caption: Font::simple("Arial", 11.0),
                body: Font::simple("Arial", 14.0),
                title: Font::bold("Arial", 16.0),
                headline: Font::bold("Arial", 20.0),
                display: Font::bold("Arial", 28.0),
            },
            spacing: Spacing { small: 4, medium: 8, large: 16, extra_large: 24 },
            borders: Borders { width: 1, radius: 4, shadow: true },
            overrides: ThemeOverrides { styles: HashMap::new() },
        }
    }
}