waft-widgets 0.10.1

Widget framework and widgets for Waft
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
1311
1312
1313
1314
1315
1316
//! Flutter counterpart: `widgets/platform_menu_bar.dart`.

use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use std::fmt::{self, Debug};
use std::rc::Rc;

use waft_embedder::{
    IconImage, MenuEntry, MenuId, MenuItem, MenuItemCheck, MenuShortcut, ProvidedItem,
    ShortcutModifiers, TargetPlatform,
};
use waft_foundation::{
    App, DiagnosticPropertiesBuilder, DiagnosticsNode, DiagnosticsProperty, FlagProperty, Handle,
    IterableProperty, StringProperty, VoidCallback,
};
use waft_services::LogicalKeyboardKey;

use crate::WidgetsBinding;
use crate::framework::{
    BuildContext, IntoWidget, KeyRef, State, StateData, StatefulWidget, WidgetRef,
};
use crate::widgets::actions::{Actions, IntentRef};
use crate::widgets::basic::SizedBox;
use crate::widgets::focus_manager::FocusManager;
use crate::widgets::shortcuts::{CharacterActivator, SingleActivator};

/// A class used by [`MenuSerializableShortcut`] to describe the shortcut for serialization to
/// send to the platform for rendering a [`PlatformMenuBar`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ShortcutSerialization {
    trigger: Option<LogicalKeyboardKey>,
    character: Option<String>,
    alt: Option<bool>,
    control: Option<bool>,
    meta: Option<bool>,
    shift: Option<bool>,
    internal: MenuShortcut,
}

impl ShortcutSerialization {
    /// Creates a [`ShortcutSerialization`] representing a single character.
    ///
    /// This is used by a [`CharacterActivator`] to serialize itself.
    pub fn character(
        character: impl Into<String>,
        alt: bool,
        control: bool,
        meta: bool,
    ) -> ShortcutSerialization {
        let character = character.into();
        debug_assert_eq!(character.chars().count(), 1);
        let key = character.chars().next().map_or(0, u64::from);
        ShortcutSerialization {
            internal: MenuShortcut {
                key,
                modifiers: ShortcutModifiers {
                    meta,
                    control,
                    alt,
                    shift: false,
                },
            },
            character: Some(character),
            trigger: None,
            alt: Some(alt),
            control: Some(control),
            meta: Some(meta),
            shift: None,
        }
    }

    /// Creates a [`ShortcutSerialization`] representing a specific [`LogicalKeyboardKey`] and
    /// modifiers.
    ///
    /// This is used by a [`SingleActivator`] to serialize itself.
    pub fn modifier(
        trigger: LogicalKeyboardKey,
        alt: bool,
        control: bool,
        meta: bool,
        shift: bool,
    ) -> ShortcutSerialization {
        debug_assert!(
            ![
                LogicalKeyboardKey::ALT,
                LogicalKeyboardKey::ALT_LEFT,
                LogicalKeyboardKey::ALT_RIGHT,
                LogicalKeyboardKey::CONTROL,
                LogicalKeyboardKey::CONTROL_LEFT,
                LogicalKeyboardKey::CONTROL_RIGHT,
                LogicalKeyboardKey::META,
                LogicalKeyboardKey::META_LEFT,
                LogicalKeyboardKey::META_RIGHT,
                LogicalKeyboardKey::SHIFT,
                LogicalKeyboardKey::SHIFT_LEFT,
                LogicalKeyboardKey::SHIFT_RIGHT,
            ]
            .contains(&trigger),
            "Specifying a modifier key as a trigger is not allowed. Use provided boolean \
             parameters instead."
        );
        ShortcutSerialization {
            internal: MenuShortcut {
                key: trigger.key_id,
                modifiers: ShortcutModifiers {
                    meta,
                    control,
                    alt,
                    shift,
                },
            },
            trigger: Some(trigger),
            character: None,
            alt: Some(alt),
            control: Some(control),
            meta: Some(meta),
            shift: Some(shift),
        }
    }

    /// The keyboard key that triggers this shortcut, if any.
    pub fn trigger(&self) -> Option<LogicalKeyboardKey> {
        self.trigger
    }

    /// The character that triggers this shortcut, if any.
    pub fn character_value(&self) -> Option<&str> {
        self.character.as_deref()
    }

    /// If this shortcut has a [`trigger`](Self::trigger), this indicates whether or not the alt
    /// modifier needs to be down or not.
    pub fn alt(&self) -> Option<bool> {
        self.alt
    }

    /// If this shortcut has a [`trigger`](Self::trigger), this indicates whether or not the
    /// control modifier needs to be down or not.
    pub fn control(&self) -> Option<bool> {
        self.control
    }

    /// If this shortcut has a [`trigger`](Self::trigger), this indicates whether or not the meta
    /// (also known as the Windows or Command key) modifier needs to be down or not.
    pub fn meta(&self) -> Option<bool> {
        self.meta
    }

    /// If this shortcut has a [`trigger`](Self::trigger), this indicates whether or not the shift
    /// modifier needs to be down or not.
    pub fn shift(&self) -> Option<bool> {
        self.shift
    }

    /// Converts the internal representation to the shortcut a menu item carries to the platform.
    pub fn to_menu_shortcut(&self) -> MenuShortcut {
        self.internal
    }
}

/// A mixin allowing a `ShortcutActivator` to provide data for serialization of the shortcut
/// when sending to the platform.
///
/// This is meant for those who have written their own `ShortcutActivator` implementor, and
/// would like to have it work for menus in a [`PlatformMenuBar`] as well.
///
/// Keep in mind that there are limits to the capabilities of the platform APIs, and not all
/// kinds of `ShortcutActivator`s will work with them.
pub trait MenuSerializableShortcut: Debug {
    /// Implement this in a `ShortcutActivator` implementor to allow it to be serialized for use
    /// in a [`PlatformMenuBar`].
    fn serialize_for_menu(&self) -> ShortcutSerialization;
}

pub type MenuSerializableShortcutRef = Rc<dyn MenuSerializableShortcut>;

impl MenuSerializableShortcut for SingleActivator {
    fn serialize_for_menu(&self) -> ShortcutSerialization {
        ShortcutSerialization::modifier(self.trigger, self.alt, self.control, self.meta, self.shift)
    }
}

impl MenuSerializableShortcut for CharacterActivator {
    fn serialize_for_menu(&self) -> ShortcutSerialization {
        ShortcutSerialization::character(self.character.clone(), self.alt, self.control, self.meta)
    }
}

/// An abstract delegate that can be used to set [`WidgetsBinding::set_platform_menu_delegate`]
/// to provide for managing platform menus.
///
/// This can be implemented to provide a different menu plugin than the default system-provided
/// plugin for managing [`PlatformMenuBar`] menus.
///
/// The [`set_menus`](Self::set_menus) method allows for setting of the menu hierarchy when the
/// [`PlatformMenuBar`] menu hierarchy changes.
///
/// This delegate doesn't handle the results of clicking on a menu item, which is left to the
/// implementor to handle for their implementation.
pub trait PlatformMenuDelegate {
    /// Sets the entire menu hierarchy for a platform-rendered menu bar.
    ///
    /// The `top_level_menus` argument is the list of menus that appear in the menu bar, which
    /// themselves can have children.
    ///
    /// To update the menu hierarchy or menu item state, call [`set_menus`](Self::set_menus) with
    /// the modified hierarchy, and it will overwrite the previous menu state.
    fn set_menus(&self, app: &mut App, top_level_menus: &[PlatformMenuItemRef]);

    /// Clears any existing platform-rendered menus and leaves the application with no menus.
    ///
    /// It is not necessary to call this before updating the menu with
    /// [`set_menus`](Self::set_menus).
    fn clear_menus(&self, app: &mut App);

    /// This is called by [`PlatformMenuBar`] when it is initialized, to be sure that only one is
    /// active at a time.
    ///
    /// If the lock is successfully acquired, it will return true.
    fn debug_lock_delegate(&self, context: BuildContext) -> bool;

    /// This is called by [`PlatformMenuBar`] when it is disposed, so that another one can take
    /// over.
    ///
    /// If it successfully unlocks the delegate, it will return true.
    fn debug_unlock_delegate(&self, context: BuildContext) -> bool;
}

pub type PlatformMenuDelegateRef = Rc<dyn PlatformMenuDelegate>;

/// The signature for a function that generates unique menu item IDs for serialization of a
/// [`PlatformMenuItem`].
pub type MenuItemSerializableIdGenerator<'a> = dyn FnMut(PlatformMenuItemRef) -> MenuId + 'a;

/// The platform menu delegate that hands the menus to the host's menu bar
/// (`Platform::menu_bar`), and dispatches a choice the host reports
/// (`EmbedderClient::menu_selected`) to the item chosen.
///
/// An instance of this is set on [`WidgetsBinding::platform_menu_delegate`] by default when the
/// [`WidgetsBinding`] is initialized.
pub struct DefaultPlatformMenuDelegate {
    // Map of distributed IDs to menu items.
    id_map: RefCell<HashMap<MenuId, PlatformMenuItemRef>>,
    // An ever increasing value used to dole out IDs.
    serial: Cell<MenuId>,
    // The context used to "lock" this delegate to a specific instance of PlatformMenuBar to
    // make sure there is only one.
    locked_context: Cell<Option<BuildContext>>,
}

impl DefaultPlatformMenuDelegate {
    /// Creates a [`DefaultPlatformMenuDelegate`], which hears the choices the host reports from
    /// now on.
    pub fn new(app: &mut App) -> Rc<DefaultPlatformMenuDelegate> {
        let delegate = Rc::new(DefaultPlatformMenuDelegate {
            id_map: RefCell::default(),
            serial: Cell::new(0),
            locked_context: Cell::new(None),
        });
        let handler = Rc::downgrade(&delegate);
        app.platform_callbacks_mut().on_menu_selected = Some(Rc::new(move |app, id| {
            if let Some(delegate) = handler.upgrade() {
                delegate.method_call_handler(app, id);
            }
        }));
        delegate
    }

    /// Get the next serialization ID.
    ///
    /// This is called by each item when serializing a new object so that it has a unique ID.
    fn get_id(&self, item: PlatformMenuItemRef) -> MenuId {
        self.serial.set(self.serial.get() + 1);
        self.id_map.borrow_mut().insert(self.serial.get(), item);
        self.serial.get()
    }

    // Handles the choice the host reports, to forward to the selection callbacks.
    fn method_call_handler(&self, app: &mut App, id: MenuId) {
        let item = self.id_map.borrow().get(&id).cloned();
        debug_assert!(
            item.is_some(),
            "Received a menu choice for a menu item with an ID that was not recognized: {id}"
        );
        let Some(item) = item else {
            return;
        };
        select_menu_item(app, &item);
    }
}

/// The body of Dart's `_methodCallHandler` for an item chosen: its `onSelected`, or its intent
/// invoked on the primary focus. A [`PlatformTrayIcon`](super::platform_tray_icon::PlatformTrayIcon)
/// dispatches its menu's choices the same way.
pub(crate) fn select_menu_item(app: &mut App, item: &PlatformMenuItemRef) {
    debug_assert!(
        item.on_selected().is_none() || item.on_selected_intent().is_none(),
        "Only one of PlatformMenuItem.onSelected or PlatformMenuItem.onSelectedIntent may be \
         specified"
    );
    if let Some(on_selected) = item.on_selected().cloned() {
        on_selected(app);
    }
    if let Some(intent) = item.on_selected_intent().cloned() {
        let context = FocusManager::instance(app)
            .primary_focus(app)
            .and_then(|focus| focus.context(app))
            .expect("a menu item's intent is invoked on the primary focus");
        Actions::maybe_invoke(app, context, &*intent);
    }
}

impl PlatformMenuDelegate for DefaultPlatformMenuDelegate {
    fn clear_menus(&self, app: &mut App) {
        self.set_menus(app, &[]);
    }

    fn set_menus(&self, app: &mut App, top_level_menus: &[PlatformMenuItemRef]) {
        self.id_map.borrow_mut().clear();
        let mut representation = Vec::new();
        for child_item in top_level_menus {
            representation
                .extend(Rc::clone(child_item).to_menu_entries(self, &mut |item| self.get_id(item)));
        }
        let platform = app.platform();
        if let Some(menu_bar) = platform.menu_bar() {
            menu_bar.set_menus(&representation);
        }
    }

    fn debug_lock_delegate(&self, context: BuildContext) -> bool {
        if cfg!(debug_assertions) {
            // It's OK to lock if the lock isn't set, but not OK if a different context is
            // locking it.
            if self
                .locked_context
                .get()
                .is_some_and(|locked| locked != context)
            {
                return false;
            }
            self.locked_context.set(Some(context));
        }
        true
    }

    fn debug_unlock_delegate(&self, context: BuildContext) -> bool {
        if cfg!(debug_assertions) {
            // It's OK to unlock if the lock isn't set, but not OK if a different context is
            // unlocking it.
            if self
                .locked_context
                .get()
                .is_some_and(|locked| locked != context)
            {
                return false;
            }
            self.locked_context.set(None);
        }
        true
    }
}

/// A menu bar that uses the platform's native APIs to construct and render a menu described by
/// a [`PlatformMenu`]/[`PlatformMenuItem`] hierarchy.
///
/// This widget is especially useful on macOS, where a system menu is a required part of every
/// application. Where the host has no menu bar (`Platform::menu_bar` is `None`), the menus show
/// nowhere, and the app draws its own.
///
/// As far as the framework is concerned, this widget has no visual representation, and
/// intercepts no events: it just returns the `child` from its build function. This is because
/// all of the rendering, shortcuts, and event handling for the menu is handled by the host. It
/// is only part of the widget tree to provide a convenient refresh mechanism for the menu data.
///
/// There can only be one [`PlatformMenuBar`] at a time using the same
/// [`PlatformMenuDelegate`]. It will assert if more than one is detected.
pub struct PlatformMenuBar {
    pub key: Option<KeyRef>,
    /// The widget below this widget in the tree.
    pub child: Option<WidgetRef>,
    /// The list of menu items that are the top level children of the [`PlatformMenuBar`].
    ///
    /// Whenever the menus list is modified, a new list should be provided.
    pub menus: Vec<PlatformMenuItemRef>,
}

impl PlatformMenuBar {
    /// Creates a [`PlatformMenuBar`] of `menus`.
    pub fn new(menus: Vec<PlatformMenuItemRef>) -> PlatformMenuBar {
        PlatformMenuBar {
            key: None,
            child: None,
            menus,
        }
    }

    pub fn key(mut self, key: KeyRef) -> PlatformMenuBar {
        self.key = Some(key);
        self
    }

    #[cfg_attr(debug_assertions, track_caller)]
    pub fn child<K>(mut self, child: impl IntoWidget<K>) -> PlatformMenuBar {
        self.child = Some(child.into_widget());
        self
    }
}

impl Debug for PlatformMenuBar {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PlatformMenuBar")
            .field("menus", &self.menus)
            .finish_non_exhaustive()
    }
}

impl StatefulWidget for PlatformMenuBar {
    type State = PlatformMenuBarState;

    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn create_state(&self) -> PlatformMenuBarState {
        PlatformMenuBarState {
            state: StateData::new(),
            descendants: Vec::new(),
        }
    }
}

pub struct PlatformMenuBarState {
    state: StateData<PlatformMenuBar>,
    descendants: Vec<PlatformMenuItemRef>,
}

impl PlatformMenuBarState {
    // Updates the data structures for the menu and send them to the platform plugin.
    fn update_menu(self: Handle<Self>, app: &mut App) {
        let menus = self.widget(app).menus.clone();
        let delegate = WidgetsBinding::instance(app).platform_menu_delegate(app);
        delegate.set_menus(app, &menus);
    }
}

impl State for PlatformMenuBarState {
    type Widget = PlatformMenuBar;
    crate::state_accessors!();

    fn init_state(self: Handle<Self>, app: &mut App) {
        let delegate = WidgetsBinding::instance(app).platform_menu_delegate(app);
        let context = self.context(app);
        debug_assert!(
            delegate.debug_lock_delegate(context),
            "More than one active PlatformMenuBar detected. Only one active platform-rendered \
             menu bar is allowed at a time."
        );
        delegate.clear_menus(app);
        self.update_menu(app);
    }

    fn dispose(self: Handle<Self>, app: &mut App) {
        let delegate = WidgetsBinding::instance(app).platform_menu_delegate(app);
        let context = self.context(app);
        debug_assert!(
            delegate.debug_unlock_delegate(context),
            "tried to unlock the DefaultPlatformMenuDelegate more than once with context \
             {context:?}."
        );
        delegate.clear_menus(app);
    }

    fn did_update_widget(self: Handle<Self>, app: &mut App, _old_widget: &PlatformMenuBar) {
        let mut new_descendants = Vec::new();
        for item in &self.widget(app).menus {
            new_descendants.push(Rc::clone(item));
            new_descendants.extend(item.descendants());
        }
        if !same_items(&new_descendants, &app.get(self).descendants) {
            app.get_mut(self).descendants = new_descendants;
            self.update_menu(app);
        }
    }

    fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
        // PlatformMenuBar is really about managing the platform menu bar, and doesn't do any
        // rendering or event handling in the framework.
        self.widget(app)
            .child
            .clone()
            .unwrap_or_else(|| SizedBox::new().into_widget())
    }
}

/// Dart's `listEquals` of menu items, which compares them by identity.
pub(crate) fn same_items(a: &[PlatformMenuItemRef], b: &[PlatformMenuItemRef]) -> bool {
    a.len() == b.len() && a.iter().zip(b).all(|(a, b)| Rc::ptr_eq(a, b))
}

pub type PlatformMenuItemRef = Rc<dyn PlatformMenuItem>;

/// An item in a [`PlatformMenu`]: Dart's `PlatformMenuItem` as the base every menu entry of a
/// [`PlatformMenuBar`] extends. A plain item is a [`PlainPlatformMenuItem`].
///
/// A [`PlatformMenuItem`] may be used to define the leaves of a menu hierarchy, its
/// [`PlatformMenu`]s the branches, and [`PlatformMenuItemGroup`]s the groups divided from the
/// rest.
pub trait PlatformMenuItem: Debug {
    /// The required label used for rendering the menu item.
    fn label(&self) -> &str;

    /// The optional tooltip text that is displayed when the user hovers over the menu item.
    fn tooltip(&self) -> Option<&str> {
        None
    }

    /// The optional shortcut that selects this [`PlatformMenuItem`].
    ///
    /// This shortcut is only enabled when [`on_selected`](Self::on_selected) is set.
    fn shortcut(&self) -> Option<&MenuSerializableShortcutRef> {
        None
    }

    /// An optional callback that is called when this [`PlatformMenuItem`] is selected.
    ///
    /// At most one of [`on_selected`](Self::on_selected) and
    /// [`on_selected_intent`](Self::on_selected_intent) may be set. If neither field is set,
    /// this menu item will be disabled.
    fn on_selected(&self) -> Option<&VoidCallback> {
        None
    }

    /// Returns a callback, if any, to be invoked if the platform menu receives a "Menu.opened"
    /// method call from the platform for this item.
    ///
    /// Only items that have submenus will have this callback invoked.
    fn on_open(&self) -> Option<&VoidCallback> {
        None
    }

    /// Returns a callback, if any, to be invoked if the platform menu receives a "Menu.closed"
    /// method call from the platform for this item.
    ///
    /// Only items that have submenus will have this callback invoked.
    fn on_close(&self) -> Option<&VoidCallback> {
        None
    }

    /// An optional intent that is invoked when this [`PlatformMenuItem`] is selected.
    ///
    /// At most one of [`on_selected`](Self::on_selected) and
    /// [`on_selected_intent`](Self::on_selected_intent) may be set. If neither field is set,
    /// this menu item will be disabled.
    fn on_selected_intent(&self) -> Option<&IntentRef> {
        None
    }

    /// The check the item shows, and which.
    fn check(&self) -> MenuItemCheck {
        MenuItemCheck::Plain
    }

    /// The image the item shows beside its label.
    fn icon(&self) -> Option<&IconImage> {
        None
    }

    /// Returns all descendant [`PlatformMenuItem`]s of this item.
    ///
    /// Returns an empty list if this type of menu item doesn't have descendants.
    fn descendants(&self) -> Vec<PlatformMenuItemRef> {
        Vec::new()
    }

    /// Returns the list of group members if this menu item is a "grouping" menu item, such as
    /// [`PlatformMenuItemGroup`].
    ///
    /// Defaults to an empty list.
    fn members(&self) -> Vec<PlatformMenuItemRef> {
        Vec::new()
    }

    /// Converts the representation of this item into the entries the host's menu takes.
    fn to_menu_entries(
        self: Rc<Self>,
        delegate: &dyn PlatformMenuDelegate,
        get_id: &mut MenuItemSerializableIdGenerator<'_>,
    ) -> Vec<MenuEntry>;

    /// Dart's `PlatformMenuItem.debugFillProperties`, which an implementor's own body calls
    /// where Dart writes `super`.
    fn debug_fill_properties(&self, properties: &mut DiagnosticPropertiesBuilder) {
        fill_platform_menu_item_properties(self, properties);
    }

    /// Dart's `PlatformMenuItem.toStringShort`: the identity, then the label in parentheses.
    fn to_string_short(&self) -> String {
        format!(
            "{}({})",
            waft_foundation::describe_identity(
                waft_foundation::bare_type_name(std::any::type_name::<Self>()),
                self as *const Self as *const () as usize as u64,
            ),
            self.label()
        )
    }

    /// Dart's `PlatformMenuItem.toDiagnosticsNode(name:)`, from `Diagnosticable`.
    fn diagnostics_node(&self, name: Option<String>) -> waft_foundation::DiagnosticableNode {
        let mut properties = DiagnosticPropertiesBuilder::new();
        self.debug_fill_properties(&mut properties);
        waft_foundation::DiagnosticableNode::from_parts(name, self.to_string_short(), properties)
    }

    /// Dart's `Diagnosticable.toString`, which a group's member list prints.
    fn diagnostic_string(&self) -> String {
        use waft_foundation::{DiagnosticLevel, DiagnosticsNode, DiagnosticsTreeStyle};
        self.diagnostics_node(None)
            .with_style(DiagnosticsTreeStyle::SingleLine)
            .to_string_with(None, DiagnosticLevel::Info)
    }
}

/// The bodies of Dart's `PlatformMenuItem` base class, which its implementors reach as `super`.
pub struct PlatformMenuItemBase;

/// Dart's `PlatformMenuItem.debugFillProperties`, one body for its implementors.
pub fn fill_platform_menu_item_properties<T: PlatformMenuItem + ?Sized>(
    item: &T,
    properties: &mut DiagnosticPropertiesBuilder,
) {
    properties.add(StringProperty::new("label", Some(item.label().to_string())));
    properties.add(
        StringProperty::new("tooltip", item.tooltip().map(str::to_string)).default_value(None),
    );
    properties.add(
        DiagnosticsProperty::<String>::new(
            "shortcut",
            item.shortcut().map(|shortcut| format!("{shortcut:?}")),
        )
        .default_value(None),
    );
    properties
        .add(FlagProperty::new("enabled", Some(item.on_selected().is_some())).if_false("DISABLED"));
}

impl PlatformMenuItemBase {
    /// Converts the given [`PlatformMenuItem`] into the entry the host's menu takes.
    pub fn serialize(
        item: PlatformMenuItemRef,
        _delegate: &dyn PlatformMenuDelegate,
        get_id: &mut MenuItemSerializableIdGenerator<'_>,
    ) -> MenuEntry {
        let shortcut = item
            .shortcut()
            .map(|shortcut| shortcut.serialize_for_menu().to_menu_shortcut());
        let menu_item = MenuItem {
            label: item.label().to_owned(),
            enabled: item.on_selected().is_some() || item.on_selected_intent().is_some(),
            check: item.check(),
            shortcut,
            icon: item.icon().cloned(),
        };
        MenuEntry::Item {
            id: get_id(item),
            item: menu_item,
        }
    }
}

/// A [`PlatformMenuItem`] that is a leaf of a menu hierarchy: Dart's `PlatformMenuItem` itself.
pub struct PlainPlatformMenuItem {
    pub label: String,
    pub tooltip: Option<String>,
    pub shortcut: Option<MenuSerializableShortcutRef>,
    pub on_selected: Option<VoidCallback>,
    pub on_selected_intent: Option<IntentRef>,
    pub check: MenuItemCheck,
    pub icon: Option<IconImage>,
}

impl PlainPlatformMenuItem {
    /// Creates a [`PlainPlatformMenuItem`] labelled `label`, disabled until it has an
    /// [`on_selected`](Self::on_selected) or an [`on_selected_intent`](Self::on_selected_intent).
    pub fn new(label: impl Into<String>) -> PlainPlatformMenuItem {
        PlainPlatformMenuItem {
            label: label.into(),
            tooltip: None,
            shortcut: None,
            on_selected: None,
            on_selected_intent: None,
            check: MenuItemCheck::Plain,
            icon: None,
        }
    }

    pub fn tooltip(mut self, tooltip: impl Into<String>) -> PlainPlatformMenuItem {
        self.tooltip = Some(tooltip.into());
        self
    }

    pub fn shortcut(mut self, shortcut: MenuSerializableShortcutRef) -> PlainPlatformMenuItem {
        self.shortcut = Some(shortcut);
        self
    }

    pub fn on_selected(mut self, on_selected: VoidCallback) -> PlainPlatformMenuItem {
        debug_assert!(
            self.on_selected_intent.is_none(),
            "Only one of onSelected or onSelectedIntent may be specified"
        );
        self.on_selected = Some(on_selected);
        self
    }

    pub fn on_selected_intent(mut self, intent: IntentRef) -> PlainPlatformMenuItem {
        debug_assert!(
            self.on_selected.is_none(),
            "Only one of onSelected or onSelectedIntent may be specified"
        );
        self.on_selected_intent = Some(intent);
        self
    }

    pub fn check(mut self, check: MenuItemCheck) -> PlainPlatformMenuItem {
        self.check = check;
        self
    }

    pub fn icon(mut self, icon: IconImage) -> PlainPlatformMenuItem {
        self.icon = Some(icon);
        self
    }
}

impl Debug for PlainPlatformMenuItem {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PlatformMenuItem")
            .field("label", &self.label)
            .field("tooltip", &self.tooltip)
            .field("shortcut", &self.shortcut)
            .field("enabled", &self.on_selected.is_some())
            .finish()
    }
}

impl PlatformMenuItem for PlainPlatformMenuItem {
    fn label(&self) -> &str {
        &self.label
    }

    fn tooltip(&self) -> Option<&str> {
        self.tooltip.as_deref()
    }

    fn shortcut(&self) -> Option<&MenuSerializableShortcutRef> {
        self.shortcut.as_ref()
    }

    fn on_selected(&self) -> Option<&VoidCallback> {
        self.on_selected.as_ref()
    }

    fn on_selected_intent(&self) -> Option<&IntentRef> {
        self.on_selected_intent.as_ref()
    }

    fn check(&self) -> MenuItemCheck {
        self.check
    }

    fn icon(&self) -> Option<&IconImage> {
        self.icon.as_ref()
    }

    fn to_menu_entries(
        self: Rc<Self>,
        delegate: &dyn PlatformMenuDelegate,
        get_id: &mut MenuItemSerializableIdGenerator<'_>,
    ) -> Vec<MenuEntry> {
        vec![PlatformMenuItemBase::serialize(self, delegate, get_id)]
    }
}

/// A class for representing menu items that have child submenus.
pub struct PlatformMenu {
    pub label: String,
    pub tooltip: Option<String>,
    /// A callback that is called when this menu is opened.
    pub on_open: Option<VoidCallback>,
    /// A callback that is called when this menu is closed.
    pub on_close: Option<VoidCallback>,
    /// The menu items in the submenu opened by this menu item.
    ///
    /// If this is an empty list, this [`PlatformMenu`] will be disabled.
    pub menus: Vec<PlatformMenuItemRef>,
}

impl PlatformMenu {
    /// Creates a [`PlatformMenu`] labelled `label`, of `menus`.
    pub fn new(label: impl Into<String>, menus: Vec<PlatformMenuItemRef>) -> PlatformMenu {
        PlatformMenu {
            label: label.into(),
            tooltip: None,
            on_open: None,
            on_close: None,
            menus,
        }
    }

    pub fn tooltip(mut self, tooltip: impl Into<String>) -> PlatformMenu {
        self.tooltip = Some(tooltip.into());
        self
    }

    pub fn on_open(mut self, on_open: VoidCallback) -> PlatformMenu {
        self.on_open = Some(on_open);
        self
    }

    pub fn on_close(mut self, on_close: VoidCallback) -> PlatformMenu {
        self.on_close = Some(on_close);
        self
    }

    /// Returns all descendant [`PlatformMenuItem`]s of this item.
    pub fn get_descendants(item: &PlatformMenu) -> Vec<PlatformMenuItemRef> {
        let mut descendants = Vec::new();
        for child in &item.menus {
            descendants.push(Rc::clone(child));
            descendants.extend(child.descendants());
        }
        descendants
    }

    /// Converts the supplied object to the entry the host's menu takes.
    pub fn serialize(
        item: Rc<PlatformMenu>,
        delegate: &dyn PlatformMenuDelegate,
        get_id: &mut MenuItemSerializableIdGenerator<'_>,
    ) -> MenuEntry {
        let mut result: Vec<MenuEntry> = Vec::new();
        for child_item in &item.menus {
            result.extend(Rc::clone(child_item).to_menu_entries(delegate, get_id));
        }
        strip_dividers(&mut result);
        let enabled = !item.menus.is_empty();
        MenuEntry::Submenu {
            label: item.label.clone(),
            id: get_id(item),
            enabled,
            entries: result,
        }
    }
}

/// The part of Dart's `PlatformMenu.serialize` that strips dividers, which a
/// [`PlatformTrayIcon`](super::platform_tray_icon::PlatformTrayIcon)'s menu, a menu of no
/// [`PlatformMenu`], strips too.
pub(crate) fn strip_dividers(result: &mut Vec<MenuEntry>) {
    // To avoid doing type checking for groups, just filter out when there are multiple
    // sequential dividers, or when they are first or last, since groups may be interleaved
    // with non-groups, and non-groups may also add dividers.
    let mut previous_is_divider: Option<bool> = None;
    result.retain(|entry| {
        let is_divider = matches!(entry, MenuEntry::Separator);
        if previous_is_divider.is_none() && is_divider {
            // Strip any leading dividers.
            return false;
        }
        if previous_is_divider == Some(true) && is_divider {
            // Strip any duplicate dividers.
            return false;
        }
        previous_is_divider = Some(is_divider);
        true
    });
    if matches!(result.last(), Some(MenuEntry::Separator)) {
        result.pop();
    }
}

impl Debug for PlatformMenu {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PlatformMenu")
            .field("label", &self.label)
            .field("enabled", &!self.menus.is_empty())
            .field("menus", &self.menus)
            .finish()
    }
}

impl PlatformMenu {
    /// Dart's `PlatformMenu.debugDescribeChildren`: the items of the submenu.
    pub fn debug_describe_children(&self) -> Vec<Rc<dyn DiagnosticsNode>> {
        self.menus
            .iter()
            .map(|menu| Rc::new(menu.diagnostics_node(None)) as Rc<dyn DiagnosticsNode>)
            .collect()
    }
}

impl PlatformMenuItem for PlatformMenu {
    fn label(&self) -> &str {
        &self.label
    }

    fn debug_fill_properties(&self, properties: &mut DiagnosticPropertiesBuilder) {
        fill_platform_menu_item_properties(self, properties);
        properties.add(StringProperty::new("label", Some(self.label.clone())));
        properties
            .add(FlagProperty::new("enabled", Some(!self.menus.is_empty())).if_false("DISABLED"));
    }

    fn tooltip(&self) -> Option<&str> {
        self.tooltip.as_deref()
    }

    fn on_open(&self) -> Option<&VoidCallback> {
        self.on_open.as_ref()
    }

    fn on_close(&self) -> Option<&VoidCallback> {
        self.on_close.as_ref()
    }

    fn descendants(&self) -> Vec<PlatformMenuItemRef> {
        PlatformMenu::get_descendants(self)
    }

    fn to_menu_entries(
        self: Rc<Self>,
        delegate: &dyn PlatformMenuDelegate,
        get_id: &mut MenuItemSerializableIdGenerator<'_>,
    ) -> Vec<MenuEntry> {
        vec![PlatformMenu::serialize(self, delegate, get_id)]
    }
}

/// A class that groups other menu items into sections delineated by dividers.
///
/// Visual dividers will be added before and after this group if other menu items appear in the
/// [`PlatformMenu`], and the leading one omitted if it is first and the trailing one omitted if
/// it is last in the menu.
pub struct PlatformMenuItemGroup {
    /// The [`PlatformMenuItem`]s that are members of this menu item group.
    ///
    /// An assertion will be thrown if there isn't at least one member of the group.
    pub members: Vec<PlatformMenuItemRef>,
}

impl PlatformMenuItemGroup {
    /// Creates a [`PlatformMenuItemGroup`] of `members`.
    pub fn new(members: Vec<PlatformMenuItemRef>) -> PlatformMenuItemGroup {
        PlatformMenuItemGroup { members }
    }

    /// Converts the supplied object to the entries the host's menu takes.
    pub fn serialize(
        group: &dyn PlatformMenuItem,
        delegate: &dyn PlatformMenuDelegate,
        get_id: &mut MenuItemSerializableIdGenerator<'_>,
    ) -> Vec<MenuEntry> {
        let mut entries = vec![MenuEntry::Separator];
        for item in group.members() {
            entries.extend(item.to_menu_entries(delegate, get_id));
        }
        entries.push(MenuEntry::Separator);
        entries
    }
}

impl Debug for PlatformMenuItemGroup {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PlatformMenuItemGroup")
            .field("members", &self.members)
            .finish()
    }
}

impl PlatformMenuItem for PlatformMenuItemGroup {
    fn label(&self) -> &str {
        ""
    }

    fn debug_fill_properties(&self, properties: &mut DiagnosticPropertiesBuilder) {
        fill_platform_menu_item_properties(self, properties);
        properties.add(IterableProperty::<String>::new(
            "members",
            self.members
                .iter()
                .map(|member| member.diagnostic_string())
                .collect::<Vec<String>>(),
        ));
    }

    fn members(&self) -> Vec<PlatformMenuItemRef> {
        self.members.clone()
    }

    fn to_menu_entries(
        self: Rc<Self>,
        delegate: &dyn PlatformMenuDelegate,
        get_id: &mut MenuItemSerializableIdGenerator<'_>,
    ) -> Vec<MenuEntry> {
        debug_assert!(
            !self.members.is_empty(),
            "There must be at least one member in a PlatformMenuItemGroup"
        );
        PlatformMenuItemGroup::serialize(&*self, delegate, get_id)
    }
}

/// A class for [`PlatformMenuItem`]s that do not have handlers, but are provided by the
/// platform.
///
/// These are typically standard menu items like "Quit" or "Hide", or Edit's commands, which the
/// host sends to the focused view.
///
/// Check [`PlatformProvidedMenuItem::has_menu`] to see if the platform provides the given type.
pub struct PlatformProvidedMenuItem {
    /// The type of default menu this is.
    pub type_: PlatformProvidedMenuItemType,
    /// True if this [`PlatformProvidedMenuItem`] should be enabled or not.
    pub enabled: bool,
}

impl PlatformProvidedMenuItem {
    /// Creates a [`PlatformProvidedMenuItem`] of `type_`, enabled.
    pub fn new(type_: PlatformProvidedMenuItemType) -> PlatformProvidedMenuItem {
        PlatformProvidedMenuItem {
            type_,
            enabled: true,
        }
    }

    pub fn enabled(mut self, enabled: bool) -> PlatformProvidedMenuItem {
        self.enabled = enabled;
        self
    }

    /// Checks to see if the given default menu type is supported on this platform.
    pub fn has_menu(app: &App, menu: PlatformProvidedMenuItemType) -> bool {
        match app.platform().target_platform() {
            TargetPlatform::Android
            | TargetPlatform::IOS
            | TargetPlatform::Fuchsia
            | TargetPlatform::Linux
            | TargetPlatform::Windows => false,
            TargetPlatform::MacOS => menu.provided_item().is_some(),
        }
    }
}

impl Debug for PlatformProvidedMenuItem {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PlatformProvidedMenuItem")
            .field("type", &self.type_)
            .field("enabled", &self.enabled)
            .finish()
    }
}

impl PlatformMenuItem for PlatformProvidedMenuItem {
    fn label(&self) -> &str {
        // The label is ignored for platform provided menus.
        ""
    }

    fn debug_fill_properties(&self, properties: &mut DiagnosticPropertiesBuilder) {
        fill_platform_menu_item_properties(self, properties);
        properties.add(FlagProperty::new("enabled", Some(self.enabled)).if_false("DISABLED"));
    }

    fn to_menu_entries(
        self: Rc<Self>,
        _delegate: &dyn PlatformMenuDelegate,
        get_id: &mut MenuItemSerializableIdGenerator<'_>,
    ) -> Vec<MenuEntry> {
        let Some(item) = self.type_.provided_item() else {
            debug_assert!(
                false,
                "No platform provides a menu for {:?}. Call PlatformProvidedMenuItem::has_menu \
                 to determine this before instantiating one.",
                self.type_
            );
            return Vec::new();
        };
        let enabled = self.enabled;
        vec![MenuEntry::Provided {
            id: get_id(self),
            item,
            enabled,
        }]
    }
}

/// The list of possible platform provided, prebuilt menus for use in a [`PlatformMenuBar`].
///
/// These are menus that the platform typically provides that cannot be reproduced in the
/// framework without platform access, or the platform's own commands for the focused view.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PlatformProvidedMenuItemType {
    /// The system provided "About" menu item.
    About,
    /// The system provided "Quit" menu item.
    Quit,
    /// The system provided "Services" submenu.
    ServicesSubmenu,
    /// The system provided "Hide" menu item.
    Hide,
    /// The system provided "Hide Others" menu item.
    HideOtherApplications,
    /// The system provided "Show All" menu item.
    ShowAllApplications,
    /// The system provided "Start Dictation..." menu item.
    StartSpeaking,
    /// The system provided "Stop Dictation..." menu item.
    StopSpeaking,
    /// The system provided "Enter Full Screen" menu item.
    ToggleFullScreen,
    /// The system provided "Minimize" menu item.
    MinimizeWindow,
    /// The system provided "Zoom" menu item.
    ZoomWindow,
    /// The system provided "Bring To Front" menu item.
    ArrangeWindowsInFront,
    /// The system provided "Undo" menu item, sent to the focused view.
    Undo,
    /// The system provided "Redo" menu item, sent to the focused view.
    Redo,
    /// The system provided "Cut" menu item, sent to the focused view.
    Cut,
    /// The system provided "Copy" menu item, sent to the focused view.
    Copy,
    /// The system provided "Paste" menu item, sent to the focused view.
    Paste,
    /// The system provided "Select All" menu item, sent to the focused view.
    SelectAll,
    /// The system provided "Delete" menu item, sent to the focused view.
    Delete,
}

impl PlatformProvidedMenuItemType {
    /// The item the host provides for this type; none for one no host provides.
    fn provided_item(self) -> Option<ProvidedItem> {
        Some(match self {
            PlatformProvidedMenuItemType::About => ProvidedItem::About,
            PlatformProvidedMenuItemType::Quit => ProvidedItem::Quit,
            PlatformProvidedMenuItemType::ServicesSubmenu => ProvidedItem::Services,
            PlatformProvidedMenuItemType::Hide => ProvidedItem::Hide,
            PlatformProvidedMenuItemType::HideOtherApplications => ProvidedItem::HideOthers,
            PlatformProvidedMenuItemType::ShowAllApplications => ProvidedItem::ShowAll,
            PlatformProvidedMenuItemType::StartSpeaking
            | PlatformProvidedMenuItemType::StopSpeaking => return None,
            PlatformProvidedMenuItemType::ToggleFullScreen => ProvidedItem::ToggleFullScreen,
            PlatformProvidedMenuItemType::MinimizeWindow => ProvidedItem::Minimize,
            PlatformProvidedMenuItemType::ZoomWindow => ProvidedItem::Zoom,
            PlatformProvidedMenuItemType::ArrangeWindowsInFront => ProvidedItem::BringAllToFront,
            PlatformProvidedMenuItemType::Undo => ProvidedItem::Undo,
            PlatformProvidedMenuItemType::Redo => ProvidedItem::Redo,
            PlatformProvidedMenuItemType::Cut => ProvidedItem::Cut,
            PlatformProvidedMenuItemType::Copy => ProvidedItem::Copy,
            PlatformProvidedMenuItemType::Paste => ProvidedItem::Paste,
            PlatformProvidedMenuItemType::SelectAll => ProvidedItem::SelectAll,
            PlatformProvidedMenuItemType::Delete => ProvidedItem::Delete,
        })
    }
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;

    use waft_services::LogicalKeyboardKey;
    use waft_test::{MockMenuBar, TestPlatform};

    use super::*;
    use crate::testing::WidgetTester;

    /// A tester on a macOS host whose menu bar is `menu_bar`.
    fn tester_with_menu_bar(menu_bar: &MockMenuBar) -> WidgetTester {
        WidgetTester::with_platform(
            TestPlatform::new()
                .on(TargetPlatform::MacOS)
                .with_menu_bar(Rc::new(menu_bar.clone())),
        )
    }

    /// A File menu with Open, which records its choice into `chosen`, a group holding Save,
    /// disabled, and the system's Quit.
    fn menus(chosen: &Rc<RefCell<Vec<&'static str>>>) -> Vec<PlatformMenuItemRef> {
        let open = Rc::clone(chosen);
        vec![Rc::new(PlatformMenu::new(
            "File",
            vec![
                Rc::new(
                    PlainPlatformMenuItem::new("Open")
                        .shortcut(Rc::new(
                            SingleActivator::new(LogicalKeyboardKey::KEY_O).meta(true),
                        ))
                        .on_selected(Rc::new(move |_app| open.borrow_mut().push("Open"))),
                ),
                Rc::new(PlatformMenuItemGroup::new(vec![Rc::new(
                    PlainPlatformMenuItem::new("Save"),
                )])),
                Rc::new(PlatformProvidedMenuItem::new(
                    PlatformProvidedMenuItemType::Quit,
                )),
            ],
        ))]
    }

    #[test]
    fn the_menu_bar_reaches_the_host_and_a_choice_reaches_its_item() {
        let menu_bar = MockMenuBar::new();
        let tester = &mut tester_with_menu_bar(&menu_bar);
        let chosen = Rc::new(RefCell::new(Vec::new()));
        tester.pump_widget(PlatformMenuBar::new(menus(&chosen)));

        let set = menu_bar.menus();
        let [
            MenuEntry::Submenu {
                label,
                enabled,
                entries,
                ..
            },
        ] = &set[..]
        else {
            panic!("one top-level menu: {set:?}");
        };
        assert_eq!(label, "File");
        assert!(enabled);
        let [
            MenuEntry::Item { item: open, .. },
            MenuEntry::Separator,
            MenuEntry::Item { item: save, .. },
            MenuEntry::Separator,
            MenuEntry::Provided { item, .. },
        ] = &entries[..]
        else {
            panic!("Open, the group between separators, and Quit: {entries:?}");
        };
        assert!(open.enabled);
        assert_eq!(
            open.shortcut,
            Some(MenuShortcut {
                key: LogicalKeyboardKey::KEY_O.key_id,
                modifiers: ShortcutModifiers {
                    meta: true,
                    ..ShortcutModifiers::default()
                },
            })
        );
        assert!(!save.enabled, "an item with no callback is disabled");
        assert_eq!(*item, ProvidedItem::Quit);

        let open_id = menu_bar.id_of(&["File", "Open"]).expect("Open has an id");
        choose(tester, open_id);
        assert_eq!(*chosen.borrow(), ["Open"]);
    }

    /// Reports the choice of `id` as the host does, through the platform callback the delegate
    /// set.
    fn choose(tester: &WidgetTester, id: MenuId) {
        tester.update(|app| {
            let callback = app
                .platform_callbacks()
                .on_menu_selected
                .clone()
                .expect("the default delegate hears choices");
            callback(app, id);
        });
    }

    #[test]
    fn leading_trailing_and_doubled_dividers_are_stripped() {
        let menu_bar = MockMenuBar::new();
        let tester = &mut tester_with_menu_bar(&menu_bar);
        let item = || -> PlatformMenuItemRef { Rc::new(PlainPlatformMenuItem::new("Item")) };
        let group =
            |members| -> PlatformMenuItemRef { Rc::new(PlatformMenuItemGroup::new(members)) };
        tester.pump_widget(PlatformMenuBar::new(vec![Rc::new(PlatformMenu::new(
            "Menu",
            vec![
                group(vec![item()]),
                group(vec![item()]),
                group(vec![item()]),
            ],
        ))]));
        let [MenuEntry::Submenu { entries, .. }] = &menu_bar.menus()[..] else {
            panic!("one menu");
        };
        let shape: Vec<bool> = entries
            .iter()
            .map(|entry| matches!(entry, MenuEntry::Separator))
            .collect();
        assert_eq!(shape, [false, true, false, true, false]);
    }

    #[test]
    fn a_platform_without_a_menu_bar_shows_the_menus_nowhere() {
        let tester = &mut WidgetTester::on(TargetPlatform::Windows);
        let chosen = Rc::new(RefCell::new(Vec::new()));
        tester.pump_widget(PlatformMenuBar::new(menus(&chosen)));
        assert!(
            !tester.update(|app| PlatformProvidedMenuItem::has_menu(
                app,
                PlatformProvidedMenuItemType::Quit
            )),
            "Windows provides no Quit item"
        );
    }
}