1use super::*;
2
3pub(crate) fn ensure_showcase_routes_registered(state: &ShowcaseState) {
9 if state.get_router_ready().replace(true) {
10 return;
11 }
12 let mut router: EuvRouter = state.get_router().clone();
13 for spec in SHOWCASE_ROUTES.iter() {
14 let path: &'static str = spec.get_path();
15 let st: ShowcaseState = state.clone();
16 router = router.route_component(path, spec.get_label(), move |_: &EuvRouteMatch| {
17 let tok: ThemeTokens = untracked(|| st.get_theme().get_tokens().get());
18 ShowcasePage::new(path, st.clone(), tok)
19 });
20 }
21 let _: EuvRouter = router;
22}
23
24pub(crate) fn mount_lazy_cmd_palette_modal(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
35 let cmd_open: Signal<bool> = state.get_cmd_palette_open();
36 let cmd_query: Signal<String> = state.get_cmd_palette_query();
37 let mut cmd_items: Vec<String> = SHOWCASE_ROUTES
38 .iter()
39 .map(|spec: &ShowcaseRouteSpec| {
40 format!("Go to {} ({})", spec.get_label(), spec.get_section())
41 })
42 .collect();
43 cmd_items.push(CMD_ITEM_TOGGLE_MODE.to_string());
44 let cmd_sel: Signal<Option<usize>> = Signal::new(None);
45 let cmd_route: Signal<String> = state.get_route();
46 let cmd_state: ShowcaseState = state.clone();
47 let cmd_list: NodeId = view! {
48 EuvCommand {
49 items: cmd_items
50 selected: cmd_sel
51 tokens: tokens
52 query: cmd_query
53 placeholder: CMD_PLACEHOLDER
54 }
55 };
56 let (cmd_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
57 Effect::new(move || {
58 if let Some(idx) = cmd_sel.get() {
59 match SHOWCASE_ROUTES.get(idx) {
60 Some(spec) => {
61 let _: bool = cmd_route.set_if_changed(spec.get_path().to_string());
62 }
63 None => {
64 cmd_state.toggle_theme_mode();
65 }
66 }
67 let _: bool = cmd_sel.set_if_changed(None);
68 let _: bool = cmd_open.set_if_changed(false);
69 }
70 })
71 });
72 let cmd_modal: NodeId = view! {
73 EuvModal {
74 open: cmd_open
75 title: CMD_MODAL_TITLE.to_string()
76 body: cmd_list
77 tokens: tokens
78 }
79 };
80 let rctx: Context = current_context();
81 let cmd_theme_sig: Memo<ThemeTokens> = state.get_theme().get_tokens();
82 let prev_cmd_tok: Rc<Cell<ThemeTokens>> = Rc::new(Cell::new(tokens));
83 let cmd_theme_ctx: Context = rctx.clone();
84 let (cmd_theme_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
85 Effect::new(move || {
86 let next_tok: ThemeTokens = cmd_theme_sig.get();
87 let old_tok: ThemeTokens = prev_cmd_tok.replace(next_tok);
88 if old_tok == next_tok {
89 return;
90 }
91 cmd_theme_ctx.get_scene().with_write(
92 |tree: &mut SceneTree| {
93 remap_subtree_theme_in_place(tree, cmd_modal, old_tok, next_tok);
94 },
95 (),
96 );
97 })
98 });
99 rctx.get_scene().with_write(
100 |tree: &mut SceneTree| {
101 tree.attach_scope(cmd_modal, cmd_scope);
102 tree.attach_scope(cmd_modal, cmd_theme_scope);
103 },
104 (),
105 );
106 cmd_modal
107}
108
109pub fn mount_showcase_root(state: &ShowcaseState) -> NodeId {
119 provide_theme(state.get_theme().clone());
120 ensure_showcase_routes_registered(state);
121 let global_banner: Signal<ShowcaseGlobalBanner> =
122 state!(global ShowcaseGlobalBanner = ShowcaseGlobalBanner::default());
123 let state_c: ShowcaseState = state.clone();
124 let bg_state: ShowcaseState = state.clone();
125 let fg_state: ShowcaseState = state.clone();
126 let sys_state: ShowcaseState = state.clone();
127 let sys_log_sig: Signal<Vec<String>> = state.get_system_event_log();
128 let sys_count_sig: Signal<u32> = state.get_system_event_count();
129 let (ev_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
130 let _: SystemEventSubscription = on_background(move || {
131 bg_state.freeze_for_background();
132 });
133 let _: SystemEventSubscription = on_foreground(move || {
134 fg_state.restore_from_foreground();
135 });
136 let _: SystemEventSubscription = on_system_event(move |ev: &SystemEvent| {
137 if matches!(ev, SystemEvent::Terminate) {
138 sys_state.freeze_for_background();
139 let _: usize = EuvOverlayStack::dismiss_all();
140 reset_fullscreen_state();
141 } else if matches!(ev, SystemEvent::Foreground) {
142 sys_state.restore_from_foreground();
143 }
144 let next_seq: u32 = sys_count_sig.get_untracked().saturating_add(1);
145 let _: bool = sys_count_sig.set(next_seq);
146 let entry: String = format!("#{next_seq}: {ev:?}");
147 let _: bool = sys_log_sig.update(|items: &mut Vec<String>| {
148 items.insert(0, entry);
149 if items.len() > SYS_EVENTS_LOG_LIMIT {
150 items.truncate(SYS_EVENTS_LOG_LIMIT);
151 }
152 });
153 });
154 on!(ShowcasePulseEvent => move |ev: &ShowcasePulseEvent| {
155 let src: String = ev.get_source().clone();
156 let _: bool = global_banner.update(|b: &mut ShowcaseGlobalBanner| {
157 let next_pulses: u32 = b.get_pulses().saturating_add(1);
158 b.set_pulses(next_pulses);
159 b.set_last_source(src);
160 });
161 })
162 });
163 let build_root_children = move || -> Vec<NodeId> {
164 let tokens: ThemeTokens = untracked(|| state_c.get_theme().get_tokens().get());
165 let hub: DynamicUpdateHub = state_c.get_dynamic_hub().clone();
166 let _: u64 = hub.routes_signal().get();
167 let cmd_open: Signal<bool> = state_c.get_cmd_palette_open();
168 let cmd_slot: NodeId = view! {
169 column {
170 style: {
171 position: Position::Absolute
172 width: Length::Px(0.0)
173 height: Length::Px(0.0)
174 }
175 }
176 };
177 let cmd_mounted: Rc<Cell<bool>> = Rc::new(Cell::new(false));
178 let cmd_state_lazy: ShowcaseState = state_c.clone();
179 let rctx: Context = current_context();
180 let cmd_ctx: Context = rctx.clone();
181 let (cmd_lazy_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
182 Effect::new(move || {
183 if !cmd_open.get() || cmd_mounted.get() {
184 return;
185 }
186 cmd_mounted.set(true);
187 let live_tok: ThemeTokens =
188 untracked(|| cmd_state_lazy.get_theme().get_tokens().get());
189 let modal_node: NodeId = with_context(cmd_ctx.clone(), || {
190 mount_lazy_cmd_palette_modal(&cmd_state_lazy, live_tok)
191 });
192 cmd_ctx.get_scene().with_write(
193 |tree: &mut SceneTree| {
194 let dirty_mask: u16 = DIRTY_CHILDREN + DIRTY_LAYOUT + DIRTY_PAINT;
195 tree.append_child(cmd_slot, modal_node);
196 tree.mark_dirty(cmd_slot, dirty_mask);
197 },
198 (),
199 );
200 })
201 });
202 rctx.get_scene().with_write(
203 |tree: &mut SceneTree| {
204 tree.attach_scope(cmd_slot, cmd_lazy_scope);
205 },
206 (),
207 );
208 let state_shell: ShowcaseState = state_c.clone();
209 let hub_shell: DynamicUpdateHub = hub.clone();
210 let render_native_shell = move || -> NodeId {
211 let shell_node: NodeId = mount_native_app_shell(&state_shell, &hub_shell, tokens);
212 shell_node
213 };
214 let global_wasm_slot: NodeId = view! {
215 slot {
216 id: WASM_GLOBAL_ROOT_SLOT
217 hub: hub
218 style: c_full_col()
219 fallback: render_native_shell
220 }
221 };
222 vec![global_wasm_slot, cmd_slot]
223 };
224 let root_node: NodeId = view! {
225 column {
226 style: {
227 width: Length::Percent(100.0)
228 height: Length::Percent(100.0)
229 }
230 bind_children: build_root_children
231 }
232 };
233 let root_ctx: Context = current_context();
234 root_ctx.get_scene().with_write(
235 |tree: &mut SceneTree| {
236 tree.attach_scope(root_node, ev_scope);
237 },
238 (),
239 );
240 root_node
241}
242
243pub(crate) fn mount_native_app_shell(
255 state_shell: &ShowcaseState,
256 hub_shell: &DynamicUpdateHub,
257 tokens: ThemeTokens,
258) -> NodeId {
259 let mut nav_items: Vec<EuvNavItem> = ShowcaseState::nav_items();
260 for dyn_route in hub_shell.registered_routes().into_iter() {
261 if dyn_route == ROUTE_DYNAMIC_PANEL {
262 continue;
263 }
264 if !nav_items
265 .iter()
266 .any(|item: &EuvNavItem| item.get_path().as_str() == dyn_route.as_str())
267 {
268 let label: String = hub_shell
269 .page_title(&dyn_route)
270 .filter(|t: &String| !t.is_empty())
271 .unwrap_or_else(|| dyn_route.clone());
272 nav_items.push(EuvNavItem::new(dyn_route, label).section(NAV_SEC_RUNTIME));
273 }
274 }
275 let nav: NodeId = view! {
276 EuvSidebarNav {
277 items: nav_items
278 active_route: state_shell.get_route()
279 tokens: tokens
280 }
281 };
282 let bottom_items: Vec<EuvNavItem> = vec![
283 EuvNavItem::new(ROUTE_OVERVIEW.to_string(), BNAV_HOME_LABEL.to_string())
284 .section(BNAV_HOME_ICON),
285 EuvNavItem::new(ROUTE_MARKDOWN.to_string(), BNAV_DOCS_LABEL.to_string())
286 .section(BNAV_DOCS_ICON),
287 EuvNavItem::new(ROUTE_LAB.to_string(), BNAV_CASES_LABEL.to_string())
288 .section(BNAV_CASES_ICON),
289 EuvNavItem::new(ROUTE_STRESS.to_string(), BNAV_STRESS_LABEL.to_string())
290 .section(BNAV_STRESS_ICON),
291 EuvNavItem::new(ROUTE_GAME_3D.to_string(), BNAV_3D_LABEL.to_string()).section(BNAV_3D_ICON),
292 ];
293 let bottom_nav: NodeId = view! {
294 EuvBottomNav {
295 items: bottom_items
296 active_route: state_shell.get_route()
297 tokens: tokens
298 }
299 };
300 let swipe_order: Vec<String> = showcase_swipe_order();
301 let state_for_outlet: ShowcaseState = state_shell.clone();
302 let router: EuvRouter = state_shell
303 .get_router()
304 .clone()
305 .with_dynamic_hub(hub_shell.clone());
306 let route_sig: Signal<String> = state_shell.get_route();
307 let cur_target: String = route_sig.get_untracked();
308 if router.get_current_path().get_untracked() != cur_target {
309 router.replace(&cur_target);
310 }
311 let breadcrumb_sig: Signal<String> =
312 Signal::new(ShowcaseState::breadcrumb_for_route(&cur_target));
313 let page_pad_sig: Signal<Edges> = Signal::new(Edges::all(SPACE_LG));
314 let router_sync_in: EuvRouter = router.clone();
315 let router_path_sig: Signal<String> = router.get_current_path();
316 let reduced_motion_sig: Signal<bool> = state_shell.get_reduced_motion();
317 let sctx: Context = current_context();
318 let (sync_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
319 let _: Effect = Effect::new(move || {
320 let target: String = route_sig.get();
321 if router_sync_in.get_current_path().get_untracked() != target {
322 router_sync_in.push(&target);
323 }
324 let _: bool =
325 breadcrumb_sig.set_if_changed(ShowcaseState::breadcrumb_for_route(&target));
326 });
327 let _: Effect = Effect::new(move || {
328 set_reduced_motion(reduced_motion_sig.get());
329 });
330 Effect::new(move || {
331 let rp: String = router_path_sig.get();
332 if route_sig.get_untracked() != rp {
333 let _: bool = route_sig.set_if_changed(rp);
334 }
335 })
336 });
337 let page_node: NodeId = router
338 .outlet(move |matched: &EuvRouteMatch| {
339 let tok: ThemeTokens = untracked(|| state_for_outlet.get_theme().get_tokens().get());
340 mount_route_by_name(matched.get_path(), &state_for_outlet, tok)
341 })
342 .with_theme(state_shell.get_theme().clone())
343 .with_page_padding(page_pad_sig)
344 .with_route_order(swipe_order.clone())
345 .with_double_back_exit(true)
346 .mount();
347 sctx.get_scene().with_write(
348 |tree: &mut SceneTree| {
349 tree.attach_scope(page_node, sync_scope);
350 },
351 (),
352 );
353 let top_actions: NodeId = mount_topbar_actions(state_shell, tokens);
354 let mobile_actions: NodeId = mount_topbar_actions(state_shell, tokens);
355 view! {
356 EuvAppShell {
357 brand: SHOWCASE_BRAND.to_string()
358 sidebar: nav
359 main_content: page_node
360 tokens: tokens
361 theme: state_shell.get_theme().clone()
362 page_padding: page_pad_sig
363 route: state_shell.get_route()
364 breadcrumb: breadcrumb_sig
365 top_bar_actions: top_actions
366 mobile_header_actions: mobile_actions
367 bottom_nav: bottom_nav
368 swipe_routes: swipe_order
369 swipe_routes_enabled: state_shell.get_swipe_routing()
370 show_fps: true
371 show_fps_signal: state_shell.get_show_fps_hud()
372 auto_hide_chrome_on_scroll: true
373 auto_hide_chrome_signal: state_shell.get_auto_hide_chrome()
374 }
375 }
376}
377
378pub fn mount_active_route(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
389 let route: String = state.get_route().get_untracked();
390 mount_route_by_name(&route, state, tokens)
391}
392
393pub(crate) fn mount_route_by_name(
405 route: &str,
406 state: &ShowcaseState,
407 tokens: ThemeTokens,
408) -> NodeId {
409 let builder: fn(&ShowcaseState, ThemeTokens) -> NodeId = SHOWCASE_ROUTES
410 .iter()
411 .find(|spec: &&ShowcaseRouteSpec| spec.get_path() == route)
412 .map(|spec: &ShowcaseRouteSpec| spec.get_mount())
413 .unwrap_or(mount_overview_page);
414 builder(state, tokens)
415}
416
417pub(crate) fn showcase_swipe_order() -> Vec<String> {
423 let mut order: Vec<String> = BOTTOM_NAV_ROUTES
424 .iter()
425 .map(|path: &&str| (*path).to_string())
426 .collect();
427 for spec in SHOWCASE_ROUTES.iter() {
428 if !BOTTOM_NAV_ROUTES.contains(&spec.get_path()) {
429 order.push(spec.get_path().to_string());
430 }
431 }
432 order
433}
434
435pub(crate) fn draw_gear_icon(bounds: Rect, dl: &mut DisplayList, color: Color) {
443 let w: f32 = bounds.get_width().max(8.0);
444 let h: f32 = bounds.get_height().max(8.0);
445 let side: f32 = w.min(h);
446 let cx: f32 = bounds.get_x() + w * 0.5;
447 let cy: f32 = bounds.get_y() + h * 0.5;
448 let sw: f32 = (side * 0.11).clamp(1.2, 1.8);
449 let tooth_r: f32 = side * 0.44;
450 let inner_r: f32 = side * 0.28;
451 let hole_r: f32 = side * 0.14;
452 let angles: [f32; 4] = [
453 0.0,
454 core::f32::consts::FRAC_PI_4,
455 core::f32::consts::FRAC_PI_2,
456 3.0 * core::f32::consts::FRAC_PI_4,
457 ];
458 for &ang in angles.iter() {
459 let (cos_a, sin_a): (f32, f32) = (ang.cos(), ang.sin());
460 dl.push(DrawCommand::Line {
461 from: Point::new(cx - tooth_r * cos_a, cy - tooth_r * sin_a),
462 to: Point::new(cx + tooth_r * cos_a, cy + tooth_r * sin_a),
463 color,
464 width: sw * 1.5,
465 });
466 }
467 dl.push(DrawCommand::FillRoundedRect {
468 rect: Rect::new(cx - inner_r, cy - inner_r, inner_r * 2.0, inner_r * 2.0),
469 color: COLOR_TRANSPARENT,
470 radius: CornerRadius::all(RADIUS_PILL),
471 border: Border::new(sw * 1.2, color),
472 });
473 dl.push(DrawCommand::FillRoundedRect {
474 rect: Rect::new(cx - hole_r, cy - hole_r, hole_r * 2.0, hole_r * 2.0),
475 color: COLOR_TRANSPARENT,
476 radius: CornerRadius::all(RADIUS_PILL),
477 border: Border::new(sw, color),
478 });
479}
480
481pub(crate) fn mount_topbar_actions(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
492 let cmd_open: Signal<bool> = state.get_cmd_palette_open();
493 let st_mode: ShowcaseState = state.clone();
494 let route_sig: Signal<String> = state.get_route();
495 let icon_btn_style = move || -> Style {
496 let live_tok: ThemeTokens = use_live_tokens(tokens);
497 let live_p: ThemePalette = live_tok.get_palette();
498 let live_g: ThemeGeometry = live_tok.get_geometry();
499 Style::default()
500 .row()
501 .width(Length::Px(TOPBAR_CONTROL_SIZE))
502 .height(Length::Px(TOPBAR_CONTROL_SIZE))
503 .flex_shrink(0.0)
504 .justify(JustifyContent::Center)
505 .align(AlignItems::Center)
506 .background(live_p.get_background())
507 .border(live_g.get_border_width(), live_p.get_border())
508 .radius(live_g.get_radius_md())
509 .cursor(CursorIcon::Pointer)
510 };
511 let paint_search = move |bounds: Rect, dl: &mut DisplayList| {
512 let live_p: ThemePalette = live_tokens_untracked(tokens).get_palette();
513 draw_euv_icon(EuvIconName::Search, bounds, dl, live_p.get_foreground());
514 };
515 let paint_mode = move |bounds: Rect, dl: &mut DisplayList| {
516 let live_tok: ThemeTokens = live_tokens_untracked(tokens);
517 let glyph: EuvIconName = match live_tok.get_mode() {
518 ThemeMode::Dark => EuvIconName::Sun,
519 ThemeMode::Light => EuvIconName::Moon,
520 };
521 draw_euv_icon(glyph, bounds, dl, live_tok.get_palette().get_foreground());
522 };
523 let paint_settings = move |bounds: Rect, dl: &mut DisplayList| {
524 let live_p: ThemePalette = live_tokens_untracked(tokens).get_palette();
525 draw_gear_icon(bounds, dl, live_p.get_foreground());
526 };
527 let on_open_cmd = move |_: &UiEvent| {
528 let _: bool = cmd_open.set_if_changed(true);
529 };
530 let on_toggle_mode = move |_: &UiEvent| {
531 st_mode.toggle_theme_mode();
532 };
533 let on_open_settings = move |_: &UiEvent| {
534 let _: bool = route_sig.set_if_changed(ROUTE_SETTINGS.to_string());
535 };
536 view! {
537 row {
538 style: {
539 gap: SPACE_XS
540 align: AlignItems::Center
541 }
542 row {
543 bind_style: icon_btn_style
544 role: A11yRole::Button
545 a11y_label: BTN_SEARCH_A11Y.to_string()
546 on_click: on_open_cmd
547 canvas {
548 style: {
549 width: Length::Px(TOPBAR_ICON_SIZE)
550 height: Length::Px(TOPBAR_ICON_SIZE)
551 }
552 painter: paint_search
553 }
554 }
555 row {
556 bind_style: icon_btn_style
557 role: A11yRole::Button
558 a11y_label: BTN_MODE_A11Y.to_string()
559 on_click: on_toggle_mode
560 canvas {
561 style: {
562 width: Length::Px(TOPBAR_ICON_SIZE)
563 height: Length::Px(TOPBAR_ICON_SIZE)
564 }
565 painter: paint_mode
566 }
567 }
568 row {
569 bind_style: icon_btn_style
570 role: A11yRole::Button
571 a11y_label: BTN_SETTINGS_A11Y.to_string()
572 on_click: on_open_settings
573 canvas {
574 style: {
575 width: Length::Px(TOPBAR_ICON_SIZE)
576 height: Length::Px(TOPBAR_ICON_SIZE)
577 }
578 painter: paint_settings
579 }
580 }
581 }
582 }
583}