bevy_material_ui 0.2.7

Material Design 3 UI components for Bevy game engine
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
//! Material Design 3 Switch component
//!
//! Switches toggle the state of a single item on or off.
//! Reference: <https://m3.material.io/components/switch/overview>
//!
//! # Example
//! ```no_run
//! use bevy::prelude::*;
//! use bevy_material_ui::switch::{SpawnSwitch, SwitchBuilder, SpawnSwitchChild};
//! use bevy_material_ui::theme::MaterialTheme;
//!
//! fn setup(mut commands: Commands, theme: Res<MaterialTheme>) {
//!     // Using the spawn extension trait (recommended)
//!     commands.spawn_switch(&theme, false, "Enable notifications");
//!
//!     // Or using the child spawner trait for more control
//!     commands.spawn(Node::default()).with_children(|parent| {
//!         parent.spawn_switch_with(&theme, SwitchBuilder::new().selected(true).with_icon(), "Label");
//!     });
//! }
//! ```

use bevy::prelude::*;

use crate::{ripple::RippleHost, theme::MaterialTheme, tokens::CornerRadius};

/// Marker component for switch state layer
#[derive(Component)]
pub struct SwitchStateLayer;

/// Marker for switch label interactions (clicking label toggles switch)
#[derive(Component, Copy, Clone)]
pub struct SwitchLabelFor(pub Entity);

/// Plugin for the switch component
pub struct SwitchPlugin;

impl Plugin for SwitchPlugin {
    fn build(&self, app: &mut App) {
        app.add_message::<SwitchChangeEvent>().add_systems(
            Update,
            (
                switch_interaction_system,
                switch_label_interaction_system,
                switch_style_system,
                switch_theme_refresh_system,
            ),
        );
        if !app.is_plugin_added::<crate::MaterialUiCorePlugin>() {
            app.add_plugins(crate::MaterialUiCorePlugin);
        }
    }
}

/// Material switch component
#[derive(Component)]
pub struct MaterialSwitch {
    /// Whether the switch is on
    pub selected: bool,
    /// Whether the switch is disabled
    pub disabled: bool,
    /// Whether the switch has icons
    pub with_icon: bool,
    /// Animation progress (0.0 = off, 1.0 = on)
    pub animation_progress: f32,
    /// Interaction states
    pub pressed: bool,
    pub hovered: bool,
}

impl MaterialSwitch {
    /// Create a new switch
    pub fn new() -> Self {
        Self {
            selected: false,
            disabled: false,
            with_icon: false,
            animation_progress: 0.0,
            pressed: false,
            hovered: false,
        }
    }

    /// Set initial selected state
    pub fn selected(mut self, selected: bool) -> Self {
        self.selected = selected;
        self.animation_progress = if selected { 1.0 } else { 0.0 };
        self
    }

    /// Set disabled state
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// Enable icons in the switch
    pub fn with_icon(mut self) -> Self {
        self.with_icon = true;
        self
    }

    /// Get the track color
    pub fn track_color(&self, theme: &MaterialTheme) -> Color {
        if self.disabled {
            if self.selected {
                return theme.on_surface.with_alpha(0.12);
            } else {
                return theme.surface_container_highest.with_alpha(0.12);
            }
        }

        if self.selected {
            theme.primary
        } else {
            theme.surface_container_highest
        }
    }

    /// Get the track outline color
    pub fn track_outline_color(&self, theme: &MaterialTheme) -> Color {
        if self.disabled {
            return theme.on_surface.with_alpha(0.12);
        }

        if self.selected {
            Color::NONE
        } else {
            theme.outline
        }
    }

    /// Get the handle (thumb) color
    pub fn handle_color(&self, theme: &MaterialTheme) -> Color {
        if self.disabled {
            if self.selected {
                return theme.surface;
            } else {
                return theme.on_surface.with_alpha(0.38);
            }
        }

        if self.selected {
            theme.on_primary
        } else if self.pressed || self.hovered {
            theme.on_surface_variant
        } else {
            theme.outline
        }
    }

    /// Get the icon color
    pub fn icon_color(&self, theme: &MaterialTheme) -> Color {
        if self.disabled {
            if self.selected {
                return theme.on_surface.with_alpha(0.38);
            } else {
                return theme.surface_container_highest.with_alpha(0.38);
            }
        }

        if self.selected {
            theme.on_primary_container
        } else {
            theme.surface_container_highest
        }
    }

    /// Get the handle size based on state
    pub fn handle_size(&self) -> f32 {
        if self.pressed {
            SWITCH_HANDLE_SIZE_PRESSED
        } else if self.selected || self.with_icon {
            SWITCH_HANDLE_SIZE_SELECTED
        } else {
            SWITCH_HANDLE_SIZE_UNSELECTED
        }
    }

    /// Get the handle position (0.0 to 1.0)
    pub fn handle_position(&self) -> f32 {
        self.animation_progress
    }
}

impl Default for MaterialSwitch {
    fn default() -> Self {
        Self::new()
    }
}

/// Event when switch state changes
#[derive(Event, bevy::prelude::Message)]
pub struct SwitchChangeEvent {
    pub entity: Entity,
    pub selected: bool,
}

/// Switch dimensions
pub const SWITCH_TRACK_WIDTH: f32 = 52.0;
pub const SWITCH_TRACK_HEIGHT: f32 = 32.0;
pub const SWITCH_HANDLE_SIZE_UNSELECTED: f32 = 16.0;
pub const SWITCH_HANDLE_SIZE_SELECTED: f32 = 24.0;
pub const SWITCH_HANDLE_SIZE_PRESSED: f32 = 28.0;

/// System to handle switch interactions
fn switch_interaction_system(
    mut interaction_query: Query<
        (Entity, &Interaction, &mut MaterialSwitch),
        (Changed<Interaction>, With<MaterialSwitch>),
    >,
    mut change_events: MessageWriter<SwitchChangeEvent>,
) {
    for (entity, interaction, mut switch) in interaction_query.iter_mut() {
        if switch.disabled {
            continue;
        }

        match *interaction {
            Interaction::Pressed => {
                switch.pressed = true;
                switch.hovered = false;
                switch.selected = !switch.selected;
                change_events.write(SwitchChangeEvent {
                    entity,
                    selected: switch.selected,
                });
            }
            Interaction::Hovered => {
                switch.pressed = false;
                switch.hovered = true;
            }
            Interaction::None => {
                switch.pressed = false;
                switch.hovered = false;
            }
        }
    }
}

/// System to handle interactions on switch labels
fn switch_label_interaction_system(
    mut label_query: Query<(&Interaction, &SwitchLabelFor), Changed<Interaction>>,
    mut switches: Query<&mut MaterialSwitch>,
    mut change_events: MessageWriter<SwitchChangeEvent>,
) {
    for (interaction, label_for) in label_query.iter_mut() {
        let Ok(mut switch) = switches.get_mut(label_for.0) else {
            continue;
        };

        if switch.disabled {
            continue;
        }

        match *interaction {
            Interaction::Pressed => {
                switch.pressed = false;
                switch.hovered = false;
                switch.selected = !switch.selected;
                switch.animation_progress = if switch.selected { 1.0 } else { 0.0 };
                change_events.write(SwitchChangeEvent {
                    entity: label_for.0,
                    selected: switch.selected,
                });
            }
            Interaction::Hovered => {
                switch.pressed = false;
                switch.hovered = true;
            }
            Interaction::None => {
                switch.pressed = false;
                switch.hovered = false;
            }
        }
    }
}

/// System to update switch visual styles when state changes
fn switch_style_system(
    theme: Option<Res<MaterialTheme>>,
    mut switches: Query<
        (
            &MaterialSwitch,
            &mut BackgroundColor,
            &mut BorderColor,
            &mut Node,
            &Children,
        ),
        Changed<MaterialSwitch>,
    >,
    mut handles: Query<
        (&mut BackgroundColor, &mut Node),
        (With<SwitchHandle>, Without<MaterialSwitch>),
    >,
) {
    let Some(theme) = theme else { return };

    for (switch, mut bg_color, mut border_color, mut node, children) in switches.iter_mut() {
        // Update track
        *bg_color = BackgroundColor(switch.track_color(&theme));
        *border_color = BorderColor::all(switch.track_outline_color(&theme));

        // Update track layout for handle position
        node.justify_content = if switch.selected {
            JustifyContent::FlexEnd
        } else {
            JustifyContent::FlexStart
        };
        node.border = UiRect::all(Val::Px(if switch.selected { 0.0 } else { 2.0 }));

        // Update handle
        let handle_color = switch.handle_color(&theme);
        let handle_size = switch.handle_size();

        for child in children.iter() {
            if let Ok((mut handle_bg, mut handle_node)) = handles.get_mut(child)
            {
                *handle_bg = BackgroundColor(handle_color);
                handle_node.width = Val::Px(handle_size);
                handle_node.height = Val::Px(handle_size);
                handle_node.border_radius = BorderRadius::all(Val::Px(handle_size / 2.0));
            }
        }
    }
}

/// Refresh switch visuals when the theme changes.
fn switch_theme_refresh_system(
    theme: Option<Res<MaterialTheme>>,
    mut switches: Query<(
        &MaterialSwitch,
        &mut BackgroundColor,
        &mut BorderColor,
        &mut Node,
        &Children,
    )>,
    mut handles: Query<
        (&mut BackgroundColor, &mut Node),
        (With<SwitchHandle>, Without<MaterialSwitch>),
    >,
) {
    let Some(theme) = theme else { return };
    if !theme.is_changed() {
        return;
    }

    for (switch, mut bg_color, mut border_color, mut node, children) in switches.iter_mut() {
        *bg_color = BackgroundColor(switch.track_color(&theme));
        *border_color = BorderColor::all(switch.track_outline_color(&theme));

        node.justify_content = if switch.selected {
            JustifyContent::FlexEnd
        } else {
            JustifyContent::FlexStart
        };
        node.border = UiRect::all(Val::Px(if switch.selected { 0.0 } else { 2.0 }));

        let handle_color = switch.handle_color(&theme);
        let handle_size = switch.handle_size();

        for child in children.iter() {
            if let Ok((mut handle_bg, mut handle_node)) = handles.get_mut(child)
            {
                *handle_bg = BackgroundColor(handle_color);
                handle_node.width = Val::Px(handle_size);
                handle_node.height = Val::Px(handle_size);
                handle_node.border_radius = BorderRadius::all(Val::Px(handle_size / 2.0));
            }
        }
    }
}

/// Builder for switches
pub struct SwitchBuilder {
    switch: MaterialSwitch,
}

impl SwitchBuilder {
    /// Create a new switch builder
    pub fn new() -> Self {
        Self {
            switch: MaterialSwitch::new(),
        }
    }

    /// Set initial state
    pub fn selected(mut self, selected: bool) -> Self {
        self.switch.selected = selected;
        self.switch.animation_progress = if selected { 1.0 } else { 0.0 };
        self
    }

    /// Set disabled state
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.switch.disabled = disabled;
        self
    }

    /// Enable icon display
    pub fn with_icon(mut self) -> Self {
        self.switch.with_icon = true;
        self
    }

    /// Build the switch bundle
    pub fn build(self, theme: &MaterialTheme) -> impl Bundle {
        let bg_color = self.switch.track_color(theme);
        let border_color = self.switch.track_outline_color(theme);
        let has_border = !self.switch.selected;

        (
            self.switch,
            Button,
            RippleHost::new(),
            Node {
                width: Val::Px(SWITCH_TRACK_WIDTH),
                height: Val::Px(SWITCH_TRACK_HEIGHT),
                justify_content: JustifyContent::FlexStart,
                align_items: AlignItems::Center,
                padding: UiRect::horizontal(Val::Px(2.0)),
                border: UiRect::all(Val::Px(if has_border { 2.0 } else { 0.0 })),
                border_radius: BorderRadius::all(Val::Px(CornerRadius::FULL)),
                ..default()
            },
            BackgroundColor(bg_color),
            BorderColor::all(border_color),
        )
    }
}

impl Default for SwitchBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Marker component for the switch handle
#[derive(Component)]
pub struct SwitchHandle;

/// Extension trait to spawn switches with full visual hierarchy
pub trait SpawnSwitch {
    /// Spawn a switch with a label
    fn spawn_switch(&mut self, theme: &MaterialTheme, selected: bool, label: &str) -> Entity;

    /// Spawn a switch using a builder for more control
    fn spawn_switch_with(
        &mut self,
        theme: &MaterialTheme,
        builder: SwitchBuilder,
        label: &str,
    ) -> Entity;
}

impl SpawnSwitch for Commands<'_, '_> {
    fn spawn_switch(&mut self, theme: &MaterialTheme, selected: bool, label: &str) -> Entity {
        let builder = SwitchBuilder::new().selected(selected);
        self.spawn_switch_with(theme, builder, label)
    }

    fn spawn_switch_with(
        &mut self,
        theme: &MaterialTheme,
        builder: SwitchBuilder,
        label: &str,
    ) -> Entity {
        let label_color = theme.on_surface;
        let label_text = label.to_string();
        let switch = builder.switch;
        let bg_color = switch.track_color(theme);
        let border_color = switch.track_outline_color(theme);
        let handle_color = switch.handle_color(theme);
        let handle_size = switch.handle_size();
        let has_border = !switch.selected;
        let justify = if switch.selected {
            JustifyContent::FlexEnd
        } else {
            JustifyContent::FlexStart
        };

        self.spawn(Node {
            flex_direction: FlexDirection::Row,
            align_items: AlignItems::Center,
            column_gap: Val::Px(12.0),
            ..default()
        })
        .with_children(|row| {
            // Switch track (the main touch target)
            let switch_entity = row
                .spawn((
                    switch,
                    Button,
                    Interaction::None,
                    RippleHost::new(),
                    Node {
                        width: Val::Px(SWITCH_TRACK_WIDTH),
                        height: Val::Px(SWITCH_TRACK_HEIGHT),
                        justify_content: justify,
                        align_items: AlignItems::Center,
                        padding: UiRect::horizontal(Val::Px(2.0)),
                        border: UiRect::all(Val::Px(if has_border { 2.0 } else { 0.0 })),
                        border_radius: BorderRadius::all(Val::Px(CornerRadius::FULL)),
                        ..default()
                    },
                    BackgroundColor(bg_color),
                    BorderColor::all(border_color),
                ))
                .with_children(|track| {
                    // Handle (thumb)
                    track.spawn((
                        SwitchHandle,
                        Node {
                            width: Val::Px(handle_size),
                            height: Val::Px(handle_size),
                            border_radius: BorderRadius::all(Val::Px(handle_size / 2.0)),
                            ..default()
                        },
                        BackgroundColor(handle_color),
                    ));
                })
                .id();

            // Label
            row.spawn((
                SwitchLabelFor(switch_entity),
                Button,
                Interaction::None,
                Text::new(label_text),
                TextFont {
                    font_size: 14.0,
                    ..default()
                },
                TextColor(label_color),
            ));
        })
        .id()
    }
}

/// Extension trait to spawn switches within a ChildSpawnerCommands context
pub trait SpawnSwitchChild {
    /// Spawn a switch with a label
    fn spawn_switch(&mut self, theme: &MaterialTheme, selected: bool, label: &str);

    /// Spawn a switch using a builder for more control
    fn spawn_switch_with(&mut self, theme: &MaterialTheme, builder: SwitchBuilder, label: &str);
}

impl SpawnSwitchChild for ChildSpawnerCommands<'_> {
    fn spawn_switch(&mut self, theme: &MaterialTheme, selected: bool, label: &str) {
        let builder = SwitchBuilder::new().selected(selected);
        self.spawn_switch_with(theme, builder, label);
    }

    fn spawn_switch_with(&mut self, theme: &MaterialTheme, builder: SwitchBuilder, label: &str) {
        let label_color = theme.on_surface;
        let label_text = label.to_string();
        let switch = builder.switch;
        let bg_color = switch.track_color(theme);
        let border_color = switch.track_outline_color(theme);
        let handle_color = switch.handle_color(theme);
        let handle_size = switch.handle_size();
        let has_border = !switch.selected;
        let justify = if switch.selected {
            JustifyContent::FlexEnd
        } else {
            JustifyContent::FlexStart
        };

        self.spawn(Node {
            flex_direction: FlexDirection::Row,
            align_items: AlignItems::Center,
            column_gap: Val::Px(12.0),
            ..default()
        })
        .with_children(|row| {
            // Switch track
            let switch_entity = row
                .spawn((
                    switch,
                    Button,
                    Interaction::None,
                    RippleHost::new(),
                    Node {
                        width: Val::Px(SWITCH_TRACK_WIDTH),
                        height: Val::Px(SWITCH_TRACK_HEIGHT),
                        justify_content: justify,
                        align_items: AlignItems::Center,
                        padding: UiRect::horizontal(Val::Px(2.0)),
                        border: UiRect::all(Val::Px(if has_border { 2.0 } else { 0.0 })),
                        border_radius: BorderRadius::all(Val::Px(CornerRadius::FULL)),
                        ..default()
                    },
                    BackgroundColor(bg_color),
                    BorderColor::all(border_color),
                ))
                .with_children(|track| {
                    // Handle (thumb)
                    track.spawn((
                        SwitchHandle,
                        Node {
                            width: Val::Px(handle_size),
                            height: Val::Px(handle_size),
                            border_radius: BorderRadius::all(Val::Px(handle_size / 2.0)),
                            ..default()
                        },
                        BackgroundColor(handle_color),
                    ));
                })
                .id();

            // Label
            row.spawn((
                SwitchLabelFor(switch_entity),
                Button,
                Interaction::None,
                Text::new(label_text),
                TextFont {
                    font_size: 14.0,
                    ..default()
                },
                TextColor(label_color),
            ));
        });
    }
}

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

    // ============================================================================
    // MaterialSwitch Tests
    // ============================================================================

    #[test]
    fn test_switch_new_defaults() {
        let switch = MaterialSwitch::new();
        assert!(!switch.selected);
        assert!(!switch.disabled);
        assert!(!switch.with_icon);
        assert_eq!(switch.animation_progress, 0.0);
        assert!(!switch.pressed);
        assert!(!switch.hovered);
    }

    #[test]
    fn test_switch_default_trait() {
        let switch = MaterialSwitch::default();
        assert!(!switch.selected);
        assert!(!switch.disabled);
    }

    #[test]
    fn test_switch_selected_true() {
        let switch = MaterialSwitch::new().selected(true);
        assert!(switch.selected);
        assert_eq!(switch.animation_progress, 1.0);
    }

    #[test]
    fn test_switch_selected_false() {
        let switch = MaterialSwitch::new().selected(false);
        assert!(!switch.selected);
        assert_eq!(switch.animation_progress, 0.0);
    }

    #[test]
    fn test_switch_disabled() {
        let switch = MaterialSwitch::new().disabled(true);
        assert!(switch.disabled);

        let switch = MaterialSwitch::new().disabled(false);
        assert!(!switch.disabled);
    }

    #[test]
    fn test_switch_with_icon() {
        let switch = MaterialSwitch::new().with_icon();
        assert!(switch.with_icon);
    }

    #[test]
    fn test_switch_handle_size_unselected() {
        let switch = MaterialSwitch::new();
        assert_eq!(switch.handle_size(), SWITCH_HANDLE_SIZE_UNSELECTED);
    }

    #[test]
    fn test_switch_handle_size_selected() {
        let switch = MaterialSwitch::new().selected(true);
        assert_eq!(switch.handle_size(), SWITCH_HANDLE_SIZE_SELECTED);
    }

    #[test]
    fn test_switch_handle_size_with_icon() {
        let switch = MaterialSwitch::new().with_icon();
        assert_eq!(switch.handle_size(), SWITCH_HANDLE_SIZE_SELECTED);
    }

    #[test]
    fn test_switch_handle_size_pressed() {
        let mut switch = MaterialSwitch::new();
        switch.pressed = true;
        assert_eq!(switch.handle_size(), SWITCH_HANDLE_SIZE_PRESSED);
    }

    #[test]
    fn test_switch_handle_position_off() {
        let switch = MaterialSwitch::new().selected(false);
        assert_eq!(switch.handle_position(), 0.0);
    }

    #[test]
    fn test_switch_handle_position_on() {
        let switch = MaterialSwitch::new().selected(true);
        assert_eq!(switch.handle_position(), 1.0);
    }

    #[test]
    fn test_switch_builder_chain() {
        let switch = MaterialSwitch::new()
            .selected(true)
            .disabled(false)
            .with_icon();

        assert!(switch.selected);
        assert!(!switch.disabled);
        assert!(switch.with_icon);
    }

    // ============================================================================
    // SwitchBuilder Tests
    // ============================================================================

    #[test]
    fn test_switch_builder_new() {
        let builder = SwitchBuilder::new();
        assert!(!builder.switch.selected);
        assert!(!builder.switch.disabled);
    }

    #[test]
    fn test_switch_builder_default() {
        let builder = SwitchBuilder::default();
        assert!(!builder.switch.selected);
    }

    #[test]
    fn test_switch_builder_selected() {
        let builder = SwitchBuilder::new().selected(true);
        assert!(builder.switch.selected);
        assert_eq!(builder.switch.animation_progress, 1.0);
    }

    #[test]
    fn test_switch_builder_disabled() {
        let builder = SwitchBuilder::new().disabled(true);
        assert!(builder.switch.disabled);
    }

    #[test]
    fn test_switch_builder_with_icon() {
        let builder = SwitchBuilder::new().with_icon();
        assert!(builder.switch.with_icon);
    }

    #[test]
    fn test_switch_builder_full_chain() {
        let builder = SwitchBuilder::new()
            .selected(true)
            .disabled(false)
            .with_icon();

        assert!(builder.switch.selected);
        assert!(!builder.switch.disabled);
        assert!(builder.switch.with_icon);
    }

    // ============================================================================
    // Constants Tests
    // ============================================================================

    #[test]
    fn test_switch_track_width() {
        assert_eq!(SWITCH_TRACK_WIDTH, 52.0);
    }

    #[test]
    fn test_switch_track_height() {
        assert_eq!(SWITCH_TRACK_HEIGHT, 32.0);
    }

    #[test]
    fn test_switch_handle_sizes() {
        assert_eq!(SWITCH_HANDLE_SIZE_UNSELECTED, 16.0);
        assert_eq!(SWITCH_HANDLE_SIZE_SELECTED, 24.0);
        assert_eq!(SWITCH_HANDLE_SIZE_PRESSED, 28.0);
    }

    #[test]
    fn test_switch_handle_size_ordering() {
        // Pressed should be largest, then selected, then unselected
        use std::hint::black_box;
        assert!(black_box(SWITCH_HANDLE_SIZE_PRESSED) > black_box(SWITCH_HANDLE_SIZE_SELECTED));
        assert!(black_box(SWITCH_HANDLE_SIZE_SELECTED) > black_box(SWITCH_HANDLE_SIZE_UNSELECTED));
    }

    #[test]
    fn test_switch_track_dimensions() {
        // Track should be wider than tall
        use std::hint::black_box;
        assert!(black_box(SWITCH_TRACK_WIDTH) > black_box(SWITCH_TRACK_HEIGHT));
    }
}