euv-ui 0.42.0

Cross-platform UI component library and 17-scheme design system for the euv framework with pixel-consistent rendering across Web, Android, iOS, iPadOS, macOS, Windows, and Linux.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use super::*;

/// Tone color resolution helper for `EuvTone`.
impl EuvTone {
    /// Resolves `(background, foreground)` colors for this tone under `palette`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Semantic tone.
    /// - `ThemePalette` - Active theme palette.
    ///
    /// # Returns
    ///
    /// - `(Color, Color)` - Background and foreground colors.
    #[must_use]
    pub fn colors(&self, palette: ThemePalette) -> (Color, Color) {
        match *self {
            Self::Neutral => (palette.get_secondary(), palette.get_secondary_foreground()),
            Self::Primary => (palette.get_primary(), palette.get_primary_foreground()),
            Self::Success => (palette.get_success(), palette.get_success_foreground()),
            Self::Warning => (palette.get_warning(), palette.get_warning_foreground()),
            Self::Destructive => (
                palette.get_destructive(),
                palette.get_destructive_foreground(),
            ),
        }
    }
}

/// Fluent tone builder for `EuvBadge`.
impl EuvBadge {
    /// Overrides the semantic tone fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source badge.
    /// - `EuvTone` - Target semantic tone.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated badge.
    #[must_use]
    pub fn tone(mut self, tone: EuvTone) -> Self {
        self.set_tone(tone);
        self
    }
}

/// Declarative `#[component]` implementation for `EuvBadge`.
#[component]
/// Inherent implementation of [`EuvBadge`].
impl EuvBadge {
    /// Renders `EuvBadge` into the ambient framework-managed `Context`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Component descriptor reference.
    ///
    /// # Returns
    ///
    /// - `NodeId` - Mounted root scene node handle.
    fn render(&self) -> NodeId {
        let p: ThemePalette = self.get_tokens().get_palette();
        let (bg, fg): (Color, Color) = self.get_tone().colors(p);
        let Self { label, .. } = self.clone();
        view! {
            row {
                height: Length::Px(24.0)
                justify: JustifyContent::Center
                align: AlignItems::Center
                padding: Edges::symmetric(0.0, 10.0)
                overflow: Overflow::Hidden
                background: bg
                border: (1.0, p.get_border().with_alpha(72))
                radius: 999.0
                text {
                    font_size: 11.0
                    font_weight: FontWeight::SemiBold
                    color: fg
                    text_align: TextAlign::Center
                    text_overflow: TextOverflow::Ellipsis
                    { label }
                }
            }
        }
    }
}

/// Declarative `#[component]` implementation for `EuvAlert`.
#[component]
/// Inherent implementation of [`EuvAlert`].
impl EuvAlert {
    /// Renders `EuvAlert` into the ambient framework-managed `Context`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Component descriptor reference.
    ///
    /// # Returns
    ///
    /// - `NodeId` - Mounted root scene node handle.
    fn render(&self) -> NodeId {
        let p: ThemePalette = self.get_tokens().get_palette();
        let g: ThemeGeometry = self.get_tokens().get_geometry();
        let (accent, _): (Color, Color) = self.get_tone().colors(p);
        let Self {
            title, description, ..
        } = self.clone();
        view! {
            column {
                width: Length::Percent(100.0)
                padding: Edges::all(SPACE_MD)
                gap: SPACE_XS
                background: p.get_muted()
                border: (g.get_border_width(), accent)
                radius: g.get_radius_md()
                overflow: Overflow::Hidden
                text {
                    width: Length::Percent(100.0)
                    font_size: 14.0
                    font_weight: FontWeight::SemiBold
                    color: p.get_foreground()
                    { title }
                }
                text {
                    width: Length::Percent(100.0)
                    font_size: 13.0
                    color: p.get_muted_foreground()
                    { description }
                }
            }
        }
    }
}

/// Fluent height configuration for `EuvProgress`.
impl EuvProgress {
    /// Sets the progress bar track height in logical pixels.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source progress bar.
    /// - `f32` - Desired track height in logical pixels.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated progress bar.
    #[must_use]
    pub fn height(mut self, height: f32) -> Self {
        self.set_height(height);
        self
    }
}

/// Declarative `#[component]` implementation for `EuvProgress`.
#[component]
/// Inherent implementation of [`EuvProgress`].
impl EuvProgress {
    /// Renders `EuvProgress` into the ambient framework-managed `Context`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Component descriptor reference.
    ///
    /// # Returns
    ///
    /// - `NodeId` - Mounted root scene node handle.
    fn render(&self) -> NodeId {
        let p: ThemePalette = self.get_tokens().get_palette();
        let h: f32 = if self.get_height() > 0.0 {
            self.get_height().max(4.0)
        } else {
            8.0
        };
        let pct: f32 = self.get_value().clamp(0.0, 1.0) * 100.0;
        view! {
            row {
                width: Length::Percent(100.0)
                height: Length::Px(h)
                background: p.get_secondary()
                radius: h * 0.5
                overflow: Overflow::Hidden
                view {
                    width: Length::Percent(pct)
                    height: Length::Percent(100.0)
                    background: p.get_primary()
                    radius: h * 0.5
                }
            }
        }
    }
}

/// Window calculation, clamped scrolling, and reactive mounting for `EuvVirtualList`.
impl EuvVirtualList {
    /// Sets the visible scroll viewport height in logical pixels fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source virtual list.
    /// - `f32` - Viewport height in logical pixels.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated virtual list.
    #[must_use]
    pub fn viewport_height(mut self, height: f32) -> Self {
        self.set_viewport_height(height);
        self
    }

    /// Sets the overscan row count mounted above and below the viewport fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source virtual list.
    /// - `usize` - Number of extra rows to mount per edge.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated virtual list.
    #[must_use]
    pub fn overscan(mut self, overscan: usize) -> Self {
        self.set_overscan(overscan);
        self
    }

    /// Resolves effective viewport height (defaulting to `320.0` when unset).
    ///
    /// # Arguments
    ///
    /// - `&Self` - Virtual list configuration.
    ///
    /// # Returns
    ///
    /// - `f32` - Resolved viewport height in logical pixels.
    #[must_use]
    fn resolved_viewport_height(&self) -> f32 {
        let h: f32 = self.get_viewport_height();
        if h > 0.0 { h } else { 320.0 }
    }

    /// Returns the maximum valid scroll Y offset in logical pixels.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Virtual list configuration.
    ///
    /// # Returns
    ///
    /// - `f32` - Upper scroll bound `>= 0.0`.
    #[must_use]
    pub fn max_scroll_y(&self) -> f32 {
        let total_h: f32 = (self.get_total_items() as f32) * self.get_item_height().max(1.0);
        (total_h - self.resolved_viewport_height().max(1.0)).max(0.0)
    }

    /// Advances the reactive scroll offset by `delta_y`, clamped to `[0.0, max_scroll_y()]`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Virtual list configuration.
    /// - `f32` - Incremental vertical scroll delta in logical pixels.
    pub fn scroll_by(&self, delta_y: f32) {
        let max_y: f32 = self.max_scroll_y();
        let next: f32 = (self.get_scroll_y().get_untracked() + delta_y).clamp(0.0, max_y);
        let _: bool = self.get_scroll_y().set_if_changed(next);
    }

    /// Computes the `(start_index, end_index_exclusive)` window of rows visible in the viewport.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Virtual list configuration.
    ///
    /// # Returns
    ///
    /// - `(usize, usize)` - Half-open range of indices to materialize.
    #[must_use]
    pub fn visible_range(&self) -> (usize, usize) {
        let total: usize = self.get_total_items();
        if total == 0 {
            return (0, 0);
        }
        let item_h: f32 = self.get_item_height().max(1.0);
        let view_h: f32 = self.resolved_viewport_height().max(1.0);
        let scroll_y: f32 = self
            .get_scroll_y()
            .get_untracked()
            .clamp(0.0, self.max_scroll_y());
        let overscan: usize = if self.get_overscan() == 0 {
            3
        } else {
            self.get_overscan()
        };
        let first_vis: usize = (scroll_y / item_h).floor() as usize;
        let vis_count: usize = (view_h / item_h).ceil() as usize;
        let start: usize = first_vis.saturating_sub(overscan);
        let end: usize = (first_vis + vis_count + overscan).min(total);
        (start, end)
    }

    /// Mounts a reactive clipped scrollable list materializing only the visible row window as `scroll_y` changes.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Virtual list configuration.
    /// - `F` - Zero-context row builder closure taking `usize` row index and returning `NodeId`.
    ///
    /// # Returns
    ///
    /// - `NodeId` - Mounted reactive scroll viewport handle.
    pub fn mount_with<F>(&self, row_builder: F) -> NodeId
    where
        F: FnMut(usize) -> NodeId + 'static,
    {
        let mut ctx: Context = current_context();
        let p: ThemePalette = self.get_tokens().get_palette();
        let g: ThemeGeometry = self.get_tokens().get_geometry();
        let list_cfg: Self = self.clone();
        let scroll_cfg: Self = self.clone();
        let scroll_sig: Signal<f32> = self.get_scroll_y();
        let view_h: f32 = self.resolved_viewport_height().max(1.0);
        let builder_cell: Rc<RefCell<F>> = Rc::new(RefCell::new(row_builder));
        let border_w: f32 = g.get_border_width();
        let inner_view_h: f32 = (view_h - border_w * 2.0).max(1.0);
        let item_h: f32 = self.get_item_height().max(1.0);
        let total_h: f32 = (self.get_total_items() as f32) * item_h;
        let max_scroll: f32 = (total_h - inner_view_h).max(0.0);
        let container: NodeId = view! {
            scroll {
                style: Style::default().column()
                width: Length::Percent(100.0)
                height: Length::Px(view_h)
                background: p.get_card()
                border: (border_w, p.get_border())
                radius: g.get_radius_md()
                stop_scroll_propagation: true
                on_scroll: move |ev: &UiEvent| {
                    if let UiEventKind::Scroll(delta) = ev.get_kind() {
                        let next: f32 = (scroll_cfg.get_scroll_y().get_untracked() + delta.get_y())
                        .clamp(0.0, max_scroll);
                        let _: bool = scroll_cfg.get_scroll_y().set_if_changed(next);
                    }
                }
            }
        };
        let init_scroll: f32 = scroll_sig.get_untracked().clamp(0.0, max_scroll);
        ctx.set_virtual_scroll(container, init_scroll, total_h);
        let range_memo: Memo<(usize, usize)> = Memo::new(move || {
            let _: f32 = scroll_sig.get();
            list_cfg.visible_range()
        });
        let range_for_scroll: Memo<(usize, usize)> = range_memo;
        let scene_for_scroll: SceneHandle = ctx.get_scene().clone();
        let scene_for_rows: SceneHandle = ctx.get_scene().clone();
        let ctx_for_rows: Context = ctx.clone();
        let (vlist_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
            let _: Effect = Effect::new(move || {
                let (start, end): (usize, usize) = range_memo.get();
                let cur_scroll: f32 = scroll_sig.get_untracked().clamp(0.0, max_scroll);
                let local_scroll_y: f32 = (cur_scroll - (start as f32) * item_h).max(0.0);
                let (child_scope, rows_host): (Scope, NodeId) = Scope::run(|_: Scope| {
                    with_context(ctx_for_rows.clone(), || {
                        let mut built: Vec<NodeId> = Vec::with_capacity(end.saturating_sub(start));
                        if let Ok(mut build_row) = builder_cell.try_borrow_mut() {
                            for idx in start..end {
                                built.push(build_row(idx));
                            }
                        }
                        view! {
                            column {
                                width: Length::Percent(100.0)
                                ..built
                            }
                        }
                    })
                });
                scene_for_rows.with_write(
                    |tree: &mut SceneTree| {
                        tree.replace_children(container, vec![rows_host]);
                        tree.attach_scope(rows_host, child_scope);
                        tree.set_virtual_scroll(container, cur_scroll, total_h);
                        tree.set_scroll_offset(container, Point::new(0.0, local_scroll_y));
                    },
                    (),
                );
            });
            Effect::new(move || {
                let cur_scroll: f32 = scroll_sig.get().clamp(0.0, max_scroll);
                let (start, _): (usize, usize) = range_for_scroll.get();
                let local_scroll_y: f32 = (cur_scroll - (start as f32) * item_h).max(0.0);
                scene_for_scroll.with_write(
                    |tree: &mut SceneTree| {
                        tree.set_virtual_scroll(container, cur_scroll, total_h);
                        tree.set_scroll_offset(container, Point::new(0.0, local_scroll_y));
                    },
                    (),
                );
            })
        });
        ctx.get_scene().with_write(
            |tree: &mut SceneTree| {
                tree.attach_scope(container, vlist_scope);
            },
            (),
        );
        container
    }
}

/// Declarative `#[component]` implementation for `EuvTable`.
#[component]
/// Inherent implementation of [`EuvTable`].
impl EuvTable {
    /// Renders `EuvTable` into the ambient framework-managed `Context`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Component descriptor reference.
    ///
    /// # Returns
    ///
    /// - `NodeId` - Mounted root scene node handle.
    fn render(&self) -> NodeId {
        let p: ThemePalette = self.get_tokens().get_palette();
        let g: ThemeGeometry = self.get_tokens().get_geometry();
        let Self { headers, rows, .. } = self.clone();
        view! {
            column {
                width: Length::Percent(100.0)
                background: p.get_card()
                border: (g.get_border_width(), p.get_border())
                radius: g.get_radius_md()
                overflow: Overflow::Hidden
                row {
                    width: Length::Percent(100.0)
                    padding: Edges::symmetric(SPACE_SM, SPACE_MD)
                    background: p.get_muted()
                    gap: SPACE_SM
                    for h in headers.into_iter() {
                        view {
                            flex_grow: 1.0
                            overflow: Overflow::Hidden
                            text {
                                font_size: 12.0
                                font_weight: FontWeight::SemiBold
                                color: p.get_muted_foreground()
                                { h }
                            }
                        }
                    }
                }
                for row_cells in rows.into_iter() {
                    row {
                        width: Length::Percent(100.0)
                        padding: Edges::symmetric(SPACE_SM, SPACE_MD)
                        border: (g.get_border_width(), p.get_border())
                        gap: SPACE_SM
                        for cell_text in row_cells.into_iter() {
                            view {
                                flex_grow: 1.0
                                overflow: Overflow::Hidden
                                text {
                                    font_size: 13.0
                                    color: p.get_foreground()
                                    { cell_text }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}