zsui 0.3.0-alpha.2

Rust-first feature-gated native UI framework for Win32, AppKit, and Linux.
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
use crate::{
    backend_profile::BackendProfile,
    platform_identity::{NativeUiBackendDescriptor, NativeUiBackendStatus, NativeUiPlatform},
    platform_style::ZsPlatformStyle,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum PlatformDesignLanguage {
    Fluent,
    AppKit,
    Gtk,
    Material,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct PlatformExperience {
    platform: NativeUiPlatform,
    design_language: PlatformDesignLanguage,
    backend: BackendProfile,
    backend_status: NativeUiBackendStatus,
    adapter_boundary: &'static str,
}

impl PlatformExperience {
    pub(crate) const fn for_platform(platform: NativeUiPlatform) -> Self {
        match platform {
            NativeUiPlatform::Windows => Self {
                platform,
                design_language: PlatformDesignLanguage::Fluent,
                backend: BackendProfile::windows(),
                backend_status: NativeUiBackendStatus::NativeHostIntegrated,
                adapter_boundary: "WindowsWin32GdiNativeWindowBoundary",
            },
            NativeUiPlatform::Macos => Self {
                platform,
                design_language: PlatformDesignLanguage::AppKit,
                backend: BackendProfile::macos(),
                backend_status: NativeUiBackendStatus::NativeHostIntegrated,
                adapter_boundary: "MacosAppKitWindowService",
            },
            NativeUiPlatform::Linux => Self {
                platform,
                design_language: PlatformDesignLanguage::Gtk,
                backend: BackendProfile::linux(),
                backend_status: NativeUiBackendStatus::NativeHostIntegrated,
                adapter_boundary: "LinuxDirectWindowHost",
            },
            NativeUiPlatform::Android => Self {
                platform,
                design_language: PlatformDesignLanguage::Material,
                backend: BackendProfile::android(),
                backend_status: NativeUiBackendStatus::AdapterBoundaryScaffold,
                adapter_boundary: "AndroidActivityAdapterBoundary",
            },
        }
    }

    pub(crate) const fn current() -> Option<Self> {
        let platform = match NativeUiPlatform::current_target() {
            Some(platform) => platform,
            None => return None,
        };
        let experience = Self::for_platform(platform);
        #[cfg(all(
            target_os = "linux",
            not(target_env = "ohos"),
            feature = "linux-direct-lite",
            not(feature = "linux-direct")
        ))]
        let experience = Self {
            backend: BackendProfile::linux_lite(),
            ..experience
        };
        Some(experience)
    }

    pub(crate) const fn platform(self) -> NativeUiPlatform {
        self.platform
    }

    pub(crate) const fn backend(self) -> BackendProfile {
        self.backend
    }

    pub(crate) const fn backend_descriptor(self) -> NativeUiBackendDescriptor {
        NativeUiBackendDescriptor {
            platform: self.platform,
            toolkit: self.backend.toolkit(),
            status: self.backend_status,
            adapter_boundary: self.adapter_boundary,
            module_path: self.backend.host().module_path(),
        }
    }

    pub(crate) const fn is_desktop(self) -> bool {
        matches!(
            self.platform,
            NativeUiPlatform::Windows | NativeUiPlatform::Macos | NativeUiPlatform::Linux
        )
    }

    /// Maps a registered platform experience to the shared component profile.
    ///
    /// Desktop backends may reuse one of these profiles without adding target
    /// branches to component modules. Mobile registrations deliberately return
    /// `None` until their own component profiles and runtime hosts exist.
    pub(crate) const fn shared_component_style(self) -> Option<ZsPlatformStyle> {
        match self.design_language {
            PlatformDesignLanguage::Fluent => Some(ZsPlatformStyle::Windows),
            PlatformDesignLanguage::AppKit => Some(ZsPlatformStyle::Macos),
            PlatformDesignLanguage::Gtk => Some(ZsPlatformStyle::Gtk),
            PlatformDesignLanguage::Material => None,
        }
    }
}

pub const SUPPORTED_NATIVE_UI_BACKENDS: [NativeUiBackendDescriptor; 4] = [
    PlatformExperience::for_platform(NativeUiPlatform::Windows).backend_descriptor(),
    PlatformExperience::for_platform(NativeUiPlatform::Macos).backend_descriptor(),
    PlatformExperience::for_platform(NativeUiPlatform::Linux).backend_descriptor(),
    PlatformExperience::for_platform(NativeUiPlatform::Android).backend_descriptor(),
];

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        backend_profile::{
            BackendProfile, NativePresenterProfile, NativeRasterProfile, NativeServicesProfile,
            NativeTextProfile,
        },
        native_ui_backend_for_platform, native_ui_platform_for_current_target,
    };

    #[test]
    fn every_platform_has_one_experience_and_backend_profile() {
        for platform in crate::SUPPORTED_NATIVE_UI_PLATFORMS {
            let experience = PlatformExperience::for_platform(platform);
            let backend = native_ui_backend_for_platform(platform)
                .expect("supported platform should have backend metadata");

            assert_eq!(experience.platform(), platform);
            assert_eq!(experience.backend().toolkit(), backend.toolkit);
            assert_eq!(
                experience.backend().host().module_path(),
                backend.module_path
            );
        }
    }

    #[test]
    fn current_experience_consumes_the_canonical_target_selector() {
        let current = PlatformExperience::current();
        assert_eq!(
            current.map(PlatformExperience::platform),
            NativeUiPlatform::current_target()
        );
        assert_eq!(
            NativeUiPlatform::current_target(),
            native_ui_platform_for_current_target()
        );
        assert_eq!(
            NativeUiPlatform::current_target()
                .map(crate::PlatformName::from)
                .unwrap_or(crate::PlatformName::Unknown),
            crate::PlatformName::current()
        );
    }

    #[test]
    fn public_backend_inventory_is_derived_from_platform_experience() {
        for platform in crate::SUPPORTED_NATIVE_UI_PLATFORMS {
            let expected = PlatformExperience::for_platform(platform).backend_descriptor();
            let actual = native_ui_backend_for_platform(platform)
                .expect("supported platform should have a backend descriptor");
            assert_eq!(*actual, expected);
        }

        let manifest = include_str!("../native_adapter_manifest.rs");
        let manifest_core = manifest
            .split_once("#[cfg(test)]")
            .map_or(manifest, |(core, _)| core);
        assert!(manifest_core
            .contains("pub use crate::platform_experience::SUPPORTED_NATIVE_UI_BACKENDS"));
        assert!(!manifest_core.contains("adapter_boundary: \""));
        assert!(!manifest_core.contains("module_path: \"src/"));

        let experience = include_str!("experience.rs");
        let experience_core = experience
            .split_once("#[cfg(test)]")
            .map_or(experience, |(core, _)| core);
        assert!(experience_core.contains("pub const SUPPORTED_NATIVE_UI_BACKENDS"));
        for boundary in [
            "WindowsWin32GdiNativeWindowBoundary",
            "MacosAppKitWindowService",
            "LinuxDirectWindowHost",
            "AndroidActivityAdapterBoundary",
        ] {
            assert_eq!(
                experience_core.matches(boundary).count(),
                1,
                "backend identity should have one registration for {boundary}"
            );
        }

        let launch = include_str!("../native_host_launch.rs");
        let launch_core = launch
            .split_once("#[cfg(test)]")
            .map_or(launch, |(core, _)| core);
        assert!(launch_core.contains("experience.backend_descriptor()"));
        assert!(!launch_core.contains("native_ui_backend_for_platform"));

        let capability = include_str!("../capability.rs");
        let capability_core = capability
            .split_once("#[cfg(test)]")
            .map_or(capability, |(core, _)| core);
        assert!(capability_core.contains("NativeUiPlatform::current_target()"));
        assert!(!capability_core.contains("cfg!(target_os"));
        assert!(!capability_core.contains("#[cfg(target_os"));
    }

    #[test]
    fn shared_component_style_is_registered_once_and_mobile_remains_explicit() {
        assert_eq!(
            PlatformExperience::for_platform(NativeUiPlatform::Windows).shared_component_style(),
            Some(ZsPlatformStyle::Windows)
        );
        assert_eq!(
            PlatformExperience::for_platform(NativeUiPlatform::Macos).shared_component_style(),
            Some(ZsPlatformStyle::Macos)
        );
        assert_eq!(
            PlatformExperience::for_platform(NativeUiPlatform::Linux).shared_component_style(),
            Some(ZsPlatformStyle::Gtk)
        );
        assert_eq!(
            PlatformExperience::for_platform(NativeUiPlatform::Android).shared_component_style(),
            None
        );
    }

    #[test]
    fn backend_profiles_keep_host_text_raster_presenter_and_services_separate() {
        let windows = PlatformExperience::for_platform(NativeUiPlatform::Windows).backend();
        assert_eq!(windows.text(), NativeTextProfile::Uniscribe);
        assert_eq!(windows.raster(), NativeRasterProfile::GdiPlus);
        assert_eq!(windows.presenter(), NativePresenterProfile::BufferedDib);
        assert_eq!(windows.services(), NativeServicesProfile::Win32);
        assert!(windows.has_real_runtime());

        let android = PlatformExperience::for_platform(NativeUiPlatform::Android).backend();
        assert!(!android.has_real_runtime());

        let lite = BackendProfile::linux_lite();
        assert_eq!(lite.text(), NativeTextProfile::ZsuiRustText);
        assert_eq!(lite.text().name(), "zsui_rust_text");
        assert_eq!(lite.raster(), NativeRasterProfile::TinySkia);
        assert_eq!(lite.presenter(), NativePresenterProfile::Softbuffer);
        assert_eq!(lite.services(), NativeServicesProfile::XdgDesktop);
    }

    #[test]
    fn acceptance_application_view_sources_do_not_branch_on_platforms() {
        let applications = [
            (
                "desktop_native_showcase",
                include_str!("../../examples/desktop_native_showcase.rs"),
            ),
            (
                "component_gallery",
                include_str!("../../examples/component_gallery.rs"),
            ),
            (
                "zsui_notepad",
                include_str!("../../examples/zsui_notepad.rs"),
            ),
            (
                "zsui_calculator",
                include_str!("../../examples/zsui_calculator.rs"),
            ),
        ];
        let forbidden = [
            "cfg!(",
            "#[cfg(",
            "target_os",
            "PlatformStyle",
            "NativeUiPlatform",
            "windows_sys",
            "objc2",
            "GtkWidget",
            "HWND",
            "NSView",
        ];

        for (name, source) in applications {
            let view_start = source
                .find("fn view(")
                .unwrap_or_else(|| panic!("{name} should define a shared view function"));
            let main_start = source
                .find("fn main()")
                .unwrap_or_else(|| panic!("{name} should define one shared main function"));
            let authoring_source = &source[view_start..main_start];

            for token in forbidden {
                assert!(
                    !authoring_source.contains(token),
                    "{name} application authoring must not contain platform token {token}"
                );
            }
            assert!(source[main_start..].contains("native_window("));
            assert!(source[main_start..].contains(".stateful_view"));
        }
    }

    #[test]
    fn ordinary_desktop_examples_do_not_select_targets_for_smoke_or_rendering() {
        let applications = [
            (
                "desktop_native_showcase",
                include_str!("../../examples/desktop_native_showcase.rs"),
            ),
            (
                "component_gallery",
                include_str!("../../examples/component_gallery.rs"),
            ),
            (
                "zsui_notepad",
                include_str!("../../examples/zsui_notepad.rs"),
            ),
            (
                "zsui_calculator",
                include_str!("../../examples/zsui_calculator.rs"),
            ),
            (
                "invoice_tool",
                include_str!("../../examples/invoice_tool.rs"),
            ),
            (
                "navigation_shell_layout",
                include_str!("../../examples/navigation_shell_layout.rs"),
            ),
            (
                "rust_first_view",
                include_str!("../../examples/rust_first_view.rs"),
            ),
        ];

        for (name, source) in applications {
            let production = source
                .split_once("#[cfg(test)]")
                .map_or(source, |(production, _)| production);
            for forbidden in [
                "cfg!(",
                "#[cfg(",
                "target_os",
                "NativeUiPlatform",
                "windows_sys",
                "objc2",
                "GtkWidget",
                "HWND",
                "NSView",
            ] {
                assert!(
                    !production.contains(forbidden),
                    "{name} must leave target selection to the framework: {forbidden}"
                );
            }
        }
    }

    #[test]
    fn public_view_builders_do_not_accept_platform_style_parameters() {
        let view_sources = [
            include_str!("../view/widgets/button.rs"),
            include_str!("../view/widgets/input.rs"),
            include_str!("../view/widgets/selection.rs"),
            include_str!("../view/widgets/navigation.rs"),
            include_str!("../view/widgets/data.rs"),
            include_str!("../view/widgets/calculator.rs"),
        ];

        for source in view_sources {
            for line in source.lines() {
                let line = line.trim_start();
                if line.starts_with("pub fn ") {
                    assert!(
                        !line.contains("PlatformStyle"),
                        "public View builder exposes a platform style: {line}"
                    );
                    assert!(
                        !line.contains("_for_style"),
                        "style-specific proof hook must remain crate-private: {line}"
                    );
                }
            }
        }
    }

    #[test]
    fn public_view_ast_payloads_do_not_store_platform_selection() {
        let source = include_str!("../view/node.rs");
        let start = source
            .find("pub enum ZsButtonPresentation")
            .expect("button presentation should remain part of the View AST");
        let end = source[start..]
            .find("pub struct ViewStyle")
            .map(|offset| start + offset)
            .expect("ViewStyle should follow the public View payload enums");
        let public_payloads = &source[start..end];

        assert!(!public_payloads.contains("PlatformStyle"));
        assert!(!public_payloads.contains("NativeUiPlatform"));
        assert!(!public_payloads.contains("platform:"));
    }

    #[test]
    fn shared_menu_and_live_view_contracts_do_not_encode_target_backends() {
        let menu = include_str!("../menu.rs");
        for forbidden in [
            "target_os",
            "windows-win32",
            "macos-appkit",
            "linux-gtk",
            "gtk_accelerator",
            "appkit_key_equivalent",
            "Page_Up",
            "\\u{f700}",
        ] {
            assert!(
                !menu.contains(forbidden),
                "shared menu model contains target encoding: {forbidden}"
            );
        }

        let live_view = include_str!("../view/focus.rs");
        for forbidden in ["#[cfg(all(windows", "windows-win32", "target_os"] {
            assert!(
                !live_view.contains(forbidden),
                "shared live View runtime contains a target gate: {forbidden}"
            );
        }
        assert!(live_view.contains("fn surface(&self) -> (Rect, Dpi);"));
        assert!(live_view.contains("pub(crate) fn surface(&self) -> (Rect, Dpi)"));

        let accelerator_adapter = include_str!("menu_accelerator.rs");
        assert!(accelerator_adapter.contains("pub(crate) fn gtk_accelerator("));
        assert!(accelerator_adapter.contains("pub(crate) fn appkit_key_equivalent("));
        assert!(include_str!("../macos_appkit_menu.rs")
            .contains("platform_menu_accelerator::appkit_key_equivalent"));
        assert!(include_str!("../linux_gtk_menu.rs")
            .contains("platform_menu_accelerator::gtk_accelerator"));
    }

    #[test]
    fn built_in_render_contracts_share_one_platform_style_type() {
        let component_sources = [
            include_str!("../password_box.rs"),
            include_str!("../progress.rs"),
            include_str!("../render_protocol.rs"),
            include_str!("../tooltip.rs"),
            include_str!("../widget_render.rs"),
        ]
        .join("\n");
        let alias_count = component_sources
            .lines()
            .filter(|line| {
                let line = line.trim_start();
                line.starts_with("pub type Zs")
                    && line.contains("PlatformStyle = crate::ZsPlatformStyle")
            })
            .count();
        let duplicated_enum_count = component_sources
            .lines()
            .filter(|line| {
                let line = line.trim_start();
                line.starts_with("pub enum Zs") && line.contains("PlatformStyle")
            })
            .count();
        let shared_style_source = include_str!("style.rs");

        assert_eq!(alias_count, 20, "update the shared-style alias inventory");
        assert_eq!(
            duplicated_enum_count, 0,
            "component render contracts must not declare separate platform enums"
        );
        assert_eq!(
            shared_style_source
                .matches("pub enum ZsPlatformStyle")
                .count(),
            1
        );
        assert_eq!(
            shared_style_source
                .matches("PlatformExperience::current()")
                .count(),
            1
        );
        assert_eq!(
            shared_style_source
                .matches("shared_component_style()")
                .count(),
            1
        );
        assert!(!component_sources.contains("PlatformExperience::"));
        assert!(!component_sources.contains("cfg!(target_os"));
        assert!(!component_sources.contains("cfg!(all(target_os"));
        assert!(!shared_style_source.contains("cfg!(target_os"));

        let experience_source = include_str!("experience.rs");
        let experience_core = experience_source
            .split_once("#[cfg(test)]")
            .map_or(experience_source, |(core, _)| core);
        assert_eq!(
            experience_core
                .matches("pub(crate) const fn shared_component_style")
                .count(),
            1
        );
        assert!(!experience_core.contains("select_desktop<"));
    }

    #[test]
    fn semantic_view_composition_resolves_through_one_internal_component_profile() {
        let profile = include_str!("component_profile/mod.rs");
        let profile_core = profile
            .split_once("#[cfg(test)]")
            .map_or(profile, |(core, _)| core);
        assert_eq!(
            profile_core
                .matches("pub(crate) const fn for_style")
                .count(),
            1
        );
        for contract in [
            "PlatformStyleTokenProfile",
            "PlatformTypographyProfile",
            "PlatformFocusVisualProfile",
            "PlatformBaseControlProfile",
            "PlatformButtonProfile",
            "PlatformNavigationItemProfile",
            "PlatformSectionComposition",
            "PlatformNavigationComposition",
            "PlatformCommandBarProfile",
            "PlatformAccordionProfile",
            "PlatformTabProfile",
            "PlatformDialogProfile",
            "PlatformInfoBarProfile",
            "PlatformTeachingTipProfile",
            "PlatformToastProfile",
            "PlatformBreadcrumbProfile",
            "PlatformToggleButtonProfile",
            "PlatformNumberBoxProfile",
            "PlatformPasswordBoxProfile",
            "PlatformTooltipProfile",
            "PlatformProgressRingProfile",
            "PlatformAutoSuggestProfile",
            "PlatformGridViewProfile",
            "PlatformTreeViewProfile",
            "PlatformTableProfile",
            "PlatformTimePickerProfile",
            "PlatformColorPickerProfile",
            "PlatformCommandPaletteProfile",
            "PlatformShellProfile",
        ] {
            assert!(
                profile_core.contains(contract),
                "component profile is missing {contract}"
            );
        }
        assert!(profile_core.contains("Host/Text/Raster/Presenter/Services"));
        assert!(!profile_core.contains("BackendProfile"));
        assert!(!profile_core.contains("NativeUiPlatform"));

        let data = include_str!("../view/widgets/data.rs");
        let data_core = data
            .split_once("mod data_tests")
            .map_or(data, |(core, _)| core);
        let button = include_str!("../view/widgets/button.rs");
        let paint = include_str!("../view/paint.rs");
        let view_impl = paint
            .find("impl<Msg: Clone> View<Msg> for ViewNode")
            .expect("View paint implementation should exist");
        let navigation_start = paint[view_impl..]
            .find("ViewNodeKind::NavigationView {")
            .map(|offset| view_impl + offset)
            .expect("NavigationView paint arm should exist");
        let navigation_end = paint[navigation_start..]
            .find("ViewNodeKind::Text { text, style }")
            .map(|offset| navigation_start + offset)
            .expect("Text paint arm should follow NavigationView");
        let navigation_paint = &paint[navigation_start..navigation_end];

        for (name, source) in [
            ("data", data_core),
            ("button", button),
            ("navigation paint", navigation_paint),
        ] {
            assert!(
                source.contains("PlatformComponentProfile::"),
                "{name} should resolve component defaults through PlatformComponentProfile"
            );
            for forbidden in [
                "ZsBaseControlPlatformStyle::Windows",
                "ZsBaseControlPlatformStyle::Macos",
                "ZsBaseControlPlatformStyle::Gtk",
            ] {
                assert!(
                    !source.contains(forbidden),
                    "{name} contains a platform composition branch outside the profile: {forbidden}"
                );
            }
        }
    }

    #[test]
    fn component_defaults_are_owned_by_separate_platform_modules() {
        let resolver = include_str!("component_profile/mod.rs");
        for (module, style, forbidden_styles) in [
            (
                include_str!("component_profile/windows.rs"),
                "ZsPlatformStyle::Windows",
                ["ZsPlatformStyle::Macos", "ZsPlatformStyle::Gtk"],
            ),
            (
                include_str!("component_profile/macos.rs"),
                "ZsPlatformStyle::Macos",
                ["ZsPlatformStyle::Windows", "ZsPlatformStyle::Gtk"],
            ),
            (
                include_str!("component_profile/gtk.rs"),
                "ZsPlatformStyle::Gtk",
                ["ZsPlatformStyle::Windows", "ZsPlatformStyle::Macos"],
            ),
        ] {
            assert_eq!(module.matches("const fn profile()").count(), 1);
            assert!(module.contains(style));
            assert!(!module.contains("BackendProfile"));
            assert!(!module.contains("NativeUiPlatform"));
            assert!(!module.contains("target_os"));
            for forbidden in forbidden_styles {
                assert!(
                    !module.contains(forbidden),
                    "{style} component module contains another platform profile: {forbidden}"
                );
            }
        }
        for delegation in [
            "ZsPlatformStyle::Windows => windows::profile()",
            "ZsPlatformStyle::Macos => macos::profile()",
            "ZsPlatformStyle::Gtk => gtk::profile()",
        ] {
            assert!(resolver.contains(delegation));
        }
    }

    #[test]
    fn tab_layout_paint_and_keyboard_behavior_consume_one_internal_profile() {
        let render = include_str!("../widget_render.rs");
        let tab_start = render
            .find("pub type ZsTabPlatformStyle")
            .expect("tab render section should exist");
        let tab_end = render[tab_start..]
            .find("pub const ZS_AUTO_SUGGEST_MAX_VISIBLE_ITEMS")
            .map(|offset| tab_start + offset)
            .expect("auto-suggest section should follow tabs");
        let tab_render = &render[tab_start..tab_end];
        assert!(tab_render.contains("PlatformComponentProfile::for_style"));

        let native = include_str!("../native.rs");
        let cycle_start = native
            .find("fn native_tab_cycle_offset")
            .expect("tab cycle helper should exist");
        let cycle_end = native[cycle_start..]
            .find("#[cfg(feature = \"combo\")]")
            .map(|offset| cycle_start + offset)
            .expect("combo section should follow tab cycle helper");
        let keyboard_start = native
            .find("if matches!(target.kind, crate::ViewHitTargetKind::Tab")
            .expect("tab keyboard route should exist");
        let keyboard_end = native[keyboard_start..]
            .find("#[cfg(feature = \"date-picker\")]")
            .map(|offset| keyboard_start + offset)
            .expect("date picker route should follow tabs");
        for (name, source) in [
            ("tab render", tab_render),
            ("tab cycle", &native[cycle_start..cycle_end]),
            ("tab keyboard", &native[keyboard_start..keyboard_end]),
        ] {
            assert!(
                source.contains("PlatformComponentProfile::"),
                "{name} should consume PlatformComponentProfile"
            );
            for forbidden in [
                "ZsTabPlatformStyle::Windows",
                "ZsTabPlatformStyle::Macos",
                "ZsTabPlatformStyle::Gtk",
            ] {
                assert!(
                    !source.contains(forbidden),
                    "{name} contains a platform branch outside the profile: {forbidden}"
                );
            }
        }
    }

    #[cfg(feature = "dialog")]
    #[test]
    fn content_dialog_layout_paint_and_keyboard_behavior_consume_one_internal_profile() {
        let render = include_str!("../widget_render.rs");
        let dialog_start = render
            .find("pub type ZsContentDialogPlatformStyle")
            .expect("content dialog render section should exist");
        let dialog_end = render[dialog_start..]
            .find("fn place_popup(")
            .map(|offset| dialog_start + offset)
            .expect("popup placement helper should follow content dialog");
        let dialog_render = &render[dialog_start..dialog_end];
        assert!(dialog_render.contains("PlatformDialogProfile::for_platform"));

        let native = include_str!("../native.rs");
        let keyboard_start = native
            .find("if let Some(dialog_target) = interaction_plan")
            .expect("content dialog keyboard route should exist");
        let keyboard_end = native[keyboard_start..]
            .find("if key == NativeViewKey::Tab && !control")
            .map(|offset| keyboard_start + offset)
            .expect("ordinary focus traversal should follow content dialog");
        let dialog_keyboard = &native[keyboard_start..keyboard_end];
        assert!(dialog_keyboard.contains("PlatformComponentProfile::current()"));

        let model = include_str!("../content_dialog.rs");
        for (name, source) in [
            ("dialog render", dialog_render),
            ("dialog keyboard", dialog_keyboard),
            ("dialog public model", model),
        ] {
            for forbidden in [
                "ZsContentDialogPlatformStyle::Windows",
                "ZsContentDialogPlatformStyle::Macos",
                "ZsContentDialogPlatformStyle::Gtk",
                "NativeUiPlatform",
                "target_os",
            ] {
                assert!(
                    !source.contains(forbidden),
                    "{name} contains a platform branch outside the profile: {forbidden}"
                );
            }
        }
    }

    #[test]
    fn feedback_layout_and_paint_consume_internal_component_profiles() {
        let render = include_str!("../widget_render.rs");
        for (name, start_marker, end_marker, profile, style_alias) in [
            (
                "info bar",
                "pub type ZsInfoBarPlatformStyle",
                "pub type ZsTeachingTipPlatformStyle",
                "PlatformInfoBarProfile::for_platform",
                "ZsInfoBarPlatformStyle",
            ),
            (
                "teaching tip",
                "pub type ZsTeachingTipPlatformStyle",
                "pub type ZsToastPlatformStyle",
                "PlatformTeachingTipProfile::for_platform",
                "ZsTeachingTipPlatformStyle",
            ),
            (
                "toast",
                "pub type ZsToastPlatformStyle",
                "pub type ZsBreadcrumbPlatformStyle",
                "PlatformToastProfile::for_platform",
                "ZsToastPlatformStyle",
            ),
        ] {
            let start = render
                .find(start_marker)
                .unwrap_or_else(|| panic!("{name} render section should exist"));
            let end = render[start..]
                .find(end_marker)
                .map(|offset| start + offset)
                .unwrap_or_else(|| panic!("{end_marker} should follow {name}"));
            let section = &render[start..end];
            assert!(
                section.contains(profile),
                "{name} should resolve through {profile}"
            );
            for platform in ["Windows", "Macos", "Gtk"] {
                let forbidden = format!("{style_alias}::{platform}");
                assert!(
                    !section.contains(&forbidden),
                    "{name} contains a platform branch outside the profile: {forbidden}"
                );
            }
        }
    }

    #[test]
    fn foundational_controls_consume_internal_component_profiles() {
        let render = include_str!("../widget_render.rs");
        for (name, start_marker, end_marker, profile, style_alias) in [
            (
                "base controls",
                "pub type ZsBaseControlPlatformStyle",
                "pub type ZsInfoBarPlatformStyle",
                "PlatformBaseControlProfile::for_platform",
                "ZsBaseControlPlatformStyle",
            ),
            (
                "breadcrumb",
                "pub type ZsBreadcrumbPlatformStyle",
                "pub type ZsToggleButtonPlatformStyle",
                "PlatformBreadcrumbProfile::for_platform",
                "ZsBreadcrumbPlatformStyle",
            ),
            (
                "toggle button",
                "pub type ZsToggleButtonPlatformStyle",
                "pub type ZsNumberBoxPlatformStyle",
                "PlatformToggleButtonProfile::for_platform",
                "ZsToggleButtonPlatformStyle",
            ),
            (
                "number box",
                "pub type ZsNumberBoxPlatformStyle",
                "pub type ZsTabPlatformStyle",
                "PlatformNumberBoxProfile::for_platform",
                "ZsNumberBoxPlatformStyle",
            ),
        ] {
            let start = render
                .find(start_marker)
                .unwrap_or_else(|| panic!("{name} render section should exist"));
            let end = render[start..]
                .find(end_marker)
                .map(|offset| start + offset)
                .unwrap_or_else(|| panic!("{end_marker} should follow {name}"));
            let section = &render[start..end];
            assert!(
                section.contains(profile),
                "{name} should resolve through {profile}"
            );
            for platform in ["Windows", "Macos", "Gtk"] {
                let forbidden = format!("{style_alias}::{platform}");
                assert!(
                    !section.contains(&forbidden),
                    "{name} contains a platform branch outside the profile: {forbidden}"
                );
            }
        }
    }

    #[test]
    fn shared_tokens_typography_and_focus_visuals_consume_platform_profiles() {
        let style = include_str!("../style.rs");
        let style_core = style
            .split_once("#[cfg(test)]")
            .map_or(style, |(core, _)| core);
        assert!(style_core.contains("PlatformStyleTokenProfile::for_platform"));

        let render = include_str!("../render_protocol.rs");
        let render_core = render
            .split_once("#[cfg(test)]")
            .map_or(render, |(core, _)| core);
        assert!(render_core.contains("PlatformTypographyProfile::for_platform"));

        let input = include_str!("../native_input_visuals.rs");
        let input_core = input
            .split_once("mod tests {")
            .map_or(input, |(core, _)| core);
        assert!(input_core.contains("PlatformFocusVisualProfile::for_platform"));
        assert!(!input_core.contains("#[cfg(windows)]"));
        assert!(!input_core.contains("#[cfg(not(windows))]"));
        assert!(include_str!("text_shaper_boundary.rs").contains("#[cfg(windows)]"));

        let shared_platform_style = include_str!("style.rs");
        let shared_platform_style_core = shared_platform_style
            .split_once("#[cfg(test)]")
            .map_or(shared_platform_style, |(core, _)| core);
        assert!(shared_platform_style_core.contains("PlatformTimePickerProfile::for_platform"));
        assert!(shared_platform_style_core.contains("PlatformPasswordBoxProfile::for_platform"));

        for (name, source) in [
            ("style tokens", style_core),
            ("typography", render_core),
            ("input focus", input_core),
            ("shared platform behavior", shared_platform_style_core),
        ] {
            for platform in ["Windows", "Macos", "Gtk"] {
                let forbidden = format!("PlatformStyle::{platform}");
                assert!(
                    !source.contains(&forbidden),
                    "{name} contains a desktop platform branch outside the profile: {forbidden}"
                );
            }
        }
    }

    #[test]
    fn password_tooltip_and_progress_ring_consume_internal_component_profiles() {
        for (name, source, profile, style_alias) in [
            (
                "password box",
                include_str!("../password_box.rs"),
                "PlatformPasswordBoxProfile::for_platform",
                "ZsPasswordBoxPlatformStyle",
            ),
            (
                "tooltip",
                include_str!("../tooltip.rs"),
                "PlatformTooltipProfile::for_platform",
                "ZsTooltipPlatformStyle",
            ),
            (
                "progress ring",
                include_str!("../progress.rs"),
                "PlatformProgressRingProfile::for_platform",
                "ZsProgressRingPlatformStyle",
            ),
        ] {
            let production = source
                .split_once("#[cfg(test)]")
                .map_or(source, |(production, _)| production);
            assert!(
                production.contains(profile),
                "{name} should resolve through {profile}"
            );
            for platform in ["Windows", "Macos", "Gtk"] {
                let forbidden = format!("{style_alias}::{platform}");
                assert!(
                    !production.contains(&forbidden),
                    "{name} contains a platform branch outside the profile: {forbidden}"
                );
            }
        }

        let platform_style = include_str!("style.rs");
        let reveal_start = platform_style
            .find("pub const fn default_password_reveal_mode")
            .expect("password reveal default should exist");
        let reveal = &platform_style[reveal_start..];
        assert!(reveal.contains("PlatformPasswordBoxProfile::for_platform"));
    }

    #[test]
    fn search_and_collection_controls_consume_internal_component_profiles() {
        let render = include_str!("../widget_render.rs");
        for (name, start_marker, end_marker, profile, style_alias) in [
            (
                "auto suggest",
                "pub type ZsAutoSuggestPlatformStyle",
                "pub type ZsGridViewPlatformStyle",
                "PlatformAutoSuggestProfile::for_platform",
                "ZsAutoSuggestPlatformStyle",
            ),
            (
                "grid view",
                "pub type ZsGridViewPlatformStyle",
                "pub type ZsTreePlatformStyle",
                "PlatformGridViewProfile::for_platform",
                "ZsGridViewPlatformStyle",
            ),
            (
                "tree view",
                "pub type ZsTreePlatformStyle",
                "pub type ZsTablePlatformStyle",
                "PlatformTreeViewProfile::for_platform",
                "ZsTreePlatformStyle",
            ),
            (
                "data grid",
                "pub type ZsTablePlatformStyle",
                "pub type ZsTimePickerPlatformStyle",
                "PlatformTableProfile::for_platform",
                "ZsTablePlatformStyle",
            ),
        ] {
            let start = render
                .find(start_marker)
                .unwrap_or_else(|| panic!("{name} render section should exist"));
            let end = render[start..]
                .find(end_marker)
                .map(|offset| start + offset)
                .unwrap_or_else(|| panic!("{end_marker} should follow {name}"));
            let section = &render[start..end];
            assert!(
                section.contains(profile),
                "{name} should resolve through {profile}"
            );
            for platform in ["Windows", "Macos", "Gtk"] {
                let forbidden = format!("{style_alias}::{platform}");
                assert!(
                    !section.contains(&forbidden),
                    "{name} contains a platform branch outside the profile: {forbidden}"
                );
            }
        }
    }

    #[test]
    fn picker_and_palette_controls_consume_internal_component_profiles() {
        let render = include_str!("../widget_render.rs");
        for (name, start_marker, end_marker, profile, style_alias) in [
            (
                "time picker",
                "pub type ZsTimePickerPlatformStyle",
                "pub type ZsColorPickerPlatformStyle",
                "PlatformTimePickerProfile::for_platform",
                "ZsTimePickerPlatformStyle",
            ),
            (
                "color picker",
                "pub type ZsColorPickerPlatformStyle",
                "pub type ZsCommandPalettePlatformStyle",
                "PlatformColorPickerProfile::for_platform",
                "ZsColorPickerPlatformStyle",
            ),
            (
                "command palette",
                "pub type ZsCommandPalettePlatformStyle",
                "pub type ZsContentDialogPlatformStyle",
                "PlatformCommandPaletteProfile::for_platform",
                "ZsCommandPalettePlatformStyle",
            ),
        ] {
            let start = render
                .find(start_marker)
                .unwrap_or_else(|| panic!("{name} render section should exist"));
            let end = render[start..]
                .find(end_marker)
                .map(|offset| start + offset)
                .unwrap_or_else(|| panic!("{end_marker} should follow {name}"));
            let section = &render[start..end];
            assert!(
                section.contains(profile),
                "{name} should resolve through {profile}"
            );
            for platform in ["Windows", "Macos", "Gtk"] {
                let forbidden = format!("{style_alias}::{platform}");
                assert!(
                    !section.contains(&forbidden),
                    "{name} contains a platform branch outside the profile: {forbidden}"
                );
            }
        }
    }

    #[test]
    fn shared_widget_renderer_has_no_desktop_platform_variant_branches() {
        let render = include_str!("../widget_render.rs");
        let production = render
            .split_once("#[cfg(test)]")
            .map_or(render, |(production, _)| production);
        for platform in ["Windows", "Macos", "Gtk"] {
            let forbidden = format!("PlatformStyle::{platform}");
            assert!(
                !production.contains(&forbidden),
                "shared widget renderer contains a desktop branch outside profiles: {forbidden}"
            );
        }
    }

    #[test]
    fn document_shell_consumes_the_internal_platform_profile() {
        let shell = include_str!("../document_shell.rs");
        let production = shell
            .split_once("#[cfg(test)]")
            .map_or(shell, |(production, _)| production);

        assert!(production.contains("PlatformComponentProfile::current()"));
        assert!(production.contains("PlatformDocumentShellProfile"));
        for forbidden in [
            "TAB_STRIP_HEIGHT_DP",
            "COMMAND_BAR_HEIGHT_DP",
            "ZSUI_FLUENT_",
            "cfg!(target_os",
            "#[cfg(target_os",
        ] {
            assert!(
                !production.contains(forbidden),
                "document shell bypasses the platform profile: {forbidden}"
            );
        }
    }

    #[test]
    fn calculator_shell_consumes_the_internal_platform_profile() {
        let calculator = include_str!("../calculator.rs");
        let production = calculator
            .split_once("#[cfg(test)]")
            .map_or(calculator, |(production, _)| production);

        assert!(production.contains("PlatformComponentProfile::current().calculator_shell"));
        assert!(production.contains("PlatformCalculatorShellProfile"));
        for forbidden in [
            "HEADER_HEIGHT_DP",
            "DISPLAY_HEIGHT_DP",
            "ZSUI_FLUENT_",
            "cfg!(target_os",
            "#[cfg(target_os",
        ] {
            assert!(
                !production.contains(forbidden),
                "calculator shell bypasses the platform profile: {forbidden}"
            );
        }
    }

    #[test]
    fn legacy_shell_authoring_is_platform_neutral_and_profile_resolved() {
        let shell = include_str!("../shell_layout.rs");
        let shell_core = shell
            .find("mod tests {")
            .map_or(shell, |offset| &shell[..offset]);

        assert!(shell_core.contains("current_shell_profile()"));
        assert!(shell_core.contains("PlatformShellProfile"));
        for forbidden in [
            "PlatformExperience::",
            "NativeUiPlatform",
            "cfg!(target_os",
            "#[cfg(target_os",
        ] {
            assert!(
                !shell_core.contains(forbidden),
                "legacy shell contains a public platform branch: {forbidden}"
            );
        }
        for line in shell_core.lines().map(str::trim_start) {
            if line.starts_with("pub fn ") {
                assert!(
                    !line.contains("PlatformStyle"),
                    "legacy shell exposes platform selection in its public API: {line}"
                );
            }
        }
    }
}