Skip to main content

euv_ui/component/surface/
impl.rs

1use super::*;
2
3/// Builder and mounting methods for `EuvPageContainer`.
4impl EuvPageContainer {
5    /// Appends a child node to the container.
6    ///
7    /// # Arguments
8    ///
9    /// - `Self` - Source container.
10    /// - `NodeId` - Mounted child node.
11    ///
12    /// # Returns
13    ///
14    /// - `Self` - Updated container.
15    #[must_use]
16    pub fn child(mut self, node: NodeId) -> Self {
17        self.get_mut_children().push(node);
18        self
19    }
20}
21
22/// Declarative `#[component]` implementation for `EuvPageContainer`.
23#[component]
24/// Inherent implementation of [`EuvPageContainer`].
25impl EuvPageContainer {
26    /// Renders `EuvPageContainer` into the ambient framework-managed `Context`.
27    ///
28    /// # Arguments
29    ///
30    /// - `&Self` - Component descriptor reference.
31    ///
32    /// # Returns
33    ///
34    /// - `NodeId` - Mounted root scene node handle.
35    fn render(&self) -> NodeId {
36        let Self { children, .. } = self;
37        let container_st: Style = style! {
38            Width(Length::Percent(100.0))
39            MaxWidth(Length::Px(1120.0))
40            Padding(Edges::all(SPACE_XL))
41            Gap(SPACE_LG)
42        }
43        .with_overrides(self.get_style());
44        let root: NodeId = view! {
45            column {
46                style: container_st
47            }
48        };
49        if !children.is_empty() {
50            let mut ctx: Context = current_context();
51            ctx.append_many(root, children);
52        }
53        root
54    }
55}
56
57/// Builder and mounting methods for `EuvPanel`.
58impl EuvPanel {
59    /// Appends a child node to the panel.
60    ///
61    /// # Arguments
62    ///
63    /// - `Self` - Source panel.
64    /// - `NodeId` - Child handle.
65    ///
66    /// # Returns
67    ///
68    /// - `Self` - Updated panel.
69    #[must_use]
70    pub fn child(mut self, node: NodeId) -> Self {
71        self.get_mut_children().push(node);
72        self
73    }
74}
75
76/// Declarative `#[component]` implementation for `EuvPanel`.
77#[component]
78/// Inherent implementation of [`EuvPanel`].
79impl EuvPanel {
80    /// Renders `EuvPanel` into the ambient framework-managed `Context`.
81    ///
82    /// # Arguments
83    ///
84    /// - `&Self` - Component descriptor reference.
85    ///
86    /// # Returns
87    ///
88    /// - `NodeId` - Mounted root scene node handle.
89    fn render(&self) -> NodeId {
90        let tokens: ThemeTokens = live_tokens_untracked(self.get_tokens());
91        let theme_palette: ThemePalette = tokens.get_palette();
92        let geometry: ThemeGeometry = tokens.get_geometry();
93        let surface: SurfaceColors = theme_palette.surface(SurfaceRole::Muted);
94        let Self {
95            title, children, ..
96        } = self;
97        let column_tertiary_style: Style = style! {
98            Width(Length::Percent(100.0))
99            Padding(Edges::all(SPACE_MD))
100            Gap(SPACE_SM)
101            Background(surface.get_bg())
102            Border::new(geometry.get_border_width(), surface.get_border())
103            Radius(geometry.get_radius_md())
104            Overflow::Hidden
105        };
106        let text_quaternary_style: Style = style! {
107            Width(Length::Percent(100.0))
108            FontSize(14.0)
109            FontWeight::SemiBold
110            TextColor(surface.get_fg())
111        };
112        let root: NodeId = view! {
113            column {
114                style: column_tertiary_style
115                if !title.is_empty() {
116                    text {
117                        style: text_quaternary_style
118                        { title.as_str() }
119                    }
120                }
121            }
122        };
123        if !children.is_empty() {
124            let mut ctx: Context = current_context();
125            ctx.append_many(root, children);
126        }
127        root
128    }
129}
130
131/// Builder methods for `EuvSpace`.
132impl EuvSpace {
133    /// Creates a horizontal `EuvSpace` with `gap`.
134    ///
135    /// # Arguments
136    ///
137    /// - `f32` - Gap in logical pixels.
138    ///
139    /// # Returns
140    ///
141    /// - `Self` - Horizontal spacing container.
142    #[must_use]
143    pub fn row(gap: f32) -> Self {
144        Self::new(false).style(style! {
145            Gap(gap)
146        })
147    }
148    /// Creates a vertical `EuvSpace` with `gap`.
149    ///
150    /// # Arguments
151    ///
152    /// - `f32` - Gap in logical pixels.
153    ///
154    /// # Returns
155    ///
156    /// - `Self` - Vertical spacing container.
157    #[must_use]
158    pub fn column(gap: f32) -> Self {
159        Self::new(true).style(style! {
160            Gap(gap)
161        })
162    }
163    /// Appends a child node.
164    ///
165    /// # Arguments
166    ///
167    /// - `Self` - Source container.
168    /// - `NodeId` - Child handle.
169    ///
170    /// # Returns
171    ///
172    /// - `Self` - Updated container.
173    #[must_use]
174    pub fn child(mut self, node: NodeId) -> Self {
175        self.get_mut_children().push(node);
176        self
177    }
178}
179
180/// Declarative `#[component]` implementation for `EuvSpace`.
181#[component]
182/// Inherent implementation of [`EuvSpace`].
183impl EuvSpace {
184    /// Renders `EuvSpace` into the ambient framework-managed `Context`.
185    ///
186    /// # Arguments
187    ///
188    /// - `&Self` - Component descriptor reference.
189    ///
190    /// # Returns
191    ///
192    /// - `NodeId` - Mounted root scene node handle.
193    fn render(&self) -> NodeId {
194        let mut base: Style = Style::default();
195        if self.get_vertical() {
196            base.get_mut_layout()
197                .set_flex_direction(FlexDirection::Column);
198        } else {
199            base.get_mut_layout()
200                .set_flex_direction(FlexDirection::Row)
201                .set_flex_wrap(FlexWrap::Wrap)
202                .set_align_items(AlignItems::Center);
203        }
204        let Self { children, .. } = self;
205        let space_st: Style = style! {
206            base
207            Gap(SPACE_SM)
208        }
209        .with_overrides(self.get_style());
210        let root: NodeId = view! {
211            view {
212                style: space_st
213            }
214        };
215        if !children.is_empty() {
216            let mut ctx: Context = current_context();
217            ctx.append_many(root, children);
218        }
219        root
220    }
221}
222
223/// Builder methods for `EuvGrid`.
224impl EuvGrid {
225    /// Appends a child cell to the grid.
226    ///
227    /// # Arguments
228    ///
229    /// - `Self` - Source grid.
230    /// - `NodeId` - Child node handle.
231    ///
232    /// # Returns
233    ///
234    /// - `Self` - Updated grid.
235    #[must_use]
236    pub fn child(mut self, node: NodeId) -> Self {
237        self.get_mut_children().push(node);
238        self
239    }
240}
241
242/// Declarative `#[component]` implementation for `EuvGrid`.
243#[component]
244/// Inherent implementation of [`EuvGrid`].
245impl EuvGrid {
246    /// Renders `EuvGrid` into the ambient framework-managed `Context`.
247    ///
248    /// # Arguments
249    ///
250    /// - `&Self` - Component descriptor reference.
251    ///
252    /// # Returns
253    ///
254    /// - `NodeId` - Mounted root scene node handle.
255    fn render(&self) -> NodeId {
256        let Self { children, .. } = self;
257        let grid_st: Style = style! {
258            GridColumns(2)
259            Width(Length::Percent(100.0))
260            Gap(SPACE_MD)
261        }
262        .with_overrides(self.get_style());
263        let root: NodeId = view! {
264            grid {
265                style: grid_st
266            }
267        };
268        if !children.is_empty() {
269            let mut ctx: Context = current_context();
270            ctx.append_many(root, children);
271        }
272        root
273    }
274}
275
276/// Builder methods for `EuvScrollArea`.
277impl EuvScrollArea {
278    /// Appends a child node into the scroll area.
279    ///
280    /// # Arguments
281    ///
282    /// - `Self` - Source scroll area.
283    /// - `NodeId` - Child handle.
284    ///
285    /// # Returns
286    ///
287    /// - `Self` - Updated scroll area.
288    #[must_use]
289    pub fn child(mut self, node: NodeId) -> Self {
290        self.get_mut_children().push(node);
291        self
292    }
293}
294
295/// Declarative `#[component]` implementation for `EuvScrollArea`.
296#[component]
297/// Inherent implementation of [`EuvScrollArea`].
298impl EuvScrollArea {
299    /// Renders `EuvScrollArea` into the ambient framework-managed `Context`.
300    ///
301    /// # Arguments
302    ///
303    /// - `&Self` - Component descriptor reference.
304    ///
305    /// # Returns
306    ///
307    /// - `NodeId` - Mounted root scene node handle.
308    fn render(&self) -> NodeId {
309        let tokens: ThemeTokens = live_tokens_untracked(self.get_tokens());
310        let theme_palette: ThemePalette = tokens.get_palette();
311        let geometry: ThemeGeometry = tokens.get_geometry();
312        let surface: SurfaceColors = theme_palette.surface(SurfaceRole::Card);
313        let Self { children, .. } = self;
314        let mut scroll_st: Style = Style::default();
315        let _: &mut LayoutStyle = scroll_st
316            .get_mut_layout()
317            .set_flex_direction(FlexDirection::Column);
318        let area_st: Style = style! {
319            scroll_st
320            Width(Length::Percent(100.0))
321            Height(Length::Px(240.0))
322            Padding(Edges::all(SPACE_SM))
323            Gap(SPACE_SM)
324            Background(surface.get_bg())
325            Border::new(geometry.get_border_width(), surface.get_border())
326            Radius(geometry.get_radius_md())
327        }
328        .with_overrides(self.get_style());
329        let root: NodeId = view! {
330            scroll {
331                style: area_st
332            }
333        };
334        let mut ctx: Context = current_context();
335        let scene_clamp: SceneHandle = ctx.get_scene().clone();
336        ctx.on_scroll(root, move |_: &UiEvent| {
337            scene_clamp.with_write(
338                |tree: &mut SceneTree| {
339                    if let Some(cur) = tree.get_node(root).map(|scene_node: &SceneNode| {
340                        scene_node.get_layout_box().get_scroll_offset()
341                    }) && cur.get_x().abs() > 0.01
342                    {
343                        tree.set_scroll_offset(root, Point::new(0.0, cur.get_y()));
344                    }
345                },
346                (),
347            );
348        });
349        if !children.is_empty() {
350            ctx.append_many(root, children);
351        }
352        root
353    }
354}
355
356/// Declarative `#[component]` implementation for `EuvSplitter`.
357#[component]
358/// Inherent implementation of [`EuvSplitter`].
359impl EuvSplitter {
360    /// Renders `EuvSplitter` into the ambient framework-managed `Context`.
361    ///
362    /// # Arguments
363    ///
364    /// - `&Self` - Component descriptor reference.
365    ///
366    /// # Returns
367    ///
368    /// - `NodeId` - Mounted root scene node handle.
369    fn render(&self) -> NodeId {
370        let tokens: ThemeTokens = live_tokens_untracked(self.get_tokens());
371        let theme_palette: ThemePalette = tokens.get_palette();
372        let ratio: f32 = self.get_ratio_percent().clamp(15.0, 85.0);
373        let primary: NodeId = self.get_primary();
374        let secondary: NodeId = self.get_secondary();
375        let row_sixth_style: Style = style! {
376            Width(Length::Percent(100.0))
377        };
378        let column_style: Style = style! {
379            Width(Length::Percent(ratio))
380            Padding(Edges::all(SPACE_SM))
381            Overflow::Hidden
382        };
383        let view_style: Style = style! {
384            Width(Length::Px(4.0))
385            FlexShrink(0.0)
386            Radius(RADIUS_PILL)
387            Background(theme_palette.get_border())
388        };
389        let column_secondary_style: Style = style! {
390            FlexGrow(1.0)
391            MinWidth(Length::Px(0.0))
392            Padding(Edges::all(SPACE_SM))
393            Overflow::Hidden
394        };
395        view! {
396            row {
397                style: row_sixth_style
398                column {
399                    style: column_style
400                    { primary }
401                }
402                view {
403                    style: view_style
404                }
405                column {
406                    style: column_secondary_style
407                    { secondary }
408                }
409            }
410        }
411    }
412}
413
414/// Builder methods for `EuvNavbar`.
415impl EuvNavbar {
416    /// Appends a trailing action node to the navbar.
417    ///
418    /// # Arguments
419    ///
420    /// - `Self` - Source navbar.
421    /// - `NodeId` - Action node handle.
422    ///
423    /// # Returns
424    ///
425    /// - `Self` - Updated navbar.
426    #[must_use]
427    pub fn action(mut self, node: NodeId) -> Self {
428        self.get_mut_actions().push(node);
429        self
430    }
431}
432
433/// Declarative `#[component]` implementation for `EuvNavbar`.
434#[component]
435/// Inherent implementation of [`EuvNavbar`].
436impl EuvNavbar {
437    /// Renders `EuvNavbar` into the ambient framework-managed `Context`.
438    ///
439    /// # Arguments
440    ///
441    /// - `&Self` - Component descriptor reference.
442    ///
443    /// # Returns
444    ///
445    /// - `NodeId` - Mounted root scene node handle.
446    fn render(&self) -> NodeId {
447        let tokens: ThemeTokens = live_tokens_untracked(self.get_tokens());
448        let theme_palette: ThemePalette = tokens.get_palette();
449        let geometry: ThemeGeometry = tokens.get_geometry();
450        let surface: SurfaceColors = theme_palette.surface(SurfaceRole::Card);
451        let Self { brand, actions, .. } = self;
452        let mut actions_row: NodeId = NodeId::default();
453        let row_quaternary_style: Style = style! {
454            Width(Length::Percent(100.0))
455            Height(Length::Px(MOBILE_HEADER_HEIGHT))
456            Padding(Edges::symmetric(0.0, SPACE_LG))
457            JustifyContent::SpaceBetween
458            AlignItems::Center
459            Gap(SPACE_SM)
460            Background(surface.get_bg())
461            Border::new(geometry.get_border_width(), surface.get_border())
462            Overflow::Hidden
463        };
464        let text_tertiary_style: Style = style! {
465            FlexShrink(1.0)
466            MinWidth(Length::Px(0.0))
467            FontSize(16.0)
468            FontWeight::Bold
469            TextColor(surface.get_fg())
470            TextOverflow::Ellipsis
471        };
472        let row_fifth_style: Style = style! {
473            FlexShrink(0.0)
474            AlignItems::Center
475            Gap(SPACE_SM)
476        };
477        let root: NodeId = view! {
478            row {
479                style: row_quaternary_style
480                text {
481                    style: text_tertiary_style
482                    { brand.as_str() }
483                }
484                row {
485                    node_ref: actions_row
486                    style: row_fifth_style
487                }
488            }
489        };
490        if !actions.is_empty() {
491            let mut ctx: Context = current_context();
492            ctx.append_many(actions_row, actions);
493        }
494        root
495    }
496}
497
498/// Palette helpers for `EuvFooter`.
499impl EuvFooter {
500    /// Resolves `(bg, fg, border)` colors for `EuvFooter` with guaranteed readable foreground contrast.
501    ///
502    /// # Arguments
503    ///
504    /// - `ThemePalette` - Live theme palette.
505    ///
506    /// # Returns
507    ///
508    /// - `SurfaceColors` - Muted footer surface with readable text foreground.
509    #[must_use]
510    pub fn surface_colors(palette: ThemePalette) -> SurfaceColors {
511        let surface: SurfaceColors = palette.surface(SurfaceRole::Muted);
512        let fg: Color =
513            palette.readable_foreground_with(palette.get_muted_foreground(), surface.get_bg());
514        SurfaceColors::new(surface.get_bg(), fg, surface.get_border())
515    }
516}
517
518/// Declarative `#[component]` implementation for `EuvFooter`.
519#[component]
520/// Inherent implementation of [`EuvFooter`].
521impl EuvFooter {
522    /// Renders `EuvFooter` into the ambient framework-managed `Context`.
523    ///
524    /// # Arguments
525    ///
526    /// - `&Self` - Component descriptor reference.
527    ///
528    /// # Returns
529    ///
530    /// - `NodeId` - Mounted root scene node handle.
531    fn render(&self) -> NodeId {
532        let ctx: Context = current_context();
533        let base_tokens: ThemeTokens = self.get_tokens();
534        let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
535        let geometry: ThemeGeometry = init_tok.get_geometry();
536        let surface: SurfaceColors = Self::surface_colors(init_tok.get_palette());
537        let Self { text, .. } = self;
538        let mut label_node: NodeId = NodeId::default();
539        let row_tertiary_style: Style = style! {
540            Width(Length::Percent(100.0))
541            MinHeight(Length::Px(geometry.get_control_md()))
542            Padding(Edges::symmetric(SPACE_MD, SPACE_LG))
543            JustifyContent::Center
544            AlignItems::Center
545            Background(surface.get_bg())
546            Border::new(geometry.get_border_width(), surface.get_border())
547            Overflow::Hidden
548        };
549        let text_secondary_style: Style = style! {
550            MaxWidth(Length::Percent(100.0))
551            FontSize(12.0)
552            TextColor(surface.get_fg())
553            TextAlign::Center
554        };
555        let root: NodeId = view! {
556            row {
557                style: row_tertiary_style
558                text {
559                    node_ref: label_node
560                    style: text_secondary_style
561                    { text.as_str() }
562                }
563            }
564        };
565        let handle: SceneHandle = ctx.get_scene().clone();
566        let prev_key: Cell<ThemeKey> = Cell::new(init_tok.key());
567        let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
568            Effect::new(move || {
569                let tok: ThemeTokens = use_live_tokens(base_tokens);
570                let old_key: ThemeKey = prev_key.replace(tok.key());
571                if old_key == tok.key() {
572                    return;
573                }
574                let live_s: SurfaceColors = Self::surface_colors(tok.get_palette());
575                let live_g: ThemeGeometry = tok.get_geometry();
576                handle.with_write(
577                    |tree: &mut SceneTree| {
578                        if let Some(item_count) = tree.get_node_mut(root) {
579                            let _: &mut PaintStyle = item_count
580                                .get_mut_style()
581                                .get_mut_paint()
582                                .set_background(Some(live_s.get_bg()))
583                                .set_border(Border::new(
584                                    live_g.get_border_width(),
585                                    live_s.get_border(),
586                                ));
587                            tree.mark_dirty(root, DIRTY_PAINT);
588                        }
589                        if let Some(node_entry) = tree.get_node_mut(label_node) {
590                            let _: &mut TextStyle = node_entry
591                                .get_mut_style()
592                                .get_mut_text()
593                                .set_color(live_s.get_fg());
594                            tree.mark_dirty(label_node, DIRTY_PAINT);
595                        }
596                    },
597                    (),
598                );
599            })
600        });
601        ctx.get_scene().with_write(
602            |tree: &mut SceneTree| {
603                mark_theme_owned(tree, &[root, label_node]);
604                tree.attach_scope(root, scope);
605            },
606            (),
607        );
608        root
609    }
610}
611
612/// Builder modifiers for `EuvButtonGroup`.
613impl EuvButtonGroup {
614    /// Enables automatic per-row width distribution (`flex_grow: 1.0` + content-safe `min_width`) with zero trailing empty space.
615    ///
616    /// # Arguments
617    ///
618    /// - `Self` - Source button group.
619    /// - `bool` - Whether wrapped rows auto-fill 100% of container width.
620    ///
621    /// # Returns
622    ///
623    /// - `Self` - Updated button group.
624    #[must_use]
625    pub fn wrap_fill(mut self, enabled: bool) -> Self {
626        self.set_wrap_fill(enabled);
627        self
628    }
629}
630
631/// Declarative `#[component]` implementation for `EuvButtonGroup`.
632#[component]
633/// Inherent implementation of [`EuvButtonGroup`].
634impl EuvButtonGroup {
635    /// Renders `EuvButtonGroup` into the ambient framework-managed `Context`.
636    ///
637    /// Applies the Equal-Inset + Equal-Gap law (`padding == gap == CONTROL_INSET`) on an outer `radius_lg`
638    /// container and aligns non-pill child items to the concentric inner radius `concentric_radius(radius_lg, CONTROL_INSET)`.
639    ///
640    /// # Arguments
641    ///
642    /// - `&Self` - Component descriptor reference.
643    ///
644    /// # Returns
645    ///
646    /// - `NodeId` - Mounted root scene node handle.
647    fn render(&self) -> NodeId {
648        let tokens: ThemeTokens = live_tokens_untracked(self.get_tokens());
649        let theme_palette: ThemePalette = tokens.get_palette();
650        let geometry: ThemeGeometry = tokens.get_geometry();
651        let surface: SurfaceColors = theme_palette.surface(SurfaceRole::Muted);
652        let wrap_fill: bool = self.get_wrap_fill();
653        let outer_radius: f32 = geometry.get_radius_lg();
654        let inner_radius: f32 = concentric_radius(outer_radius, CONTROL_INSET);
655        let Self { children, .. } = self;
656        let row_secondary_style: Style = style! {
657            Width(if wrap_fill { Length::Percent(100.0) } else { Length::Auto })
658            FlexWrap::Wrap
659            AlignItems::Center
660            Gap(CONTROL_INSET)
661            Padding(Edges::all(CONTROL_INSET))
662            Background(surface.get_bg())
663            Border::new(geometry.get_border_width(), surface.get_border())
664            Radius(outer_radius)
665            Overflow::Hidden
666        };
667        let root: NodeId = view! {
668            row {
669                style: row_secondary_style
670            }
671        };
672        if !children.is_empty() {
673            let mut ctx: Context = current_context();
674            ctx.get_scene().with_write(
675                |tree: &mut SceneTree| {
676                    for cid in children.iter().copied() {
677                        let label_len: usize = if wrap_fill {
678                            tree.get_node(cid)
679                                .map(|scene_node: &SceneNode| {
680                                    let direct: usize = match scene_node.get_kind() {
681                                        NodeKind::Text(blend_factor) => {
682                                            blend_factor.chars().count()
683                                        }
684                                        _ => 0,
685                                    };
686                                    let child_max: usize = tree
687                                        .children_iter(cid)
688                                        .filter_map(|sub: NodeId| tree.get_node(sub))
689                                        .map(|sub_n: &SceneNode| match sub_n.get_kind() {
690                                            NodeKind::Text(time_step) => time_step.chars().count(),
691                                            _ => 0,
692                                        })
693                                        .max()
694                                        .unwrap_or(0);
695                                    direct.max(child_max)
696                                })
697                                .unwrap_or(0)
698                        } else {
699                            0
700                        };
701                        if let Some(item_count) = tree.get_node_mut(cid) {
702                            let cur_r: f32 =
703                                item_count.style_ref().get_paint().get_radius().max_radius();
704                            if cur_r < RADIUS_PILL {
705                                let _: &mut PaintStyle = item_count
706                                    .get_mut_style()
707                                    .get_mut_paint()
708                                    .set_radius(CornerRadius::all(inner_radius));
709                                if let Some(mut hover_p) = item_count.hover_paint() {
710                                    let _: &mut PaintStyle =
711                                        hover_p.set_radius(CornerRadius::all(inner_radius));
712                                    let _: &mut SceneNode =
713                                        item_count.set_hover_style(Some(hover_p));
714                                }
715                            }
716                            if wrap_fill {
717                                let lay: &mut LayoutStyle =
718                                    item_count.get_mut_style().get_mut_layout();
719                                let fixed_w: bool = !matches!(lay.get_width(), Length::Auto);
720                                if !fixed_w {
721                                    let _: &mut LayoutStyle =
722                                        lay.set_flex_grow(1.0).set_flex_shrink(1.0);
723                                    if matches!(lay.get_min_width(), Length::Auto) {
724                                        let min_w: f32 =
725                                            ((label_len as f32) * 7.2 + 24.0).clamp(76.0, 210.0);
726                                        let _: &mut LayoutStyle =
727                                            lay.set_min_width(Length::Px(min_w));
728                                    }
729                                }
730                            }
731                        }
732                    }
733                },
734                (),
735            );
736            ctx.append_many(root, children);
737        }
738        root
739    }
740}
741
742/// State-dependent palette helpers for `EuvToggle`.
743impl EuvToggle {
744    /// Resolves contrast-safe `SurfaceColors` for the toggle's pressed and hovered state.
745    ///
746    /// # Arguments
747    ///
748    /// - `ThemePalette` - Live theme palette.
749    /// - `bool` - Whether the toggle is pressed/active.
750    /// - `bool` - Whether the pointer hovers the toggle.
751    ///
752    /// # Returns
753    ///
754    /// - `SurfaceColors` - Paired `(bg, fg, border)` colors guaranteed `>= 4.5:1`.
755    #[must_use]
756    pub fn state_colors(palette: ThemePalette, pressed: bool, hovered: bool) -> SurfaceColors {
757        if pressed {
758            palette.interaction_colors(InteractionRole::SolidPrimary, true, hovered, false)
759        } else {
760            palette.interaction_colors(InteractionRole::OutlineSurface, false, hovered, false)
761        }
762    }
763}
764
765/// Declarative `#[component]` implementation for `EuvToggle`.
766#[component]
767/// Inherent implementation of [`EuvToggle`].
768impl EuvToggle {
769    /// Renders `EuvToggle` into the ambient framework-managed `Context`.
770    ///
771    /// Pressed, hovered, and themed surface colors (including paired child text foreground) are owned by a
772    /// reactive `Effect` tracking `pressed`, hover state, and the live ambient theme.
773    ///
774    /// # Arguments
775    ///
776    /// - `&Self` - Component descriptor reference.
777    ///
778    /// # Returns
779    ///
780    /// - `NodeId` - Mounted root scene node handle.
781    fn render(&self) -> NodeId {
782        let mut ctx: Context = current_context();
783        let base_tokens: ThemeTokens = self.get_tokens();
784        let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
785        let theme_palette: ThemePalette = init_tok.get_palette();
786        let geometry: ThemeGeometry = init_tok.get_geometry();
787        let pressed: Signal<bool> = self.get_pressed();
788        let hovered: Signal<bool> = Signal::new(false);
789        let init_on: bool = pressed.get_untracked();
790        let init_surface: SurfaceColors = Self::state_colors(theme_palette, init_on, false);
791        let Self { label, .. } = self;
792        let label_owned: String = label.clone();
793        let a11y_label: String = label_owned.clone();
794        let mut label_node: NodeId = NodeId::default();
795        let on_toggle = move |_: &UiEvent| {
796            let _: bool = pressed.set(!pressed.get_untracked());
797        };
798        let row_style: Style = style! {
799            Height(Length::Px(geometry.get_control_sm()))
800            JustifyContent::Center
801            AlignItems::Center
802            Padding(Edges::symmetric(0.0, SPACE_MD))
803            Background(init_surface.get_bg())
804            Border::new(geometry.get_border_width().max(0.0), init_surface.get_border())
805            Radius(geometry.get_radius_sm())
806            Overflow::Hidden
807            CursorIcon::Pointer
808        };
809        let text_style: Style = style! {
810            FontSize(13.0)
811            FontWeight::Medium
812            TextColor(init_surface.get_fg())
813            TextAlign::Center
814            TextOverflow::Ellipsis
815        };
816        let root: NodeId = view! {
817            row {
818                style: row_style
819                on_click: on_toggle
820                text {
821                    node_ref: label_node
822                    style: text_style
823                    { label_owned }
824                }
825            }
826        };
827        ctx.on_hover(root, move |ui_event: &UiEvent| match ui_event.get_kind() {
828            UiEventKind::PointerEnter => {
829                let _: bool = hovered.set_if_changed(true);
830            }
831            UiEventKind::PointerLeave => {
832                let _: bool = hovered.set_if_changed(false);
833            }
834            _ => {}
835        });
836        let handle: SceneHandle = ctx.get_scene().clone();
837        let app_state_sig: Signal<AppState> = ctx.get_app_state();
838        let prev_state: Cell<(bool, bool, ThemeKey)> = Cell::new((init_on, false, init_tok.key()));
839        let a11y_fallback: String = a11y_label.clone();
840        let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
841            let _: Effect = Effect::new(move || {
842                if app_state_sig.get() != AppState::Active {
843                    let _: bool = hovered.set_if_changed(false);
844                }
845            });
846            Effect::new(move || {
847                let is_on: bool = pressed.get();
848                let is_hovered: bool = hovered.get();
849                let tok: ThemeTokens = use_live_tokens(base_tokens);
850                let (old_on, old_h, old_key): (bool, bool, ThemeKey) =
851                    prev_state.replace((is_on, is_hovered, tok.key()));
852                if old_on == is_on && old_h == is_hovered && old_key == tok.key() {
853                    return;
854                }
855                let live_p: ThemePalette = tok.get_palette();
856                let live_g: ThemeGeometry = tok.get_geometry();
857                let surface: SurfaceColors = Self::state_colors(live_p, is_on, is_hovered);
858                let hover_surface: SurfaceColors = Self::state_colors(live_p, is_on, true);
859                handle.with_write(
860                    |tree: &mut SceneTree| {
861                        if let Some(item_count) = tree.get_node_mut(root) {
862                            let paint: &mut PaintStyle = item_count.get_mut_style().get_mut_paint();
863                            let _: &mut PaintStyle = paint
864                                .set_background(Some(surface.get_bg()))
865                                .set_border(Border::new(
866                                    live_g.get_border_width().max(0.0),
867                                    surface.get_border(),
868                                ))
869                                .set_focus_ring(None);
870                            let mut hover_paint: PaintStyle = *paint;
871                            let _: &mut PaintStyle = hover_paint
872                                .set_background(Some(hover_surface.get_bg()))
873                                .set_border(Border::new(
874                                    live_g.get_border_width().max(0.0),
875                                    hover_surface.get_border(),
876                                ));
877                            let _: &mut SceneNode = item_count.set_hover_style(Some(hover_paint));
878                            if let Some(a11y) = item_count.get_mut_a11y().as_mut() {
879                                let _: &mut A11yProps = a11y.set_checked(Some(is_on));
880                            } else {
881                                let mut a11y: A11yProps =
882                                    A11yProps::new(A11yRole::Button, a11y_fallback.clone());
883                                let _: &mut A11yProps = a11y.set_checked(Some(is_on));
884                                let _: &mut SceneNode = item_count.set_a11y(Some(a11y));
885                            }
886                            tree.mark_dirty(root, DIRTY_PAINT);
887                        }
888                        if let Some(node_entry) = tree.get_node_mut(label_node) {
889                            let _: &mut TextStyle = node_entry
890                                .get_mut_style()
891                                .get_mut_text()
892                                .set_color(surface.get_fg());
893                            tree.mark_dirty(label_node, DIRTY_PAINT);
894                        }
895                    },
896                    (),
897                );
898            })
899        });
900        ctx.get_scene().with_write(
901            |tree: &mut SceneTree| {
902                if let Some(normal_vec) = tree.get_node_mut(root) {
903                    let mut a11y: A11yProps = A11yProps::new(A11yRole::Button, a11y_label);
904                    let _: &mut A11yProps = a11y.set_checked(Some(init_on));
905                    let _: &mut SceneNode = normal_vec.set_a11y(Some(a11y));
906                    let _: &mut InteractionState =
907                        normal_vec.get_mut_interaction().set_focusable(true);
908                    let hover_s: SurfaceColors = Self::state_colors(theme_palette, init_on, true);
909                    let mut hover_paint: PaintStyle = normal_vec.style_ref().get_paint();
910                    let _: &mut PaintStyle = hover_paint
911                        .set_background(Some(hover_s.get_bg()))
912                        .set_border(Border::new(
913                            geometry.get_border_width().max(0.0),
914                            hover_s.get_border(),
915                        ));
916                    let _: &mut SceneNode = normal_vec.set_hover_style(Some(hover_paint));
917                }
918                mark_theme_owned(tree, &[root, label_node]);
919                tree.attach_scope(root, scope);
920            },
921            (),
922        );
923        root
924    }
925}