euv_example/pages/reactive/
fn.rs1use super::*;
2
3pub(crate) fn reactive_preview_colors(p: &ThemePalette, idx: usize) -> (Color, Color) {
14 match idx {
15 1 => (p.get_secondary(), p.get_secondary_foreground()),
16 2 => (p.get_success(), p.get_success_foreground()),
17 3 => (p.get_warning(), p.get_warning_foreground()),
18 _ => (p.get_primary(), p.get_primary_foreground()),
19 }
20}
21
22pub(crate) fn reactive_seed_list() -> Vec<(u32, String)> {
28 REACTIVE_SEED_ITEMS
29 .iter()
30 .map(|&(id, label): &(u32, &str)| (id, label.to_string()))
31 .collect()
32}
33
34pub(crate) fn mount_reactive_signal_card(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
45 let p: ThemePalette = tokens.get_palette();
46 let counter: Signal<i32> = state.get_counter();
47 let doubled: Memo<i32> = state.get_doubled();
48 let derived_info: Memo<String> = computed!((counter) -> String => move |n: i32| {
49 let parity: &str = if n % 2 == 0 {
50 REACTIVE_PARITY_EVEN
51 } else {
52 REACTIVE_PARITY_ODD
53 };
54 let sq: i64 = (n as i64) * (n as i64);
55 format!("Derived computed!: square={sq} | parity={parity}")
56 });
57 let counter_text = move || -> String { format!("{PREFIX_COUNTER}{}", counter.get()) };
58 let doubled_text = move || -> String { format!("{PREFIX_DOUBLED}{}", doubled.get()) };
59 let derived_text = move || -> String { derived_info.get() };
60 let on_inc = move |_: &UiEvent| {
61 let _: bool = counter.update(|v: &mut i32| *v += 1);
62 };
63 let on_batch = move |_: &UiEvent| {
64 batch(|| {
65 for _ in 0..10 {
66 let _: bool = counter.update(|v: &mut i32| *v += 1);
67 }
68 });
69 };
70 let on_reset = move |_: &UiEvent| {
71 let _: bool = counter.set_if_changed(0);
72 };
73 view! {
74 EuvCard {
75 title: REACTIVE_CARD_TITLE.to_string()
76 tokens: tokens
77 subtitle: REACTIVE_CARD_SUB
78 column {
79 style: {
80 width: Length::Percent(100.0)
81 gap: SPACE_SM
82 }
83 text {
84 style: {
85 width: Length::Percent(100.0)
86 font_size: 20.0
87 font_weight: FontWeight::Bold
88 color: p.get_foreground()
89 }
90 { counter_text }
91 }
92 text {
93 style: {
94 width: Length::Percent(100.0)
95 font_size: 15.0
96 font_weight: FontWeight::SemiBold
97 color: p.get_primary()
98 }
99 { doubled_text }
100 }
101 text {
102 style: {
103 width: Length::Percent(100.0)
104 font_size: 12.5
105 font_family: FontFamily::Monospace
106 color: p.get_muted_foreground()
107 }
108 { derived_text }
109 }
110 EuvActionRow {
111 EuvButton {
112 label: BTN_INCREMENT.to_string()
113 tokens: tokens
114 on_click: on_inc
115 }
116 EuvButton {
117 label: BTN_BATCH_TEN.to_string()
118 tokens: tokens
119 secondary()
120 on_click: on_batch
121 }
122 EuvButton {
123 label: BTN_RESET.to_string()
124 tokens: tokens
125 outline()
126 on_click: on_reset
127 }
128 }
129 }
130 }
131 }
132}
133
134pub(crate) fn mount_reactive_local_card(tokens: ThemeTokens) -> NodeId {
144 let p: ThemePalette = tokens.get_palette();
145 let local_count: LocalState<u32> = state!(1_u32);
146 let local_note: Signal<String> = Signal::new(REACTIVE_LOCAL_DEFAULT_NOTE.to_string());
147 let local_summary = move || -> String {
148 format!(
149 "LocalState count: {} | Note: {}",
150 local_count.get(),
151 local_note.get()
152 )
153 };
154 let on_local_inc = move |_: &UiEvent| {
155 let _: bool = local_count.update(|v: &mut u32| *v = v.saturating_add(1));
156 };
157 let on_local_reset = move |_: &UiEvent| {
158 let _: bool = local_count.set_if_changed(1);
159 let _: bool = local_note.set_if_changed(REACTIVE_LOCAL_DEFAULT_NOTE.to_string());
160 };
161 view! {
162 EuvCard {
163 title: REACTIVE_LOCAL_CARD_TITLE.to_string()
164 tokens: tokens
165 subtitle: REACTIVE_LOCAL_CARD_SUB
166 column {
167 style: {
168 width: Length::Percent(100.0)
169 gap: SPACE_SM
170 }
171 text {
172 style: {
173 width: Length::Percent(100.0)
174 font_size: 13.5
175 font_weight: FontWeight::SemiBold
176 color: p.get_primary()
177 }
178 { local_summary }
179 }
180 EuvInput {
181 value: local_note
182 placeholder: REACTIVE_LOCAL_PLACEHOLDER.to_string()
183 tokens: tokens
184 }
185 EuvActionRow {
186 EuvButton {
187 label: REACTIVE_BTN_LOCAL_INC.to_string()
188 tokens: tokens
189 sm()
190 on_click: on_local_inc
191 }
192 EuvButton {
193 label: REACTIVE_BTN_LOCAL_RESET.to_string()
194 tokens: tokens
195 outline()
196 sm()
197 on_click: on_local_reset
198 }
199 }
200 }
201 }
202 }
203}
204
205pub(crate) fn mount_reactive_store_card(tokens: ThemeTokens) -> (NodeId, Scope) {
215 let p: ThemePalette = tokens.get_palette();
216 let global_banner: Signal<ShowcaseGlobalBanner> =
217 state!(global ShowcaseGlobalBanner = ShowcaseGlobalBanner::default());
218 let local_seq: Signal<u32> = Signal::new(0);
219 let local_received: Signal<u32> = Signal::new(0);
220 let local_last_src: Signal<String> = Signal::new(REACTIVE_SOURCE_NONE.to_string());
221 let (ev_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
222 on!(ShowcasePulseEvent => move |ev: &ShowcasePulseEvent| {
223 let _: bool = local_received.update(|n: &mut u32| *n = n.saturating_add(1));
224 let _: bool = local_last_src.set_if_changed(ev.get_source().clone());
225 })
226 });
227 let store_line = move || -> String {
228 let gb: ShowcaseGlobalBanner = global_banner.get();
229 let src: &str = if gb.get_last_source().is_empty() {
230 REACTIVE_SOURCE_NONE
231 } else {
232 gb.get_last_source().as_str()
233 };
234 format!(
235 "ShowcaseGlobalBanner -> pulses: {} | last_source: {src}",
236 gb.get_pulses()
237 )
238 };
239 let listener_line = move || -> String {
240 format!(
241 "Local on!(ShowcasePulseEvent) -> received: {} | last: {}",
242 local_received.get(),
243 local_last_src.get()
244 )
245 };
246 let on_emit = move |_: &UiEvent| {
247 let next_seq: u32 = local_seq.get_untracked().saturating_add(1);
248 let _: bool = local_seq.set(next_seq);
249 emit!(ShowcasePulseEvent {
250 source: ROUTE_REACTIVE.to_string(),
251 seq: next_seq,
252 });
253 };
254 let card: NodeId = view! {
255 EuvCard {
256 title: REACTIVE_STORE_CARD_TITLE.to_string()
257 tokens: tokens
258 subtitle: REACTIVE_STORE_CARD_SUB
259 column {
260 style: {
261 width: Length::Percent(100.0)
262 gap: SPACE_SM
263 }
264 text {
265 style: {
266 width: Length::Percent(100.0)
267 font_size: 13.5
268 font_weight: FontWeight::SemiBold
269 color: p.get_card_foreground()
270 }
271 { store_line }
272 }
273 text {
274 style: {
275 width: Length::Percent(100.0)
276 font_size: 12.5
277 font_family: FontFamily::Monospace
278 color: p.get_muted_foreground()
279 }
280 { listener_line }
281 }
282 EuvActionRow {
283 EuvButton {
284 label: REACTIVE_BTN_EMIT_PULSE.to_string()
285 tokens: tokens
286 sm()
287 on_click: on_emit
288 }
289 }
290 }
291 }
292 };
293 (card, ev_scope)
294}
295
296pub(crate) fn mount_reactive_watch_card(
307 counter: Signal<i32>,
308 tokens: ThemeTokens,
309) -> (NodeId, Scope) {
310 let p: ThemePalette = tokens.get_palette();
311 let g: ThemeGeometry = tokens.get_geometry();
312 let watch_log: Signal<Vec<String>> = Signal::new(Vec::new());
313 let watch_seq: Signal<u32> = Signal::new(0);
314 let (watch_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
315 watch!((counter) => move |val: i32| {
316 let seq: u32 = watch_seq.get_untracked().saturating_add(1);
317 let _: bool = watch_seq.set(seq);
318 let line: String = format!("#{seq} watch!(counter) => {val} (x2 = {})", val * 2);
319 let _: bool = watch_log.update(|entries: &mut Vec<String>| {
320 entries.insert(0, line);
321 if entries.len() > REACTIVE_WATCH_LIMIT {
322 entries.truncate(REACTIVE_WATCH_LIMIT);
323 }
324 });
325 })
326 });
327 let watch_count_text = move || -> String {
328 format!(
329 "Observed transitions: {} (showing latest {REACTIVE_WATCH_LIMIT})",
330 watch_seq.get()
331 )
332 };
333 let on_clear = move |_: &UiEvent| {
334 let _: bool = watch_log.set_if_changed(Vec::new());
335 };
336 let build_watch_rows = move || -> Vec<NodeId> {
337 let live: ThemeTokens = live_tokens_untracked(tokens);
338 let live_p: ThemePalette = live.get_palette();
339 let items: Vec<String> = watch_log.get();
340 if items.is_empty() {
341 return vec![view! {
342 text {
343 style: {
344 width: Length::Percent(100.0)
345 font_size: 12.0
346 font_family: FontFamily::Monospace
347 color: live_p.get_muted_foreground()
348 }
349 { REACTIVE_WATCH_EMPTY }
350 }
351 }];
352 }
353 items
354 .into_iter()
355 .map(|entry: String| {
356 view! {
357 text {
358 style: {
359 width: Length::Percent(100.0)
360 font_size: 12.0
361 font_family: FontFamily::Monospace
362 color: live_p.get_foreground()
363 }
364 { entry }
365 }
366 }
367 })
368 .collect()
369 };
370 let card: NodeId = view! {
371 EuvCard {
372 title: REACTIVE_WATCH_CARD_TITLE.to_string()
373 tokens: tokens
374 subtitle: REACTIVE_WATCH_CARD_SUB
375 column {
376 style: {
377 width: Length::Percent(100.0)
378 gap: SPACE_SM
379 }
380 text {
381 style: {
382 width: Length::Percent(100.0)
383 font_size: 12.5
384 font_weight: FontWeight::Medium
385 color: p.get_primary()
386 }
387 { watch_count_text }
388 }
389 column {
390 style: {
391 width: Length::Percent(100.0)
392 padding: Edges::all(SPACE_SM)
393 gap: SPACE_XS
394 background: p.get_muted()
395 border: (g.get_border_width(), p.get_border())
396 radius: g.get_radius_md()
397 overflow: Overflow::Hidden
398 }
399 bind_children: build_watch_rows
400 }
401 EuvActionRow {
402 EuvButton {
403 label: REACTIVE_BTN_CLEAR_WATCH.to_string()
404 tokens: tokens
405 outline()
406 sm()
407 on_click: on_clear
408 }
409 }
410 }
411 }
412 };
413 (card, watch_scope)
414}
415
416pub(crate) fn mount_reactive_tokens_card(tokens: ThemeTokens) -> NodeId {
426 var! {
427 const REACTIVE_VAR_PAD_SM: f32 = 8.0;
428 const REACTIVE_VAR_PAD_MD: f32 = 14.0;
429 const REACTIVE_VAR_PAD_LG: f32 = 20.0;
430 const REACTIVE_VAR_RAD_SM: f32 = 6.0;
431 const REACTIVE_VAR_RAD_MD: f32 = 12.0;
432 const REACTIVE_VAR_RAD_LG: f32 = 18.0;
433 }
434 let density_idx: Signal<usize> = Signal::new(1);
435 let role_idx: Signal<usize> = Signal::new(0);
436 let base_box: Style = style! {
437 FlexDirection::Column
438 Overflow::Hidden
439 width: Length::Percent(100.0)
440 gap: SPACE_XS
441 };
442 let preview_box_style = move || -> Style {
443 let live: ThemeTokens = use_live_tokens(tokens);
444 let (bg, _): (Color, Color) = reactive_preview_colors(&live.get_palette(), role_idx.get());
445 let (pad, rad): (f32, f32) = match density_idx.get() {
446 0 => (REACTIVE_VAR_PAD_SM, REACTIVE_VAR_RAD_SM),
447 2 => (REACTIVE_VAR_PAD_LG, REACTIVE_VAR_RAD_LG),
448 _ => (REACTIVE_VAR_PAD_MD, REACTIVE_VAR_RAD_MD),
449 };
450 base_box.padding(Edges::all(pad)).background(bg).radius(rad)
451 };
452 let preview_title_style = move || -> Style {
453 let live: ThemeTokens = use_live_tokens(tokens);
454 let (_, fg): (Color, Color) = reactive_preview_colors(&live.get_palette(), role_idx.get());
455 Style::default()
456 .width(Length::Percent(100.0))
457 .font_size(14.0)
458 .font_weight(FontWeight::Bold)
459 .color(fg)
460 };
461 let preview_meta_style = move || -> Style {
462 let live: ThemeTokens = use_live_tokens(tokens);
463 let (_, fg): (Color, Color) = reactive_preview_colors(&live.get_palette(), role_idx.get());
464 Style::default()
465 .width(Length::Percent(100.0))
466 .font_size(12.0)
467 .font_family(FontFamily::Monospace)
468 .color(fg)
469 };
470 let preview_meta_text = move || -> String {
471 let (pad, rad, label): (f32, f32, &str) = match density_idx.get() {
472 0 => (
473 REACTIVE_VAR_PAD_SM,
474 REACTIVE_VAR_RAD_SM,
475 REACTIVE_DENSITY_COMPACT,
476 ),
477 2 => (
478 REACTIVE_VAR_PAD_LG,
479 REACTIVE_VAR_RAD_LG,
480 REACTIVE_DENSITY_ROOMY,
481 ),
482 _ => (
483 REACTIVE_VAR_PAD_MD,
484 REACTIVE_VAR_RAD_MD,
485 REACTIVE_DENSITY_BALANCED,
486 ),
487 };
488 let role_name: &str = match role_idx.get() {
489 1 => REACTIVE_ROLE_SECONDARY,
490 2 => REACTIVE_ROLE_SUCCESS,
491 3 => REACTIVE_ROLE_WARNING,
492 _ => REACTIVE_ROLE_PRIMARY,
493 };
494 format!("var! pad={pad:.0}px radius={rad:.0}px ({label}) | Role: {role_name}")
495 };
496 view! {
497 EuvCard {
498 title: REACTIVE_TOKENS_CARD_TITLE.to_string()
499 tokens: tokens
500 subtitle: REACTIVE_TOKENS_CARD_SUB
501 column {
502 style: {
503 width: Length::Percent(100.0)
504 gap: SPACE_SM
505 }
506 EuvSegmented {
507 options: vec![
508 REACTIVE_DENSITY_COMPACT.to_string(),
509 REACTIVE_DENSITY_BALANCED.to_string(),
510 REACTIVE_DENSITY_ROOMY.to_string(),
511 ]
512 selected: density_idx
513 tokens: tokens
514 }
515 EuvSegmented {
516 options: vec![
517 REACTIVE_ROLE_PRIMARY.to_string(),
518 REACTIVE_ROLE_SECONDARY.to_string(),
519 REACTIVE_ROLE_SUCCESS.to_string(),
520 REACTIVE_ROLE_WARNING.to_string(),
521 ]
522 selected: role_idx
523 tokens: tokens
524 }
525 view {
526 bind_style: preview_box_style
527 text {
528 bind_style: preview_title_style
529 { REACTIVE_TOKEN_SAMPLE_TITLE }
530 }
531 text {
532 bind_style: preview_meta_style
533 { preview_meta_text }
534 }
535 }
536 }
537 }
538 }
539}
540
541pub(crate) fn mount_reactive_list_card(tokens: ThemeTokens) -> NodeId {
551 let p: ThemePalette = tokens.get_palette();
552 let items_sig: Signal<Vec<(u32, String)>> = Signal::new(reactive_seed_list());
553 let next_id_sig: Signal<u32> = Signal::new(5);
554 let ops_sig: Signal<u32> = Signal::new(0);
555 let summary_text = move || -> String {
556 format!(
557 "Active Keys: {} | Mutations: {}",
558 items_sig.get().len(),
559 ops_sig.get()
560 )
561 };
562 let on_add = move |_: &UiEvent| {
563 let id: u32 = next_id_sig.get_untracked();
564 let _: bool = next_id_sig.set(id.saturating_add(1));
565 let label_idx: usize = (id as usize) % REACTIVE_EXTRA_LABELS.len();
566 let label: String = format!("{} #{id}", REACTIVE_EXTRA_LABELS[label_idx]);
567 let _: bool = items_sig.update(|list: &mut Vec<(u32, String)>| {
568 list.push((id, label));
569 });
570 let _: bool = ops_sig.update(|n: &mut u32| *n = n.saturating_add(1));
571 };
572 let on_shuffle = move |_: &UiEvent| {
573 let _: bool = items_sig.update(|list: &mut Vec<(u32, String)>| {
574 if list.len() >= 2 {
575 list.rotate_left(1);
576 if list.len() >= 3 {
577 let last: usize = list.len() - 1;
578 list.swap(0, last);
579 }
580 }
581 });
582 let _: bool = ops_sig.update(|n: &mut u32| *n = n.saturating_add(1));
583 };
584 let on_remove_first = move |_: &UiEvent| {
585 let _: bool = items_sig.update(|list: &mut Vec<(u32, String)>| {
586 if !list.is_empty() {
587 let _: (u32, String) = list.remove(0);
588 }
589 });
590 let _: bool = ops_sig.update(|n: &mut u32| *n = n.saturating_add(1));
591 };
592 let on_reset = move |_: &UiEvent| {
593 let _: bool = items_sig.set(reactive_seed_list());
594 let _: bool = next_id_sig.set(5);
595 let _: bool = ops_sig.update(|n: &mut u32| *n = n.saturating_add(1));
596 };
597 let build_list_rows = move || -> Vec<NodeId> {
598 let live: ThemeTokens = live_tokens_untracked(tokens);
599 let live_p: ThemePalette = live.get_palette();
600 let live_g: ThemeGeometry = live.get_geometry();
601 let current: Vec<(u32, String)> = items_sig.get();
602 if current.is_empty() {
603 return vec![view! {
604 text {
605 style: {
606 width: Length::Percent(100.0)
607 font_size: 12.5
608 color: live_p.get_muted_foreground()
609 }
610 { REACTIVE_LIST_EMPTY }
611 }
612 }];
613 }
614 current
615 .into_iter()
616 .enumerate()
617 .map(|(pos, (key_id, label)): (usize, (u32, String))| {
618 let badge_txt: String = format!("#{key_id}");
619 let row_label: String = format!("[{pos}] {label}");
620 view! {
621 row {
622 style: {
623 width: Length::Percent(100.0)
624 padding: Edges::symmetric(SPACE_SM, SPACE_MD)
625 align: AlignItems::Center
626 gap: SPACE_SM
627 background: live_p.get_muted()
628 border: (live_g.get_border_width(), live_p.get_border())
629 radius: live_g.get_radius_sm()
630 overflow: Overflow::Hidden
631 }
632 EuvBadge {
633 label: badge_txt
634 tone: EuvTone::Primary
635 tokens: live
636 }
637 text {
638 style: {
639 flex_grow: 1.0
640 flex_shrink: 1.0
641 min_width: Length::Px(0.0)
642 font_size: 12.5
643 color: live_p.get_foreground()
644 }
645 { row_label }
646 }
647 }
648 }
649 })
650 .collect()
651 };
652 view! {
653 EuvCard {
654 title: REACTIVE_LIST_CARD_TITLE.to_string()
655 tokens: tokens
656 subtitle: REACTIVE_LIST_CARD_SUB
657 column {
658 style: {
659 width: Length::Percent(100.0)
660 gap: SPACE_SM
661 }
662 text {
663 style: {
664 width: Length::Percent(100.0)
665 font_size: 12.5
666 font_weight: FontWeight::SemiBold
667 color: p.get_primary()
668 }
669 { summary_text }
670 }
671 EuvActionRow {
672 EuvButton {
673 label: REACTIVE_BTN_ADD_ITEM.to_string()
674 tokens: tokens
675 sm()
676 on_click: on_add
677 }
678 EuvButton {
679 label: REACTIVE_BTN_SHUFFLE_ITEMS.to_string()
680 tokens: tokens
681 secondary()
682 sm()
683 on_click: on_shuffle
684 }
685 EuvButton {
686 label: REACTIVE_BTN_REMOVE_FIRST.to_string()
687 tokens: tokens
688 outline()
689 sm()
690 on_click: on_remove_first
691 }
692 EuvButton {
693 label: REACTIVE_BTN_RESET_LIST.to_string()
694 tokens: tokens
695 ghost()
696 sm()
697 on_click: on_reset
698 }
699 }
700 column {
701 style: {
702 width: Length::Percent(100.0)
703 gap: SPACE_XS
704 }
705 bind_children: build_list_rows
706 }
707 }
708 }
709 }
710}
711
712pub fn mount_reactive_page(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
723 let ctx: Context = current_context();
724 let signal_card: NodeId = mount_reactive_signal_card(state, tokens);
725 let local_card: NodeId = mount_reactive_local_card(tokens);
726 let (store_card, ev_scope): (NodeId, Scope) = mount_reactive_store_card(tokens);
727 let (watch_card, watch_scope): (NodeId, Scope) =
728 mount_reactive_watch_card(state.get_counter(), tokens);
729 let tokens_card: NodeId = mount_reactive_tokens_card(tokens);
730 let list_card: NodeId = mount_reactive_list_card(tokens);
731 let grid: NodeId = mount_responsive_grid(
732 vec![
733 signal_card,
734 local_card,
735 store_card,
736 watch_card,
737 tokens_card,
738 list_card,
739 ],
740 2,
741 );
742 let page: NodeId = view! {
743 EuvPage {
744 title: REACTIVE_TITLE.to_string()
745 subtitle: REACTIVE_SUBTITLE.to_string()
746 tokens: tokens
747 { grid }
748 }
749 };
750 ctx.get_scene().with_write(
751 |tree: &mut SceneTree| {
752 tree.attach_scope(store_card, ev_scope);
753 tree.attach_scope(watch_card, watch_scope);
754 },
755 (),
756 );
757 page
758}