use super::*;
pub(crate) fn reactive_preview_colors(palette: &ThemePalette, idx: usize) -> (Color, Color) {
match idx {
1 => (palette.get_secondary(), palette.get_secondary_foreground()),
2 => (palette.get_success(), palette.get_success_foreground()),
3 => (palette.get_warning(), palette.get_warning_foreground()),
_ => (palette.get_primary(), palette.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 palette: 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.set(counter.get_untracked() + 1);
};
let on_batch = move |_: &UiEvent| {
batch(|| {
for _ in 0..10 {
let _: bool = counter.set(counter.get_untracked() + 1);
}
});
};
let on_reset = move |_: &UiEvent| {
let _: bool = counter.set_if_changed(0);
};
let column_eighth_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_SM)
};
let text_tenth_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(20.0)
FontWeight::Bold
TextColor(palette.get_foreground())
};
let text_eleventh_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(15.0)
FontWeight::SemiBold
TextColor(palette.get_primary())
};
let text_twelfth_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.5)
FontFamily::Monospace
TextColor(palette.get_muted_foreground())
};
view! {
EuvCard {
title: REACTIVE_CARD_TITLE.to_string()
tokens: tokens
subtitle: REACTIVE_CARD_SUB
column {
style: column_eighth_style
text {
style: text_tenth_style
{ counter_text }
}
text {
style: text_eleventh_style
{ doubled_text }
}
text {
style: text_twelfth_style
{ derived_text }
}
EuvActionRow {
EuvButton {
label: BTN_INCREMENT.to_string()
tokens: tokens
on_click: on_inc
}
EuvButton {
label: BTN_BATCH_TEN.to_string()
tokens: tokens
outline()
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 palette: 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 = {
if let Some(mut updated_value) = local_count.try_get_untracked() {
{
let item_val: &mut u32 = &mut updated_value;
*item_val = item_val.saturating_add(1);
}
local_count.set(updated_value)
} else {
false
}
};
};
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());
};
let column_seventh_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_SM)
};
let text_ninth_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(13.5)
FontWeight::SemiBold
TextColor(palette.get_primary())
};
view! {
EuvCard {
title: REACTIVE_LOCAL_CARD_TITLE.to_string()
tokens: tokens
subtitle: REACTIVE_LOCAL_CARD_SUB
column {
style: column_seventh_style
text {
style: text_ninth_style
{ 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
small()
on_click: on_local_inc
}
EuvButton {
label: REACTIVE_BTN_LOCAL_RESET.to_string()
tokens: tokens
outline()
small()
on_click: on_local_reset
}
}
}
}
}
}
pub(crate) fn mount_reactive_store_card(tokens: ThemeTokens) -> (NodeId, Scope) {
let palette: 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 = {
if let Some(mut updated_value) = local_received.try_get_untracked() {
{
let n: &mut u32 = &mut updated_value;
*n = n.saturating_add(1);
}
local_received.set(updated_value)
} else {
false
}
};
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 column_sixth_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_SM)
};
let text_seventh_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(13.5)
FontWeight::SemiBold
TextColor(palette.get_card_foreground())
};
let text_eighth_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.5)
FontFamily::Monospace
TextColor(palette.get_muted_foreground())
};
let card: NodeId = view! {
EuvCard {
title: REACTIVE_STORE_CARD_TITLE.to_string()
tokens: tokens
subtitle: REACTIVE_STORE_CARD_SUB
column {
style: column_sixth_style
text {
style: text_seventh_style
{ store_line }
}
text {
style: text_eighth_style
{ listener_line }
}
EuvActionRow {
EuvButton {
label: REACTIVE_BTN_EMIT_PULSE.to_string()
tokens: tokens
small()
on_click: on_emit
}
}
}
}
};
(card, ev_scope)
}
pub(crate) fn mount_reactive_watch_card(
counter: Signal<i32>,
tokens: ThemeTokens,
) -> (NodeId, Scope) {
let palette: ThemePalette = tokens.get_palette();
let geometry: 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 = {
if let Some(mut updated_value) = watch_log.try_get_untracked() {
{
let entries: &mut Vec<String> = &mut updated_value;
entries.insert(0, line);
if entries.len() > REACTIVE_WATCH_LIMIT {
entries.truncate(REACTIVE_WATCH_LIMIT);
}
}
watch_log.set(updated_value)
} else {
false
}
};
})
});
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![{
let text_sixth_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.0)
FontFamily::Monospace
TextColor(live_p.get_muted_foreground())
};
view! {
text {
style: text_sixth_style
{ REACTIVE_WATCH_EMPTY }
}
}
}];
}
items
.into_iter()
.map(|entry: String| {
let text_fifth_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.0)
FontFamily::Monospace
TextColor(live_p.get_foreground())
};
view! {
text {
style: text_fifth_style
{ entry }
}
}
})
.collect()
};
let column_quaternary_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_SM)
};
let text_quaternary_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.5)
FontWeight::Medium
TextColor(palette.get_primary())
};
let column_fifth_style: Style = style! {
Width(Length::Percent(100.0))
Padding(Edges::all(SPACE_SM))
Gap(SPACE_XS)
Background(palette.get_muted())
Border::new(geometry.get_border_width(), palette.get_border())
Radius(geometry.get_radius_md())
Overflow::Hidden
};
let card: NodeId = view! {
EuvCard {
title: REACTIVE_WATCH_CARD_TITLE.to_string()
tokens: tokens
subtitle: REACTIVE_WATCH_CARD_SUB
column {
style: column_quaternary_style
text {
style: text_quaternary_style
{ watch_count_text }
}
column {
style: column_fifth_style
bind_children: build_watch_rows
}
EuvActionRow {
EuvButton {
label: REACTIVE_BTN_CLEAR_WATCH.to_string()
tokens: tokens
outline()
small()
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! {
Width(Length::Percent(100.0))
FontSize(14.0)
FontWeight::Bold
TextColor(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! {
Width(Length::Percent(100.0))
FontSize(12.0)
FontFamily::Monospace
TextColor(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}")
};
let column_tertiary_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_SM)
};
view! {
EuvCard {
title: REACTIVE_TOKENS_CARD_TITLE.to_string()
tokens: tokens
subtitle: REACTIVE_TOKENS_CARD_SUB
column {
style: column_tertiary_style
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 palette: 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 = {
if let Some(mut updated_value) = items_sig.try_get_untracked() {
{
let list: &mut Vec<(u32, String)> = &mut updated_value;
list.push((id, label));
}
items_sig.set(updated_value)
} else {
false
}
};
let _: bool = {
if let Some(mut updated_value) = ops_sig.try_get_untracked() {
{
let item_count: &mut u32 = &mut updated_value;
*item_count = item_count.saturating_add(1);
}
ops_sig.set(updated_value)
} else {
false
}
};
};
let on_shuffle = move |_: &UiEvent| {
let _: bool = {
if let Some(mut updated_value) = items_sig.try_get_untracked() {
{
let list: &mut Vec<(u32, String)> = &mut updated_value;
if list.len() >= 2 {
list.rotate_left(1);
if list.len() >= 3 {
let last: usize = list.len() - 1;
list.swap(0, last);
}
}
}
items_sig.set(updated_value)
} else {
false
}
};
let _: bool = {
if let Some(mut updated_value) = ops_sig.try_get_untracked() {
{
let node_entry: &mut u32 = &mut updated_value;
*node_entry = node_entry.saturating_add(1);
}
ops_sig.set(updated_value)
} else {
false
}
};
};
let on_remove_first = move |_: &UiEvent| {
let _: bool = {
if let Some(mut updated_value) = items_sig.try_get_untracked() {
{
let list: &mut Vec<(u32, String)> = &mut updated_value;
if !list.is_empty() {
let _: (u32, String) = list.remove(0);
}
}
items_sig.set(updated_value)
} else {
false
}
};
let _: bool = {
if let Some(mut updated_value) = ops_sig.try_get_untracked() {
{
let normal_vec: &mut u32 = &mut updated_value;
*normal_vec = normal_vec.saturating_add(1);
}
ops_sig.set(updated_value)
} else {
false
}
};
};
let on_reset = move |_: &UiEvent| {
let _: bool = items_sig.set(reactive_seed_list());
let _: bool = next_id_sig.set(5);
let _: bool = {
if let Some(mut updated_value) = ops_sig.try_get_untracked() {
{
let total_len: &mut u32 = &mut updated_value;
*total_len = total_len.saturating_add(1);
}
ops_sig.set(updated_value)
} else {
false
}
};
};
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![{
let text_tertiary_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.5)
TextColor(live_p.get_muted_foreground())
};
view! {
text {
style: text_tertiary_style
{ 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}");
let row_style: Style = style! {
Width(Length::Percent(100.0))
Padding(Edges::symmetric(SPACE_SM, SPACE_MD))
AlignItems::Center
Gap(SPACE_SM)
Background(live_p.get_muted())
Border::new(live_g.get_border_width(), live_p.get_border())
Radius(live_g.get_radius_sm())
Overflow::Hidden
};
let text_secondary_style: Style = style! {
FlexGrow(1.0)
FlexShrink(1.0)
MinWidth(Length::Px(0.0))
FontSize(12.5)
TextColor(live_p.get_foreground())
};
view! {
row {
style: row_style
EuvBadge {
label: badge_txt
tone: EuvTone::Primary
tokens: live
}
text {
style: text_secondary_style
{ row_label }
}
}
}
})
.collect()
};
let column_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_SM)
};
let text_style: Style = style! {
Width(Length::Percent(100.0))
FontSize(12.5)
FontWeight::SemiBold
TextColor(palette.get_primary())
};
let column_secondary_style: Style = style! {
Width(Length::Percent(100.0))
Gap(SPACE_XS)
};
view! {
EuvCard {
title: REACTIVE_LIST_CARD_TITLE.to_string()
tokens: tokens
subtitle: REACTIVE_LIST_CARD_SUB
column {
style: column_style
text {
style: text_style
{ summary_text }
}
EuvActionRow {
EuvButton {
label: REACTIVE_BTN_ADD_ITEM.to_string()
tokens: tokens
small()
on_click: on_add
}
EuvButton {
label: REACTIVE_BTN_SHUFFLE_ITEMS.to_string()
tokens: tokens
outline()
small()
on_click: on_shuffle
}
EuvButton {
label: REACTIVE_BTN_REMOVE_FIRST.to_string()
tokens: tokens
outline()
small()
on_click: on_remove_first
}
EuvButton {
label: REACTIVE_BTN_RESET_LIST.to_string()
tokens: tokens
ghost()
small()
on_click: on_reset
}
}
column {
style: column_secondary_style
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
}