euv-ui 0.47.1

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
use super::*;

/// Child composition helpers for `EuvCard`.
impl EuvCard {
    /// Sets the card subtitle text fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source card.
    /// - `impl Into<String>` - Muted subtitle string.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated card.
    #[must_use]
    pub fn subtitle(mut self, subtitle: impl Into<String>) -> Self {
        self.set_subtitle(subtitle.into());
        self
    }

    /// Appends a child `NodeId` to the card body.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source card.
    /// - `NodeId` - Mounted child node.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated card.
    #[must_use]
    pub fn child(mut self, child: NodeId) -> Self {
        self.get_mut_children().push(child);
        self
    }

    /// Sets the interactive text selection and copy mode for text inside the card (`TextSelectMode::None` by default).
    ///
    /// # Arguments
    ///
    /// - `Self` - Source card.
    /// - `TextSelectMode` - Selection mode applied to the card subtree.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated card.
    #[must_use]
    pub fn text_select(mut self, mode: TextSelectMode) -> Self {
        self.set_text_select(Some(mode));
        self
    }

    /// Controls whether text inside the card is selectable.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source card.
    /// - `bool` - `true` to enable text selection across the card subtree.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated card.
    #[must_use]
    pub fn selectable(self, enabled: bool) -> Self {
        let mode: TextSelectMode = if enabled {
            TextSelectMode::SelectAndCopy
        } else {
            TextSelectMode::None
        };
        self.text_select(mode)
    }
}

/// Declarative `#[component]` implementation for `EuvCard`.
#[component]
/// Inherent implementation of [`EuvCard`].
impl EuvCard {
    /// Renders `EuvCard` 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 tokens: ThemeTokens = live_tokens_untracked(self.get_tokens());
        let p: ThemePalette = tokens.get_palette();
        let g: ThemeGeometry = tokens.get_geometry();
        let card_surface: SurfaceColors = p.surface(SurfaceRole::Card);
        let Self {
            title,
            subtitle,
            children,
            text_select,
            ..
        } = self;
        let mut root_st: Style = tokens.card_style();
        if let Some(mode) = *text_select {
            root_st = root_st.text_select(mode);
        }
        let _: &mut LayoutStyle = root_st.get_mut_layout().set_width(Length::Percent(100.0));
        ctx.get_scene().with_write(
            |tree: &mut SceneTree| {
                let root: NodeId = tree.create_view(root_st);
                let mut head_nodes: [NodeId; 2] = [NodeId::default(); 2];
                let mut head_len: usize = 0;
                if !title.is_empty() {
                    let mut title_st: Style = Style::default();
                    let _: &mut LayoutStyle =
                        title_st.get_mut_layout().set_width(Length::Percent(100.0));
                    let _: &mut TextStyle = title_st
                        .get_mut_text()
                        .set_font_size(g.get_font_heading())
                        .set_weight(FontWeight::SemiBold)
                        .set_color(card_surface.get_fg());
                    head_nodes[head_len] = tree.create_text(title.clone(), title_st);
                    head_len += 1;
                }
                if !subtitle.is_empty() {
                    let mut sub_st: Style = Style::default();
                    let _: &mut LayoutStyle =
                        sub_st.get_mut_layout().set_width(Length::Percent(100.0));
                    let _: &mut TextStyle = sub_st
                        .get_mut_text()
                        .set_font_size(13.0)
                        .set_color(p.get_muted_foreground());
                    head_nodes[head_len] = tree.create_text(subtitle.clone(), sub_st);
                    head_len += 1;
                }
                if head_len > 0 {
                    tree.append_children(root, &head_nodes[..head_len]);
                }
                if !children.is_empty() {
                    tree.append_children(root, children);
                }
                root
            },
            NodeId::default(),
        )
    }
}

/// Declarative `#[component]` implementation for `EuvHeader`.
#[component]
/// Inherent implementation of [`EuvHeader`].
impl EuvHeader {
    /// Renders `EuvHeader` 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 tokens: ThemeTokens = live_tokens_untracked(self.get_tokens());
        let p: ThemePalette = tokens.get_palette();
        let g: ThemeGeometry = tokens.get_geometry();
        let bg_surface: SurfaceColors = p.surface(SurfaceRole::Background);
        let Self {
            title, subtitle, ..
        } = self;
        let mut a11y: A11yProps = A11yProps::new(A11yRole::Heading, title.clone());
        let _: &mut A11yProps = a11y.set_heading_level(1);
        let mut col_st: Style = Style::default();
        let _: &mut LayoutStyle = col_st
            .get_mut_layout()
            .set_flex_direction(FlexDirection::Column)
            .set_width(Length::Percent(100.0))
            .set_row_gap(SPACE_XS)
            .set_column_gap(SPACE_XS)
            .set_padding(Edges::new(0.0, 0.0, SPACE_MD, 0.0));
        let mut row_st: Style = Style::default();
        let _: &mut LayoutStyle = row_st
            .get_mut_layout()
            .set_flex_direction(FlexDirection::Row)
            .set_width(Length::Percent(100.0))
            .set_align_items(AlignItems::Center)
            .set_row_gap(SPACE_SM)
            .set_column_gap(SPACE_SM);
        let mut accent_st: Style = Style::default();
        let _: &mut LayoutStyle = accent_st
            .get_mut_layout()
            .set_width(Length::Px(4.0))
            .set_height(Length::Px(24.0))
            .set_flex_shrink(0.0);
        let _: &mut PaintStyle = accent_st
            .get_mut_paint()
            .set_radius(CornerRadius::all(RADIUS_PILL))
            .set_background(Some(p.get_primary()));
        let mut title_st: Style = Style::default();
        let _: &mut LayoutStyle = title_st
            .get_mut_layout()
            .set_flex_grow(1.0)
            .set_flex_shrink(1.0)
            .set_min_width(Length::Px(0.0));
        let _: &mut TextStyle = title_st
            .get_mut_text()
            .set_font_size(g.get_font_title())
            .set_weight(FontWeight::Bold)
            .set_color(bg_surface.get_fg());
        ctx.get_scene().with_write(
            |tree: &mut SceneTree| {
                let root: NodeId = tree.create_view(col_st);
                let header_row: NodeId = tree.create_view(row_st);
                let accent: NodeId = tree.create_view(accent_st);
                let title_id: NodeId = tree.create_text(title.clone(), title_st);
                if let Some(node) = tree.get_node_mut(title_id) {
                    let _: &mut SceneNode = node.set_a11y(Some(a11y));
                }
                tree.append_children(header_row, &[accent, title_id]);
                if !subtitle.is_empty() {
                    let mut sub_st: Style = Style::default();
                    let _: &mut LayoutStyle =
                        sub_st.get_mut_layout().set_width(Length::Percent(100.0));
                    let _: &mut TextStyle = sub_st
                        .get_mut_text()
                        .set_font_size(g.get_font_body())
                        .set_color(p.get_muted_foreground());
                    let sub_id: NodeId = tree.create_text(subtitle.clone(), sub_st);
                    tree.append_children(root, &[header_row, sub_id]);
                } else {
                    tree.append_child(root, header_row);
                }
                root
            },
            NodeId::default(),
        )
    }
}

/// Palette helpers for `EuvStatCard`.
impl EuvStatCard {
    /// Resolves contrast-safe `(badge_bg, badge_fg)` colors for the stat card caption pill.
    ///
    /// # Arguments
    ///
    /// - `ThemePalette` - Live theme palette.
    ///
    /// # Returns
    ///
    /// - `(Color, Color)` - Translucent tint background and `>= 4.5:1` readable foreground over `card`.
    #[must_use]
    pub fn caption_colors(palette: ThemePalette) -> (Color, Color) {
        let cap_bg: Color = palette.get_primary().with_alpha(24);
        let composited: Color = blend_over(cap_bg, palette.get_card());
        let cap_fg: Color = palette.readable_foreground_with(palette.get_primary(), composited);
        (cap_bg, cap_fg)
    }
}

/// Declarative `#[component]` implementation for `EuvStatCard`.
#[component]
/// Inherent implementation of [`EuvStatCard`].
impl EuvStatCard {
    /// Renders `EuvStatCard` 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 p: ThemePalette = init_tok.get_palette();
        let g: ThemeGeometry = init_tok.get_geometry();
        let card_surface: SurfaceColors = p.surface(SurfaceRole::Card);
        let Self {
            label,
            value,
            caption,
            ..
        } = self;
        let mut root_st: Style = init_tok.card_style();
        let _: &mut LayoutStyle = root_st
            .get_mut_layout()
            .set_width(Length::Percent(100.0))
            .set_row_gap(SPACE_SM)
            .set_column_gap(SPACE_SM);
        let mut head_row_st: Style = Style::default();
        let _: &mut LayoutStyle = head_row_st
            .get_mut_layout()
            .set_flex_direction(FlexDirection::Row)
            .set_width(Length::Percent(100.0))
            .set_justify_content(JustifyContent::SpaceBetween)
            .set_align_items(AlignItems::Center)
            .set_column_gap(SPACE_SM);
        let mut lbl_st: Style = Style::default();
        let _: &mut LayoutStyle = lbl_st
            .get_mut_layout()
            .set_flex_shrink(1.0)
            .set_min_width(Length::Px(0.0));
        let _: &mut TextStyle = lbl_st
            .get_mut_text()
            .set_font_size(12.0)
            .set_weight(FontWeight::SemiBold)
            .set_color(p.get_muted_foreground());
        let mut bar_st: Style = Style::default();
        let _: &mut LayoutStyle = bar_st
            .get_mut_layout()
            .set_width(Length::Px(24.0))
            .set_height(Length::Px(4.0))
            .set_flex_shrink(0.0);
        let _: &mut PaintStyle = bar_st
            .get_mut_paint()
            .set_radius(CornerRadius::all(RADIUS_PILL))
            .set_background(Some(p.get_primary()));
        let mut val_st: Style = Style::default();
        let _: &mut LayoutStyle = val_st.get_mut_layout().set_width(Length::Percent(100.0));
        let _: &mut TextStyle = val_st
            .get_mut_text()
            .set_font_size(26.0)
            .set_weight(FontWeight::Bold)
            .set_color(card_surface.get_fg());
        let (root, cap_nodes): (NodeId, Option<(NodeId, NodeId)>) = ctx.get_scene().with_write(
            |tree: &mut SceneTree| {
                let root: NodeId = tree.create_view(root_st);
                let head_row: NodeId = tree.create_view(head_row_st);
                let lbl_id: NodeId = tree.create_text(label.clone(), lbl_st);
                let bar_id: NodeId = tree.create_view(bar_st);
                tree.append_children(head_row, &[lbl_id, bar_id]);
                let val_id: NodeId = tree.create_text(value.clone(), val_st);
                if !caption.is_empty() {
                    let (cap_bg, cap_fg): (Color, Color) = Self::caption_colors(p);
                    let mut cap_row_st: Style = Style::default();
                    let _: &mut LayoutStyle = cap_row_st
                        .get_mut_layout()
                        .set_flex_direction(FlexDirection::Row)
                        .set_max_width(Length::Percent(100.0))
                        .set_padding(Edges::symmetric(SPACE_XS, 10.0))
                        .set_align_items(AlignItems::Center)
                        .set_overflow(Overflow::Hidden);
                    let _: &mut PaintStyle = cap_row_st
                        .get_mut_paint()
                        .set_radius(CornerRadius::all(g.get_radius_sm().max(0.0)))
                        .set_background(Some(cap_bg));
                    let mut cap_txt_st: Style = Style::default();
                    let _: &mut LayoutStyle = cap_txt_st
                        .get_mut_layout()
                        .set_max_width(Length::Percent(100.0))
                        .set_flex_shrink(1.0)
                        .set_min_width(Length::Px(0.0));
                    let _: &mut TextStyle = cap_txt_st
                        .get_mut_text()
                        .set_font_size(11.0)
                        .set_weight(FontWeight::SemiBold)
                        .set_color(cap_fg);
                    let cap_row: NodeId = tree.create_view(cap_row_st);
                    let cap_txt: NodeId = tree.create_text(caption.clone(), cap_txt_st);
                    tree.append_child(cap_row, cap_txt);
                    tree.append_children(root, &[head_row, val_id, cap_row]);
                    (root, Some((cap_row, cap_txt)))
                } else {
                    tree.append_children(root, &[head_row, val_id]);
                    (root, None)
                }
            },
            (NodeId::default(), None),
        );
        if let Some((cap_row, cap_txt)) = cap_nodes {
            let handle: SceneHandle = ctx.get_scene().clone();
            let prev_key: Cell<ThemeKey> = Cell::new(init_tok.key());
            let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
                Effect::new(move || {
                    let tok: ThemeTokens = use_live_tokens(base_tokens);
                    let old_key: ThemeKey = prev_key.replace(tok.key());
                    if old_key == tok.key() {
                        return;
                    }
                    let (cap_bg, cap_fg): (Color, Color) = Self::caption_colors(tok.get_palette());
                    handle.with_write(
                        |tree: &mut SceneTree| {
                            if let Some(n) = tree.get_node_mut(cap_row) {
                                let _: &mut PaintStyle = n
                                    .get_mut_style()
                                    .get_mut_paint()
                                    .set_background(Some(cap_bg));
                                tree.mark_dirty(cap_row, DIRTY_PAINT);
                            }
                            if let Some(n) = tree.get_node_mut(cap_txt) {
                                let _: &mut TextStyle =
                                    n.get_mut_style().get_mut_text().set_color(cap_fg);
                                tree.mark_dirty(cap_txt, DIRTY_PAINT);
                            }
                        },
                        (),
                    );
                })
            });
            ctx.get_scene().with_write(
                |tree: &mut SceneTree| {
                    mark_theme_owned(tree, &[cap_row, cap_txt]);
                    tree.attach_scope(root, scope);
                },
                (),
            );
        }
        root
    }
}