euv-example 0.48.0

Cross-platform native showcase application demonstrating euv fine-grained reactivity, multi-mode rendering, window controls, and euv-ui components.
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
use super::*;

/// Resolves the `(surface, paired foreground)` colors of semantic palette role `idx` so swatch text is always contrast-paired with its own surface.
///
/// # Arguments
///
/// - `&ThemePalette` - Live palette.
/// - `usize` - Role index into `THEMES_ROLE_LABELS`.
///
/// # Returns
///
/// - `(Color, Color)` - Surface color and its readable paired foreground.
pub(crate) fn themes_role_colors(p: &ThemePalette, idx: usize) -> (Color, Color) {
    match idx {
        1 => (p.get_card(), p.get_card_foreground()),
        2 => (p.get_primary(), p.get_primary_foreground()),
        3 => (p.get_secondary(), p.get_secondary_foreground()),
        4 => (p.get_muted(), p.get_foreground()),
        5 => (p.get_accent(), p.get_foreground()),
        6 => (p.get_destructive(), p.get_destructive_foreground()),
        7 => (p.get_success(), p.get_success_foreground()),
        8 => (p.get_warning(), p.get_warning_foreground()),
        _ => (p.get_background(), p.get_foreground()),
    }
}

/// Resolves the shared corner radius of scale step `idx` from the canonical geometry.
///
/// # Arguments
///
/// - `&ThemeGeometry` - Live geometry tokens.
/// - `usize` - Step index into `THEMES_RADIUS_LABELS`.
///
/// # Returns
///
/// - `f32` - Corner radius in logical pixels.
pub(crate) fn themes_radius_value(g: &ThemeGeometry, idx: usize) -> f32 {
    match idx {
        0 => g.get_radius_sm(),
        1 => g.get_radius_md(),
        2 => g.get_radius_lg(),
        3 => g.get_radius_xl(),
        _ => THEMES_PILL_RADIUS,
    }
}

/// Mounts one live semantic palette swatch whose surface and label colors both track the active scheme.
///
/// # Arguments
///
/// - `usize` - Role index into `THEMES_ROLE_LABELS`.
/// - `ThemeTokens` - Design tokens at mount time.
///
/// # Returns
///
/// - `NodeId` - Mounted swatch node.
pub(crate) fn mount_theme_swatch(idx: usize, tokens: ThemeTokens) -> NodeId {
    let label: &'static str = THEMES_ROLE_LABELS[idx];
    let swatch_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        let (bg, _): (Color, Color) = themes_role_colors(&live.get_palette(), idx);
        Style::default()
            .column()
            .width(Length::Percent(100.0))
            .height(Length::Px(THEMES_SWATCH_HEIGHT))
            .padding(Edges::all(SPACE_MD))
            .gap(SPACE_XS)
            .justify(JustifyContent::End)
            .overflow(Overflow::Hidden)
            .background(bg)
            .border(
                live.get_geometry().get_border_width(),
                live.get_palette().get_border(),
            )
            .radius(live.get_geometry().get_radius_lg())
    };
    let name_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        let (_, fg): (Color, Color) = themes_role_colors(&live.get_palette(), idx);
        Style::default()
            .width(Length::Percent(100.0))
            .font_size(live.get_geometry().get_font_body())
            .font_weight(FontWeight::SemiBold)
            .color(fg)
    };
    let hex_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        let (_, fg): (Color, Color) = themes_role_colors(&live.get_palette(), idx);
        Style::default()
            .width(Length::Percent(100.0))
            .font_size(live.get_geometry().get_font_body() - 1.5)
            .font_family(FontFamily::Monospace)
            .color(fg)
    };
    let hex_text = move || -> String {
        let live: ThemeTokens = use_live_tokens(tokens);
        let (bg, _): (Color, Color) = themes_role_colors(&live.get_palette(), idx);
        format!("#{:02X}{:02X}{:02X}", bg.get_r(), bg.get_g(), bg.get_b())
    };
    view! {
        view {
            bind_style: swatch_style
            text {
                bind_style: name_style
                { label }
            }
            text {
                bind_style: hex_style
                { hex_text }
            }
        }
    }
}

/// Mounts one labelled tile of the shared corner radius scale.
///
/// # Arguments
///
/// - `usize` - Step index into `THEMES_RADIUS_LABELS`.
/// - `ThemeTokens` - Design tokens at mount time.
///
/// # Returns
///
/// - `NodeId` - Mounted radius tile node.
pub(crate) fn mount_radius_tile(idx: usize, tokens: ThemeTokens) -> NodeId {
    let p: ThemePalette = tokens.get_palette();
    let caption: String = THEMES_RADIUS_LABELS[idx].to_string();
    let tile_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        let live_p: ThemePalette = live.get_palette();
        Style::default()
            .width(Length::Px(THEMES_RADIUS_TILE))
            .height(Length::Px(THEMES_RADIUS_TILE))
            .background(live_p.get_primary().with_alpha(36))
            .border(2.0, live_p.get_primary())
            .radius(themes_radius_value(&live.get_geometry(), idx))
    };
    view! {
        column {
            style: {
                align: AlignItems::Center
                gap: SPACE_XS
            }
            view {
                bind_style: tile_style
            }
            text {
                style: {
                    font_size: 12.0
                    font_family: FontFamily::Monospace
                    color: p.get_muted_foreground()
                }
                { caption }
            }
        }
    }
}

/// Mounts one spec row (label, visual bar sized by `value`, numeric value) used by the control-height, typography and spacing scales.
///
/// # Arguments
///
/// - `String` - Token label.
/// - `f32` - Token value in logical pixels.
/// - `NodeId` - Pre-mounted visual sample.
/// - `ThemeTokens` - Design tokens at mount time.
///
/// # Returns
///
/// - `NodeId` - Mounted spec row.
pub(crate) fn mount_token_row(
    label: String,
    value: f32,
    sample: NodeId,
    tokens: ThemeTokens,
) -> NodeId {
    let p: ThemePalette = tokens.get_palette();
    let value_txt: String = format!("{value:.0}px");
    view! {
        row {
            style: {
                width: Length::Percent(100.0)
                align: AlignItems::Center
                gap: SPACE_MD
            }
            text {
                style: {
                    width: Length::Px(96.0)
                    flex_shrink: 0.0
                    font_size: 12.5
                    font_family: FontFamily::Monospace
                    color: p.get_muted_foreground()
                }
                { label }
            }
            row {
                style: {
                    flex_grow: 1.0
                    flex_shrink: 1.0
                    min_width: Length::Px(0.0)
                    align: AlignItems::Center
                    overflow: Overflow::Hidden
                }
                { sample }
            }
            text {
                style: {
                    flex_shrink: 0.0
                    font_size: 12.5
                    font_family: FontFamily::Monospace
                    color: p.get_foreground()
                }
                { value_txt }
            }
        }
    }
}

/// Mounts a primary-tinted horizontal bar of `width` x `height` pixels used as a scale sample.
///
/// # Arguments
///
/// - `f32` - Bar width in logical pixels.
/// - `f32` - Bar height in logical pixels.
/// - `ThemeTokens` - Design tokens at mount time.
///
/// # Returns
///
/// - `NodeId` - Mounted bar node.
pub(crate) fn mount_scale_bar(width: f32, height: f32, tokens: ThemeTokens) -> NodeId {
    let bar_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        Style::default()
            .width(Length::Px(width))
            .height(Length::Px(height))
            .background(live.get_palette().get_primary().with_alpha(64))
            .border(1.0, live.get_palette().get_primary())
            .radius(live.get_geometry().get_radius_sm())
    };
    view! {
        view {
            bind_style: bar_style
        }
    }
}

/// Mounts the Design System studio: scheme selector + live preview rail beside the semantic palette, radius, control-height, typography and spacing scales.
///
/// # Arguments
///
/// - `&ShowcaseState` - Reactive showcase state.
/// - `ThemeTokens` - Active design tokens.
///
/// # Returns
///
/// - `NodeId` - Mounted themes page node.
pub fn mount_themes_page(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
    let p: ThemePalette = tokens.get_palette();
    let g: ThemeGeometry = tokens.get_geometry();
    let scheme_sig: Signal<DesignScheme> = state.get_theme().get_scheme();
    let mode_sig: Signal<ThemeMode> = state.get_theme().get_mode();
    let state_for_cycle: ShowcaseState = state.clone();
    let state_for_mode: ShowcaseState = state.clone();
    let active_text = move || -> String {
        format!(
            "{PREFIX_ACTIVE_SCHEME}{} ({:?})",
            scheme_sig.get().as_str(),
            mode_sig.get()
        )
    };
    let on_next_scheme = move |_: &UiEvent| {
        state_for_cycle.next_scheme();
    };
    let on_toggle_mode = move |_: &UiEvent| {
        state_for_mode.toggle_theme_mode();
    };
    let scheme_buttons: Vec<NodeId> = DesignScheme::all()
        .iter()
        .copied()
        .map(|scheme: DesignScheme| {
            let on_pick = move |_: &UiEvent| {
                let _: bool = scheme_sig.set_if_changed(scheme);
            };
            let btn_style = move || -> Style {
                let live: ThemeTokens = use_live_tokens(tokens);
                let live_p: ThemePalette = live.get_palette();
                let live_g: ThemeGeometry = live.get_geometry();
                let active: bool = scheme_sig.get() == scheme;
                let bg: Color = if active {
                    live_p.get_primary()
                } else {
                    live_p.get_background()
                };
                let border: Color = if active {
                    live_p.get_primary()
                } else {
                    live_p.get_border()
                };
                Style::default()
                    .row()
                    .width(Length::Percent(100.0))
                    .height(Length::Px(live_g.get_control_sm()))
                    .padding(Edges::symmetric(0.0, SPACE_SM))
                    .align(AlignItems::Center)
                    .gap(SPACE_SM)
                    .overflow(Overflow::Hidden)
                    .background(bg)
                    .border(live_g.get_border_width(), border)
                    .radius(live_g.get_radius_md())
                    .cursor(CursorIcon::Pointer)
            };
            let dot_style = move || -> Style {
                let mode: ThemeMode = mode_sig.get();
                let preview: ThemePalette = ThemeTokens::resolve(scheme, mode).get_palette();
                let live_p: ThemePalette = use_live_tokens(tokens).get_palette();
                Style::default()
                    .width(Length::Px(10.0))
                    .height(Length::Px(10.0))
                    .flex_shrink(0.0)
                    .background(preview.get_primary())
                    .border(1.0, live_p.get_border())
                    .radius(THEMES_PILL_RADIUS)
            };
            let lbl_style = move || -> Style {
                let live: ThemeTokens = use_live_tokens(tokens);
                let live_p: ThemePalette = live.get_palette();
                let active: bool = scheme_sig.get() == scheme;
                let fg: Color = if active {
                    live_p.get_primary_foreground()
                } else {
                    live_p.get_foreground()
                };
                Style::default()
                    .flex_grow(1.0)
                    .flex_shrink(1.0)
                    .min_width(Length::Px(0.0))
                    .font_size(12.5)
                    .font_weight(FontWeight::Medium)
                    .color(fg)
                    .overflow(Overflow::Hidden)
                    .text_overflow(TextOverflow::Ellipsis)
            };
            view! {
                row {
                    bind_style: btn_style
                    role: A11yRole::Button
                    on_click: on_pick
                    view {
                        bind_style: dot_style
                    }
                    text {
                        bind_style: lbl_style
                        { scheme.as_str().to_string() }
                    }
                }
            }
        })
        .collect();
    let swatches: Vec<NodeId> = (0..THEMES_ROLE_LABELS.len())
        .map(|idx: usize| mount_theme_swatch(idx, tokens))
        .collect();
    let palette_grid: NodeId = mount_responsive_grid(swatches, 3);
    let radius_tiles: Vec<NodeId> = (0..THEMES_RADIUS_LABELS.len())
        .map(|idx: usize| mount_radius_tile(idx, tokens))
        .collect();
    let control_values: [f32; 3] = [g.get_control_sm(), g.get_control_md(), g.get_control_lg()];
    let control_rows: Vec<NodeId> = control_values
        .iter()
        .enumerate()
        .map(|(idx, value): (usize, &f32)| {
            let bar: NodeId = mount_scale_bar(*value * 3.0, *value, tokens);
            mount_token_row(THEMES_CONTROL_LABELS[idx].to_string(), *value, bar, tokens)
        })
        .collect();
    let type_values: [f32; 3] = [g.get_font_body(), g.get_font_heading(), g.get_font_title()];
    let type_rows: Vec<NodeId> = type_values
        .iter()
        .enumerate()
        .map(|(idx, value): (usize, &f32)| {
            let size: f32 = *value;
            let sample: NodeId = view! {
                text {
                    style: {
                        font_size: size
                        font_weight: FontWeight::SemiBold
                        color: p.get_foreground()
                        overflow: Overflow::Hidden
                        text_overflow: TextOverflow::Ellipsis
                    }
                    { THEMES_TYPE_SAMPLE }
                }
            };
            mount_token_row(THEMES_TYPE_LABELS[idx].to_string(), size, sample, tokens)
        })
        .collect();
    let space_rows: Vec<NodeId> = THEMES_SPACE_VALUES
        .iter()
        .enumerate()
        .map(|(idx, value): (usize, &f32)| {
            let bar: NodeId = mount_scale_bar(*value * 4.0, SPACE_MD, tokens);
            mount_token_row(THEMES_SPACE_LABELS[idx].to_string(), *value, bar, tokens)
        })
        .collect();
    let preview_query: Signal<String> = Signal::new(String::new());
    let preview_switch: Signal<bool> = Signal::new(true);
    let selector_card: NodeId = view! {
        EuvCard {
            title: THEMES_CARD_TITLE.to_string()
            tokens: tokens
            subtitle: THEMES_CARD_SUB
            text {
                style: {
                    width: Length::Percent(100.0)
                    font_size: 14.0
                    font_weight: FontWeight::SemiBold
                    color: p.get_foreground()
                }
                { active_text }
            }
            EuvActionRow {
                EuvButton {
                    label: BTN_NEXT_SCHEME.to_string()
                    tokens: tokens
                    sm()
                    on_click: on_next_scheme
                }
                EuvButton {
                    label: BTN_TOGGLE_MODE.to_string()
                    tokens: tokens
                    outline()
                    sm()
                    on_click: on_toggle_mode
                }
            }
            grid {
                style: {
                    width: Length::Percent(100.0)
                    columns: 2
                    gap: SPACE_SM
                }
                ..scheme_buttons
            }
        }
    };
    let preview_card: NodeId = view! {
        EuvCard {
            title: THEMES_PREVIEW_TITLE.to_string()
            tokens: tokens
            subtitle: THEMES_PREVIEW_SUB
            EuvInput {
                value: preview_query
                placeholder: THEMES_PREVIEW_INPUT.to_string()
                tokens: tokens
            }
            EuvActionRow {
                EuvButton {
                    label: THEMES_PREVIEW_PRIMARY.to_string()
                    tokens: tokens
                    sm()
                }
                EuvButton {
                    label: THEMES_PREVIEW_SECONDARY.to_string()
                    tokens: tokens
                    secondary()
                    sm()
                }
                EuvButton {
                    label: THEMES_PREVIEW_OUTLINE.to_string()
                    tokens: tokens
                    outline()
                    sm()
                }
            }
            row {
                style: {
                    width: Length::Percent(100.0)
                    flex_wrap: FlexWrap::Wrap
                    align: AlignItems::Center
                    gap: SPACE_SM
                }
                EuvBadge {
                    label: THEMES_BADGE_STABLE.to_string()
                    tone: EuvTone::Success
                    tokens: tokens
                }
                EuvBadge {
                    label: THEMES_BADGE_BETA.to_string()
                    tone: EuvTone::Warning
                    tokens: tokens
                }
                EuvBadge {
                    label: THEMES_BADGE_NEW.to_string()
                    tone: EuvTone::Primary
                    tokens: tokens
                }
            }
            EuvSwitch {
                checked: preview_switch
                label: THEMES_PREVIEW_SWITCH.to_string()
                tokens: tokens
            }
        }
    };
    let rail: NodeId = view! {
        column {
            style: {
                width: Length::Percent(100.0)
                gap: SPACE_MD
            }
            { selector_card }
            { preview_card }
        }
    };
    let scale_cards: Vec<NodeId> = vec![
        view! {
            EuvCard {
                title: THEMES_RADIUS_TITLE.to_string()
                tokens: tokens
                subtitle: THEMES_RADIUS_SUB
                row {
                    style: {
                        width: Length::Percent(100.0)
                        flex_wrap: FlexWrap::Wrap
                        gap: SPACE_LG
                    }
                    ..radius_tiles
                }
            }
        },
        view! {
            EuvCard {
                title: THEMES_CONTROL_TITLE.to_string()
                tokens: tokens
                subtitle: THEMES_CONTROL_SUB
                ..control_rows
            }
        },
        view! {
            EuvCard {
                title: THEMES_TYPE_TITLE.to_string()
                tokens: tokens
                subtitle: THEMES_TYPE_SUB
                ..type_rows
            }
        },
        view! {
            EuvCard {
                title: THEMES_SPACE_TITLE.to_string()
                tokens: tokens
                subtitle: THEMES_SPACE_SUB
                ..space_rows
            }
        },
    ];
    let scale_grid: NodeId = mount_responsive_grid(scale_cards, 2);
    let main: NodeId = view! {
        column {
            style: {
                width: Length::Percent(100.0)
                gap: SPACE_MD
            }
            EuvCard {
                title: THEMES_PALETTE_TITLE.to_string()
                tokens: tokens
                subtitle: THEMES_PALETTE_SUB
                { palette_grid }
            }
            { scale_grid }
        }
    };
    let studio: NodeId = mount_studio_layout(main, rail);
    view! {
        EuvPage {
            title: THEMES_TITLE.to_string()
            subtitle: THEMES_SUBTITLE.to_string()
            tokens: tokens
            { studio }
        }
    }
}