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