euv-ui 0.51.0

Cross-platform UI component library and 17-scheme design system for the euv framework with pixel-consistent rendering across Web, Android, iOS, iPadOS, macOS, Windows, and Linux.
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
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
use super::*;

/// Fluent configuration builders for `EuvInput`.
impl EuvInput {
    /// Enables or disables password masking fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source input.
    /// - `bool` - Whether characters are masked.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated input.
    #[must_use]
    pub fn password(mut self, password: bool) -> Self {
        self.set_password(password);
        self
    }

    /// Enables or disables interaction fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source input.
    /// - `bool` - Whether editing is disabled.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated input.
    #[must_use]
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.set_disabled(disabled);
        self
    }

    /// Removes outer chrome so the input embeds cleanly inside `EuvInputGroup`.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source input.
    /// - `bool` - Whether borderless embedding is active.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated input.
    #[must_use]
    pub fn borderless(mut self, borderless: bool) -> Self {
        self.set_borderless(borderless);
        self
    }
}

/// Declarative `#[component]` implementation for `EuvInput`.
#[component]
/// Inherent implementation of [`EuvInput`].
impl EuvInput {
    /// Renders `EuvInput` into the ambient framework-managed `Context`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Component descriptor reference.
    ///
    /// # Returns
    ///
    /// - `NodeId` - Mounted root scene node handle.
    fn render(&self) -> NodeId {
        let ctx: Context = current_context();
        let base_tokens: ThemeTokens = self.get_tokens();
        let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
        let theme_palette: ThemePalette = init_tok.get_palette();
        let theme_geometry: ThemeGeometry = init_tok.get_geometry();
        let sig: Signal<String> = self.get_value();
        let initial: String = sig.get_untracked();
        let caret: usize = initial.len();
        let password: bool = self.get_password();
        let disabled: bool = self.get_disabled();
        let borderless: bool = self.get_borderless();
        let Self { placeholder, .. } = self;
        let placeholder_owned: String = placeholder.clone();
        let a11y_label: String = placeholder_owned.clone();
        let card_surface: SurfaceColors = theme_palette.surface(SurfaceRole::Card);
        let mut style: Style = Style::default();
        style
            .get_mut_layout()
            .set_width(Length::Percent(100.0))
            .set_height(Length::Px(theme_geometry.get_control_md()))
            .set_padding(Edges::symmetric(0.0, SPACE_MD))
            .set_overflow(Overflow::Hidden);
        style
            .get_mut_text()
            .set_color(card_surface.get_fg())
            .set_font_size(13.0);
        if borderless {
            style
                .get_mut_paint()
                .set_background(Some(COLOR_TRANSPARENT));
        } else {
            style
                .get_mut_paint()
                .set_background(Some(card_surface.get_bg()))
                .set_border(Border::new(
                    theme_geometry.get_border_width(),
                    card_surface.get_border(),
                ))
                .set_radius(CornerRadius::all(theme_geometry.get_radius_md().max(0.0)))
                .set_focus_ring(None);
        }
        let on_input_change = move |ui_event: &UiEvent| {
            if let UiEventKind::Change(val) = ui_event.get_kind() {
                let _: bool = sig.set_if_changed(val.clone());
            }
        };
        let node: NodeId = view! {
            input {
                style: style
                value: initial.clone()
                placeholder: placeholder_owned
                caret: caret
                password: password
                multiline: false
                disabled: disabled
                role: A11yRole::TextInput
                a11y_label: a11y_label.clone()
                a11y_value: initial
                a11y_disabled: disabled
                on_change: on_input_change
            }
        };
        let handle: SceneHandle = ctx.get_scene().clone();
        let prev_key: Cell<ThemeKey> = Cell::new(init_tok.key());
        let a11y_fallback: Rc<str> = Rc::from(a11y_label.as_str());
        let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
            Effect::new(move || {
                let next_val: String = sig.get();
                let live_tok: ThemeTokens = use_live_tokens(base_tokens);
                let theme_changed: bool = prev_key.replace(live_tok.key()) != live_tok.key();
                let fallback_label: Rc<str> = a11y_fallback.clone();
                handle.mutate(move |tree: &mut SceneTree| {
                    let mut changed: bool = false;
                    if let Some(item_count) = tree.get_node_mut(node) {
                        if let NodeKind::Input(input_state) = item_count.get_mut_kind()
                            && input_state.get_value() != &next_val
                        {
                            let len: usize = next_val.len();
                            input_state.set_value(next_val.clone());
                            input_state.set_caret(len);
                            changed = true;
                        }
                        if theme_changed {
                            let live_p: ThemePalette = live_tok.get_palette();
                            let live_g: ThemeGeometry = live_tok.get_geometry();
                            let surf: SurfaceColors = live_p.surface(SurfaceRole::Card);
                            let computed_style: &mut Style = item_count.get_mut_style();
                            computed_style.get_mut_text().set_color(surf.get_fg());
                            if !borderless {
                                computed_style
                                    .get_mut_paint()
                                    .set_background(Some(surf.get_bg()))
                                    .set_border(Border::new(
                                        live_g.get_border_width(),
                                        surf.get_border(),
                                    ))
                                    .set_focus_ring(None);
                            }
                            changed = true;
                        }
                        if let Some(a11y) = item_count.get_mut_a11y().as_mut() {
                            a11y.set_value(next_val);
                            a11y.set_disabled(disabled);
                        } else {
                            let mut next_a11y: A11yProps =
                                A11yProps::new(A11yRole::TextInput, fallback_label.to_string());
                            next_a11y.set_value(next_val);
                            next_a11y.set_disabled(disabled);
                            item_count.set_a11y(Some(next_a11y));
                        }
                    }
                    if changed {
                        tree.mark_dirty(node, DIRTY_PAINT);
                    }
                });
            })
        });
        ctx.get_scene().mutate(move |tree: &mut SceneTree| {
            mark_theme_owned(tree, &[node]);
            tree.attach_scope(node, scope);
        });
        node
    }
}

/// Fluent configuration builders for `EuvField`.
impl EuvField {
    /// Sets a validation error caption rendered beneath the field control.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source field.
    /// - `TextValue` - Error message text.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated field.
    #[must_use]
    pub fn error<TextValue>(mut self, error: TextValue) -> Self
    where
        TextValue: Into<String>,
    {
        self.set_error(error.into());
        self
    }
}

/// Declarative `#[component]` implementation for `EuvField`.
#[component]
/// Inherent implementation of [`EuvField`].
impl EuvField {
    /// Renders `EuvField` into the ambient framework-managed `Context`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Component descriptor reference.
    ///
    /// # Returns
    ///
    /// - `NodeId` - Mounted root scene node handle.
    fn render(&self) -> NodeId {
        let theme_palette: ThemePalette = live_tokens_untracked(self.get_tokens()).get_palette();
        let control: NodeId = self.get_control();
        let Self { label, error, .. } = self;
        let column_style: Style = style! {
            Width(Length::Percent(100.0))
            Gap(SPACE_XS)
        };
        let text_quaternary_style: Style = style! {
            FontSize(13.0)
            FontWeight::Medium
            TextColor(theme_palette.get_foreground())
        };
        let text_fifth_style: Style = style! {
            FontSize(12.0)
            TextColor(theme_palette.get_destructive())
        };
        view! {
            column {
                style: column_style
                text {
                    style: text_quaternary_style
                    { label.clone() }
                }
                { control }
                if !error.is_empty() {
                    text {
                        style: text_fifth_style
                        { error.clone() }
                    }
                }
            }
        }
    }
}

/// Declarative `#[component]` implementation for `EuvSwitch`.
#[component]
/// Inherent implementation of [`EuvSwitch`].
impl EuvSwitch {
    /// Renders `EuvSwitch` into the ambient framework-managed `Context`.
    ///
    /// The pill track and circular thumb use `RADIUS_PILL` with a uniform `2px` inset on all four sides, and their
    /// state-dependent colors are owned by a reactive `Effect` tracking both `checked` and the live ambient theme, so
    /// toggling after a theme switch never reverts to stale colors.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Component descriptor reference.
    ///
    /// # Returns
    ///
    /// - `NodeId` - Mounted root scene node handle.
    fn render(&self) -> NodeId {
        let ctx: Context = current_context();
        let base_tokens: ThemeTokens = self.get_tokens();
        let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
        let theme_palette: ThemePalette = init_tok.get_palette();
        let theme_geometry: ThemeGeometry = init_tok.get_geometry();
        let checked: Signal<bool> = self.get_checked();
        let Self { label, .. } = self;
        let label_owned: String = label.clone();
        let a11y_label: String = label_owned.clone();
        let init_on: bool = checked.get_untracked();
        let (init_track, init_thumb): (Color, Color) = Self::state_colors(theme_palette, init_on);
        let mut track: NodeId = NodeId::default();
        let mut thumb: NodeId = NodeId::default();
        let mut label_node: NodeId = NodeId::default();
        let on_toggle = move |_: &UiEvent| {
            let _: bool = checked.set(!checked.get_untracked());
        };
        let row_quaternary_style: Style = style! {
            AlignItems::Center
            Padding(Edges::symmetric(SPACE_XS, SPACE_SM))
            Radius(theme_geometry.get_radius_sm())
            Gap(SPACE_SM)
            CursorIcon::Pointer
        };
        let row_fifth_style: Style = style! {
            Width(Length::Px(SWITCH_TRACK_WIDTH))
            Height(Length::Px(SWITCH_TRACK_HEIGHT))
            FlexShrink(0.0)
            Padding(Edges::all(SWITCH_TRACK_INSET))
            AlignItems::Center
            if init_on { JustifyContent::End } else { JustifyContent::Start }
            Radius(RADIUS_PILL)
            Background(init_track)
        };
        let view_style: Style = style! {
            Width(Length::Px(SWITCH_THUMB_SIZE))
            Height(Length::Px(SWITCH_THUMB_SIZE))
            Radius(RADIUS_PILL)
            Background(init_thumb)
            theme_geometry.get_shadow_xs()
        };
        let text_tertiary_style: Style = style! {
            FlexGrow(1.0)
            FontSize(13.0)
            FontWeight::Medium
            TextColor(theme_palette.get_foreground())
        };
        let row: NodeId = view! {
            row {
                style: row_quaternary_style
                on_click: on_toggle
                row {
                    node_ref: track
                    style: row_fifth_style
                    view {
                        node_ref: thumb
                        style: view_style
                    }
                }
                text {
                    node_ref: label_node
                    style: text_tertiary_style
                    { label_owned }
                }
            }
        };
        let handle: SceneHandle = ctx.get_scene().clone();
        let prev_state: Cell<(bool, ThemeKey)> = Cell::new((init_on, init_tok.key()));
        let a11y_fallback: Rc<str> = Rc::from(a11y_label.as_str());
        let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
            Effect::new(move || {
                let is_on: bool = checked.get();
                let tok: ThemeTokens = use_live_tokens(base_tokens);
                let (prev_on, prev_key): (bool, ThemeKey) = prev_state.replace((is_on, tok.key()));
                if prev_on == is_on && prev_key == tok.key() {
                    return;
                }
                let live_p: ThemePalette = tok.get_palette();
                let (track_bg, thumb_bg): (Color, Color) = Self::state_colors(live_p, is_on);
                let thumb_shadow: Shadow = tok.get_geometry().get_shadow_xs();
                let theme_changed: bool = prev_key != tok.key();
                let fallback_label: Rc<str> = a11y_fallback.clone();
                handle.mutate(move |tree: &mut SceneTree| {
                    if let Some(item_count) = tree.get_node_mut(track) {
                        let computed_style: &mut Style = item_count.get_mut_style();
                        computed_style
                            .get_mut_layout()
                            .set_justify_content(if is_on {
                                JustifyContent::End
                            } else {
                                JustifyContent::Start
                            });
                        computed_style
                            .get_mut_paint()
                            .set_background(Some(track_bg));
                        let dirty: u16 = if prev_on == is_on {
                            DIRTY_PAINT
                        } else {
                            let layout_dirty: u16 = DIRTY_LAYOUT | DIRTY_SUBTREE_LAYOUT;
                            layout_dirty | DIRTY_PAINT
                        };
                        tree.mark_dirty(track, dirty);
                    }
                    if let Some(node_entry) = tree.get_node_mut(thumb) {
                        node_entry
                            .get_mut_style()
                            .get_mut_paint()
                            .set_background(Some(thumb_bg))
                            .set_shadow(Some(thumb_shadow));
                        tree.mark_dirty(thumb, DIRTY_PAINT);
                    }
                    if theme_changed && let Some(normal_vec) = tree.get_node_mut(label_node) {
                        normal_vec
                            .get_mut_style()
                            .get_mut_text()
                            .set_color(live_p.get_foreground());
                        tree.mark_dirty(label_node, DIRTY_PAINT);
                    }
                    if let Some(total_len) = tree.get_node_mut(row) {
                        if let Some(a11y) = total_len.get_mut_a11y().as_mut() {
                            a11y.set_checked(Some(is_on));
                        } else {
                            let mut a11y: A11yProps =
                                A11yProps::new(A11yRole::Switch, fallback_label.to_string());
                            a11y.set_checked(Some(is_on));
                            total_len.set_a11y(Some(a11y));
                        }
                    }
                });
            })
        });
        ctx.get_scene().mutate(move |tree: &mut SceneTree| {
            if let Some(item_count_primary) = tree.get_node_mut(row) {
                let mut a11y: A11yProps = A11yProps::new(A11yRole::Switch, a11y_label);
                a11y.set_checked(Some(init_on));
                item_count_primary.set_a11y(Some(a11y));
            }
            mark_theme_owned(tree, &[track, thumb, label_node]);
            tree.attach_scope(track, scope);
        });
        row
    }
}

/// State-dependent palette helpers for `EuvSwitch`.
impl EuvSwitch {
    /// Resolves `(track, thumb)` fill colors for the given checked state.
    ///
    /// # Arguments
    ///
    /// - `ThemePalette` - Live theme palette.
    /// - `bool` - Whether the switch is on.
    ///
    /// # Returns
    ///
    /// - `(Color, Color)` - Track and thumb fill colors.
    #[must_use]
    pub fn state_colors(palette: ThemePalette, is_on: bool) -> (Color, Color) {
        if is_on {
            (palette.get_primary(), palette.get_primary_foreground())
        } else {
            (palette.get_switch_track_off(), palette.get_switch_thumb())
        }
    }
}

/// Declarative `#[component]` implementation for `EuvCheckbox`.
#[component]
/// Inherent implementation of [`EuvCheckbox`].
impl EuvCheckbox {
    /// Renders `EuvCheckbox` into the ambient framework-managed `Context`.
    ///
    /// The indicator box is a rounded square (`radius_sm`, never circular) whose fill, border, and check-mark colors
    /// are owned by a reactive `Effect` tracking both `checked` and the live ambient theme.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Component descriptor reference.
    ///
    /// # Returns
    ///
    /// - `NodeId` - Mounted root scene node handle.
    fn render(&self) -> NodeId {
        let ctx: Context = current_context();
        let base_tokens: ThemeTokens = self.get_tokens();
        let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
        let theme_palette: ThemePalette = init_tok.get_palette();
        let theme_geometry: ThemeGeometry = init_tok.get_geometry();
        let checked: Signal<bool> = self.get_checked();
        let Self { label, .. } = self;
        let label_owned: String = label.clone();
        let a11y_label: String = label_owned.clone();
        let init_on: bool = checked.get_untracked();
        let init_bg: Color = Self::box_fill(theme_palette, init_on);
        let box_radius: f32 = Self::box_radius(theme_geometry);
        let mut indicator: NodeId = NodeId::default();
        let mut mark: NodeId = NodeId::default();
        let mut label_node: NodeId = NodeId::default();
        let on_toggle = move |_: &UiEvent| {
            let _: bool = checked.set(!checked.get_untracked());
        };
        let paint_check = move |bounds: Rect, display_list: &mut DisplayList| {
            if checked.get_untracked() {
                let check_fg: Color = live_tokens_untracked(base_tokens)
                    .get_palette()
                    .surface(SurfaceRole::Primary)
                    .get_fg();
                let x0: f32 = bounds.get_x() + 1.5;
                let y0: f32 = bounds.get_y() + 6.0;
                let xm: f32 = bounds.get_x() + 4.8;
                let ym: f32 = bounds.get_y() + 9.5;
                let x1: f32 = bounds.get_x() + 10.5;
                let y1: f32 = bounds.get_y() + 2.5;
                display_list.push(DrawCommand::Line {
                    from: Point::new(x0, y0),
                    to: Point::new(xm, ym),
                    color: check_fg,
                    width: 2.0,
                });
                display_list.push(DrawCommand::Line {
                    from: Point::new(xm, ym),
                    to: Point::new(x1, y1),
                    color: check_fg,
                    width: 2.0,
                });
            }
        };
        let row_secondary_style: Style = style! {
            AlignItems::Center
            Padding(Edges::symmetric(SPACE_XS, SPACE_SM))
            Radius(theme_geometry.get_radius_sm())
            Gap(SPACE_SM)
            CursorIcon::Pointer
        };
        let row_tertiary_style: Style = style! {
            JustifyContent::Center
            AlignItems::Center
            Width(Length::Px(CHECKBOX_BOX_SIZE))
            Height(Length::Px(CHECKBOX_BOX_SIZE))
            FlexShrink(0.0)
            Background(init_bg)
            Border::new(theme_geometry.get_border_width(), theme_palette.get_primary())
            Radius(box_radius)
        };
        let canvas_style: Style = style! {
            Width(Length::Px(12.0))
            Height(Length::Px(12.0))
        };
        let text_secondary_style: Style = style! {
            FlexGrow(1.0)
            FontSize(14.0)
            TextColor(theme_palette.get_foreground())
        };
        let row: NodeId = view! {
            row {
                style: row_secondary_style
                on_click: on_toggle
                row {
                    node_ref: indicator
                    style: row_tertiary_style
                    canvas {
                        node_ref: mark
                        style: canvas_style
                        painter: paint_check
                    }
                }
                text {
                    node_ref: label_node
                    style: text_secondary_style
                    { label_owned }
                }
            }
        };
        let handle: SceneHandle = ctx.get_scene().clone();
        let prev_state: Cell<(bool, ThemeKey)> = Cell::new((init_on, init_tok.key()));
        let a11y_fallback: Rc<str> = Rc::from(a11y_label.as_str());
        let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
            Effect::new(move || {
                let is_on: bool = checked.get();
                let tok: ThemeTokens = use_live_tokens(base_tokens);
                let (prev_on, prev_key): (bool, ThemeKey) = prev_state.replace((is_on, tok.key()));
                if prev_on == is_on && prev_key == tok.key() {
                    return;
                }
                let live_p: ThemePalette = tok.get_palette();
                let live_g: ThemeGeometry = tok.get_geometry();
                let fill: Color = Self::box_fill(live_p, is_on);
                let theme_changed: bool = prev_key != tok.key();
                let fallback_label: Rc<str> = a11y_fallback.clone();
                handle.mutate(move |tree: &mut SceneTree| {
                    if let Some(item_count) = tree.get_node_mut(indicator) {
                        item_count
                            .get_mut_style()
                            .get_mut_paint()
                            .set_background(Some(fill))
                            .set_border(Border::new(
                                live_g.get_border_width(),
                                live_p.get_primary(),
                            ))
                            .set_radius(CornerRadius::all(Self::box_radius(live_g)));
                        tree.mark_dirty(indicator, DIRTY_PAINT);
                    }
                    tree.mark_dirty(mark, DIRTY_PAINT);
                    if theme_changed && let Some(node_entry) = tree.get_node_mut(label_node) {
                        node_entry
                            .get_mut_style()
                            .get_mut_text()
                            .set_color(live_p.get_foreground());
                        tree.mark_dirty(label_node, DIRTY_PAINT);
                    }
                    if let Some(normal_vec) = tree.get_node_mut(row) {
                        if let Some(a11y) = normal_vec.get_mut_a11y().as_mut() {
                            a11y.set_checked(Some(is_on));
                        } else {
                            let mut a11y: A11yProps =
                                A11yProps::new(A11yRole::Checkbox, fallback_label.to_string());
                            a11y.set_checked(Some(is_on));
                            normal_vec.set_a11y(Some(a11y));
                        }
                    }
                });
            })
        });
        ctx.get_scene().mutate(move |tree: &mut SceneTree| {
            if let Some(total_len) = tree.get_node_mut(row) {
                let mut a11y: A11yProps = A11yProps::new(A11yRole::Checkbox, a11y_label);
                a11y.set_checked(Some(init_on));
                total_len.set_a11y(Some(a11y));
            }
            mark_theme_owned(tree, &[indicator, mark, label_node]);
            tree.attach_scope(indicator, scope);
        });
        row
    }
}

/// State-dependent palette and geometry helpers for `EuvCheckbox`.
impl EuvCheckbox {
    /// Resolves the indicator box fill for the given checked state.
    ///
    /// # Arguments
    ///
    /// - `ThemePalette` - Live theme palette.
    /// - `bool` - Whether the checkbox is checked.
    ///
    /// # Returns
    ///
    /// - `Color` - Indicator fill color.
    #[must_use]
    pub fn box_fill(palette: ThemePalette, is_on: bool) -> Color {
        if is_on {
            palette.get_primary()
        } else {
            palette.get_card()
        }
    }

    /// Resolves the rounded-square indicator radius, capped so the box can never read as a circular radio.
    ///
    /// # Arguments
    ///
    /// - `ThemeGeometry` - Live theme geometry.
    ///
    /// # Returns
    ///
    /// - `f32` - Corner radius in logical pixels.
    #[must_use]
    pub fn box_radius(geometry: ThemeGeometry) -> f32 {
        geometry.get_radius_sm().clamp(2.0, CHECKBOX_MAX_RADIUS)
    }
}

/// Declarative `#[component]` implementation for `EuvSegmented`.
#[component]
/// Inherent implementation of [`EuvSegmented`].
impl EuvSegmented {
    /// Renders `EuvSegmented` into the ambient framework-managed `Context`.
    ///
    /// The track uses a uniform `CONTROL_INSET` on all four sides equal to the inter-segment gap, and segment pills
    /// use the concentric radius `radius_lg - CONTROL_INSET`. Active segment paint is owned by a reactive `Effect`
    /// tracking both the selection and the live ambient theme.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Component descriptor reference.
    ///
    /// # Returns
    ///
    /// - `NodeId` - Mounted root scene node handle.
    fn render(&self) -> NodeId {
        let mut ctx: Context = current_context();
        let base_tokens: ThemeTokens = self.get_tokens();
        let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
        let theme_palette: ThemePalette = init_tok.get_palette();
        let theme_geometry: ThemeGeometry = init_tok.get_geometry();
        let selected: Signal<usize> = self.get_selected();
        let Self { options, .. } = self;
        let init_sel: usize = selected.get_untracked();
        let track_radius: f32 = theme_geometry.get_radius_lg();
        let segment_radius: f32 = concentric_radius(track_radius, CONTROL_INSET);
        let hovered_idx: Signal<Option<usize>> = Signal::new(None);
        let tab_pairs: Vec<(NodeId, NodeId)> = options
            .iter()
            .enumerate()
            .map(|(idx, opt): (usize, &String)| {
                let active: bool = init_sel == idx;
                let on_pick = move |_: &UiEvent| {
                    let _: bool = selected.set_if_changed(idx);
                };
                let on_enter = move |_: &UiEvent| {
                    let _: bool = hovered_idx.set_if_changed(Some(idx));
                };
                let on_leave = move |_: &UiEvent| {
                    if hovered_idx.get_untracked() == Some(idx) {
                        let _: bool = hovered_idx.set_if_changed(None);
                    }
                };
                let init_surface: SurfaceColors = theme_palette.interaction_colors(
                    InteractionRole::SegmentedPill,
                    active,
                    false,
                    false,
                );
                let segment_pad_x: f32 = if options.len() >= 5 {
                    SPACE_XS
                } else {
                    SPACE_SM
                };
                let segment_font_size: f32 = if options.len() >= 6 { 11.5 } else { 13.0 };
                let mut tab_style: Style = style! {
                    FlexDirection::Row
                    FlexGrow(1.0)
                    FlexShrink(1.0)
                    FlexBasis(Length::Px(0.0))
                    MinWidth(Length::Px(0.0))
                    Height(Length::Px(theme_geometry.get_control_sm()))
                    JustifyContent::Center
                    AlignItems::Center
                    Padding(Edges::symmetric(0.0, segment_pad_x))
                    Overflow::Hidden
                    Radius(segment_radius)
                    CursorIcon::Pointer
                };
                Self::apply_segment_paint(tab_style.get_mut_paint(), init_tok, active, false);
                let mut label_id: NodeId = NodeId::default();
                let text_style: Style = style! {
                    FlexShrink(1.0)
                    MinWidth(Length::Px(0.0))
                    MaxWidth(Length::Percent(100.0))
                    FontSize(segment_font_size)
                    FontWeight::Medium
                    TextColor(init_surface.get_fg())
                    TextAlign::Center
                    TextOverflow::Ellipsis
                    Overflow::Hidden
                };
                let tab_id: NodeId = view! {
                    row {
                        style: tab_style
                        role: A11yRole::Tab
                        a11y_label: opt.clone()
                        on_click: on_pick
                        on_pointer_enter: on_enter
                        on_pointer_leave: on_leave
                        text {
                            node_ref: label_id
                            style: text_style
                            { opt.clone() }
                        }
                    }
                };
                (tab_id, label_id)
            })
            .collect();
        let tab_nodes: Vec<NodeId> = tab_pairs
            .iter()
            .map(|&(tab_id, _): &(NodeId, NodeId)| tab_id)
            .collect();
        let row_style: Style = style! {
            FlexWrap::NoWrap
            Width(Length::Percent(100.0))
            Padding(Edges::all(CONTROL_INSET))
            Gap(CONTROL_INSET)
            Overflow::Hidden
            Background(theme_palette.get_muted())
            Radius(track_radius)
        };
        let root: NodeId = view! {
            row {
                style: row_style
                role: A11yRole::TabList
            }
        };
        if !tab_nodes.is_empty() {
            ctx.append_many(root, tab_nodes.as_slice());
        }
        let handle: SceneHandle = ctx.get_scene().clone();
        let app_state_sig: Signal<AppState> = ctx.get_app_state();
        let mut owned_nodes: Vec<NodeId> = Vec::with_capacity(1 + tab_pairs.len() * 2);
        owned_nodes.push(root);
        for &(tab_id, label_id) in tab_pairs.iter() {
            owned_nodes.push(tab_id);
            owned_nodes.push(label_id);
        }
        let init_pairs: Vec<(NodeId, NodeId)> = tab_pairs.clone();
        let shared_pairs: Rc<[(NodeId, NodeId)]> = Rc::from(tab_pairs);
        let prev_state: Cell<(usize, Option<usize>, ThemeKey)> =
            Cell::new((init_sel, None, init_tok.key()));
        let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
            let _: Effect = Effect::new(move || {
                if app_state_sig.get() != AppState::Active {
                    let _: bool = hovered_idx.set_if_changed(None);
                }
            });
            Effect::new(move || {
                let next_sel: usize = selected.get();
                let next_hov: Option<usize> = hovered_idx.get();
                let tok: ThemeTokens = use_live_tokens(base_tokens);
                let (old_sel, old_hov, old_key): (usize, Option<usize>, ThemeKey) =
                    prev_state.replace((next_sel, next_hov, tok.key()));
                if old_sel == next_sel && old_hov == next_hov && old_key == tok.key() {
                    return;
                }
                let live_p: ThemePalette = tok.get_palette();
                let theme_changed: bool = old_key != tok.key();
                let pairs_ref: Rc<[(NodeId, NodeId)]> = shared_pairs.clone();
                handle.mutate(move |tree: &mut SceneTree| {
                    if theme_changed && let Some(node_ref) = tree.get_node_mut(root) {
                        let _: &mut PaintStyle = node_ref
                            .get_mut_style()
                            .get_mut_paint()
                            .set_background(Some(live_p.get_muted()));
                        tree.mark_dirty(root, DIRTY_PAINT);
                    }
                    for (idx, &(tab, label_id)) in pairs_ref.iter().enumerate() {
                        let is_active: bool = idx == next_sel;
                        let is_hovered: bool = next_hov == Some(idx);
                        let seg_surface: SurfaceColors = live_p.interaction_colors(
                            InteractionRole::SegmentedPill,
                            is_active,
                            is_hovered,
                            false,
                        );
                        if let Some(node_ref) = tree.get_node_mut(tab) {
                            Self::apply_segment_paint(
                                node_ref.get_mut_style().get_mut_paint(),
                                tok,
                                is_active,
                                is_hovered,
                            );
                            let _: &mut SceneNode =
                                node_ref.set_hover_style(Self::segment_hover_paint(tok, is_active));
                            tree.mark_dirty(tab, DIRTY_PAINT);
                        }
                        if let Some(label_node) = tree.get_node_mut(label_id) {
                            let _: &mut TextStyle = label_node
                                .get_mut_style()
                                .get_mut_text()
                                .set_color(seg_surface.get_fg());
                            tree.mark_dirty(label_id, DIRTY_PAINT);
                        }
                    }
                });
            })
        });
        ctx.get_scene().mutate(move |tree: &mut SceneTree| {
            for (idx, &(tab, _)) in init_pairs.iter().enumerate() {
                if let Some(node_ref) = tree.get_node_mut(tab) {
                    let _: &mut SceneNode = node_ref
                        .set_hover_style(Self::segment_hover_paint(init_tok, idx == init_sel));
                }
            }
            mark_theme_owned(tree, owned_nodes.as_slice());
            tree.attach_scope(root, scope);
        });
        root
    }
}

/// Theme-aware paint helpers for `EuvSegmented`.
impl EuvSegmented {
    /// Writes active / hovered / inactive segment fill, border, and elevation for `tokens` onto `paint`.
    ///
    /// # Arguments
    ///
    /// - `&mut PaintStyle` - Segment paint to update.
    /// - `ThemeTokens` - Live theme tokens.
    /// - `bool` - Whether the segment is selected.
    /// - `bool` - Whether the segment is hovered.
    pub fn apply_segment_paint(
        paint: &mut PaintStyle,
        tokens: ThemeTokens,
        active: bool,
        hovered: bool,
    ) {
        let palette: ThemePalette = tokens.get_palette();
        let geometry: ThemeGeometry = tokens.get_geometry();
        let surface: SurfaceColors =
            palette.interaction_colors(InteractionRole::SegmentedPill, active, hovered, false);
        let _: &mut PaintStyle = paint
            .set_background(Some(surface.get_bg()))
            .set_border(Border::new(
                geometry.get_border_width(),
                surface.get_border(),
            ))
            .set_shadow(if active {
                Some(geometry.get_shadow_xs())
            } else {
                None
            });
    }

    /// Returns the subtle hover paint applied to inactive segments (preserving the uniform `CONTROL_INSET` gap next to the active pill).
    ///
    /// # Arguments
    ///
    /// - `ThemeTokens` - Live theme tokens.
    /// - `bool` - Whether the segment is currently active.
    ///
    /// # Returns
    ///
    /// - `Option<PaintStyle>` - Accent hover surface for inactive segments, `None` for the active segment.
    #[must_use]
    pub fn segment_hover_paint(tokens: ThemeTokens, active: bool) -> Option<PaintStyle> {
        if active {
            return None;
        }
        let palette: ThemePalette = tokens.get_palette();
        let geometry: ThemeGeometry = tokens.get_geometry();
        let segment_radius: f32 = concentric_radius(geometry.get_radius_lg(), CONTROL_INSET);
        let surface: SurfaceColors =
            palette.interaction_colors(InteractionRole::SegmentedPill, false, true, false);
        let mut hover_paint: PaintStyle = PaintStyle::default();
        let _: &mut PaintStyle = hover_paint
            .set_background(Some(surface.get_bg()))
            .set_radius(CornerRadius::all(segment_radius))
            .set_cursor(CursorIcon::Pointer);
        Some(hover_paint)
    }
}