euv-example 0.48.1

Cross-platform native showcase application demonstrating euv fine-grained reactivity, multi-mode rendering, window controls, and euv-ui components.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
use super::*;

/// Registers every `SHOWCASE_ROUTES` entry lazily onto `router` using typed `#[component(retention = Freeze)]` pages.
///
/// # Arguments
///
/// - `&ShowcaseState` - Shared reactive showcase state.
pub(crate) fn ensure_showcase_routes_registered(state: &ShowcaseState) {
    if state.get_router_ready().replace(true) {
        return;
    }
    let mut router: EuvRouter = state.get_router().clone();
    for spec in SHOWCASE_ROUTES.iter() {
        let path: &'static str = spec.get_path();
        let st: ShowcaseState = state.clone();
        router = router.route_component(path, spec.get_label(), move |_: &EuvRouteMatch| {
            let tok: ThemeTokens = untracked(|| st.get_theme().get_tokens().get());
            ShowcasePage::new(path, st.clone(), tok)
        });
    }
    let _: EuvRouter = router;
}

/// Lazily mounts the global Command Palette `EuvCommand` + `EuvModal` subtree on first open so Frame 1 mounts zero hidden portals.
///
/// # Arguments
///
/// - `&ShowcaseState` - Shared reactive showcase state.
/// - `ThemeTokens` - Active design tokens at first open.
///
/// # Returns
///
/// - `NodeId` - Mounted `EuvModal` handle with selection and in-place theme-remap scopes attached.
pub(crate) fn mount_lazy_cmd_palette_modal(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
    let cmd_open: Signal<bool> = state.get_cmd_palette_open();
    let cmd_query: Signal<String> = state.get_cmd_palette_query();
    let mut cmd_items: Vec<String> = SHOWCASE_ROUTES
        .iter()
        .map(|spec: &ShowcaseRouteSpec| {
            format!("Go to {} ({})", spec.get_label(), spec.get_section())
        })
        .collect();
    cmd_items.push(CMD_ITEM_TOGGLE_MODE.to_string());
    let cmd_sel: Signal<Option<usize>> = Signal::new(None);
    let cmd_route: Signal<String> = state.get_route();
    let cmd_state: ShowcaseState = state.clone();
    let cmd_list: NodeId = view! {
        EuvCommand {
            items: cmd_items
            selected: cmd_sel
            tokens: tokens
            query: cmd_query
            placeholder: CMD_PLACEHOLDER
        }
    };
    let (cmd_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
        Effect::new(move || {
            if let Some(idx) = cmd_sel.get() {
                match SHOWCASE_ROUTES.get(idx) {
                    Some(spec) => {
                        let _: bool = cmd_route.set_if_changed(spec.get_path().to_string());
                    }
                    None => {
                        cmd_state.toggle_theme_mode();
                    }
                }
                let _: bool = cmd_sel.set_if_changed(None);
                let _: bool = cmd_open.set_if_changed(false);
            }
        })
    });
    let cmd_modal: NodeId = view! {
        EuvModal {
            open: cmd_open
            title: CMD_MODAL_TITLE.to_string()
            body: cmd_list
            tokens: tokens
        }
    };
    let rctx: Context = current_context();
    let cmd_theme_sig: Memo<ThemeTokens> = state.get_theme().get_tokens();
    let prev_cmd_tok: Rc<Cell<ThemeTokens>> = Rc::new(Cell::new(tokens));
    let cmd_theme_ctx: Context = rctx.clone();
    let (cmd_theme_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
        Effect::new(move || {
            let next_tok: ThemeTokens = cmd_theme_sig.get();
            let old_tok: ThemeTokens = prev_cmd_tok.replace(next_tok);
            if old_tok == next_tok {
                return;
            }
            cmd_theme_ctx.get_scene().with_write(
                |tree: &mut SceneTree| {
                    remap_subtree_theme_in_place(tree, cmd_modal, old_tok, next_tok);
                },
                (),
            );
        })
    });
    rctx.get_scene().with_write(
        |tree: &mut SceneTree| {
            tree.attach_scope(cmd_modal, cmd_scope);
            tree.attach_scope(cmd_modal, cmd_theme_scope);
        },
        (),
    );
    cmd_modal
}

/// Mounts the fully reactive showcase application shell with lazy `PageRetention::Freeze` routing and cross-component events.
///
/// # Arguments
///
/// - `&ShowcaseState` - Reactive showcase state.
///
/// # Returns
///
/// - `NodeId` - Mounted reactive root container handle.
pub fn mount_showcase_root(state: &ShowcaseState) -> NodeId {
    provide_theme(state.get_theme().clone());
    ensure_showcase_routes_registered(state);
    let global_banner: Signal<ShowcaseGlobalBanner> =
        state!(global ShowcaseGlobalBanner = ShowcaseGlobalBanner::default());
    let state_c: ShowcaseState = state.clone();
    let bg_state: ShowcaseState = state.clone();
    let fg_state: ShowcaseState = state.clone();
    let sys_state: ShowcaseState = state.clone();
    let sys_log_sig: Signal<Vec<String>> = state.get_system_event_log();
    let sys_count_sig: Signal<u32> = state.get_system_event_count();
    let (ev_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
        let _: SystemEventSubscription = on_background(move || {
            bg_state.freeze_for_background();
        });
        let _: SystemEventSubscription = on_foreground(move || {
            fg_state.restore_from_foreground();
        });
        let _: SystemEventSubscription = on_system_event(move |ev: &SystemEvent| {
            if matches!(ev, SystemEvent::Terminate) {
                sys_state.freeze_for_background();
                let _: usize = EuvOverlayStack::dismiss_all();
                reset_fullscreen_state();
            } else if matches!(ev, SystemEvent::Foreground) {
                sys_state.restore_from_foreground();
            }
            let next_seq: u32 = sys_count_sig.get_untracked().saturating_add(1);
            let _: bool = sys_count_sig.set(next_seq);
            let entry: String = format!("#{next_seq}: {ev:?}");
            let _: bool = sys_log_sig.update(|items: &mut Vec<String>| {
                items.insert(0, entry);
                if items.len() > SYS_EVENTS_LOG_LIMIT {
                    items.truncate(SYS_EVENTS_LOG_LIMIT);
                }
            });
        });
        on!(ShowcasePulseEvent => move |ev: &ShowcasePulseEvent| {
            let src: String = ev.get_source().clone();
            let _: bool = global_banner.update(|b: &mut ShowcaseGlobalBanner| {
                let next_pulses: u32 = b.get_pulses().saturating_add(1);
                b.set_pulses(next_pulses);
                b.set_last_source(src);
            });
        })
    });
    let build_root_children = move || -> Vec<NodeId> {
        let tokens: ThemeTokens = untracked(|| state_c.get_theme().get_tokens().get());
        let hub: DynamicUpdateHub = state_c.get_dynamic_hub().clone();
        let _: u64 = hub.routes_signal().get();
        let cmd_open: Signal<bool> = state_c.get_cmd_palette_open();
        let cmd_slot: NodeId = view! {
            column {
                style: {
                    position: Position::Absolute
                    width: Length::Px(0.0)
                    height: Length::Px(0.0)
                }
            }
        };
        let cmd_mounted: Rc<Cell<bool>> = Rc::new(Cell::new(false));
        let cmd_state_lazy: ShowcaseState = state_c.clone();
        let rctx: Context = current_context();
        let cmd_ctx: Context = rctx.clone();
        let (cmd_lazy_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
            Effect::new(move || {
                if !cmd_open.get() || cmd_mounted.get() {
                    return;
                }
                cmd_mounted.set(true);
                let live_tok: ThemeTokens =
                    untracked(|| cmd_state_lazy.get_theme().get_tokens().get());
                let modal_node: NodeId = with_context(cmd_ctx.clone(), || {
                    mount_lazy_cmd_palette_modal(&cmd_state_lazy, live_tok)
                });
                cmd_ctx.get_scene().with_write(
                    |tree: &mut SceneTree| {
                        let dirty_mask: u16 = DIRTY_CHILDREN + DIRTY_LAYOUT + DIRTY_PAINT;
                        tree.append_child(cmd_slot, modal_node);
                        tree.mark_dirty(cmd_slot, dirty_mask);
                    },
                    (),
                );
            })
        });
        rctx.get_scene().with_write(
            |tree: &mut SceneTree| {
                tree.attach_scope(cmd_slot, cmd_lazy_scope);
            },
            (),
        );
        let state_shell: ShowcaseState = state_c.clone();
        let hub_shell: DynamicUpdateHub = hub.clone();
        let render_native_shell = move || -> NodeId {
            let shell_node: NodeId = mount_native_app_shell(&state_shell, &hub_shell, tokens);
            shell_node
        };
        let global_wasm_slot: NodeId = view! {
            slot {
                id: WASM_GLOBAL_ROOT_SLOT
                hub: hub
                style: c_full_col()
                fallback: render_native_shell
            }
        };
        vec![global_wasm_slot, cmd_slot]
    };
    let root_node: NodeId = view! {
        column {
            style: {
                width: Length::Percent(100.0)
                height: Length::Percent(100.0)
            }
            bind_children: build_root_children
        }
    };
    let root_ctx: Context = current_context();
    root_ctx.get_scene().with_write(
        |tree: &mut SceneTree| {
            tree.attach_scope(root_node, ev_scope);
        },
        (),
    );
    root_node
}

/// Mounts the native dual-mode `EuvAppShell` (Desktop `>= 960px`: full-width topbar + permanent left sidebar + zero bottom nav; Mobile `< 960px`: compact topbar + overlay drawer + floating bottom navigation bar).
///
/// # Arguments
///
/// - `&ShowcaseState` - Reactive showcase state.
/// - `&DynamicUpdateHub` - Active OTA dynamic route hub.
/// - `ThemeTokens` - Active design tokens at mount time.
///
/// # Returns
///
/// - `NodeId` - Mounted `EuvAppShell` root handle.
pub(crate) fn mount_native_app_shell(
    state_shell: &ShowcaseState,
    hub_shell: &DynamicUpdateHub,
    tokens: ThemeTokens,
) -> NodeId {
    let mut nav_items: Vec<EuvNavItem> = ShowcaseState::nav_items();
    for dyn_route in hub_shell.registered_routes().into_iter() {
        if dyn_route == ROUTE_DYNAMIC_PANEL {
            continue;
        }
        if !nav_items
            .iter()
            .any(|item: &EuvNavItem| item.get_path().as_str() == dyn_route.as_str())
        {
            let label: String = hub_shell
                .page_title(&dyn_route)
                .filter(|t: &String| !t.is_empty())
                .unwrap_or_else(|| dyn_route.clone());
            nav_items.push(EuvNavItem::new(dyn_route, label).section(NAV_SEC_RUNTIME));
        }
    }
    let nav: NodeId = view! {
        EuvSidebarNav {
            items: nav_items
            active_route: state_shell.get_route()
            tokens: tokens
        }
    };
    let bottom_items: Vec<EuvNavItem> = vec![
        EuvNavItem::new(ROUTE_OVERVIEW.to_string(), BNAV_HOME_LABEL.to_string())
            .section(BNAV_HOME_ICON),
        EuvNavItem::new(ROUTE_MARKDOWN.to_string(), BNAV_DOCS_LABEL.to_string())
            .section(BNAV_DOCS_ICON),
        EuvNavItem::new(ROUTE_LAB.to_string(), BNAV_CASES_LABEL.to_string())
            .section(BNAV_CASES_ICON),
        EuvNavItem::new(ROUTE_STRESS.to_string(), BNAV_STRESS_LABEL.to_string())
            .section(BNAV_STRESS_ICON),
        EuvNavItem::new(ROUTE_GAME_3D.to_string(), BNAV_3D_LABEL.to_string()).section(BNAV_3D_ICON),
    ];
    let bottom_nav: NodeId = view! {
        EuvBottomNav {
            items: bottom_items
            active_route: state_shell.get_route()
            tokens: tokens
        }
    };
    let swipe_order: Vec<String> = showcase_swipe_order();
    let state_for_outlet: ShowcaseState = state_shell.clone();
    let router: EuvRouter = state_shell
        .get_router()
        .clone()
        .with_dynamic_hub(hub_shell.clone());
    let route_sig: Signal<String> = state_shell.get_route();
    let cur_target: String = route_sig.get_untracked();
    if router.get_current_path().get_untracked() != cur_target {
        router.replace(&cur_target);
    }
    let breadcrumb_sig: Signal<String> =
        Signal::new(ShowcaseState::breadcrumb_for_route(&cur_target));
    let page_pad_sig: Signal<Edges> = Signal::new(Edges::all(SPACE_LG));
    let router_sync_in: EuvRouter = router.clone();
    let router_path_sig: Signal<String> = router.get_current_path();
    let reduced_motion_sig: Signal<bool> = state_shell.get_reduced_motion();
    let sctx: Context = current_context();
    let (sync_scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
        let _: Effect = Effect::new(move || {
            let target: String = route_sig.get();
            if router_sync_in.get_current_path().get_untracked() != target {
                router_sync_in.push(&target);
            }
            let _: bool =
                breadcrumb_sig.set_if_changed(ShowcaseState::breadcrumb_for_route(&target));
        });
        let _: Effect = Effect::new(move || {
            set_reduced_motion(reduced_motion_sig.get());
        });
        Effect::new(move || {
            let rp: String = router_path_sig.get();
            if route_sig.get_untracked() != rp {
                let _: bool = route_sig.set_if_changed(rp);
            }
        })
    });
    let page_node: NodeId = router
        .outlet(move |matched: &EuvRouteMatch| {
            let tok: ThemeTokens = untracked(|| state_for_outlet.get_theme().get_tokens().get());
            mount_route_by_name(matched.get_path(), &state_for_outlet, tok)
        })
        .with_theme(state_shell.get_theme().clone())
        .with_page_padding(page_pad_sig)
        .with_route_order(swipe_order.clone())
        .with_double_back_exit(true)
        .mount();
    sctx.get_scene().with_write(
        |tree: &mut SceneTree| {
            tree.attach_scope(page_node, sync_scope);
        },
        (),
    );
    let scheme_menu_open: Signal<bool> = Signal::new(false);
    let (top_actions, palette_btn_desk): (NodeId, NodeId) =
        mount_topbar_actions(state_shell, scheme_menu_open, tokens);
    let (mobile_actions, palette_btn_mob): (NodeId, NodeId) =
        mount_topbar_actions(state_shell, scheme_menu_open, tokens);
    let shell_node: NodeId = view! {
        EuvAppShell {
            brand: SHOWCASE_BRAND.to_string()
            sidebar: nav
            main_content: page_node
            tokens: tokens
            theme: state_shell.get_theme().clone()
            page_padding: page_pad_sig
            route: state_shell.get_route()
            breadcrumb: breadcrumb_sig
            top_bar_actions: top_actions
            mobile_header_actions: mobile_actions
            bottom_nav: bottom_nav
            swipe_routes: swipe_order
            swipe_routes_enabled: state_shell.get_swipe_routing()
            show_fps: true
            show_fps_signal: state_shell.get_show_fps_hud()
            auto_hide_chrome_on_scroll: true
            auto_hide_chrome_signal: state_shell.get_auto_hide_chrome()
        }
    };
    let scheme_portal: NodeId = mount_topbar_scheme_dropdown_portal(
        state_shell,
        scheme_menu_open,
        tokens,
        vec![palette_btn_desk, palette_btn_mob],
    );
    view! {
        column {
            style: {
                width: Length::Percent(100.0)
                height: Length::Percent(100.0)
            }
            { shell_node }
            { scheme_portal }
        }
    }
}

/// Mounts the showcase page corresponding to the active route in `state`.
///
/// # Arguments
///
/// - `&ShowcaseState` - Reactive showcase state.
/// - `ThemeTokens` - Active resolved design tokens.
///
/// # Returns
///
/// - `NodeId` - Mounted page container handle.
pub fn mount_active_route(state: &ShowcaseState, tokens: ThemeTokens) -> NodeId {
    let route: String = state.get_route().get_untracked();
    mount_route_by_name(&route, state, tokens)
}

/// Dispatches a route string to its corresponding declarative showcase page builder.
///
/// # Arguments
///
/// - `&str` - Active route path.
/// - `&ShowcaseState` - Reactive showcase state.
/// - `ThemeTokens` - Active resolved design tokens.
///
/// # Returns
///
/// - `NodeId` - Mounted page container handle.
pub(crate) fn mount_route_by_name(
    route: &str,
    state: &ShowcaseState,
    tokens: ThemeTokens,
) -> NodeId {
    let builder: fn(&ShowcaseState, ThemeTokens) -> NodeId = SHOWCASE_ROUTES
        .iter()
        .find(|spec: &&ShowcaseRouteSpec| spec.get_path() == route)
        .map(|spec: &ShowcaseRouteSpec| spec.get_mount())
        .unwrap_or(mount_overview_page);
    builder(state, tokens)
}

/// Builds the horizontal swipe route order: the five bottom-nav destinations first, then every remaining registry route in sidebar order.
///
/// # Returns
///
/// - `Vec<String>` - Ordered swipe route paths.
pub(crate) fn showcase_swipe_order() -> Vec<String> {
    let mut order: Vec<String> = BOTTOM_NAV_ROUTES
        .iter()
        .map(|path: &&str| (*path).to_string())
        .collect();
    for spec in SHOWCASE_ROUTES.iter() {
        if !BOTTOM_NAV_ROUTES.contains(&spec.get_path()) {
            order.push(spec.get_path().to_string());
        }
    }
    order
}

/// Computes normalized 24x24 viewbox origin, unit scale, and Lucide-grade stroke width centered inside `bounds`.
///
/// # Arguments
///
/// - `Rect` - Target logical bounding box.
///
/// # Returns
///
/// - `(f32, f32, f32, f32)` - Tuple `(ox, oy, u, stroke_width)`.
pub(crate) fn topbar_icon_basis(bounds: Rect) -> (f32, f32, f32, f32) {
    let w: f32 = bounds.get_width().max(8.0);
    let h: f32 = bounds.get_height().max(8.0);
    let side: f32 = w.min(h);
    let ox: f32 = bounds.get_x() + (w - side) * 0.5;
    let oy: f32 = bounds.get_y() + (h - side) * 0.5;
    let u: f32 = side / 24.0;
    let sw: f32 = (1.75 * u).clamp(1.2, 2.0);
    (ox, oy, u, sw)
}

/// Emits an anti-aliased line segment with precision rounded endcaps into `dl`.
///
/// # Arguments
///
/// - `&mut DisplayList` - Destination vector draw command list.
/// - `Point` - Segment start point.
/// - `Point` - Segment end point.
/// - `Color` - Stroke color.
/// - `f32` - Stroke width in logical pixels.
pub(crate) fn push_rounded_stroke(
    dl: &mut DisplayList,
    from: Point,
    to: Point,
    color: Color,
    width: f32,
) {
    let cap_r: f32 = (width * 0.48).max(0.45);
    dl.push(DrawCommand::Line {
        from,
        to,
        color,
        width,
    });
    dl.push(DrawCommand::Circle {
        center: from,
        radius: cap_r,
        color,
    });
    dl.push(DrawCommand::Circle {
        center: to,
        radius: cap_r,
        color,
    });
}

/// Renders a Lucide-grade 24x24 search magnifying glass icon into `dl`.
///
/// # Arguments
///
/// - `Rect` - Target logical bounding box.
/// - `&mut DisplayList` - Destination vector draw command list.
/// - `Color` - Foreground stroke color.
pub(crate) fn draw_topbar_search_icon(bounds: Rect, dl: &mut DisplayList, color: Color) {
    let (ox, oy, u, sw): (f32, f32, f32, f32) = topbar_icon_basis(bounds);
    let lens_d: f32 = 13.6 * u;
    dl.push(DrawCommand::FillRoundedRect {
        rect: Rect::new(ox + 4.2 * u, oy + 4.2 * u, lens_d, lens_d),
        color: COLOR_TRANSPARENT,
        radius: CornerRadius::all(RADIUS_PILL),
        border: Border::new(sw, color),
    });
    push_rounded_stroke(
        dl,
        Point::new(ox + 16.0 * u, oy + 16.0 * u),
        Point::new(ox + 20.2 * u, oy + 20.2 * u),
        color,
        sw,
    );
}

/// Renders a Lucide / SF-Symbols grade 24x24 Sun icon (hollow center circle + 8 evenly-spaced rounded rays) into `dl`.
///
/// # Arguments
///
/// - `Rect` - Target logical bounding box.
/// - `&mut DisplayList` - Destination vector draw command list.
/// - `Color` - Foreground stroke color.
pub(crate) fn draw_theme_sun_icon(bounds: Rect, dl: &mut DisplayList, color: Color) {
    let (ox, oy, u, sw): (f32, f32, f32, f32) = topbar_icon_basis(bounds);
    let cx: f32 = ox + 12.0 * u;
    let cy: f32 = oy + 12.0 * u;
    let core_r: f32 = 4.2 * u;
    dl.push(DrawCommand::FillRoundedRect {
        rect: Rect::new(cx - core_r, cy - core_r, core_r * 2.0, core_r * 2.0),
        color: COLOR_TRANSPARENT,
        radius: CornerRadius::all(RADIUS_PILL),
        border: Border::new(sw, color),
    });
    let r_in: f32 = 7.0 * u;
    let r_out: f32 = 9.4 * u;
    for idx in 0..8_u8 {
        let ang: f32 = (idx as f32) * (PI * 0.25);
        let (cos_a, sin_a): (f32, f32) = (ang.cos(), ang.sin());
        push_rounded_stroke(
            dl,
            Point::new(cx + r_in * cos_a, cy + r_in * sin_a),
            Point::new(cx + r_out * cos_a, cy + r_out * sin_a),
            color,
            sw,
        );
    }
}

/// Renders a Lucide `MoonStar`-grade 24x24 crescent moon (exact circle-circle analytical arcs + 4-point star sparkle) into `dl`.
///
/// # Arguments
///
/// - `Rect` - Target logical bounding box.
/// - `&mut DisplayList` - Destination vector draw command list.
/// - `Color` - Foreground stroke color.
pub(crate) fn draw_theme_moon_icon(bounds: Rect, dl: &mut DisplayList, color: Color) {
    let (ox, oy, u, sw): (f32, f32, f32, f32) = topbar_icon_basis(bounds);
    let c1x: f32 = 11.8;
    let c1y: f32 = 12.4;
    let r1: f32 = 8.2;
    let c2x: f32 = 16.4;
    let c2y: f32 = 8.2;
    let r2: f32 = 6.6;
    let dx: f32 = c2x - c1x;
    let dy: f32 = c2y - c1y;
    let dist: f32 = (dx * dx + dy * dy).sqrt();
    let base_ang: f32 = dy.atan2(dx);
    let a: f32 = (r1 * r1 - r2 * r2 + dist * dist) / (2.0 * dist);
    let alpha1: f32 = (a / r1).clamp(-1.0, 1.0).acos();
    let theta1_top: f32 = base_ang - alpha1;
    let theta1_bot: f32 = base_ang + alpha1;
    let theta1_end: f32 = theta1_top + 2.0 * PI;
    let outer_steps: usize = 28;
    let mut prev_outer: Point = Point::new(
        ox + (c1x + r1 * theta1_bot.cos()) * u,
        oy + (c1y + r1 * theta1_bot.sin()) * u,
    );
    for step in 1..=outer_steps {
        let t: f32 = (step as f32) / (outer_steps as f32);
        let ang: f32 = theta1_bot + (theta1_end - theta1_bot) * t;
        let next_pt: Point = Point::new(
            ox + (c1x + r1 * ang.cos()) * u,
            oy + (c1y + r1 * ang.sin()) * u,
        );
        dl.push(DrawCommand::Line {
            from: prev_outer,
            to: next_pt,
            color,
            width: sw,
        });
        prev_outer = next_pt;
    }
    let p_top_x: f32 = c1x + r1 * theta1_top.cos();
    let p_top_y: f32 = c1y + r1 * theta1_top.sin();
    let p_bot_x: f32 = c1x + r1 * theta1_bot.cos();
    let p_bot_y: f32 = c1y + r1 * theta1_bot.sin();
    let mut theta2_top: f32 = (p_top_y - c2y).atan2(p_top_x - c2x);
    let theta2_bot: f32 = (p_bot_y - c2y).atan2(p_bot_x - c2x);
    if theta2_top < theta2_bot {
        theta2_top += 2.0 * PI;
    }
    let inner_steps: usize = 18;
    let mut prev_inner: Point = Point::new(ox + p_top_x * u, oy + p_top_y * u);
    for step in 1..=inner_steps {
        let t: f32 = (step as f32) / (inner_steps as f32);
        let ang: f32 = theta2_top + (theta2_bot - theta2_top) * t;
        let next_pt: Point = Point::new(
            ox + (c2x + r2 * ang.cos()) * u,
            oy + (c2y + r2 * ang.sin()) * u,
        );
        dl.push(DrawCommand::Line {
            from: prev_inner,
            to: next_pt,
            color,
            width: sw,
        });
        prev_inner = next_pt;
    }
    let tip_r: f32 = (sw * 0.48).max(0.45);
    dl.push(DrawCommand::Circle {
        center: Point::new(ox + p_top_x * u, oy + p_top_y * u),
        radius: tip_r,
        color,
    });
    dl.push(DrawCommand::Circle {
        center: Point::new(ox + p_bot_x * u, oy + p_bot_y * u),
        radius: tip_r,
        color,
    });
    let star_x: f32 = ox + 17.8 * u;
    let star_y: f32 = oy + 6.4 * u;
    let star_arm: f32 = 2.1 * u;
    let star_sw: f32 = (1.45 * u).clamp(1.05, 1.75);
    push_rounded_stroke(
        dl,
        Point::new(star_x, star_y - star_arm),
        Point::new(star_x, star_y + star_arm),
        color,
        star_sw,
    );
    push_rounded_stroke(
        dl,
        Point::new(star_x - star_arm, star_y),
        Point::new(star_x + star_arm, star_y),
        color,
        star_sw,
    );
}

/// Renders a Lucide / SF-Symbols grade 24x24 6-tooth smooth scalloped gear icon with concentric hollow hub into `dl`.
///
/// # Arguments
///
/// - `Rect` - Target logical bounding box.
/// - `&mut DisplayList` - Destination vector draw command list.
/// - `Color` - Foreground stroke color.
pub(crate) fn draw_gear_icon(bounds: Rect, dl: &mut DisplayList, color: Color) {
    let (ox, oy, u, sw): (f32, f32, f32, f32) = topbar_icon_basis(bounds);
    let cx: f32 = ox + 12.0 * u;
    let cy: f32 = oy + 12.0 * u;
    let teeth: f32 = 6.0;
    let r_valley: f32 = 6.7 * u;
    let r_crown: f32 = 9.4 * u;
    let steepness: f32 = 2.6;
    let norm: f32 = steepness.tanh();
    let steps: usize = 72;
    let start_ang: f32 = -PI * 0.5;
    let step_ang: f32 = 2.0 * PI / (steps as f32);
    let mut prev_pt: Point = Point::new(cx, cy - r_crown);
    for idx in 1..=steps {
        let ang: f32 = start_ang + ((idx % steps) as f32) * step_ang;
        let wave: f32 = (teeth * (ang - start_ang)).cos();
        let blend: f32 = 0.5 + 0.5 * ((steepness * wave).tanh() / norm);
        let r: f32 = r_valley + (r_crown - r_valley) * blend;
        let next_pt: Point = Point::new(cx + r * ang.cos(), cy + r * ang.sin());
        dl.push(DrawCommand::Line {
            from: prev_pt,
            to: next_pt,
            color,
            width: sw,
        });
        prev_pt = next_pt;
    }
    let hole_r: f32 = 3.1 * u;
    dl.push(DrawCommand::FillRoundedRect {
        rect: Rect::new(cx - hole_r, cy - hole_r, hole_r * 2.0, hole_r * 2.0),
        color: COLOR_TRANSPARENT,
        radius: CornerRadius::all(RADIUS_PILL),
        border: Border::new(sw, color),
    });
}

/// Returns the clean human-readable display name for one of the 17 built-in `DesignScheme` presets.
///
/// # Arguments
///
/// - `DesignScheme` - Target design scheme variant.
///
/// # Returns
///
/// - `&'static str` - Human-readable scheme title.
pub(crate) fn scheme_display_label(scheme: DesignScheme) -> &'static str {
    match scheme {
        DesignScheme::Shadcn => "Shadcn",
        DesignScheme::Cupertino => "Cupertino",
        DesignScheme::Material => "Material",
        DesignScheme::Fluent => "Fluent",
        DesignScheme::AntDesign => "Ant Design",
        DesignScheme::Bootstrap => "Bootstrap",
        DesignScheme::DaisyUi => "DaisyUI",
        DesignScheme::Geist => "Geist",
        DesignScheme::HeroUi => "HeroUI",
        DesignScheme::Linear => "Linear",
        DesignScheme::Mantine => "Mantine",
        DesignScheme::Monochrome => "Monochrome",
        DesignScheme::Notion => "Notion",
        DesignScheme::Park => "Park UI",
        DesignScheme::Radix => "Radix",
        DesignScheme::Stripe => "Stripe",
        DesignScheme::Supabase => "Supabase",
    }
}

/// Renders a Lucide-grade 24×24 `Palette` vector icon with an active theme accent swatch dot into `dl`.
///
/// # Arguments
///
/// - `Rect` - Target logical bounding box.
/// - `&mut DisplayList` - Destination vector draw command list.
/// - `Color` - Foreground stroke color.
/// - `Color` - Active theme primary accent color rendered inside the swatch dot.
pub(crate) fn draw_topbar_palette_icon(
    bounds: Rect,
    dl: &mut DisplayList,
    color: Color,
    accent: Color,
) {
    let (ox, oy, u, sw): (f32, f32, f32, f32) = topbar_icon_basis(bounds);
    let cx: f32 = ox + 12.0 * u;
    let cy: f32 = oy + 12.0 * u;
    let r: f32 = 9.0 * u;
    let start_ang: f32 = 0.46;
    let end_ang: f32 = 2.0 * PI - 0.14;
    let arc_steps: usize = 28;
    let mut prev: Point = Point::new(cx + r * start_ang.cos(), cy + r * start_ang.sin());
    for s in 1..=arc_steps {
        let t: f32 = (s as f32) / (arc_steps as f32);
        let a: f32 = start_ang + (end_ang - start_ang) * t;
        let cur: Point = Point::new(cx + r * a.cos(), cy + r * a.sin());
        dl.push(DrawCommand::Line {
            from: prev,
            to: cur,
            color,
            width: sw,
        });
        prev = cur;
    }
    let notch: [Point; 4] = [
        prev,
        Point::new(ox + 16.4 * u, oy + 12.8 * u),
        Point::new(ox + 14.5 * u, oy + 15.2 * u),
        Point::new(cx + r * start_ang.cos(), cy + r * start_ang.sin()),
    ];
    for i in 0..3usize {
        dl.push(DrawCommand::Line {
            from: notch[i],
            to: notch[i + 1],
            color,
            width: sw,
        });
    }
    let dot_r: f32 = (1.35 * u).max(1.0);
    for &(dx, dy) in [(7.6_f32, 10.0_f32), (11.2, 6.8), (15.6, 8.6)].iter() {
        dl.push(DrawCommand::Circle {
            center: Point::new(ox + dx * u, oy + dy * u),
            radius: dot_r,
            color,
        });
    }
    let swatch_r: f32 = (2.15 * u).max(1.5);
    dl.push(DrawCommand::Circle {
        center: Point::new(ox + 9.2 * u, oy + 15.2 * u),
        radius: swatch_r,
        color: accent,
    });
}

/// Mounts the viewport-clamped scrollable floating 17-Scheme Theme Color Dropdown portal anchored beneath the showcase topbar Palette icon button.
///
/// # Arguments
///
/// - `&ShowcaseState` - Shared reactive showcase state.
/// - `Signal<bool>` - Controlled dropdown open state signal.
/// - `ThemeTokens` - Base design tokens.
/// - `Vec<NodeId>` - Desktop and Mobile Palette button node handles.
///
/// # Returns
///
/// - `NodeId` - Mounted floating scheme dropdown portal handle.
pub(crate) fn mount_topbar_scheme_dropdown_portal(
    state: &ShowcaseState,
    open_sig: Signal<bool>,
    tokens: ThemeTokens,
    triggers: Vec<NodeId>,
) -> NodeId {
    let mut ctx: Context = current_context();
    let scheme_sig: Signal<DesignScheme> = state.get_theme().get_scheme();
    let mode_sig: Signal<ThemeMode> = state.get_theme().get_mode();
    let vp_sig: Signal<Size> = ctx.get_viewport_size();
    let safe_sig: Signal<SafeAreaInsets> = ctx.get_safe_area();
    let init_tok: ThemeTokens = live_tokens_untracked(tokens);
    let p: ThemePalette = init_tok.get_palette();
    let g: ThemeGeometry = init_tok.get_geometry();
    let card_s: SurfaceColors = p.surface(SurfaceRole::Card);
    let menu_r: f32 = g.get_radius_lg();
    let item_r: f32 = concentric_radius(menu_r, CONTROL_INSET);
    let all_schemes: [DesignScheme; 17] = DesignScheme::all();
    let init_scheme: DesignScheme = scheme_sig.get_untracked();
    let init_mode: ThemeMode = mode_sig.get_untracked();
    let hovered_idx: Signal<Option<usize>> = Signal::new(None);
    let mut item_handles: Vec<(DesignScheme, NodeId, NodeId, NodeId, NodeId)> =
        Vec::with_capacity(all_schemes.len());
    let mut item_nodes: Vec<NodeId> = Vec::with_capacity(all_schemes.len());
    for (idx, scheme) in all_schemes.into_iter().enumerate() {
        let is_sel: bool = scheme == init_scheme;
        let (init_bg, init_fg): (Color, Color) = if is_sel {
            let ps: SurfaceColors = p.surface(SurfaceRole::Primary);
            (ps.get_bg(), ps.get_fg())
        } else {
            (COLOR_TRANSPARENT, card_s.get_fg())
        };
        let init_swatch: Color = ThemeTokens::resolve(scheme, init_mode)
            .get_palette()
            .get_primary();
        let lbl_str: &'static str = scheme_display_label(scheme);
        let on_pick = move |_: &UiEvent| {
            let _: bool = scheme_sig.set_if_changed(scheme);
            let _: bool = open_sig.set_if_changed(false);
        };
        let paint_check = move |bounds: Rect, dl: &mut DisplayList| {
            if scheme_sig.get_untracked() == scheme {
                let lp: ThemePalette = live_tokens_untracked(tokens).get_palette();
                let fg: Color = lp.surface(SurfaceRole::Primary).get_fg();
                draw_euv_icon(EuvIconName::Check, bounds, dl, fg);
            }
        };
        let mut dot_id: NodeId = NodeId::default();
        let mut txt_id: NodeId = NodeId::default();
        let mut chk_id: NodeId = NodeId::default();
        let row_id: NodeId = view! {
            row {
                style: {
                    width: Length::Percent(100.0)
                    height: Length::Px(SCHEME_DROPDOWN_ITEM_HEIGHT)
                    padding: Edges::symmetric(0.0, SPACE_SM)
                    align: AlignItems::Center
                    gap: SPACE_SM
                    background: init_bg
                    radius: item_r
                    overflow: Overflow::Hidden
                    cursor: CursorIcon::Pointer
                    stop_event_propagation: true
                }
                role: A11yRole::MenuItem
                a11y_label: lbl_str.to_string()
                on_click: on_pick
                view {
                    node_ref: dot_id
                    style: {
                        width: Length::Px(12.0)
                        height: Length::Px(12.0)
                        flex_shrink: 0.0
                        background: init_swatch
                        border: (1.0, p.get_border())
                        radius: RADIUS_PILL
                    }
                }
                text {
                    node_ref: txt_id
                    style: {
                        flex_grow: 1.0
                        flex_shrink: 1.0
                        min_width: Length::Px(0.0)
                        font_size: 13.0
                        font_weight: if is_sel { FontWeight::SemiBold } else { FontWeight::Normal }
                        color: init_fg
                        overflow: Overflow::Hidden
                        text_overflow: TextOverflow::Ellipsis
                    }
                    { lbl_str }
                }
                canvas {
                    node_ref: chk_id
                    style: {
                        width: Length::Px(13.0)
                        height: Length::Px(13.0)
                        flex_shrink: 0.0
                    }
                    painter: paint_check
                }
            }
        };
        ctx.on_pointer_enter(row_id, move |_: &UiEvent| {
            let _: bool = hovered_idx.set_if_changed(Some(idx));
        });
        ctx.on_pointer_leave(row_id, move |_: &UiEvent| {
            if hovered_idx.get_untracked() == Some(idx) {
                let _: bool = hovered_idx.set_if_changed(None);
            }
        });
        item_handles.push((scheme, row_id, dot_id, txt_id, chk_id));
        item_nodes.push(row_id);
    }
    let mut card: NodeId = NodeId::default();
    let mut scroll_list: NodeId = NodeId::default();
    let portal: NodeId = view! {
        portal {
            style: {
                display: Display::None
                position: Position::Fixed
                inset: (Some(0.0), Some(0.0), Some(0.0), Some(0.0))
                width: Length::Percent(100.0)
                height: Length::Percent(100.0)
                background: COLOR_TRANSPARENT
                pointer_events: PointerEvents::None
                stop_scroll_propagation: true
                z_index: SCHEME_DROPDOWN_Z_INDEX
            }
            column {
                node_ref: card
                style: {
                    position: Position::Absolute
                    inset: (Some(SHELL_DESKTOP_HEADER_HEIGHT + 6.0), None, None, Some(8.0))
                    width: Length::Px(SCHEME_DROPDOWN_WIDTH)
                    padding: Edges::all(CONTROL_INSET)
                    background: card_s.get_bg()
                    border: (g.get_border_width(), card_s.get_border())
                    radius: menu_r
                    shadow: g.get_shadow_lg()
                    overflow: Overflow::Hidden
                    stop_event_propagation: true
                }
                role: A11yRole::Menu
                a11y_label: BTN_SCHEME_A11Y.to_string()
                scroll {
                    node_ref: scroll_list
                    style: {
                        Style::default().column()
                        width: Length::Percent(100.0)
                        max_height: Length::Px(SCHEME_DROPDOWN_MAX_HEIGHT)
                        gap: CONTROL_INSET
                    }
                    ..item_nodes
                }
            }
        }
    };
    ctx.on_click(portal, move |ev: &UiEvent| {
        if ev.get_target() == portal {
            let _: bool = open_sig.set_if_changed(false);
        }
    });
    for key_target in [portal, card].into_iter() {
        ctx.on_key(key_target, move |ev: &UiEvent| {
            let is_esc: bool = matches!(ev.get_kind(), UiEventKind::KeyDown(KeyCode::Escape, _))
                || matches!(ev.get_kind(), UiEventKind::KeyDown(KeyCode::Back, _));
            if is_esc && open_sig.get_untracked() {
                let _: bool = open_sig.set_if_changed(false);
            }
        });
    }
    let sc_pos: SceneHandle = ctx.get_scene().clone();
    let sc_paint: SceneHandle = sc_pos.clone();
    let owned_nodes: Vec<NodeId> = {
        let mut v: Vec<NodeId> = Vec::with_capacity(item_handles.len() * 4 + 3);
        v.push(portal);
        v.push(card);
        v.push(scroll_list);
        for &(_, rid, did, tid, cid) in item_handles.iter() {
            v.push(rid);
            v.push(did);
            v.push(tid);
            v.push(cid);
        }
        v
    };
    let (scope, _): (Scope, Effect) = Scope::run(|_: Scope| {
        let _: Effect = EuvOverlayStack::bind_signal(
            open_sig,
            SCHEME_DROPDOWN_OVERLAY_NAME,
            SCHEME_DROPDOWN_Z_INDEX,
        );
        let _: Effect = Effect::new(move || {
            let is_open: bool = open_sig.get();
            let vp: Size = vp_sig.get();
            let safe: SafeAreaInsets = safe_sig.get();
            let vp_w: f32 = vp.get_width().max(320.0);
            let vp_h: f32 = vp.get_height().max(320.0);
            let margin: f32 = SCHEME_DROPDOWN_EDGE_MARGIN;
            let safe_l: f32 = safe.get_left().max(0.0);
            let safe_r: f32 = safe.get_right().max(0.0);
            let safe_t: f32 = safe.get_top().max(0.0);
            let safe_b: f32 = safe.get_bottom().max(0.0);
            let menu_w: f32 =
                SCHEME_DROPDOWN_WIDTH.min((vp_w - safe_l - safe_r - margin * 2.0).max(160.0));
            let anchor_opt: Option<Rect> = sc_pos.with_read(
                |tree: &SceneTree| {
                    for &tid in triggers.iter() {
                        if tree.is_node_visible(tid)
                            && let Some(r) = tree.node_screen_bounds(tid)
                            && r.get_width() > 4.0
                            && r.get_height() > 4.0
                        {
                            return Some(r);
                        }
                    }
                    None
                },
                None,
            );
            let (raw_left, raw_top): (f32, f32) = match anchor_opt {
                Some(r) => (
                    r.get_x() + r.get_width() - menu_w,
                    r.get_y() + r.get_height() + 6.0,
                ),
                None => (
                    vp_w - safe_r - menu_w - margin,
                    SHELL_DESKTOP_HEADER_HEIGHT + safe_t + 6.0,
                ),
            };
            let min_left: f32 = safe_l + margin;
            let max_left: f32 = (vp_w - safe_r - menu_w - margin).max(min_left);
            let left: f32 = raw_left.clamp(min_left, max_left);
            let min_top: f32 = safe_t + margin;
            let top_clamped: f32 = raw_top.max(min_top);
            let avail_h: f32 = (vp_h - safe_b - top_clamped - margin).max(120.0);
            let max_h: f32 = SCHEME_DROPDOWN_MAX_HEIGHT.min(avail_h).max(120.0);
            let max_top: f32 = (vp_h - safe_b - max_h - margin).max(min_top);
            let top: f32 = top_clamped.min(max_top);
            let inner_max_h: f32 = (max_h - CONTROL_INSET * 2.0).max(96.0);
            if !is_open {
                let _: bool = hovered_idx.set_if_changed(None);
            }
            sc_pos.with_write(
                |tree: &mut SceneTree| {
                    let dirty_l: u16 = DIRTY_LAYOUT | DIRTY_SUBTREE_LAYOUT;
                    let dirty_all: u16 = dirty_l | DIRTY_PAINT;
                    if let Some(n) = tree.get_node_mut(scroll_list) {
                        n.get_mut_style()
                            .get_mut_layout()
                            .set_max_height(Length::Px(inner_max_h));
                        tree.mark_dirty(scroll_list, dirty_all);
                    }
                    if let Some(n) = tree.get_node_mut(card) {
                        let lay: &mut LayoutStyle = n.get_mut_style().get_mut_layout();
                        lay.set_left(Some(left))
                            .set_top(Some(top))
                            .set_width(Length::Px(menu_w));
                        tree.mark_dirty(card, dirty_all);
                    }
                    if let Some(n) = tree.get_node_mut(portal) {
                        let st: &mut Style = n.get_mut_style();
                        st.get_mut_layout().set_display(if is_open {
                            Display::Flex
                        } else {
                            Display::None
                        });
                        st.get_mut_paint().set_pointer_events(if is_open {
                            PointerEvents::Auto
                        } else {
                            PointerEvents::None
                        });
                        tree.mark_dirty(portal, dirty_all);
                    }
                },
                (),
            );
        });
        Effect::new(move || {
            let cur_scheme: DesignScheme = scheme_sig.get();
            let cur_mode: ThemeMode = mode_sig.get();
            let hov: Option<usize> = hovered_idx.get();
            let tok: ThemeTokens = use_live_tokens(tokens);
            let lp: ThemePalette = tok.get_palette();
            let lg: ThemeGeometry = tok.get_geometry();
            let cs: SurfaceColors = lp.surface(SurfaceRole::Card);
            let ps: SurfaceColors = lp.surface(SurfaceRole::Primary);
            let ac: SurfaceColors = lp.surface(SurfaceRole::Accent);
            sc_paint.with_write(
                |tree: &mut SceneTree| {
                    if let Some(n) = tree.get_node_mut(card) {
                        n.get_mut_style()
                            .get_mut_paint()
                            .set_background(Some(cs.get_bg()))
                            .set_border(Border::new(lg.get_border_width(), cs.get_border()))
                            .set_shadow(Some(lg.get_shadow_lg()));
                        tree.mark_dirty(card, DIRTY_PAINT);
                    }
                    for (idx, &(sc_item, rid, did, tid, cid)) in item_handles.iter().enumerate() {
                        let is_sel: bool = sc_item == cur_scheme;
                        let is_hov: bool = hov == Some(idx);
                        let (bg, fg): (Color, Color) = if is_sel {
                            (ps.get_bg(), ps.get_fg())
                        } else if is_hov {
                            (ac.get_bg(), ac.get_fg())
                        } else {
                            (COLOR_TRANSPARENT, cs.get_fg())
                        };
                        let sw_col: Color = ThemeTokens::resolve(sc_item, cur_mode)
                            .get_palette()
                            .get_primary();
                        let sw_border: Color = if is_sel {
                            ps.get_fg().with_alpha(160)
                        } else {
                            lp.get_border()
                        };
                        if let Some(n) = tree.get_node_mut(rid) {
                            n.get_mut_style().get_mut_paint().set_background(Some(bg));
                            tree.mark_dirty(rid, DIRTY_PAINT);
                        }
                        if let Some(n) = tree.get_node_mut(did) {
                            n.get_mut_style()
                                .get_mut_paint()
                                .set_background(Some(sw_col))
                                .set_border(Border::new(1.0, sw_border));
                            tree.mark_dirty(did, DIRTY_PAINT);
                        }
                        if let Some(n) = tree.get_node_mut(tid) {
                            n.get_mut_style()
                                .get_mut_text()
                                .set_color(fg)
                                .set_weight(if is_sel {
                                    FontWeight::SemiBold
                                } else {
                                    FontWeight::Normal
                                });
                            tree.mark_dirty(tid, DIRTY_PAINT);
                        }
                        tree.mark_dirty(cid, DIRTY_PAINT);
                    }
                },
                (),
            );
        })
    });
    ctx.get_scene().with_write(
        |tree: &mut SceneTree| {
            mark_theme_owned(tree, owned_nodes.as_slice());
            tree.attach_scope(portal, scope);
        },
        (),
    );
    portal
}

/// Mounts one uniform topbar action cluster (command palette, 17-scheme Palette dropdown trigger, Light/Dark toggle, Settings gear) whose square icon buttons share the shell control-line height and `radius_md`.
///
/// # Arguments
///
/// - `&ShowcaseState` - Shared reactive showcase state.
/// - `Signal<bool>` - Controlled scheme dropdown visibility signal.
/// - `ThemeTokens` - Active design tokens at mount time.
///
/// # Returns
///
/// - `(NodeId, NodeId)` - Mounted action row handle and Palette trigger button handle.
pub(crate) fn mount_topbar_actions(
    state: &ShowcaseState,
    scheme_menu_open: Signal<bool>,
    tokens: ThemeTokens,
) -> (NodeId, NodeId) {
    let cmd_open: Signal<bool> = state.get_cmd_palette_open();
    let st_mode: ShowcaseState = state.clone();
    let route_sig: Signal<String> = state.get_route();
    let icon_btn_style = move || -> Style {
        let live_tok: ThemeTokens = use_live_tokens(tokens);
        let live_p: ThemePalette = live_tok.get_palette();
        let live_g: ThemeGeometry = live_tok.get_geometry();
        Style::default()
            .row()
            .width(Length::Px(TOPBAR_CONTROL_SIZE))
            .height(Length::Px(TOPBAR_CONTROL_SIZE))
            .flex_shrink(0.0)
            .justify(JustifyContent::Center)
            .align(AlignItems::Center)
            .background(live_p.get_background())
            .border(live_g.get_border_width(), live_p.get_border())
            .radius(live_g.get_radius_md())
            .cursor(CursorIcon::Pointer)
    };
    let paint_search = move |bounds: Rect, dl: &mut DisplayList| {
        let live_p: ThemePalette = live_tokens_untracked(tokens).get_palette();
        draw_topbar_search_icon(bounds, dl, live_p.get_foreground());
    };
    let paint_palette = move |bounds: Rect, dl: &mut DisplayList| {
        let live_p: ThemePalette = live_tokens_untracked(tokens).get_palette();
        draw_topbar_palette_icon(bounds, dl, live_p.get_foreground(), live_p.get_primary());
    };
    let paint_mode = move |bounds: Rect, dl: &mut DisplayList| {
        let live_tok: ThemeTokens = live_tokens_untracked(tokens);
        let fg: Color = live_tok.get_palette().get_foreground();
        match live_tok.get_mode() {
            ThemeMode::Dark => draw_theme_sun_icon(bounds, dl, fg),
            ThemeMode::Light => draw_theme_moon_icon(bounds, dl, fg),
        }
    };
    let paint_settings = move |bounds: Rect, dl: &mut DisplayList| {
        let live_p: ThemePalette = live_tokens_untracked(tokens).get_palette();
        draw_gear_icon(bounds, dl, live_p.get_foreground());
    };
    let on_open_cmd = move |_: &UiEvent| {
        let _: bool = scheme_menu_open.set_if_changed(false);
        let _: bool = cmd_open.set_if_changed(true);
    };
    let on_toggle_scheme = move |_: &UiEvent| {
        let _: bool = scheme_menu_open.update(|v: &mut bool| *v = !*v);
    };
    let on_toggle_mode = move |_: &UiEvent| {
        st_mode.toggle_theme_mode();
    };
    let on_open_settings = move |_: &UiEvent| {
        let _: bool = scheme_menu_open.set_if_changed(false);
        let _: bool = route_sig.set_if_changed(ROUTE_SETTINGS.to_string());
    };
    let mut palette_btn: NodeId = NodeId::default();
    let cluster: NodeId = view! {
        row {
            style: {
                gap: SPACE_XS
                align: AlignItems::Center
            }
            row {
                bind_style: icon_btn_style
                role: A11yRole::Button
                a11y_label: BTN_SEARCH_A11Y.to_string()
                on_click: on_open_cmd
                canvas {
                    style: {
                        width: Length::Px(TOPBAR_ICON_SIZE)
                        height: Length::Px(TOPBAR_ICON_SIZE)
                    }
                    caching: CanvasCaching::RetainedStatic
                    painter: paint_search
                }
            }
            row {
                node_ref: palette_btn
                bind_style: icon_btn_style
                role: A11yRole::Button
                a11y_label: BTN_SCHEME_A11Y.to_string()
                on_click: on_toggle_scheme
                canvas {
                    style: {
                        width: Length::Px(TOPBAR_ICON_SIZE)
                        height: Length::Px(TOPBAR_ICON_SIZE)
                    }
                    caching: CanvasCaching::RetainedStatic
                    painter: paint_palette
                }
            }
            row {
                bind_style: icon_btn_style
                role: A11yRole::Button
                a11y_label: BTN_MODE_A11Y.to_string()
                on_click: on_toggle_mode
                canvas {
                    style: {
                        width: Length::Px(TOPBAR_ICON_SIZE)
                        height: Length::Px(TOPBAR_ICON_SIZE)
                    }
                    caching: CanvasCaching::RetainedStatic
                    painter: paint_mode
                }
            }
            row {
                bind_style: icon_btn_style
                role: A11yRole::Button
                a11y_label: BTN_SETTINGS_A11Y.to_string()
                on_click: on_open_settings
                canvas {
                    style: {
                        width: Length::Px(TOPBAR_ICON_SIZE)
                        height: Length::Px(TOPBAR_ICON_SIZE)
                    }
                    caching: CanvasCaching::RetainedStatic
                    painter: paint_settings
                }
            }
        }
    };
    (cluster, palette_btn)
}