use super::*;
pub(crate) fn reactive_preview_colors(p: &ThemePalette, idx: usize) -> (Color, Color) {
match idx {
1 => (p.get_secondary(), p.get_secondary_foreground()),
2 => (p.get_success(), p.get_success_foreground()),
3 => (p.get_warning(), p.get_warning_foreground()),
_ => (p.get_primary(), p.get_primary_foreground()),
}
}
pub(crate) fn reactive_seed_list() -> Vec<(u32, String)> {
REACTIVE_SEED_ITEMS
.iter()
.map(|&(id, label): &(u32, &str)| (id, label.to_string()))
.collect()
}
pub(crate) fn mount_reactive_signal_card(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
let p: ThemePalette = tokens.get_palette();
let counter: Signal<i32> = state.get_counter();
let doubled: Memo<i32> = state.get_doubled();
let derived_info: Memo<String> = computed!((counter) -> String => move |n: i32| {
let parity: &str = if n % 2 == 0 {
REACTIVE_PARITY_EVEN
} else {
REACTIVE_PARITY_ODD
};
let sq: i64 = (n as i64) * (n as i64);
format!("Derived computed!: square={sq} | parity={parity}")
});
let counter_text = move || -> String { format!("{PREFIX_COUNTER}{}", counter.get()) };
let doubled_text = move || -> String { format!("{PREFIX_DOUBLED}{}", doubled.get()) };
let derived_text = move || -> String { derived_info.get() };
let on_inc = move |_: &UiEvent| {
let _: bool = counter.update(|v: &mut i32| *v += 1);
};
let on_batch = move |_: &UiEvent| {
batch(|| {
for _ in 0..10 {
let _: bool = counter.update(|v: &mut i32| *v += 1);
}
});
};
let on_reset = move |_: &UiEvent| {
let _: bool = counter.set_if_changed(0);
};
view! {
EuvCard {
title: REACTIVE_CARD_TITLE.to_string()
tokens: tokens
subtitle: REACTIVE_CARD_SUB
column {
style: {
width: Length::Percent(100.0)
gap: SPACE_SM
}
text {
style: {
width: Length::Percent(100.0)
font_size: 20.0
font_weight: FontWeight::Bold
color: p.get_foreground()
}
{ counter_text }
}
text {
style: {
width: Length::Percent(100.0)
font_size: 15.0
font_weight: FontWeight::SemiBold
color: p.get_primary()
}
{ doubled_text }
}
text {
style: {
width: Length::Percent(100.0)
font_size: 12.5
font_family: FontFamily::Monospace
color: p.get_muted_foreground()
}
{ derived_text }
}
EuvActionRow {
EuvButton {
label: BTN_INCREMENT.to_string()
tokens: tokens
on_click: on_inc
}
EuvButton {
label: BTN_BATCH_TEN.to_string()
tokens: tokens
secondary()
on_click: on_batch
}
EuvButton {
label: BTN_RESET.to_string()
tokens: tokens
outline()
on_click: on_reset
}
}
}
}
}
}
pub(crate) fn mount_reactive_local_card(tokens: ThemeTokens) -> NodeId {
let p: ThemePalette = tokens.get_palette();
let local_count: LocalState<u32> = state!(1_u32);
let local_note: Signal<String> = Signal::new(REACTIVE_LOCAL_DEFAULT_NOTE.to_string());
let local_summary = move || -> String {
format!(
"LocalState count: {} | Note: {}",
local_count.get(),
local_note.get()
)
};
let on_local_inc = move |_: &UiEvent| {
let _: bool = local_count.update(|v: &mut u32| *v = v.saturating_add(1));
};
let on_local_reset = move |_: &UiEvent| {
let _: bool = local_count.set_if_changed(1);
let _: bool = local_note.set_if_changed(REACTIVE_LOCAL_DEFAULT_NOTE.to_string());
};
view! {
EuvCard {
title: REACTIVE_LOCAL_CARD_TITLE.to_string()
tokens: tokens
subtitle: REACTIVE_LOCAL_CARD_SUB
column {
style: {
width: Length::Percent(100.0)
gap: SPACE_SM
}
text {
style: {
width: Length::Percent(100.0)
font_size: 13.5
font_weight: FontWeight::SemiBold
color: p.get_primary()
}
{ local_summary }
}
EuvInput {
value: local_note
placeholder: REACTIVE_LOCAL_PLACEHOLDER.to_string()
tokens: tokens
}
EuvActionRow {
EuvButton {
label: REACTIVE_BTN_LOCAL_INC.to_string()
tokens: tokens
sm()
on_click: on_local_inc
}
EuvButton {
label: REACTIVE_BTN_LOCAL_RESET.to_string()
tokens: tokens
outline()
sm()
on_click: on_local_reset
}
}
}
}
}
}
pub(crate) fn mount_reactive_store_card(tokens: ThemeTokens) -> (NodeId, Scope) {
let p: ThemePalette = tokens.get_palette();
let global_banner: Signal<ShowcaseGlobalBanner> =
state!(global ShowcaseGlobalBanner = ShowcaseGlobalBanner::default());
let local_seq: Signal<u32> = Signal::new(0);
let local_received: Signal<u32> = Signal::new(0);
let local_last_src: Signal<String> = Signal::new(REACTIVE_SOURCE_NONE.to_string());
let (ev_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
on!(ShowcasePulseEvent => move |ev: &ShowcasePulseEvent| {
let _: bool = local_received.update(|n: &mut u32| *n = n.saturating_add(1));
let _: bool = local_last_src.set_if_changed(ev.get_source().clone());
})
});
let store_line = move || -> String {
let gb: ShowcaseGlobalBanner = global_banner.get();
let src: &str = if gb.get_last_source().is_empty() {
REACTIVE_SOURCE_NONE
} else {
gb.get_last_source().as_str()
};
format!(
"ShowcaseGlobalBanner -> pulses: {} | last_source: {src}",
gb.get_pulses()
)
};
let listener_line = move || -> String {
format!(
"Local on!(ShowcasePulseEvent) -> received: {} | last: {}",
local_received.get(),
local_last_src.get()
)
};
let on_emit = move |_: &UiEvent| {
let next_seq: u32 = local_seq.get_untracked().saturating_add(1);
let _: bool = local_seq.set(next_seq);
emit!(ShowcasePulseEvent {
source: ROUTE_REACTIVE.to_string(),
seq: next_seq,
});
};
let card: NodeId = view! {
EuvCard {
title: REACTIVE_STORE_CARD_TITLE.to_string()
tokens: tokens
subtitle: REACTIVE_STORE_CARD_SUB
column {
style: {
width: Length::Percent(100.0)
gap: SPACE_SM
}
text {
style: {
width: Length::Percent(100.0)
font_size: 13.5
font_weight: FontWeight::SemiBold
color: p.get_card_foreground()
}
{ store_line }
}
text {
style: {
width: Length::Percent(100.0)
font_size: 12.5
font_family: FontFamily::Monospace
color: p.get_muted_foreground()
}
{ listener_line }
}
EuvActionRow {
EuvButton {
label: REACTIVE_BTN_EMIT_PULSE.to_string()
tokens: tokens
sm()
on_click: on_emit
}
}
}
}
};
(card, ev_scope)
}
pub(crate) fn mount_reactive_watch_card(
counter: Signal<i32>,
tokens: ThemeTokens,
) -> (NodeId, Scope) {
let p: ThemePalette = tokens.get_palette();
let g: ThemeGeometry = tokens.get_geometry();
let watch_log: Signal<Vec<String>> = Signal::new(Vec::new());
let watch_seq: Signal<u32> = Signal::new(0);
let (watch_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
watch!((counter) => move |val: i32| {
let seq: u32 = watch_seq.get_untracked().saturating_add(1);
let _: bool = watch_seq.set(seq);
let line: String = format!("#{seq} watch!(counter) => {val} (x2 = {})", val * 2);
let _: bool = watch_log.update(|entries: &mut Vec<String>| {
entries.insert(0, line);
if entries.len() > REACTIVE_WATCH_LIMIT {
entries.truncate(REACTIVE_WATCH_LIMIT);
}
});
})
});
let watch_count_text = move || -> String {
format!(
"Observed transitions: {} (showing latest {REACTIVE_WATCH_LIMIT})",
watch_seq.get()
)
};
let on_clear = move |_: &UiEvent| {
let _: bool = watch_log.set_if_changed(Vec::new());
};
let build_watch_rows = move || -> Vec<NodeId> {
let live: ThemeTokens = live_tokens_untracked(tokens);
let live_p: ThemePalette = live.get_palette();
let items: Vec<String> = watch_log.get();
if items.is_empty() {
return vec![view! {
text {
style: {
width: Length::Percent(100.0)
font_size: 12.0
font_family: FontFamily::Monospace
color: live_p.get_muted_foreground()
}
{ REACTIVE_WATCH_EMPTY }
}
}];
}
items
.into_iter()
.map(|entry: String| {
view! {
text {
style: {
width: Length::Percent(100.0)
font_size: 12.0
font_family: FontFamily::Monospace
color: live_p.get_foreground()
}
{ entry }
}
}
})
.collect()
};
let card: NodeId = view! {
EuvCard {
title: REACTIVE_WATCH_CARD_TITLE.to_string()
tokens: tokens
subtitle: REACTIVE_WATCH_CARD_SUB
column {
style: {
width: Length::Percent(100.0)
gap: SPACE_SM
}
text {
style: {
width: Length::Percent(100.0)
font_size: 12.5
font_weight: FontWeight::Medium
color: p.get_primary()
}
{ watch_count_text }
}
column {
style: {
width: Length::Percent(100.0)
padding: Edges::all(SPACE_SM)
gap: SPACE_XS
background: p.get_muted()
border: (g.get_border_width(), p.get_border())
radius: g.get_radius_md()
overflow: Overflow::Hidden
}
bind_children: build_watch_rows
}
EuvActionRow {
EuvButton {
label: REACTIVE_BTN_CLEAR_WATCH.to_string()
tokens: tokens
outline()
sm()
on_click: on_clear
}
}
}
}
};
(card, watch_scope)
}
pub(crate) fn mount_reactive_tokens_card(tokens: ThemeTokens) -> NodeId {
var! {
const REACTIVE_VAR_PAD_SM: f32 = 8.0;
const REACTIVE_VAR_PAD_MD: f32 = 14.0;
const REACTIVE_VAR_PAD_LG: f32 = 20.0;
const REACTIVE_VAR_RAD_SM: f32 = 6.0;
const REACTIVE_VAR_RAD_MD: f32 = 12.0;
const REACTIVE_VAR_RAD_LG: f32 = 18.0;
}
let density_idx: Signal<usize> = Signal::new(1);
let role_idx: Signal<usize> = Signal::new(0);
let base_box: Style = style! {
FlexDirection::Column
Overflow::Hidden
width: Length::Percent(100.0)
gap: SPACE_XS
};
let preview_box_style = move || -> Style {
let live: ThemeTokens = use_live_tokens(tokens);
let (bg, _): (Color, Color) = reactive_preview_colors(&live.get_palette(), role_idx.get());
let (pad, rad): (f32, f32) = match density_idx.get() {
0 => (REACTIVE_VAR_PAD_SM, REACTIVE_VAR_RAD_SM),
2 => (REACTIVE_VAR_PAD_LG, REACTIVE_VAR_RAD_LG),
_ => (REACTIVE_VAR_PAD_MD, REACTIVE_VAR_RAD_MD),
};
base_box.padding(Edges::all(pad)).background(bg).radius(rad)
};
let preview_title_style = move || -> Style {
let live: ThemeTokens = use_live_tokens(tokens);
let (_, fg): (Color, Color) = reactive_preview_colors(&live.get_palette(), role_idx.get());
Style::default()
.width(Length::Percent(100.0))
.font_size(14.0)
.font_weight(FontWeight::Bold)
.color(fg)
};
let preview_meta_style = move || -> Style {
let live: ThemeTokens = use_live_tokens(tokens);
let (_, fg): (Color, Color) = reactive_preview_colors(&live.get_palette(), role_idx.get());
Style::default()
.width(Length::Percent(100.0))
.font_size(12.0)
.font_family(FontFamily::Monospace)
.color(fg)
};
let preview_meta_text = move || -> String {
let (pad, rad, label): (f32, f32, &str) = match density_idx.get() {
0 => (
REACTIVE_VAR_PAD_SM,
REACTIVE_VAR_RAD_SM,
REACTIVE_DENSITY_COMPACT,
),
2 => (
REACTIVE_VAR_PAD_LG,
REACTIVE_VAR_RAD_LG,
REACTIVE_DENSITY_ROOMY,
),
_ => (
REACTIVE_VAR_PAD_MD,
REACTIVE_VAR_RAD_MD,
REACTIVE_DENSITY_BALANCED,
),
};
let role_name: &str = match role_idx.get() {
1 => REACTIVE_ROLE_SECONDARY,
2 => REACTIVE_ROLE_SUCCESS,
3 => REACTIVE_ROLE_WARNING,
_ => REACTIVE_ROLE_PRIMARY,
};
format!("var! pad={pad:.0}px radius={rad:.0}px ({label}) | Role: {role_name}")
};
view! {
EuvCard {
title: REACTIVE_TOKENS_CARD_TITLE.to_string()
tokens: tokens
subtitle: REACTIVE_TOKENS_CARD_SUB
column {
style: {
width: Length::Percent(100.0)
gap: SPACE_SM
}
EuvSegmented {
options: vec![
REACTIVE_DENSITY_COMPACT.to_string(),
REACTIVE_DENSITY_BALANCED.to_string(),
REACTIVE_DENSITY_ROOMY.to_string(),
]
selected: density_idx
tokens: tokens
}
EuvSegmented {
options: vec![
REACTIVE_ROLE_PRIMARY.to_string(),
REACTIVE_ROLE_SECONDARY.to_string(),
REACTIVE_ROLE_SUCCESS.to_string(),
REACTIVE_ROLE_WARNING.to_string(),
]
selected: role_idx
tokens: tokens
}
view {
bind_style: preview_box_style
text {
bind_style: preview_title_style
{ REACTIVE_TOKEN_SAMPLE_TITLE }
}
text {
bind_style: preview_meta_style
{ preview_meta_text }
}
}
}
}
}
}
pub(crate) fn mount_reactive_list_card(tokens: ThemeTokens) -> NodeId {
let p: ThemePalette = tokens.get_palette();
let items_sig: Signal<Vec<(u32, String)>> = Signal::new(reactive_seed_list());
let next_id_sig: Signal<u32> = Signal::new(5);
let ops_sig: Signal<u32> = Signal::new(0);
let summary_text = move || -> String {
format!(
"Active Keys: {} | Mutations: {}",
items_sig.get().len(),
ops_sig.get()
)
};
let on_add = move |_: &UiEvent| {
let id: u32 = next_id_sig.get_untracked();
let _: bool = next_id_sig.set(id.saturating_add(1));
let label_idx: usize = (id as usize) % REACTIVE_EXTRA_LABELS.len();
let label: String = format!("{} #{id}", REACTIVE_EXTRA_LABELS[label_idx]);
let _: bool = items_sig.update(|list: &mut Vec<(u32, String)>| {
list.push((id, label));
});
let _: bool = ops_sig.update(|n: &mut u32| *n = n.saturating_add(1));
};
let on_shuffle = move |_: &UiEvent| {
let _: bool = items_sig.update(|list: &mut Vec<(u32, String)>| {
if list.len() >= 2 {
list.rotate_left(1);
if list.len() >= 3 {
let last: usize = list.len() - 1;
list.swap(0, last);
}
}
});
let _: bool = ops_sig.update(|n: &mut u32| *n = n.saturating_add(1));
};
let on_remove_first = move |_: &UiEvent| {
let _: bool = items_sig.update(|list: &mut Vec<(u32, String)>| {
if !list.is_empty() {
let _: (u32, String) = list.remove(0);
}
});
let _: bool = ops_sig.update(|n: &mut u32| *n = n.saturating_add(1));
};
let on_reset = move |_: &UiEvent| {
let _: bool = items_sig.set(reactive_seed_list());
let _: bool = next_id_sig.set(5);
let _: bool = ops_sig.update(|n: &mut u32| *n = n.saturating_add(1));
};
let build_list_rows = move || -> Vec<NodeId> {
let live: ThemeTokens = live_tokens_untracked(tokens);
let live_p: ThemePalette = live.get_palette();
let live_g: ThemeGeometry = live.get_geometry();
let current: Vec<(u32, String)> = items_sig.get();
if current.is_empty() {
return vec![view! {
text {
style: {
width: Length::Percent(100.0)
font_size: 12.5
color: live_p.get_muted_foreground()
}
{ REACTIVE_LIST_EMPTY }
}
}];
}
current
.into_iter()
.enumerate()
.map(|(pos, (key_id, label)): (usize, (u32, String))| {
let badge_txt: String = format!("#{key_id}");
let row_label: String = format!("[{pos}] {label}");
view! {
row {
style: {
width: Length::Percent(100.0)
padding: Edges::symmetric(SPACE_SM, SPACE_MD)
align: AlignItems::Center
gap: SPACE_SM
background: live_p.get_muted()
border: (live_g.get_border_width(), live_p.get_border())
radius: live_g.get_radius_sm()
overflow: Overflow::Hidden
}
EuvBadge {
label: badge_txt
tone: EuvTone::Primary
tokens: live
}
text {
style: {
flex_grow: 1.0
flex_shrink: 1.0
min_width: Length::Px(0.0)
font_size: 12.5
color: live_p.get_foreground()
}
{ row_label }
}
}
}
})
.collect()
};
view! {
EuvCard {
title: REACTIVE_LIST_CARD_TITLE.to_string()
tokens: tokens
subtitle: REACTIVE_LIST_CARD_SUB
column {
style: {
width: Length::Percent(100.0)
gap: SPACE_SM
}
text {
style: {
width: Length::Percent(100.0)
font_size: 12.5
font_weight: FontWeight::SemiBold
color: p.get_primary()
}
{ summary_text }
}
EuvActionRow {
EuvButton {
label: REACTIVE_BTN_ADD_ITEM.to_string()
tokens: tokens
sm()
on_click: on_add
}
EuvButton {
label: REACTIVE_BTN_SHUFFLE_ITEMS.to_string()
tokens: tokens
secondary()
sm()
on_click: on_shuffle
}
EuvButton {
label: REACTIVE_BTN_REMOVE_FIRST.to_string()
tokens: tokens
outline()
sm()
on_click: on_remove_first
}
EuvButton {
label: REACTIVE_BTN_RESET_LIST.to_string()
tokens: tokens
ghost()
sm()
on_click: on_reset
}
}
column {
style: {
width: Length::Percent(100.0)
gap: SPACE_XS
}
bind_children: build_list_rows
}
}
}
}
}
pub fn mount_reactive_page(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
let ctx: Context = current_context();
let signal_card: NodeId = mount_reactive_signal_card(state, tokens);
let local_card: NodeId = mount_reactive_local_card(tokens);
let (store_card, ev_scope): (NodeId, Scope) = mount_reactive_store_card(tokens);
let (watch_card, watch_scope): (NodeId, Scope) =
mount_reactive_watch_card(state.get_counter(), tokens);
let tokens_card: NodeId = mount_reactive_tokens_card(tokens);
let list_card: NodeId = mount_reactive_list_card(tokens);
let grid: NodeId = mount_responsive_grid(
vec![
signal_card,
local_card,
store_card,
watch_card,
tokens_card,
list_card,
],
2,
);
let page: NodeId = view! {
EuvPage {
title: REACTIVE_TITLE.to_string()
subtitle: REACTIVE_SUBTITLE.to_string()
tokens: tokens
{ grid }
}
};
ctx.get_scene().with_write(
|tree: &mut SceneTree| {
tree.attach_scope(store_card, ev_scope);
tree.attach_scope(watch_card, watch_scope);
},
(),
);
page
}