euv-ui 0.48.8

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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
use super::*;

/// Styling and click handler helpers for `EuvButton`.
impl EuvButton {
    /// Attaches a click callback invoked when the button is activated.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source button.
    /// - `F` - Click callback closure.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated button.
    #[must_use]
    pub fn on_click<F>(mut self, callback: F) -> Self
    where
        F: FnMut(&UiEvent) + 'static,
    {
        self.set_on_click(Some(Rc::new(RefCell::new(callback))));
        self
    }

    /// Sets the visual emphasis variant fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source button.
    /// - `EuvButtonVariant` - Visual variant.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated button.
    #[must_use]
    pub fn variant(mut self, variant: EuvButtonVariant) -> Self {
        self.set_variant(variant);
        self
    }

    /// Sets the control size tier fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source button.
    /// - `EuvButtonSize` - Unified control size.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated button.
    #[must_use]
    pub fn size(mut self, size: EuvButtonSize) -> Self {
        self.set_size(size);
        self
    }

    /// Applies the `Secondary` button variant fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source button.
    ///
    /// # Returns
    ///
    /// - `Self` - Secondary button.
    #[must_use]
    pub fn secondary(self) -> Self {
        self.variant(EuvButtonVariant::Secondary)
    }

    /// Applies the `Outline` button variant fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source button.
    ///
    /// # Returns
    ///
    /// - `Self` - Outline button.
    #[must_use]
    pub fn outline(self) -> Self {
        self.variant(EuvButtonVariant::Outline)
    }

    /// Applies the `Ghost` button variant fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source button.
    ///
    /// # Returns
    ///
    /// - `Self` - Ghost button.
    #[must_use]
    pub fn ghost(self) -> Self {
        self.variant(EuvButtonVariant::Ghost)
    }

    /// Applies the `Destructive` button variant fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source button.
    ///
    /// # Returns
    ///
    /// - `Self` - Destructive button.
    #[must_use]
    pub fn destructive(self) -> Self {
        self.variant(EuvButtonVariant::Destructive)
    }

    /// Applies the `Link` button variant fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source button.
    ///
    /// # Returns
    ///
    /// - `Self` - Link button.
    #[must_use]
    pub fn link(self) -> Self {
        self.variant(EuvButtonVariant::Link)
    }

    /// Applies the `Small` unified control height fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source button.
    ///
    /// # Returns
    ///
    /// - `Self` - Small button.
    #[must_use]
    pub fn sm(self) -> Self {
        self.size(EuvButtonSize::Small)
    }

    /// Applies the `Large` unified control height fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source button.
    ///
    /// # Returns
    ///
    /// - `Self` - Large button.
    #[must_use]
    pub fn lg(self) -> Self {
        self.size(EuvButtonSize::Large)
    }

    /// Applies the square `Icon` control preset fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source button.
    ///
    /// # Returns
    ///
    /// - `Self` - Icon button.
    #[must_use]
    pub fn icon(self) -> Self {
        self.size(EuvButtonSize::Icon)
    }

    /// Sets the disabled interaction state fluently.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source button.
    /// - `bool` - Whether the button is disabled.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated button.
    #[must_use]
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.set_disabled(disabled);
        self
    }

    /// Enables or disables automatic flex stretching (`flex_grow: 1.0`) inside horizontal rows.
    ///
    /// # Arguments
    ///
    /// - `Self` - Source button.
    /// - `bool` - Whether the button stretches to fill remaining row width.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated button.
    #[must_use]
    pub fn fill(mut self, enabled: bool) -> Self {
        self.set_fill(enabled);
        self
    }

    /// Resolves `(background, foreground, border_color)` for a button variant and interaction state using contrast-safe `SurfaceColors`.
    ///
    /// # Arguments
    ///
    /// - `EuvButtonVariant` - Visual emphasis variant.
    /// - `ThemePalette` - Live theme palette.
    /// - `bool` - Whether the button is hovered.
    /// - `bool` - Whether the button is actively pressed.
    ///
    /// # Returns
    ///
    /// - `(Option<Color>, Color, Option<Color>)` - Paired `(background, foreground, border_color)`.
    #[must_use]
    pub fn state_colors(
        variant: EuvButtonVariant,
        palette: ThemePalette,
        hovered: bool,
        pressed: bool,
    ) -> (Option<Color>, Color, Option<Color>) {
        if pressed {
            match variant {
                EuvButtonVariant::Primary => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Primary);
                    let bg: Color = blend_over(s.get_fg().with_alpha(44), s.get_bg());
                    let fg: Color = palette.readable_foreground_with(s.get_fg(), bg);
                    (Some(bg), fg, None)
                }
                EuvButtonVariant::Secondary => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Muted);
                    let bg: Color = blend_over(s.get_fg().with_alpha(28), s.get_bg());
                    let fg: Color = palette.readable_foreground_with(s.get_fg(), bg);
                    (Some(bg), fg, None)
                }
                EuvButtonVariant::Destructive => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Destructive);
                    let bg: Color = blend_over(s.get_fg().with_alpha(44), s.get_bg());
                    let fg: Color = palette.readable_foreground_with(s.get_fg(), bg);
                    (Some(bg), fg, None)
                }
                EuvButtonVariant::Outline => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Muted);
                    let bg: Color = blend_over(s.get_fg().with_alpha(28), s.get_bg());
                    let fg: Color = palette.readable_foreground_with(s.get_fg(), bg);
                    (Some(bg), fg, Some(s.get_border()))
                }
                EuvButtonVariant::Ghost => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Muted);
                    let bg: Color = blend_over(s.get_fg().with_alpha(28), s.get_bg());
                    let fg: Color = palette.readable_foreground_with(s.get_fg(), bg);
                    (Some(bg), fg, None)
                }
                EuvButtonVariant::Link => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Muted);
                    let bg: Color = blend_over(s.get_fg().with_alpha(20), s.get_bg());
                    let opaque_bg: Color = blend_over(bg, palette.get_background());
                    let fg: Color =
                        palette.readable_foreground_with(palette.get_primary(), opaque_bg);
                    (Some(bg), fg, None)
                }
            }
        } else if hovered {
            match variant {
                EuvButtonVariant::Primary => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Primary);
                    let bg: Color = blend_over(s.get_fg().with_alpha(24), s.get_bg());
                    let fg: Color = palette.readable_foreground_with(s.get_fg(), bg);
                    (Some(bg), fg, None)
                }
                EuvButtonVariant::Secondary => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Accent);
                    (Some(s.get_bg()), s.get_fg(), None)
                }
                EuvButtonVariant::Destructive => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Destructive);
                    let bg: Color = blend_over(s.get_fg().with_alpha(24), s.get_bg());
                    let fg: Color = palette.readable_foreground_with(s.get_fg(), bg);
                    (Some(bg), fg, None)
                }
                EuvButtonVariant::Outline => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Accent);
                    (Some(s.get_bg()), s.get_fg(), Some(s.get_border()))
                }
                EuvButtonVariant::Ghost => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Accent);
                    (Some(s.get_bg()), s.get_fg(), None)
                }
                EuvButtonVariant::Link => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Muted);
                    let opaque_bg: Color = blend_over(s.get_bg(), palette.get_background());
                    let fg: Color =
                        palette.readable_foreground_with(palette.get_primary(), opaque_bg);
                    (Some(s.get_bg()), fg, None)
                }
            }
        } else {
            match variant {
                EuvButtonVariant::Primary => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Primary);
                    (Some(s.get_bg()), s.get_fg(), None)
                }
                EuvButtonVariant::Secondary => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Secondary);
                    (Some(s.get_bg()), s.get_fg(), None)
                }
                EuvButtonVariant::Destructive => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Destructive);
                    (Some(s.get_bg()), s.get_fg(), None)
                }
                EuvButtonVariant::Outline => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Card);
                    (Some(s.get_bg()), s.get_fg(), Some(s.get_border()))
                }
                EuvButtonVariant::Ghost => {
                    let s: SurfaceColors = palette.surface(SurfaceRole::Background);
                    (None, s.get_fg(), None)
                }
                EuvButtonVariant::Link => {
                    let fg: Color = palette
                        .readable_foreground_with(palette.get_primary(), palette.get_background());
                    (None, fg, None)
                }
            }
        }
    }

    /// Writes variant and interaction-state paint onto `paint` and returns the paired text color.
    ///
    /// # Arguments
    ///
    /// - `&mut PaintStyle` - Target button paint style.
    /// - `EuvButtonVariant` - Visual emphasis variant.
    /// - `ThemeTokens` - Live theme tokens.
    /// - `bool` - Whether the button is hovered.
    /// - `bool` - Whether the button is actively pressed.
    ///
    /// # Returns
    ///
    /// - `Color` - Contrast-paired foreground text color.
    pub fn apply_state_paint(
        paint: &mut PaintStyle,
        variant: EuvButtonVariant,
        tokens: ThemeTokens,
        hovered: bool,
        pressed: bool,
    ) -> Color {
        let p: ThemePalette = tokens.get_palette();
        let g: ThemeGeometry = tokens.get_geometry();
        let (bg, fg, border_col): (Option<Color>, Color, Option<Color>) =
            Self::state_colors(variant, p, hovered, pressed);
        let has_shadow: bool = matches!(
            variant,
            EuvButtonVariant::Primary | EuvButtonVariant::Destructive | EuvButtonVariant::Outline
        );
        let border: Border = match border_col {
            Some(col) => Border::new(g.get_border_width().max(0.0), col),
            None => Border::default(),
        };
        let _: &mut PaintStyle = paint
            .set_background(bg)
            .set_border(border)
            .set_shadow(if has_shadow {
                Some(g.get_shadow_xs())
            } else {
                None
            })
            .set_focus_ring(None);
        fg
    }

    /// Resolves the deterministic compiled `Style` for this button under `tokens`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Button configuration.
    /// - `ThemeTokens` - Active theme tokens.
    ///
    /// # Returns
    ///
    /// - `Style` - Compiled cross-platform style.
    #[must_use]
    pub fn resolved_style_with_tokens(&self, tokens: ThemeTokens) -> Style {
        let g: ThemeGeometry = tokens.get_geometry();
        let size: EuvButtonSize = self.get_size();
        let disabled: bool = self.get_disabled();
        let variant: EuvButtonVariant = self.get_variant();
        let (height, pad_x, font_sz, radius): (f32, f32, f32, f32) = match size {
            EuvButtonSize::Small => (g.get_control_sm(), SPACE_SM, 12.0, g.get_radius_sm()),
            EuvButtonSize::Default => (g.get_control_md(), SPACE_MD, 13.0, g.get_radius_md()),
            EuvButtonSize::Large => (g.get_control_lg(), SPACE_XL, 14.0, g.get_radius_md()),
            EuvButtonSize::Icon => (g.get_control_md(), 0.0, 13.0, RADIUS_PILL),
        };
        let mut style: Style = Style::default();
        let layout: &mut LayoutStyle = style.get_mut_layout();
        let _: &mut LayoutStyle = layout
            .set_flex_direction(FlexDirection::Row)
            .set_justify_content(JustifyContent::Center)
            .set_align_items(AlignItems::Center)
            .set_height(Length::Px(height))
            .set_padding(Edges::symmetric(0.0, pad_x))
            .set_overflow(Overflow::Hidden);
        if size == EuvButtonSize::Icon {
            let _: &mut LayoutStyle = layout.set_width(Length::Px(height));
        } else if self.get_fill() {
            let _: &mut LayoutStyle = layout.set_flex_grow(1.0).set_flex_shrink(1.0);
        }
        let paint: &mut PaintStyle = style.get_mut_paint();
        let _: &mut PaintStyle = paint
            .set_radius(CornerRadius::all(radius.max(0.0)))
            .set_cursor(if disabled {
                CursorIcon::NotAllowed
            } else {
                CursorIcon::Pointer
            });
        let text_col: Color = Self::apply_state_paint(paint, variant, tokens, false, false);
        if disabled {
            let _: &mut PaintStyle = paint.set_opacity(0.5);
        }
        let _: &mut TextStyle = style
            .get_mut_text()
            .set_font_size(font_sz)
            .set_weight(FontWeight::Medium)
            .set_color(text_col);
        style.with_overrides(self.get_style())
    }

    /// Resolves the deterministic compiled `Style` for this button.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Button configuration.
    ///
    /// # Returns
    ///
    /// - `Style` - Compiled cross-platform style.
    #[inline(always)]
    #[must_use]
    pub fn resolved_style(&self) -> Style {
        self.resolved_style_with_tokens(self.get_tokens())
    }
}

/// Declarative `#[component]` implementation for `EuvButton`.
#[component]
/// Inherent implementation of [`EuvButton`].
impl EuvButton {
    /// Renders `EuvButton` into the ambient framework-managed `Context`.
    ///
    /// Variant backgrounds, borders, and paired foreground text colors are owned by a reactive `Effect` tracking
    /// hover, press, and the live ambient theme so every interactive state preserves `>= 4.5:1` contrast without
    /// stale colors after theme switches.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Component descriptor reference.
    ///
    /// # Returns
    ///
    /// - `NodeId` - Mounted root scene node handle.
    fn render(&self) -> NodeId {
        let mut ctx: Context = current_context();
        let base_tokens: ThemeTokens = self.get_tokens();
        let init_tok: ThemeTokens = live_tokens_untracked(base_tokens);
        let variant: EuvButtonVariant = self.get_variant();
        let style: Style = self.resolved_style_with_tokens(init_tok);
        let text_color: Color = style.get_text().get_color();
        let font_sz: f32 = style.get_text().get_font_size();
        let font_wt: FontWeight = style.get_text().get_weight();
        let disabled: bool = self.get_disabled();
        let Self {
            label, on_click, ..
        } = self;
        let mut a11y: A11yProps = A11yProps::new(A11yRole::Button, label.clone());
        let _: &mut A11yProps = a11y.set_disabled(disabled);
        let handler_opt: Option<UiEventHandler> = if !disabled {
            on_click.as_ref().map(Rc::clone)
        } else {
            None
        };
        let mut txt_st: Style = Style::default();
        let _: &mut LayoutStyle = txt_st
            .get_mut_layout()
            .set_max_width(Length::Percent(100.0));
        let _: &mut TextStyle = txt_st
            .get_mut_text()
            .set_color(text_color)
            .set_font_size(font_sz)
            .set_weight(font_wt)
            .set_align(TextAlign::Center)
            .set_overflow(TextOverflow::Ellipsis);
        let (btn_node, txt_node): (NodeId, NodeId) = ctx.get_scene().with_write(
            |tree: &mut SceneTree| {
                let btn_node: NodeId = tree.create_view(style);
                if let Some(node) = tree.get_node_mut(btn_node) {
                    let _: &mut SceneNode = node.set_a11y(Some(a11y));
                    if let Some(handler_rc) = handler_opt {
                        let _: &mut EventHandlers =
                            node.get_mut_handlers().set_on_click(Some(handler_rc));
                        let _: &mut InteractionState =
                            node.get_mut_interaction().set_focusable(true);
                    }
                    if !disabled {
                        let mut hover_paint: PaintStyle = style.get_paint();
                        let _: Color = Self::apply_state_paint(
                            &mut hover_paint,
                            variant,
                            init_tok,
                            true,
                            false,
                        );
                        let _: &mut SceneNode = node.set_hover_style(Some(hover_paint));
                    }
                }
                let txt_node: NodeId = tree.create_text(label.clone(), txt_st);
                tree.append_child(btn_node, txt_node);
                (btn_node, txt_node)
            },
            (NodeId::default(), NodeId::default()),
        );
        let hovered_sig: Signal<bool> = Signal::new(false);
        let pressed_sig: Signal<bool> = Signal::new(false);
        if !disabled {
            ctx.on_hover(btn_node, move |ev: &UiEvent| match ev.get_kind() {
                UiEventKind::PointerEnter => {
                    let _: bool = hovered_sig.set_if_changed(true);
                }
                UiEventKind::PointerLeave => {
                    let _: bool = hovered_sig.set_if_changed(false);
                    let _: bool = pressed_sig.set_if_changed(false);
                }
                _ => {}
            });
            ctx.on_pointer_down(btn_node, move |_: &UiEvent| {
                let _: bool = pressed_sig.set_if_changed(true);
            });
            ctx.on_pointer_up(btn_node, move |_: &UiEvent| {
                let _: bool = pressed_sig.set_if_changed(false);
            });
            ctx.on_pointer_cancel(btn_node, move |_: &UiEvent| {
                let _: bool = hovered_sig.set_if_changed(false);
                let _: bool = pressed_sig.set_if_changed(false);
            });
        }
        let handle: SceneHandle = ctx.get_scene().clone();
        let app_state_sig: Signal<AppState> = ctx.get_app_state();
        let prev_state: Cell<(bool, bool, ThemeKey)> = Cell::new((false, false, init_tok.key()));
        let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
            let _: Effect = Effect::new(move || {
                if app_state_sig.get() != AppState::Active {
                    let _: bool = hovered_sig.set_if_changed(false);
                    let _: bool = pressed_sig.set_if_changed(false);
                }
            });
            Effect::new(move || {
                let is_hovered: bool = !disabled && hovered_sig.get();
                let is_pressed: bool = !disabled && pressed_sig.get();
                let tok: ThemeTokens = use_live_tokens(base_tokens);
                let (old_h, old_p, old_key): (bool, bool, ThemeKey) =
                    prev_state.replace((is_hovered, is_pressed, tok.key()));
                if old_h == is_hovered && old_p == is_pressed && old_key == tok.key() {
                    return;
                }
                handle.with_write(
                    |tree: &mut SceneTree| {
                        let mut fg_color: Color = text_color;
                        if let Some(node) = tree.get_node_mut(btn_node) {
                            let st: &mut Style = node.get_mut_style();
                            fg_color = Self::apply_state_paint(
                                st.get_mut_paint(),
                                variant,
                                tok,
                                is_hovered,
                                is_pressed,
                            );
                            let _: &mut TextStyle = st.get_mut_text().set_color(fg_color);
                            if !disabled {
                                let mut hover_paint: PaintStyle = st.get_paint();
                                if !is_pressed {
                                    let _: Color = Self::apply_state_paint(
                                        &mut hover_paint,
                                        variant,
                                        tok,
                                        true,
                                        false,
                                    );
                                }
                                let _: &mut SceneNode = node.set_hover_style(Some(hover_paint));
                            }
                            tree.mark_dirty(btn_node, DIRTY_PAINT);
                        }
                        if let Some(txt) = tree.get_node_mut(txt_node) {
                            let _: &mut TextStyle =
                                txt.get_mut_style().get_mut_text().set_color(fg_color);
                            tree.mark_dirty(txt_node, DIRTY_PAINT);
                        }
                    },
                    (),
                );
            })
        });
        ctx.get_scene().with_write(
            |tree: &mut SceneTree| {
                mark_theme_owned(tree, &[btn_node, txt_node]);
                tree.attach_scope(btn_node, scope);
            },
            (),
        );
        btn_node
    }
}