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
use crate::{
    AppEvent, Command, HostCapabilities, MenuItemSpec, MenuSpec, Point, SettingsPageSpec,
    SettingsValue, Size, TraySpec, UiCommand, UiRect, WindowSpec,
};

#[derive(Debug, Clone, PartialEq)]
pub struct NativeRuntimeStartupRequest {
    pub app_name: String,
    pub main_window: NativeMainWindowRequest,
    pub status_item_tooltip: Option<String>,
    pub status_item: Option<TraySpec>,
    pub settings_pages: Vec<SettingsPageSpec>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeRuntimeStartupResult<Handle: Copy + Eq> {
    Started(NativeMainWindowHandles<Handle>),
    Failed,
}

pub trait NativeRuntimeDriver<UiCommandT = UiCommand, ApplicationEventT = AppEvent> {
    type WindowHandle: Copy + Eq;

    fn start_runtime(
        &mut self,
        request: NativeRuntimeStartupRequest,
    ) -> NativeRuntimeStartupResult<Self::WindowHandle>;
    fn dispatch_ui_command(&mut self, command: UiCommandT);
    fn poll_application_event(&mut self) -> Option<ApplicationEventT>;
    fn request_shutdown(&mut self);
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeRuntimeDriverOperation {
    StartRuntime,
    DispatchUiCommand,
    PollApplicationEvent,
    RequestShutdown,
}

impl NativeRuntimeDriverOperation {
    pub const fn operation_name(self) -> &'static str {
        match self {
            Self::StartRuntime => "start_runtime",
            Self::DispatchUiCommand => "dispatch_ui_command",
            Self::PollApplicationEvent => "poll_application_event",
            Self::RequestShutdown => "request_shutdown",
        }
    }
}

pub const REQUIRED_NATIVE_RUNTIME_DRIVER_OPERATIONS: [NativeRuntimeDriverOperation; 4] = [
    NativeRuntimeDriverOperation::StartRuntime,
    NativeRuntimeDriverOperation::DispatchUiCommand,
    NativeRuntimeDriverOperation::PollApplicationEvent,
    NativeRuntimeDriverOperation::RequestShutdown,
];

pub fn required_native_runtime_driver_operation_names() -> Vec<&'static str> {
    REQUIRED_NATIVE_RUNTIME_DRIVER_OPERATIONS
        .iter()
        .map(|operation| operation.operation_name())
        .collect()
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NativeMainWindowRequest {
    pub title: String,
    pub size: Size,
    pub options: NativeWindowOptions,
    pub icon_path: Option<String>,
    pub main_visible: bool,
    pub degraded_capabilities: Vec<String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NativeWindowOptions {
    pub min_size: Option<Size>,
    pub resizable: bool,
    pub decorations: bool,
    /// Decorated window whose caption area belongs to the View.
    pub custom_title_bar: bool,
    pub always_on_top: bool,
    pub transparent: bool,
}

impl NativeWindowOptions {
    pub const fn standard() -> Self {
        Self {
            min_size: None,
            resizable: true,
            decorations: true,
            custom_title_bar: false,
            always_on_top: false,
            transparent: false,
        }
    }

    pub const fn tool_window() -> Self {
        Self {
            min_size: None,
            resizable: false,
            decorations: false,
            custom_title_bar: false,
            always_on_top: true,
            transparent: false,
        }
    }

    pub const fn from_parts(
        min_size: Option<Size>,
        resizable: bool,
        decorations: bool,
        always_on_top: bool,
        transparent: bool,
    ) -> Self {
        Self {
            min_size,
            resizable,
            decorations,
            custom_title_bar: false,
            always_on_top,
            transparent,
        }
    }

    pub const fn with_min_size(mut self, size: Size) -> Self {
        self.min_size = Some(size);
        self
    }

    pub const fn with_custom_title_bar(mut self, custom_title_bar: bool) -> Self {
        self.custom_title_bar = custom_title_bar;
        self
    }

    pub fn from_zsui_window(window: &WindowSpec) -> Self {
        let min_size = match (window.min_width, window.min_height) {
            (Some(width), Some(height)) => Some(Size {
                width: u32_to_i32_saturating(width).max(1),
                height: u32_to_i32_saturating(height).max(1),
            }),
            _ => None,
        };
        Self {
            min_size,
            resizable: window.resizable,
            decorations: window.decorations,
            custom_title_bar: window.custom_title_bar && window.decorations,
            always_on_top: window.always_on_top,
            transparent: window.transparent,
        }
    }

    pub fn from_zsui_window_for_host(window: &WindowSpec, capabilities: &HostCapabilities) -> Self {
        let resolved = window.resolve_for(capabilities);
        Self::from_zsui_window(&resolved.effective)
    }
}

impl Default for NativeWindowOptions {
    fn default() -> Self {
        Self::standard()
    }
}

impl NativeMainWindowRequest {
    pub fn from_zsui_window(window: &WindowSpec) -> Self {
        Self {
            title: window.title.clone(),
            size: Size {
                width: u32_to_i32_saturating(window.width).max(1),
                height: u32_to_i32_saturating(window.height).max(1),
            },
            options: NativeWindowOptions::from_zsui_window(window),
            icon_path: window.icon_path.clone(),
            main_visible: window.visible,
            degraded_capabilities: Vec::new(),
        }
    }

    pub fn from_zsui_window_for_host(window: &WindowSpec, capabilities: &HostCapabilities) -> Self {
        let resolved = window.resolve_for(capabilities);
        let mut request = Self::from_zsui_window(&resolved.effective);
        request.degraded_capabilities = resolved.degraded_capabilities;
        request
    }
}

fn u32_to_i32_saturating(value: u32) -> i32 {
    value.min(i32::MAX as u32) as i32
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NativeMainWindowHandles<Handle: Copy + Eq> {
    pub main: Handle,
    pub quick: Handle,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeMainWindowPresentation<Handle: Copy + Eq> {
    Created(NativeMainWindowHandles<Handle>),
    Failed,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeMainWindowPresentMode {
    ActivateAndFocus,
    NoActivate,
}

pub trait NativeMainWindowHost {
    type Handle: Copy + Eq;
    type AppIcon: Copy + Eq;

    fn create_main_windows(
        &mut self,
        request: NativeMainWindowRequest,
    ) -> NativeMainWindowPresentation<Self::Handle>;
    fn apply_main_window_appearance(&mut self, handle: Self::Handle);
    fn set_main_window_app_icon(
        &mut self,
        handle: Self::Handle,
        icon: NativeAppIconResource<Self::AppIcon>,
    );
    fn hide_main_window(&mut self, handle: Self::Handle);
    fn present_main_window(&mut self, handle: Self::Handle, mode: NativeMainWindowPresentMode);
    fn set_main_window_bounds(&mut self, handle: Self::Handle, bounds: UiRect);
    fn activate_main_window(&mut self, handle: Self::Handle);
    fn foreground_main_window(&mut self, handle: Self::Handle);
    fn restore_main_window(&mut self, handle: Self::Handle);
    fn close_main_window(&mut self, handle: Self::Handle);
    fn set_main_window_activation_policy(&mut self, handle: Self::Handle, allow_activation: bool);
    fn request_main_window_close(&mut self, handle: Self::Handle);
    fn destroy_main_window(&mut self, handle: Self::Handle);
    fn capture_main_pointer(&mut self, handle: Self::Handle);
    fn release_main_pointer(&mut self, handle: Self::Handle);
    fn begin_main_window_drag(&mut self, handle: Self::Handle);
    fn track_main_pointer_leave(&mut self, handle: Self::Handle) -> bool;
    fn request_main_window_area_repaint(
        &mut self,
        handle: Self::Handle,
        area: Option<UiRect>,
        erase: bool,
    ) -> bool;
    fn main_window_layout_dpi(&mut self, handle: Self::Handle) -> u32;
    fn main_window_client_bounds(&mut self, handle: Self::Handle) -> Option<UiRect>;
    fn main_window_bounds(&mut self, handle: Self::Handle) -> Option<UiRect>;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeMainWindowHostOperation {
    CreateMainWindows,
    ApplyMainWindowAppearance,
    SetMainWindowAppIcon,
    HideMainWindow,
    PresentMainWindow,
    SetMainWindowBounds,
    ActivateMainWindow,
    ForegroundMainWindow,
    RestoreMainWindow,
    CloseMainWindow,
    SetMainWindowActivationPolicy,
    RequestMainWindowClose,
    DestroyMainWindow,
    CaptureMainPointer,
    ReleaseMainPointer,
    BeginMainWindowDrag,
    TrackMainPointerLeave,
    RequestMainWindowAreaRepaint,
    MainWindowLayoutDpi,
    MainWindowClientBounds,
    MainWindowBounds,
}

impl NativeMainWindowHostOperation {
    pub const fn operation_name(self) -> &'static str {
        match self {
            Self::CreateMainWindows => "create_main_windows",
            Self::ApplyMainWindowAppearance => "apply_main_window_appearance",
            Self::SetMainWindowAppIcon => "set_main_window_app_icon",
            Self::HideMainWindow => "hide_main_window",
            Self::PresentMainWindow => "present_main_window",
            Self::SetMainWindowBounds => "set_main_window_bounds",
            Self::ActivateMainWindow => "activate_main_window",
            Self::ForegroundMainWindow => "foreground_main_window",
            Self::RestoreMainWindow => "restore_main_window",
            Self::CloseMainWindow => "close_main_window",
            Self::SetMainWindowActivationPolicy => "set_main_window_activation_policy",
            Self::RequestMainWindowClose => "request_main_window_close",
            Self::DestroyMainWindow => "destroy_main_window",
            Self::CaptureMainPointer => "capture_main_pointer",
            Self::ReleaseMainPointer => "release_main_pointer",
            Self::BeginMainWindowDrag => "begin_main_window_drag",
            Self::TrackMainPointerLeave => "track_main_pointer_leave",
            Self::RequestMainWindowAreaRepaint => "request_main_window_area_repaint",
            Self::MainWindowLayoutDpi => "main_window_layout_dpi",
            Self::MainWindowClientBounds => "main_window_client_bounds",
            Self::MainWindowBounds => "main_window_bounds",
        }
    }
}

pub const REQUIRED_NATIVE_MAIN_WINDOW_HOST_OPERATIONS: [NativeMainWindowHostOperation; 21] = [
    NativeMainWindowHostOperation::CreateMainWindows,
    NativeMainWindowHostOperation::ApplyMainWindowAppearance,
    NativeMainWindowHostOperation::SetMainWindowAppIcon,
    NativeMainWindowHostOperation::HideMainWindow,
    NativeMainWindowHostOperation::PresentMainWindow,
    NativeMainWindowHostOperation::SetMainWindowBounds,
    NativeMainWindowHostOperation::ActivateMainWindow,
    NativeMainWindowHostOperation::ForegroundMainWindow,
    NativeMainWindowHostOperation::RestoreMainWindow,
    NativeMainWindowHostOperation::CloseMainWindow,
    NativeMainWindowHostOperation::SetMainWindowActivationPolicy,
    NativeMainWindowHostOperation::RequestMainWindowClose,
    NativeMainWindowHostOperation::DestroyMainWindow,
    NativeMainWindowHostOperation::CaptureMainPointer,
    NativeMainWindowHostOperation::ReleaseMainPointer,
    NativeMainWindowHostOperation::BeginMainWindowDrag,
    NativeMainWindowHostOperation::TrackMainPointerLeave,
    NativeMainWindowHostOperation::RequestMainWindowAreaRepaint,
    NativeMainWindowHostOperation::MainWindowLayoutDpi,
    NativeMainWindowHostOperation::MainWindowClientBounds,
    NativeMainWindowHostOperation::MainWindowBounds,
];

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NativeAppIconResource<Icon: Copy + Eq> {
    pub small: Icon,
    pub big: Icon,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NativeStatusItemRequest {
    pub tooltip: Option<String>,
    pub icon_path: Option<String>,
    pub menu: MenuSpec,
}

impl NativeStatusItemRequest {
    pub fn from_tray_spec(spec: &TraySpec) -> Self {
        Self {
            tooltip: spec.tooltip.clone(),
            icon_path: spec.icon_path.clone(),
            menu: spec.menu.clone(),
        }
    }

    pub fn into_tray_spec(self) -> TraySpec {
        TraySpec {
            tooltip: self.tooltip,
            icon_path: self.icon_path,
            menu: self.menu,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeStatusItemPresentation<Handle: Copy + Eq> {
    Created(Handle),
    Failed,
}

pub trait NativeStatusItemHost {
    type Handle: Copy + Eq;

    fn create_status_item(
        &mut self,
        request: NativeStatusItemRequest,
    ) -> NativeStatusItemPresentation<Self::Handle>;
    fn set_status_item_tooltip(&mut self, handle: Self::Handle, tooltip: Option<String>);
    fn set_status_item_menu(&mut self, handle: Self::Handle, menu: MenuSpec);
    fn destroy_status_item(&mut self, handle: Self::Handle);
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeStatusItemHostOperation {
    CreateStatusItem,
    SetStatusItemTooltip,
    SetStatusItemMenu,
    DestroyStatusItem,
}

impl NativeStatusItemHostOperation {
    pub const fn operation_name(self) -> &'static str {
        match self {
            Self::CreateStatusItem => "create_status_item",
            Self::SetStatusItemTooltip => "set_status_item_tooltip",
            Self::SetStatusItemMenu => "set_status_item_menu",
            Self::DestroyStatusItem => "destroy_status_item",
        }
    }
}

pub const REQUIRED_NATIVE_STATUS_ITEM_HOST_OPERATIONS: [NativeStatusItemHostOperation; 4] = [
    NativeStatusItemHostOperation::CreateStatusItem,
    NativeStatusItemHostOperation::SetStatusItemTooltip,
    NativeStatusItemHostOperation::SetStatusItemMenu,
    NativeStatusItemHostOperation::DestroyStatusItem,
];

pub fn required_native_status_item_host_operation_names() -> Vec<&'static str> {
    REQUIRED_NATIVE_STATUS_ITEM_HOST_OPERATIONS
        .iter()
        .map(|operation| operation.operation_name())
        .collect()
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NativeStatusMenuCommandRequest {
    pub status_item_index: usize,
    pub item_id: Option<String>,
    pub label: Option<String>,
}

impl NativeStatusMenuCommandRequest {
    pub fn by_id(status_item_index: usize, item_id: impl Into<String>) -> Self {
        Self {
            status_item_index,
            item_id: Some(item_id.into()),
            label: None,
        }
    }

    pub fn by_label(status_item_index: usize, label: impl Into<String>) -> Self {
        Self {
            status_item_index,
            item_id: None,
            label: Some(label.into()),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NativeStatusMenuCommandResult {
    Dispatched(Command),
    Disabled,
    NotFound,
}

pub trait NativeStatusMenuCommandHost {
    fn dispatch_status_menu_command(
        &mut self,
        request: NativeStatusMenuCommandRequest,
    ) -> NativeStatusMenuCommandResult;
}

pub fn native_status_menu_command_from_menu(
    menu: &MenuSpec,
    request: &NativeStatusMenuCommandRequest,
) -> NativeStatusMenuCommandResult {
    for item in &menu.items {
        match item {
            MenuItemSpec::Command {
                id,
                label,
                command,
                enabled,
                ..
            } if status_menu_item_matches(id.as_deref(), label, request) => {
                return if *enabled {
                    NativeStatusMenuCommandResult::Dispatched(command.clone())
                } else {
                    NativeStatusMenuCommandResult::Disabled
                };
            }
            MenuItemSpec::Submenu {
                id,
                label,
                enabled,
                menu,
            } => {
                if status_menu_item_matches(id.as_deref(), label, request) {
                    return if *enabled {
                        NativeStatusMenuCommandResult::NotFound
                    } else {
                        NativeStatusMenuCommandResult::Disabled
                    };
                }
                let result = native_status_menu_command_from_menu(menu, request);
                if !matches!(result, NativeStatusMenuCommandResult::NotFound) {
                    return result;
                }
            }
            MenuItemSpec::Command { .. } | MenuItemSpec::Separator => {}
        }
    }
    NativeStatusMenuCommandResult::NotFound
}

fn status_menu_item_matches(
    item_id: Option<&str>,
    label: &str,
    request: &NativeStatusMenuCommandRequest,
) -> bool {
    if let Some(requested_id) = request.item_id.as_deref() {
        return item_id == Some(requested_id);
    }
    request.label.as_deref() == Some(label)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeStatusMenuCommandHostOperation {
    DispatchStatusMenuCommand,
}

impl NativeStatusMenuCommandHostOperation {
    pub const fn operation_name(self) -> &'static str {
        match self {
            Self::DispatchStatusMenuCommand => "dispatch_status_menu_command",
        }
    }
}

pub const REQUIRED_NATIVE_STATUS_MENU_COMMAND_HOST_OPERATIONS:
    [NativeStatusMenuCommandHostOperation; 1] =
    [NativeStatusMenuCommandHostOperation::DispatchStatusMenuCommand];

pub fn required_native_status_menu_command_host_operation_names() -> Vec<&'static str> {
    REQUIRED_NATIVE_STATUS_MENU_COMMAND_HOST_OPERATIONS
        .iter()
        .map(|operation| operation.operation_name())
        .collect()
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NativeMainSearchControlRequest<Owner: Copy + Eq> {
    pub owner: Owner,
    pub id: i64,
    pub bounds: UiRect,
    pub visible: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NativeMainSearchStyleRequest<Handle: Copy + Eq, StyleResource: Copy + Eq> {
    pub handle: Handle,
    pub font_family: String,
    pub font_px: i32,
    pub previous_resource: Option<StyleResource>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeMainSearchStylePresentation<StyleResource: Copy + Eq> {
    Applied(Option<StyleResource>),
    Failed,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeMainSearchControlPresentation<Handle: Copy + Eq> {
    Created(Handle),
    Failed,
}

pub trait NativeMainSearchControlHost {
    type Owner: Copy + Eq;
    type Handle: Copy + Eq;
    type StyleResource: Copy + Eq;

    fn create_search_control(
        &mut self,
        request: NativeMainSearchControlRequest<Self::Owner>,
    ) -> NativeMainSearchControlPresentation<Self::Handle>;
    fn apply_search_style(
        &mut self,
        request: NativeMainSearchStyleRequest<Self::Handle, Self::StyleResource>,
    ) -> NativeMainSearchStylePresentation<Self::StyleResource>;
    fn release_search_style_resource(&mut self, resource: Self::StyleResource);
    fn set_search_bounds(&mut self, handle: Self::Handle, bounds: UiRect);
    fn set_search_visible(&mut self, handle: Self::Handle, visible: bool);
    fn search_text(&self, handle: Self::Handle) -> String;
    fn set_search_text(&mut self, handle: Self::Handle, text: &str);
    fn focus_search(&mut self, handle: Self::Handle);
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeMainSearchControlHostOperation {
    CreateSearchControl,
    ApplySearchStyle,
    ReleaseSearchStyleResource,
    SetSearchBounds,
    SetSearchVisible,
    SearchText,
    SetSearchText,
    FocusSearch,
}

impl NativeMainSearchControlHostOperation {
    pub const fn operation_name(self) -> &'static str {
        match self {
            Self::CreateSearchControl => "create_search_control",
            Self::ApplySearchStyle => "apply_search_style",
            Self::ReleaseSearchStyleResource => "release_search_style_resource",
            Self::SetSearchBounds => "set_search_bounds",
            Self::SetSearchVisible => "set_search_visible",
            Self::SearchText => "search_text",
            Self::SetSearchText => "set_search_text",
            Self::FocusSearch => "focus_search",
        }
    }
}

pub const REQUIRED_NATIVE_MAIN_SEARCH_CONTROL_HOST_OPERATIONS:
    [NativeMainSearchControlHostOperation; 8] = [
    NativeMainSearchControlHostOperation::CreateSearchControl,
    NativeMainSearchControlHostOperation::ApplySearchStyle,
    NativeMainSearchControlHostOperation::ReleaseSearchStyleResource,
    NativeMainSearchControlHostOperation::SetSearchBounds,
    NativeMainSearchControlHostOperation::SetSearchVisible,
    NativeMainSearchControlHostOperation::SearchText,
    NativeMainSearchControlHostOperation::SetSearchText,
    NativeMainSearchControlHostOperation::FocusSearch,
];

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NativeSettingsWindowRequest<Handle: Copy + Eq> {
    pub owner: Handle,
    pub existing: Option<Handle>,
    pub bounds: UiRect,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeSettingsWindowPresentation<Handle: Copy + Eq> {
    FocusedExisting(Handle),
    Created(Handle),
    Failed,
}

pub trait NativeSettingsWindowHost {
    type Handle: Copy + Eq;

    fn present_settings_window(
        &mut self,
        request: NativeSettingsWindowRequest<Self::Handle>,
    ) -> NativeSettingsWindowPresentation<Self::Handle>;
    fn set_settings_window_bounds(&mut self, handle: Self::Handle, bounds: UiRect);
    fn destroy_settings_window(&mut self, handle: Self::Handle);
    fn focus_settings_window(&mut self, handle: Self::Handle);
    fn track_settings_pointer_leave(&mut self, handle: Self::Handle) -> bool;
    fn capture_settings_pointer(&mut self, handle: Self::Handle);
    fn release_settings_pointer(&mut self, handle: Self::Handle);
    fn request_settings_window_repaint(&mut self, handle: Self::Handle) -> bool;
    fn request_settings_window_area_repaint(
        &mut self,
        handle: Self::Handle,
        area: Option<UiRect>,
        erase: bool,
    ) -> bool;
    fn settings_window_layout_dpi(&mut self, handle: Self::Handle) -> u32;
    fn settings_window_client_to_screen(
        &mut self,
        handle: Self::Handle,
        point: Point,
    ) -> Option<Point>;
    fn settings_window_client_bounds(&mut self, handle: Self::Handle) -> Option<UiRect>;
    fn settings_window_bounds(&mut self, handle: Self::Handle) -> Option<UiRect>;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeSettingsWindowHostOperation {
    PresentSettingsWindow,
    SetSettingsWindowBounds,
    DestroySettingsWindow,
    FocusSettingsWindow,
    TrackSettingsPointerLeave,
    CaptureSettingsPointer,
    ReleaseSettingsPointer,
    RequestSettingsWindowRepaint,
    RequestSettingsWindowAreaRepaint,
    SettingsWindowLayoutDpi,
    SettingsWindowClientToScreen,
    SettingsWindowClientBounds,
    SettingsWindowBounds,
}

impl NativeSettingsWindowHostOperation {
    pub const fn operation_name(self) -> &'static str {
        match self {
            Self::PresentSettingsWindow => "present_settings_window",
            Self::SetSettingsWindowBounds => "set_settings_window_bounds",
            Self::DestroySettingsWindow => "destroy_settings_window",
            Self::FocusSettingsWindow => "focus_settings_window",
            Self::TrackSettingsPointerLeave => "track_settings_pointer_leave",
            Self::CaptureSettingsPointer => "capture_settings_pointer",
            Self::ReleaseSettingsPointer => "release_settings_pointer",
            Self::RequestSettingsWindowRepaint => "request_settings_window_repaint",
            Self::RequestSettingsWindowAreaRepaint => "request_settings_window_area_repaint",
            Self::SettingsWindowLayoutDpi => "settings_window_layout_dpi",
            Self::SettingsWindowClientToScreen => "settings_window_client_to_screen",
            Self::SettingsWindowClientBounds => "settings_window_client_bounds",
            Self::SettingsWindowBounds => "settings_window_bounds",
        }
    }
}

pub const REQUIRED_NATIVE_SETTINGS_WINDOW_HOST_OPERATIONS: [NativeSettingsWindowHostOperation; 13] = [
    NativeSettingsWindowHostOperation::PresentSettingsWindow,
    NativeSettingsWindowHostOperation::SetSettingsWindowBounds,
    NativeSettingsWindowHostOperation::DestroySettingsWindow,
    NativeSettingsWindowHostOperation::FocusSettingsWindow,
    NativeSettingsWindowHostOperation::TrackSettingsPointerLeave,
    NativeSettingsWindowHostOperation::CaptureSettingsPointer,
    NativeSettingsWindowHostOperation::ReleaseSettingsPointer,
    NativeSettingsWindowHostOperation::RequestSettingsWindowRepaint,
    NativeSettingsWindowHostOperation::RequestSettingsWindowAreaRepaint,
    NativeSettingsWindowHostOperation::SettingsWindowLayoutDpi,
    NativeSettingsWindowHostOperation::SettingsWindowClientToScreen,
    NativeSettingsWindowHostOperation::SettingsWindowClientBounds,
    NativeSettingsWindowHostOperation::SettingsWindowBounds,
];

#[derive(Debug, Clone, PartialEq)]
pub struct NativeSettingsPageModelRequest {
    pub pages: Vec<SettingsPageSpec>,
}

impl NativeSettingsPageModelRequest {
    pub fn new(pages: impl IntoIterator<Item = SettingsPageSpec>) -> Self {
        Self {
            pages: pages.into_iter().collect(),
        }
    }

    pub fn page_count(&self) -> usize {
        self.pages.len()
    }

    pub fn item_count(&self) -> usize {
        self.pages.iter().map(|page| page.items.len()).sum()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeSettingsPageModelPresentation {
    Bound {
        page_count: usize,
        item_count: usize,
    },
    Failed,
}

pub trait NativeSettingsPageModelHost {
    fn bind_settings_pages(
        &mut self,
        request: NativeSettingsPageModelRequest,
    ) -> NativeSettingsPageModelPresentation;
    fn update_settings_pages(&mut self, request: NativeSettingsPageModelRequest);
    fn clear_settings_pages(&mut self);
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeSettingsPageModelHostOperation {
    BindSettingsPages,
    UpdateSettingsPages,
    ClearSettingsPages,
}

impl NativeSettingsPageModelHostOperation {
    pub const fn operation_name(self) -> &'static str {
        match self {
            Self::BindSettingsPages => "bind_settings_pages",
            Self::UpdateSettingsPages => "update_settings_pages",
            Self::ClearSettingsPages => "clear_settings_pages",
        }
    }
}

pub const REQUIRED_NATIVE_SETTINGS_PAGE_MODEL_HOST_OPERATIONS:
    [NativeSettingsPageModelHostOperation; 3] = [
    NativeSettingsPageModelHostOperation::BindSettingsPages,
    NativeSettingsPageModelHostOperation::UpdateSettingsPages,
    NativeSettingsPageModelHostOperation::ClearSettingsPages,
];

pub fn required_native_settings_page_model_host_operation_names() -> Vec<&'static str> {
    REQUIRED_NATIVE_SETTINGS_PAGE_MODEL_HOST_OPERATIONS
        .iter()
        .map(|operation| operation.operation_name())
        .collect()
}

#[derive(Debug, Clone, PartialEq)]
pub struct NativeSettingsItemUpdateRequest {
    pub page_id: String,
    pub item_id: String,
    pub value: SettingsValue,
}

impl NativeSettingsItemUpdateRequest {
    pub fn new(
        page_id: impl Into<String>,
        item_id: impl Into<String>,
        value: SettingsValue,
    ) -> Self {
        Self {
            page_id: page_id.into(),
            item_id: item_id.into(),
            value,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeSettingsItemUpdateResult {
    Updated,
    NotBound,
    PageNotFound,
    ItemNotFound,
}

pub trait NativeSettingsItemUpdateHost {
    fn update_settings_item_value(
        &mut self,
        request: NativeSettingsItemUpdateRequest,
    ) -> NativeSettingsItemUpdateResult;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeSettingsItemUpdateHostOperation {
    UpdateSettingsItemValue,
}

impl NativeSettingsItemUpdateHostOperation {
    pub const fn operation_name(self) -> &'static str {
        match self {
            Self::UpdateSettingsItemValue => "update_settings_item_value",
        }
    }
}

pub const REQUIRED_NATIVE_SETTINGS_ITEM_UPDATE_HOST_OPERATIONS:
    [NativeSettingsItemUpdateHostOperation; 1] =
    [NativeSettingsItemUpdateHostOperation::UpdateSettingsItemValue];

pub fn required_native_settings_item_update_host_operation_names() -> Vec<&'static str> {
    REQUIRED_NATIVE_SETTINGS_ITEM_UPDATE_HOST_OPERATIONS
        .iter()
        .map(|operation| operation.operation_name())
        .collect()
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NativeSettingsDropdownRequest<Owner: Copy + Eq> {
    pub owner: Owner,
    pub control_id: isize,
    pub anchor: UiRect,
    pub items: Vec<String>,
    pub selected: usize,
    pub width: i32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeSettingsDropdownPresentation<Handle: Copy + Eq> {
    Created(Handle),
    Failed,
}

pub trait NativeSettingsDropdownHost {
    type Handle: Copy + Eq;
    type Owner: Copy + Eq;

    fn present_settings_dropdown(
        &mut self,
        request: NativeSettingsDropdownRequest<Self::Owner>,
    ) -> NativeSettingsDropdownPresentation<Self::Handle>;
    fn destroy_settings_dropdown(&mut self, handle: Self::Handle);
    fn settings_dropdown_bounds(&self, handle: Self::Handle) -> Option<UiRect>;
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeSettingsDropdownHostOperation {
    PresentSettingsDropdown,
    DestroySettingsDropdown,
    SettingsDropdownBounds,
}

impl NativeSettingsDropdownHostOperation {
    pub const fn operation_name(self) -> &'static str {
        match self {
            Self::PresentSettingsDropdown => "present_settings_dropdown",
            Self::DestroySettingsDropdown => "destroy_settings_dropdown",
            Self::SettingsDropdownBounds => "settings_dropdown_bounds",
        }
    }
}

pub const REQUIRED_NATIVE_SETTINGS_DROPDOWN_HOST_OPERATIONS: [NativeSettingsDropdownHostOperation;
    3] = [
    NativeSettingsDropdownHostOperation::PresentSettingsDropdown,
    NativeSettingsDropdownHostOperation::DestroySettingsDropdown,
    NativeSettingsDropdownHostOperation::SettingsDropdownBounds,
];

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{CapabilitySupport, HostCapabilities, PlatformName, UiCommand};

    #[derive(Default)]
    struct RecordingRuntimeDriver {
        started: bool,
        commands: Vec<UiCommand>,
        events: Vec<crate::AppEvent>,
        shutdown_requested: bool,
    }

    impl NativeRuntimeDriver for RecordingRuntimeDriver {
        type WindowHandle = u32;

        fn start_runtime(
            &mut self,
            request: NativeRuntimeStartupRequest,
        ) -> NativeRuntimeStartupResult<Self::WindowHandle> {
            assert_eq!(request.app_name, "Example");
            assert_eq!(request.main_window.title, "Example");
            self.started = true;
            NativeRuntimeStartupResult::Started(NativeMainWindowHandles { main: 1, quick: 2 })
        }

        fn dispatch_ui_command(&mut self, command: UiCommand) {
            self.commands.push(command);
            self.events.push(crate::AppEvent::Started);
        }

        fn poll_application_event(&mut self) -> Option<crate::AppEvent> {
            self.events.pop()
        }

        fn request_shutdown(&mut self) {
            self.shutdown_requested = true;
        }
    }

    #[test]
    fn native_runtime_driver_contract_executes_framework_path() {
        let mut driver = RecordingRuntimeDriver::default();
        let startup = driver.start_runtime(NativeRuntimeStartupRequest {
            app_name: "Example".to_string(),
            main_window: NativeMainWindowRequest {
                title: "Example".to_string(),
                size: Size {
                    width: 320,
                    height: 240,
                },
                options: NativeWindowOptions::standard(),
                icon_path: None,
                main_visible: true,
                degraded_capabilities: Vec::new(),
            },
            status_item_tooltip: Some("Example".to_string()),
            status_item: None,
            settings_pages: Vec::new(),
        });

        assert_eq!(
            startup,
            NativeRuntimeStartupResult::Started(NativeMainWindowHandles { main: 1, quick: 2 })
        );
        driver.dispatch_ui_command(UiCommand::app(crate::CommandId("example.open")));
        assert_eq!(driver.commands[0].id.0, "example.open");
        assert_eq!(
            driver.poll_application_event(),
            Some(crate::AppEvent::Started)
        );
        driver.request_shutdown();
        assert!(driver.started);
        assert!(driver.shutdown_requested);
    }

    #[test]
    fn native_window_request_resolves_unsupported_traits_for_host() {
        let mut capabilities = HostCapabilities::all_supported(PlatformName::Unknown);
        capabilities.window_always_on_top = CapabilitySupport::unsupported("topmost unavailable");
        capabilities.window_transparency =
            CapabilitySupport::unsupported("transparency unavailable");

        let window = WindowSpec::new("Example")
            .always_on_top(true)
            .transparent(true);
        let request = NativeMainWindowRequest::from_zsui_window_for_host(&window, &capabilities);

        assert_eq!(request.title, "Example");
        assert!(!request.options.always_on_top);
        assert!(!request.options.transparent);
        assert_eq!(request.degraded_capabilities.len(), 2);
    }

    #[test]
    fn native_window_request_carries_declarative_icon_path() {
        let request = NativeMainWindowRequest::from_zsui_window(
            &WindowSpec::new("Example").icon_path("assets/app.ico"),
        );

        assert_eq!(request.icon_path.as_deref(), Some("assets/app.ico"));
    }

    #[test]
    fn status_menu_command_helper_routes_nested_typed_commands() {
        let menu = MenuSpec::new()
            .item("Open", Command::ShowMainWindow)
            .submenu(
                "More",
                MenuSpec::new().item("Refresh", Command::custom("example.refresh")),
            );

        assert_eq!(
            native_status_menu_command_from_menu(
                &menu,
                &NativeStatusMenuCommandRequest::by_label(0, "Refresh"),
            ),
            NativeStatusMenuCommandResult::Dispatched(Command::custom("example.refresh"))
        );
        assert_eq!(
            native_status_menu_command_from_menu(
                &menu,
                &NativeStatusMenuCommandRequest::by_label(0, "Missing"),
            ),
            NativeStatusMenuCommandResult::NotFound
        );
    }

    #[test]
    fn native_main_window_host_operations_are_stable() {
        let names: Vec<_> = REQUIRED_NATIVE_MAIN_WINDOW_HOST_OPERATIONS
            .iter()
            .map(|operation| operation.operation_name())
            .collect();

        assert_eq!(names.len(), 21);
        assert_eq!(names[0], "create_main_windows");
        assert_eq!(names[20], "main_window_bounds");
    }

    #[test]
    fn status_item_and_settings_model_operations_are_stable() {
        assert_eq!(
            required_native_status_item_host_operation_names(),
            vec![
                "create_status_item",
                "set_status_item_tooltip",
                "set_status_item_menu",
                "destroy_status_item"
            ]
        );
        assert_eq!(
            required_native_settings_page_model_host_operation_names(),
            vec![
                "bind_settings_pages",
                "update_settings_pages",
                "clear_settings_pages"
            ]
        );
        assert_eq!(
            required_native_status_menu_command_host_operation_names(),
            vec!["dispatch_status_menu_command"]
        );
        assert_eq!(
            required_native_settings_item_update_host_operation_names(),
            vec!["update_settings_item_value"]
        );
    }
}