Skip to main content

euv_ui/component/card/
impl.rs

1use super::*;
2
3/// Child composition helpers for `EuvCard`.
4impl EuvCard {
5    /// Sets the card subtitle text fluently.
6    ///
7    /// # Arguments
8    ///
9    /// - `Self` - Source card.
10    /// - `impl Into<String>` - Muted subtitle string.
11    ///
12    /// # Returns
13    ///
14    /// - `Self` - Updated card.
15    #[must_use]
16    pub fn subtitle(mut self, subtitle: impl Into<String>) -> Self {
17        self.set_subtitle(subtitle.into());
18        self
19    }
20
21    /// Appends a child `NodeId` to the card body.
22    ///
23    /// # Arguments
24    ///
25    /// - `Self` - Source card.
26    /// - `NodeId` - Mounted child node.
27    ///
28    /// # Returns
29    ///
30    /// - `Self` - Updated card.
31    #[must_use]
32    pub fn child(mut self, child: NodeId) -> Self {
33        self.get_mut_children().push(child);
34        self
35    }
36}
37
38/// Declarative `#[component]` implementation for `EuvCard`.
39#[component]
40/// Inherent implementation of [`EuvCard`].
41impl EuvCard {
42    /// Renders `EuvCard` into the ambient framework-managed `Context`.
43    ///
44    /// # Arguments
45    ///
46    /// - `&Self` - Component descriptor reference.
47    ///
48    /// # Returns
49    ///
50    /// - `NodeId` - Mounted root scene node handle.
51    fn render(&self) -> NodeId {
52        let ctx: Context = current_context();
53        let tokens: ThemeTokens = live_tokens_untracked(self.get_tokens());
54        let theme_palette: ThemePalette = tokens.get_palette();
55        let geometry: ThemeGeometry = tokens.get_geometry();
56        let card_surface: SurfaceColors = theme_palette.surface(SurfaceRole::Card);
57        let Self {
58            title,
59            subtitle,
60            children,
61            style,
62            ..
63        } = self;
64        let mut root_st: Style = tokens.card_style();
65        let _: &mut LayoutStyle = root_st.get_mut_layout().set_width(Length::Percent(100.0));
66        root_st = root_st.with_overrides(*style);
67        ctx.get_scene().with_write(
68            |tree: &mut SceneTree| {
69                let root: NodeId = tree.create_view(root_st);
70                let mut head_nodes: [NodeId; 2] = [NodeId::default(); 2];
71                let mut head_len: usize = 0;
72                if !title.is_empty() {
73                    let mut title_st: Style = Style::default();
74                    let _: &mut LayoutStyle =
75                        title_st.get_mut_layout().set_width(Length::Percent(100.0));
76                    let _: &mut TextStyle = title_st
77                        .get_mut_text()
78                        .set_font_size(geometry.get_font_heading())
79                        .set_weight(FontWeight::SemiBold)
80                        .set_color(card_surface.get_fg());
81                    head_nodes[head_len] = tree.create_text(title.clone(), title_st);
82                    head_len += 1;
83                }
84                if !subtitle.is_empty() {
85                    let mut sub_st: Style = Style::default();
86                    let _: &mut LayoutStyle =
87                        sub_st.get_mut_layout().set_width(Length::Percent(100.0));
88                    let _: &mut TextStyle = sub_st
89                        .get_mut_text()
90                        .set_font_size(13.0)
91                        .set_color(theme_palette.get_muted_foreground());
92                    head_nodes[head_len] = tree.create_text(subtitle.clone(), sub_st);
93                    head_len += 1;
94                }
95                if head_len > 0 {
96                    tree.append_children(root, &head_nodes[..head_len]);
97                }
98                if !children.is_empty() {
99                    tree.append_children(root, children);
100                }
101                root
102            },
103            NodeId::default(),
104        )
105    }
106}
107
108/// Declarative `#[component]` implementation for `EuvHeader`.
109#[component]
110/// Inherent implementation of [`EuvHeader`].
111impl EuvHeader {
112    /// Renders `EuvHeader` into the ambient framework-managed `Context`.
113    ///
114    /// # Arguments
115    ///
116    /// - `&Self` - Component descriptor reference.
117    ///
118    /// # Returns
119    ///
120    /// - `NodeId` - Mounted root scene node handle.
121    fn render(&self) -> NodeId {
122        let ctx: Context = current_context();
123        let tokens: ThemeTokens = live_tokens_untracked(self.get_tokens());
124        let theme_palette: ThemePalette = tokens.get_palette();
125        let geometry: ThemeGeometry = tokens.get_geometry();
126        let bg_surface: SurfaceColors = theme_palette.surface(SurfaceRole::Background);
127        let Self {
128            title, subtitle, ..
129        } = self;
130        let mut a11y: A11yProps = A11yProps::new(A11yRole::Heading, title.clone());
131        let _: &mut A11yProps = a11y.set_heading_level(1);
132        let mut col_st: Style = Style::default();
133        let _: &mut LayoutStyle = col_st
134            .get_mut_layout()
135            .set_flex_direction(FlexDirection::Column)
136            .set_width(Length::Percent(100.0))
137            .set_min_width(Length::Px(0.0))
138            .set_row_gap(SPACE_XS)
139            .set_column_gap(SPACE_XS)
140            .set_padding(Edges::new(0.0, 0.0, SPACE_MD, 0.0));
141        let mut row_st: Style = Style::default();
142        let _: &mut LayoutStyle = row_st
143            .get_mut_layout()
144            .set_flex_direction(FlexDirection::Row)
145            .set_width(Length::Percent(100.0))
146            .set_min_width(Length::Px(0.0))
147            .set_align_items(AlignItems::Center)
148            .set_row_gap(SPACE_SM)
149            .set_column_gap(SPACE_SM);
150        let mut accent_st: Style = Style::default();
151        let _: &mut LayoutStyle = accent_st
152            .get_mut_layout()
153            .set_width(Length::Px(4.0))
154            .set_height(Length::Px(24.0))
155            .set_flex_shrink(0.0);
156        let _: &mut PaintStyle = accent_st
157            .get_mut_paint()
158            .set_radius(CornerRadius::all(RADIUS_PILL))
159            .set_background(Some(theme_palette.get_primary()));
160        let mut title_st: Style = Style::default();
161        let _: &mut LayoutStyle = title_st
162            .get_mut_layout()
163            .set_flex_grow(1.0)
164            .set_flex_shrink(1.0)
165            .set_min_width(Length::Px(0.0));
166        let _: &mut TextStyle = title_st
167            .get_mut_text()
168            .set_font_size(geometry.get_font_title())
169            .set_weight(FontWeight::Bold)
170            .set_overflow(TextOverflow::Wrap)
171            .set_color(bg_surface.get_fg());
172        ctx.get_scene().with_write(
173            |tree: &mut SceneTree| {
174                let root: NodeId = tree.create_view(col_st);
175                let header_row: NodeId = tree.create_view(row_st);
176                let accent: NodeId = tree.create_view(accent_st);
177                let title_id: NodeId = tree.create_text(title.clone(), title_st);
178                if let Some(node) = tree.get_node_mut(title_id) {
179                    let _: &mut SceneNode = node.set_a11y(Some(a11y));
180                }
181                tree.append_children(header_row, &[accent, title_id]);
182                if !subtitle.is_empty() {
183                    let mut sub_st: Style = Style::default();
184                    let _: &mut LayoutStyle =
185                        sub_st.get_mut_layout().set_width(Length::Percent(100.0));
186                    let _: &mut TextStyle = sub_st
187                        .get_mut_text()
188                        .set_font_size(geometry.get_font_body())
189                        .set_color(theme_palette.get_muted_foreground());
190                    let sub_id: NodeId = tree.create_text(subtitle.clone(), sub_st);
191                    tree.append_children(root, &[header_row, sub_id]);
192                } else {
193                    tree.append_child(root, header_row);
194                }
195                root
196            },
197            NodeId::default(),
198        )
199    }
200}
201
202/// Palette helpers for `EuvStatCard`.
203impl EuvStatCard {
204    /// Resolves contrast-safe `(badge_bg, badge_fg)` colors for the stat card caption pill.
205    ///
206    /// # Arguments
207    ///
208    /// - `ThemePalette` - Live theme palette.
209    ///
210    /// # Returns
211    ///
212    /// - `(Color, Color)` - Translucent tint background and `>= 4.5:1` readable foreground over `card`.
213    #[must_use]
214    pub fn caption_colors(palette: ThemePalette) -> (Color, Color) {
215        let cap_bg: Color = palette.get_primary().with_alpha(24);
216        let composited: Color = blend_over(cap_bg, palette.get_card());
217        let cap_fg: Color = palette.readable_foreground_with(palette.get_primary(), composited);
218        (cap_bg, cap_fg)
219    }
220}
221
222/// Declarative `#[component]` implementation for `EuvStatCard`.
223#[component]
224/// Inherent implementation of [`EuvStatCard`].
225impl EuvStatCard {
226    /// Renders `EuvStatCard` into the ambient framework-managed `Context`.
227    ///
228    /// # Arguments
229    ///
230    /// - `&Self` - Component descriptor reference.
231    ///
232    /// # Returns
233    ///
234    /// - `NodeId` - Mounted root scene node handle.
235    fn render(&self) -> NodeId {
236        let ctx: Context = current_context();
237        let base_tokens: ThemeTokens = self.get_tokens();
238        let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
239        let theme_palette: ThemePalette = init_tok.get_palette();
240        let geometry: ThemeGeometry = init_tok.get_geometry();
241        let card_surface: SurfaceColors = theme_palette.surface(SurfaceRole::Card);
242        let Self {
243            label,
244            value,
245            caption,
246            ..
247        } = self;
248        let mut root_st: Style = init_tok.card_style();
249        let _: &mut LayoutStyle = root_st
250            .get_mut_layout()
251            .set_width(Length::Percent(100.0))
252            .set_row_gap(SPACE_SM)
253            .set_column_gap(SPACE_SM);
254        let mut head_row_st: Style = Style::default();
255        let _: &mut LayoutStyle = head_row_st
256            .get_mut_layout()
257            .set_flex_direction(FlexDirection::Row)
258            .set_width(Length::Percent(100.0))
259            .set_justify_content(JustifyContent::SpaceBetween)
260            .set_align_items(AlignItems::Center)
261            .set_column_gap(SPACE_SM);
262        let mut lbl_st: Style = Style::default();
263        let _: &mut LayoutStyle = lbl_st
264            .get_mut_layout()
265            .set_flex_shrink(1.0)
266            .set_min_width(Length::Px(0.0));
267        let _: &mut TextStyle = lbl_st
268            .get_mut_text()
269            .set_font_size(12.0)
270            .set_weight(FontWeight::SemiBold)
271            .set_color(theme_palette.get_muted_foreground());
272        let mut bar_st: Style = Style::default();
273        let _: &mut LayoutStyle = bar_st
274            .get_mut_layout()
275            .set_width(Length::Px(24.0))
276            .set_height(Length::Px(4.0))
277            .set_flex_shrink(0.0);
278        let _: &mut PaintStyle = bar_st
279            .get_mut_paint()
280            .set_radius(CornerRadius::all(RADIUS_PILL))
281            .set_background(Some(theme_palette.get_primary()));
282        let mut val_st: Style = Style::default();
283        let _: &mut LayoutStyle = val_st.get_mut_layout().set_width(Length::Percent(100.0));
284        let _: &mut TextStyle = val_st
285            .get_mut_text()
286            .set_font_size(26.0)
287            .set_weight(FontWeight::Bold)
288            .set_color(card_surface.get_fg());
289        let (root, cap_nodes): (NodeId, Option<(NodeId, NodeId)>) = ctx.get_scene().with_write(
290            |tree: &mut SceneTree| {
291                let root: NodeId = tree.create_view(root_st);
292                let head_row: NodeId = tree.create_view(head_row_st);
293                let lbl_id: NodeId = tree.create_text(label.clone(), lbl_st);
294                let bar_id: NodeId = tree.create_view(bar_st);
295                tree.append_children(head_row, &[lbl_id, bar_id]);
296                let val_id: NodeId = tree.create_text(value.clone(), val_st);
297                if !caption.is_empty() {
298                    let (cap_bg, cap_fg): (Color, Color) = Self::caption_colors(theme_palette);
299                    let mut cap_row_st: Style = Style::default();
300                    let _: &mut LayoutStyle = cap_row_st
301                        .get_mut_layout()
302                        .set_flex_direction(FlexDirection::Row)
303                        .set_max_width(Length::Percent(100.0))
304                        .set_padding(Edges::symmetric(SPACE_XS, 10.0))
305                        .set_align_items(AlignItems::Center)
306                        .set_overflow(Overflow::Hidden);
307                    let _: &mut PaintStyle = cap_row_st
308                        .get_mut_paint()
309                        .set_radius(CornerRadius::all(geometry.get_radius_sm().max(0.0)))
310                        .set_background(Some(cap_bg));
311                    let mut cap_txt_st: Style = Style::default();
312                    let _: &mut LayoutStyle = cap_txt_st
313                        .get_mut_layout()
314                        .set_max_width(Length::Percent(100.0))
315                        .set_flex_shrink(1.0)
316                        .set_min_width(Length::Px(0.0));
317                    let _: &mut TextStyle = cap_txt_st
318                        .get_mut_text()
319                        .set_font_size(11.0)
320                        .set_weight(FontWeight::SemiBold)
321                        .set_color(cap_fg);
322                    let cap_row: NodeId = tree.create_view(cap_row_st);
323                    let cap_txt: NodeId = tree.create_text(caption.clone(), cap_txt_st);
324                    tree.append_child(cap_row, cap_txt);
325                    tree.append_children(root, &[head_row, val_id, cap_row]);
326                    (root, Some((cap_row, cap_txt)))
327                } else {
328                    tree.append_children(root, &[head_row, val_id]);
329                    (root, None)
330                }
331            },
332            (NodeId::default(), None),
333        );
334        if let Some((cap_row, cap_txt)) = cap_nodes {
335            let handle: SceneHandle = ctx.get_scene().clone();
336            let prev_key: Cell<ThemeKey> = Cell::new(init_tok.key());
337            let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
338                Effect::new(move || {
339                    let tok: ThemeTokens = use_live_tokens(base_tokens);
340                    let old_key: ThemeKey = prev_key.replace(tok.key());
341                    if old_key == tok.key() {
342                        return;
343                    }
344                    let (cap_bg, cap_fg): (Color, Color) = Self::caption_colors(tok.get_palette());
345                    handle.with_write(
346                        |tree: &mut SceneTree| {
347                            if let Some(item_count) = tree.get_node_mut(cap_row) {
348                                let _: &mut PaintStyle = item_count
349                                    .get_mut_style()
350                                    .get_mut_paint()
351                                    .set_background(Some(cap_bg));
352                                tree.mark_dirty(cap_row, DIRTY_PAINT);
353                            }
354                            if let Some(node_entry) = tree.get_node_mut(cap_txt) {
355                                let _: &mut TextStyle =
356                                    node_entry.get_mut_style().get_mut_text().set_color(cap_fg);
357                                tree.mark_dirty(cap_txt, DIRTY_PAINT);
358                            }
359                        },
360                        (),
361                    );
362                })
363            });
364            ctx.get_scene().with_write(
365                |tree: &mut SceneTree| {
366                    mark_theme_owned(tree, &[cap_row, cap_txt]);
367                    tree.attach_scope(root, scope);
368                },
369                (),
370            );
371        }
372        root
373    }
374}