mirui 0.45.0

A lightweight, no_std ECS-driven UI framework for embedded, desktop, and WebAssembly
Documentation
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
758
759
760
use alloc::collections::BTreeMap;
use alloc::vec::Vec;

use crate::ecs::World;
use crate::types::{Color, Fixed};

/// Token = role. State modifiers (Disabled / Hovered / …) belong on
/// [`WidgetState`], not in this enum.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ColorToken {
    Primary,
    OnPrimary,
    Secondary,
    OnSecondary,
    Tertiary,
    OnTertiary,
    Surface,
    OnSurface,
    SurfaceVariant,
    OnSurfaceVariant,
    Success,
    Error,
    Outline,
    Shadow,
    Custom(&'static str),
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum WidgetState {
    #[default]
    Enabled,
    Disabled,
    Hovered,
    Pressed,
    Error,
}

impl ColorToken {
    pub const fn custom(name: &'static str) -> Self {
        Self::Custom(name)
    }
}

/// A colour value that's either fixed or routed through a
/// [`ColorToken`]. Built-in widgets and `Style` carry `ThemedColor`
/// fields so user code mixes literals and tokens freely.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ThemedColor {
    Raw(Color),
    Token(ColorToken),
}

impl ThemedColor {
    pub fn resolve(self, theme: &Theme) -> Color {
        match self {
            Self::Raw(c) => c,
            Self::Token(t) => theme.resolve(t),
        }
    }

    pub fn resolve_in(self, theme: &Theme, state: WidgetState) -> Color {
        match self {
            Self::Raw(c) => theme.blend_color_in(c, state),
            Self::Token(t) => theme.resolve_in(t, state),
        }
    }
}

impl From<Color> for ThemedColor {
    fn from(c: Color) -> Self {
        Self::Raw(c)
    }
}

impl From<ColorToken> for ThemedColor {
    fn from(t: ColorToken) -> Self {
        Self::Token(t)
    }
}

/// Magenta: paint when a `Custom` token isn't bound. Loud in any
/// palette so an unbound token shows up immediately.
const MISSING_TOKEN_FALLBACK: Color = Color::rgb(255, 0, 255);

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ThemeId(&'static str);

impl ThemeId {
    pub const fn new(value: &'static str) -> Self {
        Self(value)
    }

    pub const fn as_str(self) -> &'static str {
        self.0
    }
}

impl From<&'static str> for ThemeId {
    fn from(value: &'static str) -> Self {
        Self::new(value)
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ThemeInfo {
    pub id: ThemeId,
    pub name: &'static str,
    pub description: &'static str,
}

impl ThemeInfo {
    pub const fn new(id: &'static str, name: &'static str, description: &'static str) -> Self {
        Self {
            id: ThemeId::new(id),
            name,
            description,
        }
    }
}

/// Colour palette consumed by built-in widgets. World resource;
/// `App::new` inserts `Theme::default()`.
///
/// Token roles:
/// - `Primary` / `OnPrimary`: main accent + text on it
/// - `Secondary` / `OnSecondary`, `Tertiary` / `OnTertiary`: alt accents
/// - `Surface` / `OnSurface`: root background + primary text
/// - `SurfaceVariant` / `OnSurfaceVariant`: dim panel + secondary
///   / placeholder text
/// - `Success` / `Error`: state feedback
/// - `Outline` / `Shadow`: borders / elevation
#[derive(Clone, Debug)]
pub struct Theme {
    info: ThemeInfo,
    primary: Color,
    on_primary: Color,
    secondary: Color,
    on_secondary: Color,
    tertiary: Color,
    on_tertiary: Color,
    surface: Color,
    on_surface: Color,
    surface_variant: Color,
    on_surface_variant: Color,
    success: Color,
    error: Color,
    outline: Color,
    shadow: Color,
    extras: BTreeMap<&'static str, Color>,
}

impl Theme {
    /// Dark palette; the default for `App::new`.
    pub fn dark() -> Self {
        Self {
            info: ThemeInfo::new("dark", "Dark", "Bundled low-light palette"),
            primary: Color::rgb(88, 166, 255),
            on_primary: Color::rgb(255, 255, 255),
            secondary: Color::rgb(140, 200, 220),
            on_secondary: Color::rgb(20, 20, 30),
            tertiary: Color::rgb(200, 140, 220),
            on_tertiary: Color::rgb(20, 20, 30),
            surface: Color::rgb(20, 20, 30),
            on_surface: Color::rgb(220, 220, 230),
            surface_variant: Color::rgb(60, 60, 80),
            on_surface_variant: Color::rgb(120, 120, 140),
            success: Color::rgb(63, 185, 80),
            error: Color::rgb(220, 80, 80),
            outline: Color::rgb(80, 80, 100),
            shadow: Color::rgb(0, 0, 0),
            extras: BTreeMap::new(),
        }
    }

    pub fn light() -> Self {
        Self {
            info: ThemeInfo::new("light", "Light", "Bundled high-contrast light palette"),
            primary: Color::rgb(0, 100, 200),
            on_primary: Color::rgb(255, 255, 255),
            secondary: Color::rgb(40, 120, 160),
            on_secondary: Color::rgb(255, 255, 255),
            tertiary: Color::rgb(140, 80, 180),
            on_tertiary: Color::rgb(255, 255, 255),
            surface: Color::rgb(248, 248, 250),
            on_surface: Color::rgb(20, 20, 30),
            surface_variant: Color::rgb(220, 220, 230),
            on_surface_variant: Color::rgb(120, 120, 140),
            success: Color::rgb(40, 160, 70),
            error: Color::rgb(200, 60, 60),
            outline: Color::rgb(180, 180, 200),
            shadow: Color::rgb(60, 60, 80),
            extras: BTreeMap::new(),
        }
    }

    pub fn resolve(&self, token: ColorToken) -> Color {
        match token {
            ColorToken::Primary => self.primary,
            ColorToken::OnPrimary => self.on_primary,
            ColorToken::Secondary => self.secondary,
            ColorToken::OnSecondary => self.on_secondary,
            ColorToken::Tertiary => self.tertiary,
            ColorToken::OnTertiary => self.on_tertiary,
            ColorToken::Surface => self.surface,
            ColorToken::OnSurface => self.on_surface,
            ColorToken::SurfaceVariant => self.surface_variant,
            ColorToken::OnSurfaceVariant => self.on_surface_variant,
            ColorToken::Success => self.success,
            ColorToken::Error => self.error,
            ColorToken::Outline => self.outline,
            ColorToken::Shadow => self.shadow,
            ColorToken::Custom(name) => self
                .extras
                .get(name)
                .copied()
                .unwrap_or(MISSING_TOKEN_FALLBACK),
        }
    }

    pub const fn info(&self) -> ThemeInfo {
        self.info
    }

    pub const fn id(&self) -> ThemeId {
        self.info.id
    }

    pub const fn name(&self) -> &'static str {
        self.info.name
    }

    pub const fn description(&self) -> &'static str {
        self.info.description
    }

    pub fn resolve_in(&self, token: ColorToken, state: WidgetState) -> Color {
        let base = self.resolve(token);
        match state {
            WidgetState::Enabled => base,
            WidgetState::Disabled => match token {
                ColorToken::OnSurface
                | ColorToken::OnSurfaceVariant
                | ColorToken::OnPrimary
                | ColorToken::OnSecondary
                | ColorToken::OnTertiary => self.surface.blend_with(base, Fixed::from_f32(0.38)),
                ColorToken::Primary
                | ColorToken::Secondary
                | ColorToken::Tertiary
                | ColorToken::SurfaceVariant => self
                    .surface
                    .blend_with(self.on_surface, Fixed::from_f32(0.12)),
                _ => base,
            },
            WidgetState::Hovered => base.blend_with(self.on_surface, Fixed::from_f32(0.08)),
            WidgetState::Pressed => base.blend_with(self.on_surface, Fixed::from_f32(0.12)),
            WidgetState::Error => base.blend_with(self.error, Fixed::from_f32(0.16)),
        }
    }

    pub fn blend_color_in(&self, color: Color, state: WidgetState) -> Color {
        match state {
            WidgetState::Enabled => color,
            WidgetState::Disabled => self.surface.blend_with(color, Fixed::from_f32(0.38)),
            WidgetState::Hovered => color.blend_with(self.on_surface, Fixed::from_f32(0.08)),
            WidgetState::Pressed => color.blend_with(self.on_surface, Fixed::from_f32(0.12)),
            WidgetState::Error => color.blend_with(self.error, Fixed::from_f32(0.16)),
        }
    }
}

impl Theme {
    pub fn set_info(&mut self, info: ThemeInfo) -> &mut Self {
        self.info = info;
        self
    }

    pub fn with_info(mut self, info: ThemeInfo) -> Self {
        self.info = info;
        self
    }

    /// Bind a colour to a token, builtin or custom.
    pub fn set(&mut self, token: ColorToken, color: Color) -> &mut Self {
        match token {
            ColorToken::Primary => self.primary = color,
            ColorToken::OnPrimary => self.on_primary = color,
            ColorToken::Secondary => self.secondary = color,
            ColorToken::OnSecondary => self.on_secondary = color,
            ColorToken::Tertiary => self.tertiary = color,
            ColorToken::OnTertiary => self.on_tertiary = color,
            ColorToken::Surface => self.surface = color,
            ColorToken::OnSurface => self.on_surface = color,
            ColorToken::SurfaceVariant => self.surface_variant = color,
            ColorToken::OnSurfaceVariant => self.on_surface_variant = color,
            ColorToken::Success => self.success = color,
            ColorToken::Error => self.error = color,
            ColorToken::Outline => self.outline = color,
            ColorToken::Shadow => self.shadow = color,
            ColorToken::Custom(name) => {
                self.extras.insert(name, color);
            }
        }
        self
    }

    /// Drop a `Custom` token. No-op for builtins (which always have a value).
    pub fn unset(&mut self, token: ColorToken) -> &mut Self {
        if let ColorToken::Custom(name) = token {
            self.extras.remove(name);
        }
        self
    }

    /// Owning chainable variant of `set` — `Theme::dark().with(Token, color)…`.
    pub fn with(mut self, token: ColorToken, color: Color) -> Self {
        self.set(token, color);
        self
    }

    pub fn with_many<I>(mut self, pairs: I) -> Self
    where
        I: IntoIterator<Item = (ColorToken, Color)>,
    {
        for (token, color) in pairs {
            self.set(token, color);
        }
        self
    }
}

impl Default for Theme {
    fn default() -> Self {
        Self::dark()
    }
}

#[derive(Clone, Debug)]
pub struct ThemeCatalog {
    themes: Vec<Theme>,
}

impl ThemeCatalog {
    pub fn new() -> Self {
        Self { themes: Vec::new() }
    }

    pub fn with_builtins() -> Self {
        let themes = alloc::vec![Theme::dark(), Theme::light()];
        Self { themes }
    }

    pub fn get(&self, id: impl Into<ThemeId>) -> Option<&Theme> {
        let id = id.into();
        self.themes.iter().find(|theme| theme.id() == id)
    }

    pub fn iter(&self) -> impl ExactSizeIterator<Item = &Theme> {
        self.themes.iter()
    }

    pub fn insert(&mut self, theme: Theme) -> Option<Theme> {
        if let Some(current) = self
            .themes
            .iter_mut()
            .find(|current| current.id() == theme.id())
        {
            return Some(core::mem::replace(current, theme));
        }
        self.themes.push(theme);
        None
    }

    pub fn len(&self) -> usize {
        self.themes.len()
    }

    pub fn is_empty(&self) -> bool {
        self.themes.is_empty()
    }
}

impl Default for ThemeCatalog {
    fn default() -> Self {
        Self::with_builtins()
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ThemeError {
    NotFound(ThemeId),
}

#[derive(Clone, Debug)]
pub enum ThemeSource {
    Theme(Theme),
    Id(ThemeId),
}

impl From<Theme> for ThemeSource {
    fn from(theme: Theme) -> Self {
        Self::Theme(theme)
    }
}

impl From<ThemeId> for ThemeSource {
    fn from(id: ThemeId) -> Self {
        Self::Id(id)
    }
}

impl From<&'static str> for ThemeSource {
    fn from(id: &'static str) -> Self {
        Self::Id(id.into())
    }
}

/// Free-function counterpart to `App::set_theme`, for handlers and
/// systems that don't have an `App` reference.
pub fn set_theme(world: &mut World, source: impl Into<ThemeSource>) -> Result<(), ThemeError> {
    let theme = match source.into() {
        ThemeSource::Theme(theme) => {
            register(world, theme.clone());
            theme
        }
        ThemeSource::Id(id) => world
            .resource::<ThemeCatalog>()
            .and_then(|catalog| catalog.get(id))
            .cloned()
            .ok_or(ThemeError::NotFound(id))?,
    };
    world.insert_resource(theme);
    if let Some(super::WidgetRoot(root)) = world.resource::<super::WidgetRoot>().copied() {
        world.mark_subtree_dirty(root);
    }
    Ok(())
}

pub fn register(world: &mut World, theme: Theme) -> Option<Theme> {
    if world.resource::<ThemeCatalog>().is_none() {
        world.insert_resource(ThemeCatalog::new());
    }
    world
        .resource_mut::<ThemeCatalog>()
        .expect("ThemeCatalog was just inserted")
        .insert(theme)
}

pub fn active(world: &World) -> Option<&Theme> {
    world.resource::<Theme>()
}

pub fn edit(world: &mut World, update: impl FnOnce(&mut Theme)) -> bool {
    let Some(theme) = world.resource_mut::<Theme>() else {
        return false;
    };
    update(theme);
    let snapshot = theme.clone();
    register(world, snapshot);
    if let Some(super::WidgetRoot(root)) = world.resource::<super::WidgetRoot>().copied() {
        world.mark_subtree_dirty(root);
    }
    true
}

pub fn edit_registered(
    world: &mut World,
    id: impl Into<ThemeId>,
    update: impl FnOnce(&mut Theme),
) -> Result<(), ThemeError> {
    let id = id.into();
    let snapshot = {
        let catalog = world
            .resource_mut::<ThemeCatalog>()
            .ok_or(ThemeError::NotFound(id))?;
        let theme = catalog
            .themes
            .iter_mut()
            .find(|theme| theme.id() == id)
            .ok_or(ThemeError::NotFound(id))?;
        update(theme);
        theme.clone()
    };
    if world
        .resource::<Theme>()
        .is_some_and(|theme| theme.id() == id)
    {
        world.insert_resource(snapshot);
        if let Some(super::WidgetRoot(root)) = world.resource::<super::WidgetRoot>().copied() {
            world.mark_subtree_dirty(root);
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn dark_primary_pinned() {
        assert_eq!(
            Theme::dark().resolve(ColorToken::Primary),
            Color::rgb(88, 166, 255),
        );
    }

    #[test]
    fn bundled_themes_expose_stable_identity() {
        let dark = Theme::dark();
        assert_eq!(dark.id().as_str(), "dark");
        assert_eq!(dark.name(), "Dark");
        assert!(!dark.description().is_empty());

        let light = Theme::light();
        assert_eq!(light.id().as_str(), "light");
        assert_eq!(light.name(), "Light");
    }

    #[test]
    fn custom_theme_metadata_is_all_borrowed() {
        let theme =
            Theme::dark().with_info(ThemeInfo::new("ocean", "Ocean", "Low-glare cyan palette"));
        assert_eq!(theme.info().id.as_str(), "ocean");
        assert_eq!(theme.info().name, "Ocean");
    }

    #[test]
    fn edit_mutates_active_theme_and_invalidates_root() {
        let mut world = World::new();
        let root = world.spawn_empty();
        world.insert_resource(super::super::WidgetRoot(root));
        world.insert_resource(Theme::dark());

        assert!(edit(&mut world, |theme| {
            theme.set(ColorToken::Primary, Color::rgb(1, 2, 3));
        }));
        assert_eq!(
            active(&world).unwrap().resolve(ColorToken::Primary),
            Color::rgb(1, 2, 3)
        );
        assert!(world.get::<crate::ui::dirty::Dirty>(root).is_some());
    }

    #[test]
    fn default_is_dark() {
        let d = Theme::default();
        let dark = Theme::dark();
        for token in [
            ColorToken::Primary,
            ColorToken::OnPrimary,
            ColorToken::Surface,
            ColorToken::OnSurface,
            ColorToken::Success,
        ] {
            assert_eq!(d.resolve(token), dark.resolve(token));
        }
    }

    #[test]
    fn custom_token_round_trip() {
        const BRAND: ColorToken = ColorToken::custom("brand_red");
        let mut t = Theme::dark();
        assert_eq!(t.resolve(BRAND), MISSING_TOKEN_FALLBACK);
        t.set(BRAND, Color::rgb(220, 60, 70));
        assert_eq!(t.resolve(BRAND), Color::rgb(220, 60, 70));
        t.unset(BRAND);
        assert_eq!(t.resolve(BRAND), MISSING_TOKEN_FALLBACK);
    }

    #[test]
    fn set_chain_returns_self() {
        const A: ColorToken = ColorToken::custom("a");
        const B: ColorToken = ColorToken::custom("b");
        let mut t = Theme::dark();
        t.set(A, Color::rgb(1, 0, 0)).set(B, Color::rgb(0, 1, 0));
        assert_eq!(t.resolve(A), Color::rgb(1, 0, 0));
        assert_eq!(t.resolve(B), Color::rgb(0, 1, 0));
    }

    #[test]
    fn themed_color_raw_ignores_theme() {
        let dark = Theme::dark();
        let light = Theme::light();
        let red = ThemedColor::Raw(Color::rgb(255, 0, 0));
        assert_eq!(red.resolve(&dark), Color::rgb(255, 0, 0));
        assert_eq!(red.resolve(&light), Color::rgb(255, 0, 0));
    }

    #[test]
    fn themed_color_token_follows_theme() {
        let dark = Theme::dark();
        let light = Theme::light();
        let primary = ThemedColor::Token(ColorToken::Primary);
        assert_eq!(primary.resolve(&dark), Color::rgb(88, 166, 255));
        assert_eq!(primary.resolve(&light), Color::rgb(0, 100, 200));
    }

    #[test]
    fn from_color_and_token() {
        let from_color: ThemedColor = Color::rgb(1, 2, 3).into();
        assert!(matches!(from_color, ThemedColor::Raw(_)));
        let from_token: ThemedColor = ColorToken::Surface.into();
        assert!(matches!(
            from_token,
            ThemedColor::Token(ColorToken::Surface)
        ));
    }

    #[test]
    fn set_builtin_overrides_resolve() {
        let mut t = Theme::dark();
        t.set(ColorToken::Primary, Color::rgb(255, 0, 0));
        assert_eq!(t.resolve(ColorToken::Primary), Color::rgb(255, 0, 0));
    }

    #[test]
    fn unset_builtin_is_noop() {
        let mut t = Theme::dark();
        let before = t.resolve(ColorToken::Primary);
        t.unset(ColorToken::Primary);
        assert_eq!(t.resolve(ColorToken::Primary), before);
    }

    #[test]
    fn with_chain_owning() {
        const ACCENT: ColorToken = ColorToken::custom("accent");
        let t = Theme::dark()
            .with(ColorToken::Primary, Color::rgb(255, 0, 0))
            .with(ACCENT, Color::rgb(0, 200, 0));
        assert_eq!(t.resolve(ColorToken::Primary), Color::rgb(255, 0, 0));
        assert_eq!(t.resolve(ACCENT), Color::rgb(0, 200, 0));
        // untouched builtins keep their dark default
        assert_eq!(t.resolve(ColorToken::Surface), Theme::dark().surface);
    }

    #[test]
    fn with_many_iterates_all_pairs() {
        let pairs = [
            (ColorToken::Primary, Color::rgb(1, 1, 1)),
            (ColorToken::Surface, Color::rgb(2, 2, 2)),
            (ColorToken::custom("brand"), Color::rgb(3, 3, 3)),
        ];
        let t = Theme::dark().with_many(pairs);
        assert_eq!(t.resolve(ColorToken::Primary), Color::rgb(1, 1, 1));
        assert_eq!(t.resolve(ColorToken::Surface), Color::rgb(2, 2, 2));
        assert_eq!(t.resolve(ColorToken::custom("brand")), Color::rgb(3, 3, 3));
    }

    #[test]
    fn resolve_in_enabled_falls_through() {
        let t = Theme::dark();
        for tok in [
            ColorToken::Primary,
            ColorToken::OnSurface,
            ColorToken::SurfaceVariant,
            ColorToken::Outline,
        ] {
            assert_eq!(t.resolve_in(tok, WidgetState::Enabled), t.resolve(tok));
        }
    }

    #[test]
    fn resolve_in_disabled_text_blends_38() {
        let t = Theme::dark();
        let expected = t.surface.blend_with(t.on_surface, Fixed::from_f32(0.38));
        assert_eq!(
            t.resolve_in(ColorToken::OnSurface, WidgetState::Disabled),
            expected
        );
    }

    #[test]
    fn resolve_in_disabled_container_blends_12() {
        let t = Theme::dark();
        let expected = t.surface.blend_with(t.on_surface, Fixed::from_f32(0.12));
        assert_eq!(
            t.resolve_in(ColorToken::Primary, WidgetState::Disabled),
            expected
        );
        assert_eq!(
            t.resolve_in(ColorToken::SurfaceVariant, WidgetState::Disabled),
            expected
        );
    }

    #[test]
    fn resolve_in_outline_unchanged_when_disabled() {
        let t = Theme::dark();
        assert_eq!(
            t.resolve_in(ColorToken::Outline, WidgetState::Disabled),
            t.resolve(ColorToken::Outline),
        );
    }

    #[test]
    fn themed_color_raw_passes_through_when_enabled() {
        let t = Theme::dark();
        let raw = ThemedColor::Raw(Color::rgb(7, 8, 9));
        assert_eq!(
            raw.resolve_in(&t, WidgetState::Enabled),
            Color::rgb(7, 8, 9)
        );
    }

    #[test]
    fn themed_color_raw_blends_38_when_disabled() {
        let t = Theme::dark();
        let raw_color = Color::rgb(248, 81, 73);
        let expected = t.surface.blend_with(raw_color, Fixed::from_f32(0.38));
        assert_eq!(
            ThemedColor::Raw(raw_color).resolve_in(&t, WidgetState::Disabled),
            expected,
        );
    }

    #[test]
    fn resolve_in_hovered_overlays_8_percent() {
        let t = Theme::dark();
        let base = t.resolve(ColorToken::Primary);
        let expected = base.blend_with(t.on_surface, Fixed::from_f32(0.08));
        assert_eq!(
            t.resolve_in(ColorToken::Primary, WidgetState::Hovered),
            expected,
        );
    }

    #[test]
    fn resolve_in_pressed_overlays_12_percent() {
        let t = Theme::dark();
        let base = t.resolve(ColorToken::Primary);
        let expected = base.blend_with(t.on_surface, Fixed::from_f32(0.12));
        assert_eq!(
            t.resolve_in(ColorToken::Primary, WidgetState::Pressed),
            expected,
        );
    }

    #[test]
    fn resolve_in_error_overlays_error_token() {
        let t = Theme::dark();
        let base = t.resolve(ColorToken::Primary);
        let expected = base.blend_with(t.error, Fixed::from_f32(0.16));
        assert_eq!(
            t.resolve_in(ColorToken::Primary, WidgetState::Error),
            expected,
        );
    }

    #[test]
    fn raw_blends_in_hover_press_error() {
        let t = Theme::dark();
        let raw = Color::rgb(100, 200, 50);
        let h = ThemedColor::Raw(raw).resolve_in(&t, WidgetState::Hovered);
        assert_eq!(h, raw.blend_with(t.on_surface, Fixed::from_f32(0.08)));
        let p = ThemedColor::Raw(raw).resolve_in(&t, WidgetState::Pressed);
        assert_eq!(p, raw.blend_with(t.on_surface, Fixed::from_f32(0.12)));
        let er = ThemedColor::Raw(raw).resolve_in(&t, WidgetState::Error);
        assert_eq!(er, raw.blend_with(t.error, Fixed::from_f32(0.16)));
    }
}