use super::*;
fn mermaid_category_indices(cat_idx: usize) -> &'static [usize] {
match cat_idx {
1 => &[0, 1, 2],
2 => &[3, 4, 8],
3 => &[5, 6, 7, 9, 10],
_ => &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
}
}
fn mermaid_family_category_name(fam_idx: usize) -> &'static str {
match fam_idx {
0..=2 => MERMAID_CAT_FLOW,
3 | 4 | 8 => MERMAID_CAT_MODEL,
_ => MERMAID_CAT_PLAN,
}
}
fn build_mermaid_family_row(
cat_idx: usize,
family_sig: Signal<usize>,
tokens: ThemeTokens,
) -> NodeId {
let active_fam: usize = family_sig.get();
let indices: &'static [usize] = mermaid_category_indices(cat_idx);
let mut row: EuvActionRow = EuvActionRow::default();
for &idx in indices.iter() {
let label: &'static str = MERMAID_FAMILY_LABELS[idx];
let on_pick = move |_: &UiEvent| {
let _: bool = family_sig.set_if_changed(idx);
};
let btn: NodeId = if active_fam == idx {
view! {
EuvButton {
label: label.to_string()
tokens: tokens
sm()
on_click: on_pick
}
}
} else {
view! {
EuvButton {
label: label.to_string()
tokens: tokens
outline()
sm()
on_click: on_pick
}
}
};
row = row.child(btn);
}
row.mount()
}
pub fn mount_mermaid_page(_state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
let cat_sig: Signal<usize> = Signal::new(0);
let family_sig: Signal<usize> = Signal::new(0);
let copy_log: Signal<String> = Signal::new(MERMAID_COPY_READY.to_string());
let ctx: Context = current_context();
let on_prev = move |_: &UiEvent| {
let cur_cat: usize = cat_sig.get_untracked();
let list: &'static [usize] = mermaid_category_indices(cur_cat);
let cur_fam: usize = family_sig.get_untracked();
let pos: usize = list.iter().position(|&i: &usize| i == cur_fam).unwrap_or(0);
let prev_pos: usize = if pos == 0 { list.len() - 1 } else { pos - 1 };
let _: bool = family_sig.set_if_changed(list[prev_pos]);
};
let on_next = move |_: &UiEvent| {
let cur_cat: usize = cat_sig.get_untracked();
let list: &'static [usize] = mermaid_category_indices(cur_cat);
let cur_fam: usize = family_sig.get_untracked();
let pos: usize = list.iter().position(|&i: &usize| i == cur_fam).unwrap_or(0);
let next_pos: usize = (pos + 1) % list.len();
let _: bool = family_sig.set_if_changed(list[next_pos]);
};
let active_summary_text = move || -> String {
let idx: usize = family_sig.get().min(MERMAID_FAMILY_COUNT - 1);
format!(
"{} ({}/{}) — {}",
MERMAID_FAMILY_LABELS[idx],
idx + 1,
MERMAID_FAMILY_COUNT,
MERMAID_FAMILY_SUMMARIES[idx]
)
};
let telemetry_text = move || -> String {
let idx: usize = family_sig.get().min(MERMAID_FAMILY_COUNT - 1);
let parsed: MermaidDiagram = parse_mermaid_diagram(MERMAID_FAMILY_SOURCES[idx]);
let sz: Size = measure_mermaid_diagram(&parsed);
format!(
"Family: {} | Category: {} | Intrinsic: {:.0}x{:.0}px | {}",
MERMAID_FAMILY_LABELS[idx],
mermaid_family_category_name(idx),
sz.get_width(),
sz.get_height(),
copy_log.get()
)
};
let summary_style = move || -> Style {
let live: ThemeTokens = use_live_tokens(tokens);
Style::default()
.width(Length::Percent(100.0))
.font_size(12.5)
.color(live.get_palette().get_muted_foreground())
};
let telemetry_style = move || -> Style {
let live: ThemeTokens = use_live_tokens(tokens);
Style::default()
.width(Length::Percent(100.0))
.font_size(12.0)
.font_family(FontFamily::Monospace)
.color(live.get_palette().get_primary())
};
let build_family_bar = move || -> Vec<NodeId> {
let c: usize = cat_sig.get();
let live_tok: ThemeTokens = live_tokens_untracked(tokens);
vec![build_mermaid_family_row(c, family_sig, live_tok)]
};
let build_diagram_slot = move || -> Vec<NodeId> {
let idx: usize = family_sig.get().min(MERMAID_FAMILY_COUNT - 1);
let src: &'static str = MERMAID_FAMILY_SOURCES[idx];
let label: &'static str = MERMAID_FAMILY_LABELS[idx];
let live_tok: ThemeTokens = live_tokens_untracked(tokens);
let cb_log: Signal<String> = copy_log;
let diagram_node: NodeId = EuvMermaid::new(src.to_string(), live_tok)
.style(style! {
max_height: Length::Px(MERMAID_INLINE_MAX_HEIGHT)
})
.on_copy(move |raw: &str| {
let _: bool = cb_log.set(format!("Copied {} bytes ({label})", raw.len()));
})
.mount();
vec![diagram_node]
};
let build_source_slot = move || -> Vec<NodeId> {
let idx: usize = family_sig.get().min(MERMAID_FAMILY_COUNT - 1);
let src: &'static str = MERMAID_FAMILY_SOURCES[idx];
let label: &'static str = MERMAID_FAMILY_LABELS[idx];
let fname: &'static str = MERMAID_FAMILY_FILES[idx];
let live_tok: ThemeTokens = live_tokens_untracked(tokens);
let cb_log: Signal<String> = copy_log;
let code_node: NodeId = EuvCodeBlock::new(src.to_string(), live_tok)
.language(MERMAID_CODE_LANG)
.filename(fname)
.line_numbers(true)
.size_mode(EuvCodeBlockSizeMode::Fixed)
.max_height(MERMAID_SOURCE_MAX_HEIGHT)
.selectable(true)
.on_copy(move |raw: &str| {
let _: bool = cb_log.set(format!("Copied {} bytes ({label} source)", raw.len()));
})
.mount();
vec![code_node]
};
let main_col: NodeId = view! {
EuvCard {
title: MERMAID_CANVAS_TITLE.to_string()
tokens: tokens
subtitle: MERMAID_CANVAS_SUB
text {
bind_style: summary_style
{ active_summary_text }
}
column {
style: {
width: Length::Percent(100.0)
}
bind_children: build_diagram_slot
}
}
};
let rail_col: NodeId = view! {
column {
style: {
width: Length::Percent(100.0)
gap: SPACE_MD
}
EuvCard {
title: MERMAID_SOURCE_TITLE.to_string()
tokens: tokens
subtitle: MERMAID_SOURCE_SUB
column {
style: {
width: Length::Percent(100.0)
}
bind_children: build_source_slot
}
}
EuvCard {
title: MERMAID_TIPS_TITLE.to_string()
tokens: tokens
subtitle: MERMAID_TIPS_SUB
text {
bind_style: telemetry_style
{ telemetry_text }
}
}
}
};
let studio_node: NodeId = mount_studio_layout(main_col, rail_col);
let page_node: NodeId = view! {
EuvPage {
title: MERMAID_TITLE.to_string()
subtitle: MERMAID_SUBTITLE.to_string()
tokens: tokens
EuvCard {
title: MERMAID_EXPLORER_TITLE.to_string()
tokens: tokens
subtitle: MERMAID_EXPLORER_SUB
EuvSegmented {
options: vec![
MERMAID_CAT_ALL.to_string(),
MERMAID_CAT_FLOW.to_string(),
MERMAID_CAT_MODEL.to_string(),
MERMAID_CAT_PLAN.to_string(),
]
selected: cat_sig
tokens: tokens
}
column {
style: {
width: Length::Percent(100.0)
}
bind_children: build_family_bar
}
EuvActionRow {
EuvButton {
label: MERMAID_BTN_PREV.to_string()
tokens: tokens
outline()
sm()
on_click: on_prev
}
EuvButton {
label: MERMAID_BTN_NEXT.to_string()
tokens: tokens
secondary()
sm()
on_click: on_next
}
}
}
{ studio_node }
}
};
let (cat_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
Effect::new(move || {
let c: usize = cat_sig.get();
let list: &'static [usize] = mermaid_category_indices(c);
let cur: usize = family_sig.get_untracked();
if !list.contains(&cur)
&& let Some(&first) = list.first()
{
let _: bool = family_sig.set_if_changed(first);
}
})
});
ctx.get_scene().with_write(
|tree: &mut SceneTree| {
tree.attach_scope(page_node, cat_scope);
},
(),
);
page_node
}