euv-example 0.50.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
use super::*;

/// Maps a segmented selector index (`0..=3`) to its corresponding `EuvCodeBlockSizeMode` variant.
///
/// # Arguments
///
/// - `usize` - Active size-mode selector index.
///
/// # Returns
///
/// - `EuvCodeBlockSizeMode` - Resolved sizing policy for `EuvCodeBlock`.
fn resolve_code_block_size_mode(idx: usize) -> EuvCodeBlockSizeMode {
    match idx {
        1 => EuvCodeBlockSizeMode::AutoExpandHeight,
        2 => EuvCodeBlockSizeMode::AutoExpandWidth,
        3 => EuvCodeBlockSizeMode::Fixed,
        _ => EuvCodeBlockSizeMode::AutoExpand,
    }
}

/// Returns the human-readable description for a `EuvCodeBlockSizeMode` selector index.
///
/// # Arguments
///
/// - `usize` - Active size-mode selector index.
///
/// # Returns
///
/// - `&'static str` - Concise explanation of the selected sizing mode.
fn size_mode_description(idx: usize) -> &'static str {
    match idx {
        1 => MD_SIZE_DESC_AUTO_H,
        2 => MD_SIZE_DESC_AUTO_W,
        3 => MD_SIZE_DESC_FIXED,
        _ => MD_SIZE_DESC_AUTO,
    }
}

/// Returns the canonical variant label for a `EuvCodeBlockSizeMode` selector index.
///
/// # Arguments
///
/// - `usize` - Active size-mode selector index.
///
/// # Returns
///
/// - `&'static str` - Enum variant display label.
fn size_mode_label(idx: usize) -> &'static str {
    match idx {
        1 => MD_SIZE_MODE_AUTO_H,
        2 => MD_SIZE_MODE_AUTO_W,
        3 => MD_SIZE_MODE_FIXED,
        _ => MD_SIZE_MODE_AUTO,
    }
}

/// Mutates text selectability modes in-place across a mounted subtree without tearing down `bind_children` or mounted canvases.
///
/// # Arguments
///
/// - `&mut SceneTree` - Active retained scene tree.
/// - `NodeId` - Root node of the subtree to update.
/// - `bool` - Target selectability state (`true` restores content nodes, `false` disables selectable content nodes).
/// - `&mut Vec<(NodeId, TextSelectMode)>` - Stored list of `(node_id, previous_mode)` pairs disabled while `enabled == false`.
pub(crate) fn apply_subtree_selectable_in_place(
    tree: &mut SceneTree,
    root: NodeId,
    enabled: bool,
    disabled_ids: &mut Vec<(NodeId, TextSelectMode)>,
) {
    if !enabled {
        disabled_ids.clear();
        let mut stack: Vec<NodeId> = vec![root];
        while let Some(id) = stack.pop() {
            let mut prev_mode: Option<TextSelectMode> = None;
            if let Some(node) = tree.get_node(id) {
                if !node.get_alive() {
                    continue;
                }
                if let Some(mode) = node.style_ref().get_text().text_select_mode()
                    && mode != TextSelectMode::None
                {
                    prev_mode = Some(mode);
                }
            }
            stack.extend(tree.children_iter(id));
            if let Some(mode) = prev_mode
                && let Some(node_mut) = tree.get_node_mut(id)
            {
                node_mut
                    .get_mut_style()
                    .get_mut_text()
                    .set_text_select(Some(TextSelectMode::None));
                disabled_ids.push((id, mode));
                tree.mark_dirty(id, DIRTY_PAINT);
            }
        }
        tree.clear_text_selection();
    } else {
        for (id, prev_mode) in disabled_ids.drain(..) {
            if let Some(node_mut) = tree.get_node_mut(id)
                && node_mut.get_alive()
            {
                node_mut
                    .get_mut_style()
                    .get_mut_text()
                    .set_text_select(Some(prev_mode));
                tree.mark_dirty(id, DIRTY_PAINT);
            }
        }
    }
}

/// Mounts the dedicated Markdown & Code Studio page showcasing GFM prose, alerts, math, footnotes, no-ellipsis fullscreen tables, and Eva Bold `EuvCodeBlock` with interactive `EuvCodeBlockSizeMode` switching.
///
/// # Arguments
///
/// - `&ShowcaseState` - Reactive showcase state.
/// - `ThemeTokens` - Active design tokens.
///
/// # Returns
///
/// - `NodeId` - Mounted Markdown & Code Studio page handle.
pub fn mount_markdown_page(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
    let st_toggle: ShowcaseState = state.clone();
    let theme_mode_sig: Signal<ThemeMode> = state.get_theme().get_mode();
    let lang_tab: Signal<usize> = state.get_markdown_lang_tab();
    let size_mode_sig: Signal<usize> = Signal::new(0);
    let selectable_sig: Signal<bool> = state.get_markdown_selectable();
    let copy_log: Signal<String> = state.get_markdown_copy_log();
    let link_log: Signal<String> = state.get_markdown_link_log();
    let tasks_toggled: Signal<u32> = state.get_markdown_tasks_toggled();
    let copy_log_doc: Signal<String> = copy_log;
    let link_log_doc: Signal<String> = link_log;
    let link_log_wv_btn: Signal<String> = link_log;
    let link_log_ext_btn: Signal<String> = link_log;
    let tasks_doc: Signal<u32> = tasks_toggled;
    let link_open_mode_sig: Signal<usize> = Signal::new(0);
    let webview_sheet_open_sig: Signal<bool> = Signal::new(false);
    let webview_sheet_url_sig: Signal<String> = Signal::new(MD_TEST_WEBVIEW_URL.to_string());
    let disabled_code_ids: Signal<Vec<(NodeId, TextSelectMode)>> = Signal::new(Vec::new());
    let disabled_doc_ids: Signal<Vec<(NodeId, TextSelectMode)>> = Signal::new(Vec::new());
    let md_ctx: Context = current_context();
    let active_theme_desc = move || -> String {
        match theme_mode_sig.get() {
            ThemeMode::Light => MD_THEME_DESC_LIGHT.to_string(),
            ThemeMode::Dark => MD_THEME_DESC_DARK.to_string(),
        }
    };
    let theme_desc_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        let live_eva: CodeThemePalette = CodeThemePalette::resolve(live.get_mode());
        style! {
            Width(Length::Percent(100.0))
            FontSize(12.0)
            FontFamily::Monospace
            FontWeight::SemiBold
            TextColor(live_eva.get_keyword())
        }
    };
    let mode_label_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        style! {
            Width(Length::Percent(100.0))
            FontSize(12.0)
            FontWeight::SemiBold
            TextColor(live.get_palette().get_foreground())
        }
    };
    let mode_desc_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        style! {
            Width(Length::Percent(100.0))
            FontSize(12.0)
            TextColor(live.get_palette().get_muted_foreground())
        }
    };
    let telemetry_style = move || -> Style {
        let live: ThemeTokens = use_live_tokens(tokens);
        style! {
            Width(Length::Percent(100.0))
            FontSize(12.0)
            FontFamily::Monospace
            TextColor(live.get_palette().get_primary())
        }
    };
    let active_mode_desc = move || -> String {
        let idx: usize = size_mode_sig.get();
        size_mode_description(idx).to_string()
    };
    let swatch_nodes: Vec<NodeId> = (0usize..6usize)
        .map(|slot: usize| {
            let swatch_style = move || -> Style {
                let live_eva: CodeThemePalette = CodeThemePalette::resolve(theme_mode_sig.get());
                let col: Color = match slot {
                    0 => live_eva.get_background(),
                    1 => live_eva.get_keyword(),
                    2 => live_eva.get_function(),
                    3 => live_eva.get_number(),
                    4 => live_eva.get_constant(),
                    _ => live_eva.get_property(),
                };
                style! {
                    Width(Length::Px(22.0))
                    Height(Length::Px(22.0))
                    Radius(6.0)
                    Background(col)
                    Border::new(1.0, live_eva.get_border())
                }
            };
            view! {
                view {
                    bind_style: swatch_style
                }
            }
        })
        .collect();
    let on_toggle_eva = move |_: &UiEvent| {
        st_toggle.toggle_theme_mode();
    };
    let on_emit_pulse = move |_: &UiEvent| {
        emit!(ShowcasePulseEvent {
            source: ROUTE_MARKDOWN.to_string(),
            seq: 1,
        });
    };
    let code_ctx: Context = md_ctx.clone();
    let build_code_tab = move || -> Vec<NodeId> {
        let tab: usize = lang_tab.get();
        let mode_idx: usize = size_mode_sig.get();
        let mode: EuvCodeBlockSizeMode = resolve_code_block_size_mode(mode_idx);
        let is_sel: bool = selectable_sig.get_untracked();
        let live_tok: ThemeTokens = live_tokens_untracked(tokens);
        let (src, lang, fname): (&str, &str, &str) = match tab {
            1 => (MD_SAMPLE_TS, MD_LANG_TS, MD_FILE_TS),
            2 => (MD_SAMPLE_JSON, MD_LANG_JSON, MD_FILE_JSON),
            3 => (MD_SAMPLE_DIFF, MD_LANG_DIFF, MD_FILE_DIFF),
            _ => (MD_SAMPLE_RUST, MD_LANG_RUST, MD_FILE_RUST),
        };
        let cb_log: Signal<String> = copy_log;
        let block: NodeId = EuvCodeBlock::new(src.to_string(), live_tok)
            .language(lang)
            .filename(fname)
            .line_numbers(true)
            .size_mode(mode)
            .max_height(MD_CODE_MAX_HEIGHT)
            .text_select(TextSelectMode::SelectAndCopy)
            .on_copy(move |txt: &str| {
                let _: bool = cb_log.set(format!(
                    "Copied {} bytes ({lang}) via Eva Bold EuvCodeBlock",
                    txt.len()
                ));
            })
            .mount();
        let _: bool = {
            if let Some(mut updated_value) = disabled_code_ids.try_get_untracked() {
                {
                    let ids: &mut Vec<(NodeId, TextSelectMode)> = &mut updated_value;
                    ids.clear();
                    if !is_sel {
                        code_ctx.get_scene().with_write(
                            |tree: &mut SceneTree| {
                                apply_subtree_selectable_in_place(tree, block, false, ids);
                            },
                            (),
                        );
                    }
                }
                disabled_code_ids.set(updated_value)
            } else {
                false
            }
        };
        if mode == EuvCodeBlockSizeMode::AutoExpandWidth {
            let scroll_style: Style = style! {
                Width(Length::Percent(100.0))
            };
            let wrapped: NodeId = view! {
                scroll {
                    style: scroll_style
                    { block }
                }
            };
            vec![wrapped]
        } else {
            vec![block]
        }
    };
    let telemetry_text = move || -> String {
        format!(
            "Mode: {} | Selectable: {} | Tasks: {} | Copy: {} | Link: {}",
            size_mode_label(size_mode_sig.get()),
            selectable_sig.get(),
            tasks_toggled.get(),
            copy_log.get(),
            link_log.get()
        )
    };
    let live_tok_doc: ThemeTokens = live_tokens_untracked(tokens);
    let md_doc_node: NodeId = EuvMarkdown::new(MD_FULL_DOCUMENT.to_string(), live_tok_doc)
        .style(style! {
            TextSelectMode::SelectAndCopy
        })
        .on_link(move |url: &str| {
            if link_open_mode_sig.get_untracked() == 0 {
                let _: bool = webview_sheet_url_sig.set(url.to_string());
                let _: bool = webview_sheet_open_sig.set(true);
                let _: bool = link_log_doc.set(format!("WebView Sheet: {url}"));
            } else {
                let launched: bool = open_external_url(url);
                let _: bool = link_log_doc.set(format!("System Browser ({launched}): {url}"));
            }
        })
        .on_copy(move |code: &str| {
            let _: bool =
                copy_log_doc.set(format!("Copied {} bytes from Markdown block", code.len()));
        })
        .on_toggle_task(move |_idx: usize, _checked: bool| {
            let _: bool = tasks_doc.set(tasks_doc.get_untracked() + 1);
        })
        .mount();
    let mut code_host_id: NodeId = NodeId::default();
    let column_tertiary_style: Style = style! {
        Width(Length::Percent(100.0))
    };
    let main_col: NodeId = view! {
        EuvCard {
            title: MD_DOC_CARD_TITLE.to_string()
            tokens: tokens
            subtitle: MD_DOC_CARD_SUB
            column {
                style: column_tertiary_style
                { md_doc_node }
            }
        }
    };
    let column_style: Style = style! {
        Width(Length::Percent(100.0))
        Gap(SPACE_MD)
    };
    let row_style: Style = style! {
        Width(Length::Percent(100.0))
        AlignItems::Center
        FlexWrap::Wrap
        Gap(SPACE_SM)
    };
    let column_secondary_style: Style = style! {
        Width(Length::Percent(100.0))
    };
    let rail_col: NodeId = view! {
        column {
            style: column_style
            EuvCard {
                title: MD_THEME_CARD_TITLE.to_string()
                tokens: tokens
                subtitle: MD_THEME_CARD_SUB
                text {
                    bind_style: theme_desc_style
                    { active_theme_desc }
                }
                row {
                    style: row_style
                    ..swatch_nodes
                }
                EuvActionRow {
                    EuvButton {
                        label: BTN_TOGGLE_EVA_MODE.to_string()
                        tokens: tokens
                        small()
                        on_click: on_toggle_eva
                    }
                    EuvButton {
                        label: BTN_EMIT_PULSE.to_string()
                        tokens: tokens
                        outline()
                        small()
                        on_click: on_emit_pulse
                    }
                }
                EuvSwitch {
                    checked: selectable_sig
                    label: MD_SELECTABLE_LABEL.to_string()
                    tokens: tokens
                }
            }
            EuvCard {
                title: MD_CODE_CARD_TITLE.to_string()
                tokens: tokens
                subtitle: MD_CODE_CARD_SUB
                EuvSegmented {
                    options: vec![
                        MD_TAB_RUST.to_string(),
                        MD_TAB_TS.to_string(),
                        MD_TAB_JSON.to_string(),
                        MD_TAB_DIFF.to_string(),
                    ]
                    selected: lang_tab
                    tokens: tokens
                }
                text {
                    bind_style: mode_label_style
                    { MD_SIZE_MODE_LABEL }
                }
                EuvSegmented {
                    options: vec![
                        MD_SIZE_MODE_AUTO.to_string(),
                        MD_SIZE_MODE_AUTO_H.to_string(),
                        MD_SIZE_MODE_AUTO_W.to_string(),
                        MD_SIZE_MODE_FIXED.to_string(),
                    ]
                    selected: size_mode_sig
                    tokens: tokens
                }
                text {
                    bind_style: mode_desc_style
                    { active_mode_desc }
                }
                column {
                    node_ref: code_host_id
                    style: column_secondary_style
                    bind_children: build_code_tab
                }
            }
            EuvCard {
                title: MD_LINK_BENCH_TITLE.to_string()
                tokens: tokens
                subtitle: MD_LINK_BENCH_SUB
                EuvSegmented {
                    options: vec![
                        MD_LINK_MODE_WEBVIEW.to_string(),
                        MD_LINK_MODE_BROWSER.to_string(),
                    ]
                    selected: link_open_mode_sig
                    tokens: tokens
                }
                EuvActionRow {
                    EuvButton {
                        label: MD_BTN_OPEN_WEBVIEW.to_string()
                        tokens: tokens
                        small()
                        on_click: move |_: &UiEvent| {
                            let _: bool = webview_sheet_url_sig.set(MD_TEST_WEBVIEW_URL.to_string());
                            let _: bool = webview_sheet_open_sig.set(true);
                            let _: bool = link_log_wv_btn.set(format!("WebView Sheet: {MD_TEST_WEBVIEW_URL}"));
                        }
                    }
                    EuvExternalLink {
                        label: MD_BTN_OPEN_BROWSER.to_string()
                        url: MD_TEST_BROWSER_URL.to_string()
                        tokens: tokens
                        on_open: move |opened_url: &str, launched_ok: bool| {
                            let _: bool = link_log_ext_btn.set(format!("System Browser ({launched_ok}): {opened_url}"));
                        }
                    }
                }
            }
            EuvCard {
                title: MD_TELEMETRY_CARD_TITLE.to_string()
                tokens: tokens
                subtitle: MD_TELEMETRY_CARD_SUB
                text {
                    bind_style: telemetry_style
                    { telemetry_text }
                }
            }
        }
    };
    let studio_node: NodeId = mount_studio_layout(main_col, rail_col);
    let webview_sheet_node: NodeId = view! {
        EuvWebViewSheet {
            open: webview_sheet_open_sig
            url: webview_sheet_url_sig
            title: MD_WEBVIEW_SHEET_TITLE.to_string()
            tokens: tokens
        }
    };
    let page_node: NodeId = view! {
        EuvPage {
            title: MD_STUDIO_TITLE.to_string()
            subtitle: MD_STUDIO_SUBTITLE.to_string()
            tokens: tokens
            { studio_node }
            { webview_sheet_node }
        }
    };
    let prev_sel: Rc<Cell<bool>> = Rc::new(Cell::new(true));
    let sel_ctx: Context = md_ctx.clone();
    let (sel_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
        Effect::new(move || {
            let next_sel: bool = selectable_sig.get();
            if prev_sel.replace(next_sel) == next_sel {
                return;
            }
            let _: bool = {
                if let Some(mut updated_value) = disabled_code_ids.try_get_untracked() {
                    {
                        let ids: &mut Vec<(NodeId, TextSelectMode)> = &mut updated_value;
                        sel_ctx.get_scene().with_write(
                            |tree: &mut SceneTree| {
                                apply_subtree_selectable_in_place(
                                    tree,
                                    code_host_id,
                                    next_sel,
                                    ids,
                                );
                            },
                            (),
                        );
                    }
                    disabled_code_ids.set(updated_value)
                } else {
                    false
                }
            };
            let _: bool = {
                if let Some(mut updated_value) = disabled_doc_ids.try_get_untracked() {
                    {
                        let ids: &mut Vec<(NodeId, TextSelectMode)> = &mut updated_value;
                        sel_ctx.get_scene().with_write(
                            |tree: &mut SceneTree| {
                                apply_subtree_selectable_in_place(tree, md_doc_node, next_sel, ids);
                            },
                            (),
                        );
                    }
                    disabled_doc_ids.set(updated_value)
                } else {
                    false
                }
            };
        })
    });
    md_ctx.get_scene().with_write(
        |tree: &mut SceneTree| {
            tree.attach_scope(page_node, sel_scope);
        },
        (),
    );
    page_node
}