euv_example/pages/markdown/
fn.rs1use super::*;
2
3fn resolve_code_block_size_mode(idx: usize) -> EuvCodeBlockSizeMode {
13 match idx {
14 1 => EuvCodeBlockSizeMode::AutoExpandHeight,
15 2 => EuvCodeBlockSizeMode::AutoExpandWidth,
16 3 => EuvCodeBlockSizeMode::Fixed,
17 _ => EuvCodeBlockSizeMode::AutoExpand,
18 }
19}
20
21fn size_mode_description(idx: usize) -> &'static str {
31 match idx {
32 1 => MD_SIZE_DESC_AUTO_H,
33 2 => MD_SIZE_DESC_AUTO_W,
34 3 => MD_SIZE_DESC_FIXED,
35 _ => MD_SIZE_DESC_AUTO,
36 }
37}
38
39fn size_mode_label(idx: usize) -> &'static str {
49 match idx {
50 1 => MD_SIZE_MODE_AUTO_H,
51 2 => MD_SIZE_MODE_AUTO_W,
52 3 => MD_SIZE_MODE_FIXED,
53 _ => MD_SIZE_MODE_AUTO,
54 }
55}
56
57pub(crate) fn apply_subtree_selectable_in_place(
66 tree: &mut SceneTree,
67 root: NodeId,
68 enabled: bool,
69 disabled_ids: &mut Vec<(NodeId, TextSelectMode)>,
70) {
71 if !enabled {
72 disabled_ids.clear();
73 let mut stack: Vec<NodeId> = vec![root];
74 while let Some(id) = stack.pop() {
75 let mut prev_mode: Option<TextSelectMode> = None;
76 if let Some(node) = tree.get_node(id) {
77 if !node.get_alive() {
78 continue;
79 }
80 if let Some(mode) = node.style_ref().get_text().text_select_mode()
81 && mode != TextSelectMode::None
82 {
83 prev_mode = Some(mode);
84 }
85 }
86 stack.extend(tree.children_iter(id));
87 if let Some(mode) = prev_mode
88 && let Some(node_mut) = tree.get_node_mut(id)
89 {
90 node_mut
91 .get_mut_style()
92 .get_mut_text()
93 .set_text_select(Some(TextSelectMode::None));
94 disabled_ids.push((id, mode));
95 tree.mark_dirty(id, DIRTY_PAINT);
96 }
97 }
98 tree.clear_text_selection();
99 } else {
100 for (id, prev_mode) in disabled_ids.drain(..) {
101 if let Some(node_mut) = tree.get_node_mut(id)
102 && node_mut.get_alive()
103 {
104 node_mut
105 .get_mut_style()
106 .get_mut_text()
107 .set_text_select(Some(prev_mode));
108 tree.mark_dirty(id, DIRTY_PAINT);
109 }
110 }
111 }
112}
113
114pub fn mount_markdown_page(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
125 let st_toggle: ShowcaseState = state.clone();
126 let theme_mode_sig: Signal<ThemeMode> = state.get_theme().get_mode();
127 let lang_tab: Signal<usize> = state.get_markdown_lang_tab();
128 let size_mode_sig: Signal<usize> = Signal::new(0);
129 let selectable_sig: Signal<bool> = state.get_markdown_selectable();
130 let copy_log: Signal<String> = state.get_markdown_copy_log();
131 let link_log: Signal<String> = state.get_markdown_link_log();
132 let tasks_toggled: Signal<u32> = state.get_markdown_tasks_toggled();
133 let copy_log_doc: Signal<String> = copy_log;
134 let link_log_doc: Signal<String> = link_log;
135 let link_log_wv_btn: Signal<String> = link_log;
136 let link_log_ext_btn: Signal<String> = link_log;
137 let tasks_doc: Signal<u32> = tasks_toggled;
138 let link_open_mode_sig: Signal<usize> = Signal::new(0);
139 let webview_sheet_open_sig: Signal<bool> = Signal::new(false);
140 let webview_sheet_url_sig: Signal<String> = Signal::new(MD_TEST_WEBVIEW_URL.to_string());
141 let disabled_code_ids: Signal<Vec<(NodeId, TextSelectMode)>> = Signal::new(Vec::new());
142 let disabled_doc_ids: Signal<Vec<(NodeId, TextSelectMode)>> = Signal::new(Vec::new());
143 let md_ctx: Context = current_context();
144 let active_theme_desc = move || -> String {
145 match theme_mode_sig.get() {
146 ThemeMode::Light => MD_THEME_DESC_LIGHT.to_string(),
147 ThemeMode::Dark => MD_THEME_DESC_DARK.to_string(),
148 }
149 };
150 let theme_desc_style = move || -> Style {
151 let live: ThemeTokens = use_live_tokens(tokens);
152 let live_eva: CodeThemePalette = CodeThemePalette::resolve(live.get_mode());
153 style! {
154 Width(Length::Percent(100.0))
155 FontSize(12.0)
156 FontFamily::Monospace
157 FontWeight::SemiBold
158 TextColor(live_eva.get_keyword())
159 }
160 };
161 let mode_label_style = move || -> Style {
162 let live: ThemeTokens = use_live_tokens(tokens);
163 style! {
164 Width(Length::Percent(100.0))
165 FontSize(12.0)
166 FontWeight::SemiBold
167 TextColor(live.get_palette().get_foreground())
168 }
169 };
170 let mode_desc_style = move || -> Style {
171 let live: ThemeTokens = use_live_tokens(tokens);
172 style! {
173 Width(Length::Percent(100.0))
174 FontSize(12.0)
175 TextColor(live.get_palette().get_muted_foreground())
176 }
177 };
178 let telemetry_style = move || -> Style {
179 let live: ThemeTokens = use_live_tokens(tokens);
180 style! {
181 Width(Length::Percent(100.0))
182 FontSize(12.0)
183 FontFamily::Monospace
184 TextColor(live.get_palette().get_primary())
185 }
186 };
187 let active_mode_desc = move || -> String {
188 let idx: usize = size_mode_sig.get();
189 size_mode_description(idx).to_string()
190 };
191 let swatch_nodes: Vec<NodeId> = (0usize..6usize)
192 .map(|slot: usize| {
193 let swatch_style = move || -> Style {
194 let live_eva: CodeThemePalette = CodeThemePalette::resolve(theme_mode_sig.get());
195 let col: Color = match slot {
196 0 => live_eva.get_background(),
197 1 => live_eva.get_keyword(),
198 2 => live_eva.get_function(),
199 3 => live_eva.get_number(),
200 4 => live_eva.get_constant(),
201 _ => live_eva.get_property(),
202 };
203 style! {
204 Width(Length::Px(22.0))
205 Height(Length::Px(22.0))
206 Radius(6.0)
207 Background(col)
208 Border::new(1.0, live_eva.get_border())
209 }
210 };
211 view! {
212 view {
213 bind_style: swatch_style
214 }
215 }
216 })
217 .collect();
218 let on_toggle_eva = move |_: &UiEvent| {
219 st_toggle.toggle_theme_mode();
220 };
221 let on_emit_pulse = move |_: &UiEvent| {
222 emit!(ShowcasePulseEvent {
223 source: ROUTE_MARKDOWN.to_string(),
224 seq: 1,
225 });
226 };
227 let code_ctx: Context = md_ctx.clone();
228 let build_code_tab = move || -> Vec<NodeId> {
229 let tab: usize = lang_tab.get();
230 let mode_idx: usize = size_mode_sig.get();
231 let mode: EuvCodeBlockSizeMode = resolve_code_block_size_mode(mode_idx);
232 let is_sel: bool = selectable_sig.get_untracked();
233 let live_tok: ThemeTokens = live_tokens_untracked(tokens);
234 let (src, lang, fname): (&str, &str, &str) = match tab {
235 1 => (MD_SAMPLE_TS, MD_LANG_TS, MD_FILE_TS),
236 2 => (MD_SAMPLE_JSON, MD_LANG_JSON, MD_FILE_JSON),
237 3 => (MD_SAMPLE_DIFF, MD_LANG_DIFF, MD_FILE_DIFF),
238 _ => (MD_SAMPLE_RUST, MD_LANG_RUST, MD_FILE_RUST),
239 };
240 let cb_log: Signal<String> = copy_log;
241 let block: NodeId = EuvCodeBlock::new(src.to_string(), live_tok)
242 .language(lang)
243 .filename(fname)
244 .line_numbers(true)
245 .size_mode(mode)
246 .max_height(MD_CODE_MAX_HEIGHT)
247 .text_select(TextSelectMode::SelectAndCopy)
248 .on_copy(move |txt: &str| {
249 let _: bool = cb_log.set(format!(
250 "Copied {} bytes ({lang}) via Eva Bold EuvCodeBlock",
251 txt.len()
252 ));
253 })
254 .mount();
255 let _: bool = {
256 if let Some(mut updated_value) = disabled_code_ids.try_get_untracked() {
257 {
258 let ids: &mut Vec<(NodeId, TextSelectMode)> = &mut updated_value;
259 ids.clear();
260 if !is_sel {
261 code_ctx.get_scene().with_write(
262 |tree: &mut SceneTree| {
263 apply_subtree_selectable_in_place(tree, block, false, ids);
264 },
265 (),
266 );
267 }
268 }
269 disabled_code_ids.set(updated_value)
270 } else {
271 false
272 }
273 };
274 if mode == EuvCodeBlockSizeMode::AutoExpandWidth {
275 let scroll_style: Style = style! {
276 Width(Length::Percent(100.0))
277 };
278 let wrapped: NodeId = view! {
279 scroll {
280 style: scroll_style
281 { block }
282 }
283 };
284 vec![wrapped]
285 } else {
286 vec![block]
287 }
288 };
289 let telemetry_text = move || -> String {
290 format!(
291 "Mode: {} | Selectable: {} | Tasks: {} | Copy: {} | Link: {}",
292 size_mode_label(size_mode_sig.get()),
293 selectable_sig.get(),
294 tasks_toggled.get(),
295 copy_log.get(),
296 link_log.get()
297 )
298 };
299 let live_tok_doc: ThemeTokens = live_tokens_untracked(tokens);
300 let md_doc_node: NodeId = EuvMarkdown::new(MD_FULL_DOCUMENT.to_string(), live_tok_doc)
301 .style(style! {
302 TextSelectMode::SelectAndCopy
303 })
304 .on_link(move |url: &str| {
305 if link_open_mode_sig.get_untracked() == 0 {
306 let _: bool = webview_sheet_url_sig.set(url.to_string());
307 let _: bool = webview_sheet_open_sig.set(true);
308 let _: bool = link_log_doc.set(format!("WebView Sheet: {url}"));
309 } else {
310 let launched: bool = open_external_url(url);
311 let _: bool = link_log_doc.set(format!("System Browser ({launched}): {url}"));
312 }
313 })
314 .on_copy(move |code: &str| {
315 let _: bool =
316 copy_log_doc.set(format!("Copied {} bytes from Markdown block", code.len()));
317 })
318 .on_toggle_task(move |_idx: usize, _checked: bool| {
319 let _: bool = tasks_doc.set(tasks_doc.get_untracked() + 1);
320 })
321 .mount();
322 let mut code_host_id: NodeId = NodeId::default();
323 let column_tertiary_style: Style = style! {
324 Width(Length::Percent(100.0))
325 };
326 let main_col: NodeId = view! {
327 EuvCard {
328 title: MD_DOC_CARD_TITLE.to_string()
329 tokens: tokens
330 subtitle: MD_DOC_CARD_SUB
331 column {
332 style: column_tertiary_style
333 { md_doc_node }
334 }
335 }
336 };
337 let column_style: Style = style! {
338 Width(Length::Percent(100.0))
339 Gap(SPACE_MD)
340 };
341 let row_style: Style = style! {
342 Width(Length::Percent(100.0))
343 AlignItems::Center
344 FlexWrap::Wrap
345 Gap(SPACE_SM)
346 };
347 let column_secondary_style: Style = style! {
348 Width(Length::Percent(100.0))
349 };
350 let rail_col: NodeId = view! {
351 column {
352 style: column_style
353 EuvCard {
354 title: MD_THEME_CARD_TITLE.to_string()
355 tokens: tokens
356 subtitle: MD_THEME_CARD_SUB
357 text {
358 bind_style: theme_desc_style
359 { active_theme_desc }
360 }
361 row {
362 style: row_style
363 ..swatch_nodes
364 }
365 EuvActionRow {
366 EuvButton {
367 label: BTN_TOGGLE_EVA_MODE.to_string()
368 tokens: tokens
369 small()
370 on_click: on_toggle_eva
371 }
372 EuvButton {
373 label: BTN_EMIT_PULSE.to_string()
374 tokens: tokens
375 outline()
376 small()
377 on_click: on_emit_pulse
378 }
379 }
380 EuvSwitch {
381 checked: selectable_sig
382 label: MD_SELECTABLE_LABEL.to_string()
383 tokens: tokens
384 }
385 }
386 EuvCard {
387 title: MD_CODE_CARD_TITLE.to_string()
388 tokens: tokens
389 subtitle: MD_CODE_CARD_SUB
390 EuvSegmented {
391 options: vec![
392 MD_TAB_RUST.to_string(),
393 MD_TAB_TS.to_string(),
394 MD_TAB_JSON.to_string(),
395 MD_TAB_DIFF.to_string(),
396 ]
397 selected: lang_tab
398 tokens: tokens
399 }
400 text {
401 bind_style: mode_label_style
402 { MD_SIZE_MODE_LABEL }
403 }
404 EuvSegmented {
405 options: vec![
406 MD_SIZE_MODE_AUTO.to_string(),
407 MD_SIZE_MODE_AUTO_H.to_string(),
408 MD_SIZE_MODE_AUTO_W.to_string(),
409 MD_SIZE_MODE_FIXED.to_string(),
410 ]
411 selected: size_mode_sig
412 tokens: tokens
413 }
414 text {
415 bind_style: mode_desc_style
416 { active_mode_desc }
417 }
418 column {
419 node_ref: code_host_id
420 style: column_secondary_style
421 bind_children: build_code_tab
422 }
423 }
424 EuvCard {
425 title: MD_LINK_BENCH_TITLE.to_string()
426 tokens: tokens
427 subtitle: MD_LINK_BENCH_SUB
428 EuvSegmented {
429 options: vec![
430 MD_LINK_MODE_WEBVIEW.to_string(),
431 MD_LINK_MODE_BROWSER.to_string(),
432 ]
433 selected: link_open_mode_sig
434 tokens: tokens
435 }
436 EuvActionRow {
437 EuvButton {
438 label: MD_BTN_OPEN_WEBVIEW.to_string()
439 tokens: tokens
440 small()
441 on_click: move |_: &UiEvent| {
442 let _: bool = webview_sheet_url_sig.set(MD_TEST_WEBVIEW_URL.to_string());
443 let _: bool = webview_sheet_open_sig.set(true);
444 let _: bool = link_log_wv_btn.set(format!("WebView Sheet: {MD_TEST_WEBVIEW_URL}"));
445 }
446 }
447 EuvExternalLink {
448 label: MD_BTN_OPEN_BROWSER.to_string()
449 url: MD_TEST_BROWSER_URL.to_string()
450 tokens: tokens
451 on_open: move |opened_url: &str, launched_ok: bool| {
452 let _: bool = link_log_ext_btn.set(format!("System Browser ({launched_ok}): {opened_url}"));
453 }
454 }
455 }
456 }
457 EuvCard {
458 title: MD_TELEMETRY_CARD_TITLE.to_string()
459 tokens: tokens
460 subtitle: MD_TELEMETRY_CARD_SUB
461 text {
462 bind_style: telemetry_style
463 { telemetry_text }
464 }
465 }
466 }
467 };
468 let studio_node: NodeId = mount_studio_layout(main_col, rail_col);
469 let webview_sheet_node: NodeId = view! {
470 EuvWebViewSheet {
471 open: webview_sheet_open_sig
472 url: webview_sheet_url_sig
473 title: MD_WEBVIEW_SHEET_TITLE.to_string()
474 tokens: tokens
475 }
476 };
477 let page_node: NodeId = view! {
478 EuvPage {
479 title: MD_STUDIO_TITLE.to_string()
480 subtitle: MD_STUDIO_SUBTITLE.to_string()
481 tokens: tokens
482 { studio_node }
483 { webview_sheet_node }
484 }
485 };
486 let prev_sel: Rc<Cell<bool>> = Rc::new(Cell::new(true));
487 let sel_ctx: Context = md_ctx.clone();
488 let (sel_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
489 Effect::new(move || {
490 let next_sel: bool = selectable_sig.get();
491 if prev_sel.replace(next_sel) == next_sel {
492 return;
493 }
494 let _: bool = {
495 if let Some(mut updated_value) = disabled_code_ids.try_get_untracked() {
496 {
497 let ids: &mut Vec<(NodeId, TextSelectMode)> = &mut updated_value;
498 sel_ctx.get_scene().with_write(
499 |tree: &mut SceneTree| {
500 apply_subtree_selectable_in_place(
501 tree,
502 code_host_id,
503 next_sel,
504 ids,
505 );
506 },
507 (),
508 );
509 }
510 disabled_code_ids.set(updated_value)
511 } else {
512 false
513 }
514 };
515 let _: bool = {
516 if let Some(mut updated_value) = disabled_doc_ids.try_get_untracked() {
517 {
518 let ids: &mut Vec<(NodeId, TextSelectMode)> = &mut updated_value;
519 sel_ctx.get_scene().with_write(
520 |tree: &mut SceneTree| {
521 apply_subtree_selectable_in_place(tree, md_doc_node, next_sel, ids);
522 },
523 (),
524 );
525 }
526 disabled_doc_ids.set(updated_value)
527 } else {
528 false
529 }
530 };
531 })
532 });
533 md_ctx.get_scene().with_write(
534 |tree: &mut SceneTree| {
535 tree.attach_scope(page_node, sel_scope);
536 },
537 (),
538 );
539 page_node
540}