muda 0.20.0

Menu Utilities for Desktop Applications
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
// Copyright 2022-2022 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::{cell::RefCell, mem, rc::Rc};

use crate::{
    accelerator::{Accelerator, Code, MenuAccelerator, Modifiers, CMD_OR_CTRL},
    platform_impl::PlatformMenuItem,
    util, AboutMetadata, IsMenuItem, MenuId, MenuItemAction, MenuItemKind, StateCell,
};

/// A predefined (native) menu item which has a predefined behavior by the OS or by this crate.
#[derive(Clone)]
pub struct PredefinedMenuItem {
    pub(crate) id: Rc<MenuId>,
    pub(crate) state: StateCell<PredefinedMenuItemState>,
    pub(crate) platform: Rc<RefCell<PlatformMenuItem>>,
}

/// Shared state of a [`PredefinedMenuItem`].
#[derive(Debug, Clone)]
pub(crate) struct PredefinedMenuItemState {
    pub text: String,
    pub predefined_item_type: PredefinedMenuItemType,
    pub enabled: bool,
}

impl crate::sealed::Sealed for PredefinedMenuItem {}
impl IsMenuItem for PredefinedMenuItem {
    fn kind(&self) -> MenuItemKind {
        MenuItemKind::Predefined(self.clone())
    }

    fn id(&self) -> &MenuId {
        self.id()
    }

    fn into_id(self) -> MenuId {
        self.into_id()
    }
}

impl PredefinedMenuItem {
    /// Separator menu item
    pub fn separator() -> PredefinedMenuItem {
        PredefinedMenuItem::new::<&str>(PredefinedMenuItemType::Separator, None)
    }

    /// Non-interactive section header menu item.
    ///
    /// ## Platform-specific:
    ///
    /// - **macOS:** Uses the native section-header appearance on macOS 14 and later. The item is
    ///   rendered as a disabled menu item on older versions.
    /// - **Windows / GTK 3 / GTK 4:** Rendered as a disabled menu item.
    pub fn section_header(text: &str) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::SectionHeader, Some(text))
    }

    /// Copy menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **GTK 3:** Requires the `libxdo` feature.
    /// - **GTK 4:** Unsupported.
    pub fn copy(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::Copy, text)
    }

    /// Cut menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **GTK 3:** Requires the `libxdo` feature.
    /// - **GTK 4:** Unsupported.
    pub fn cut(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::Cut, text)
    }

    /// Paste menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **GTK 3:** Requires the `libxdo` feature.
    /// - **GTK 4:** Unsupported.
    pub fn paste(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::Paste, text)
    }

    /// Paste and Match Style menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **Windows / GTK 3 / GTK 4:** Unsupported.
    pub fn paste_and_match_style(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::PasteAndMatchStyle, text)
    }

    /// Delete menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **Windows / GTK 3 / GTK 4:** Unsupported.
    pub fn delete(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::Delete, text)
    }

    /// SelectAll menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **GTK 3:** Requires the `libxdo` feature.
    /// - **GTK 4:** Unsupported.
    pub fn select_all(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::SelectAll, text)
    }

    /// Undo menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **GTK 3 / GTK 4:** Unsupported.
    pub fn undo(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::Undo, text)
    }
    /// Redo menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **GTK 3 / GTK 4:** Unsupported.
    pub fn redo(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::Redo, text)
    }

    /// Minimize window menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **GTK 3:** Unsupported.
    pub fn minimize(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::Minimize, text)
    }

    /// Maximize window menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **GTK 3:** Unsupported.
    pub fn maximize(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::Maximize, text)
    }

    /// Zoom window menu item
    ///
    /// This is an alias for [`Self::maximize`]. On macOS, the native maximize
    /// window action is conventionally named "Zoom".
    pub fn zoom(text: Option<&str>) -> PredefinedMenuItem {
        Self::maximize(text)
    }

    /// Actual Size menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **Windows / GTK 3 / GTK 4:** Unsupported.
    pub fn actual_size(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::ActualSize, text)
    }

    /// Zoom In menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **Windows / GTK 3 / GTK 4:** Unsupported.
    pub fn zoom_in(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::ZoomIn, text)
    }

    /// Zoom Out menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **Windows / GTK 3 / GTK 4:** Unsupported.
    pub fn zoom_out(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::ZoomOut, text)
    }

    /// Fullscreen menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **Windows / GTK 3:** Unsupported.
    pub fn fullscreen(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::Fullscreen, text)
    }

    /// Hide window menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **GTK 3:** Unsupported.
    pub fn hide(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::Hide, text)
    }

    /// Hide other windows menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **Windows / GTK 3 / GTK 4:** Unsupported.
    pub fn hide_others(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::HideOthers, text)
    }

    /// Show all app windows menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **Windows / GTK 3 / GTK 4:** Unsupported.
    pub fn show_all(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::ShowAll, text)
    }

    /// Close window menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **GTK 3:** Unsupported.
    pub fn close_window(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::CloseWindow, text)
    }

    /// Quit app menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **GTK 3:** Unsupported.
    pub fn quit(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::Quit, text)
    }

    /// About app menu item
    pub fn about(text: Option<&str>, metadata: Option<AboutMetadata>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::About(metadata), text)
    }

    /// Services menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **Windows / GTK 3 / GTK 4:** Unsupported.
    pub fn services(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::Services, text)
    }

    /// 'Bring all to front' menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **Windows / GTK 3 / GTK 4:** Unsupported.
    pub fn bring_all_to_front(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::BringAllToFront, text)
    }

    /// Start Speaking menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **Windows / GTK 3 / GTK 4:** Unsupported.
    pub fn start_speaking(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::StartSpeaking, text)
    }

    /// Stop Speaking menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **Windows / GTK 3 / GTK 4:** Unsupported.
    pub fn stop_speaking(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::StopSpeaking, text)
    }

    /// Start Dictation menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **Windows / GTK 3 / GTK 4:** Unsupported.
    pub fn start_dictation(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::StartDictation, text)
    }

    /// Emoji & Symbols menu item
    ///
    /// ## Platform-specific:
    ///
    /// - **Windows / GTK 3 / GTK 4:** Unsupported.
    pub fn emoji_and_symbols(text: Option<&str>) -> PredefinedMenuItem {
        PredefinedMenuItem::new(PredefinedMenuItemType::EmojiAndSymbols, text)
    }

    fn new<S: AsRef<str>>(item: PredefinedMenuItemType, text: Option<S>) -> Self {
        let id = util::next_id(None);

        let resolved_text = text
            .as_ref()
            .map(|text| text.as_ref().to_string())
            .unwrap_or_else(|| item.default_text(app_name().as_deref()));
        let enabled = item.is_enabled();
        let state = StateCell::new(PredefinedMenuItemState {
            text: resolved_text,
            predefined_item_type: item,
            enabled,
        });

        // A predefined item emits no event; what it does instead is decided from its kind at
        // click time, which is why the action needs a handle to state rather than the id.
        let click = MenuItemAction::Predefined(state.downgrade());
        let platform = PlatformMenuItem::new(click);

        Self {
            id: Rc::new(id),
            state,
            platform: Rc::new(RefCell::new(platform)),
        }
    }

    /// Returns a unique identifier associated with this predefined menu item.
    pub fn id(&self) -> &MenuId {
        &self.id
    }

    /// Get the text for this predefined menu item.
    pub fn text(&self) -> String {
        let text = self.platform.borrow().text();
        text.unwrap_or_else(|| self.state.borrow().text.clone())
    }

    /// Set the text for this predefined menu item.
    pub fn set_text<S: AsRef<str>>(&self, text: S) {
        let accelerator = {
            let mut state = self.state.borrow_mut();
            state.text = text.as_ref().to_string();
            state.predefined_item_type.accelerator()
        };

        self.platform
            .borrow_mut()
            .set_text(text.as_ref(), accelerator.as_ref())
    }

    /// Convert this menu item into its menu ID.
    pub fn into_id(mut self) -> MenuId {
        // Note: `Rc::into_inner` is available from Rust 1.70
        if let Some(id) = Rc::get_mut(&mut self.id) {
            mem::take(id)
        } else {
            self.id().clone()
        }
    }
}

/// The running application's name, for the macOS items that splice it into their label.
///
/// The one construction-time platform call left in the crate, and the reason
/// [`PredefinedMenuItemState::new`] takes the name as an argument instead of fetching it: the
/// other three platforms' labels never mention it, so everywhere else this is a constant.
fn app_name() -> Option<String> {
    #[cfg(target_os = "macos")]
    {
        crate::platform_impl::app_name()
    }
    #[cfg(not(target_os = "macos"))]
    {
        None
    }
}

#[derive(Debug, Clone)]
#[non_exhaustive]
#[allow(clippy::large_enum_variant)]
pub(crate) enum PredefinedMenuItemType {
    Separator,
    SectionHeader,
    Copy,
    Cut,
    Paste,
    PasteAndMatchStyle,
    Delete,
    SelectAll,
    Undo,
    Redo,
    Minimize,
    Maximize,
    ActualSize,
    ZoomIn,
    ZoomOut,
    Fullscreen,
    Hide,
    HideOthers,
    ShowAll,
    CloseWindow,
    Quit,
    About(Option<AboutMetadata>),
    Services,
    BringAllToFront,
    StartSpeaking,
    StopSpeaking,
    StartDictation,
    EmojiAndSymbols,
}

impl PredefinedMenuItemType {
    pub(crate) fn text(&self) -> &str {
        match self {
            PredefinedMenuItemType::Separator => "",
            PredefinedMenuItemType::SectionHeader => "",
            PredefinedMenuItemType::Copy => "&Copy",
            PredefinedMenuItemType::Cut => "Cu&t",
            PredefinedMenuItemType::Paste => "&Paste",
            PredefinedMenuItemType::PasteAndMatchStyle => "Paste and Match Style",
            PredefinedMenuItemType::Delete => "&Delete",
            PredefinedMenuItemType::SelectAll => "Select &All",
            PredefinedMenuItemType::Undo => "Undo",
            PredefinedMenuItemType::Redo => "Redo",
            PredefinedMenuItemType::Minimize => "&Minimize",
            #[cfg(target_os = "macos")]
            PredefinedMenuItemType::Maximize => "Zoom",
            #[cfg(not(target_os = "macos"))]
            PredefinedMenuItemType::Maximize => "Ma&ximize",
            PredefinedMenuItemType::ActualSize => "Actual Size",
            PredefinedMenuItemType::ZoomIn => "Zoom In",
            PredefinedMenuItemType::ZoomOut => "Zoom Out",
            PredefinedMenuItemType::Fullscreen => "Toggle Full Screen",
            PredefinedMenuItemType::Hide => "&Hide",
            PredefinedMenuItemType::HideOthers => "Hide Others",
            PredefinedMenuItemType::ShowAll => "Show All",
            #[cfg(windows)]
            PredefinedMenuItemType::CloseWindow => "Close",
            #[cfg(not(windows))]
            PredefinedMenuItemType::CloseWindow => "C&lose Window",
            #[cfg(windows)]
            PredefinedMenuItemType::Quit => "&Exit",
            #[cfg(not(windows))]
            PredefinedMenuItemType::Quit => "&Quit",
            PredefinedMenuItemType::About(_) => "&About",
            PredefinedMenuItemType::Services => "Services",
            PredefinedMenuItemType::BringAllToFront => "Bring All to Front",
            PredefinedMenuItemType::StartSpeaking => "Start Speaking",
            PredefinedMenuItemType::StopSpeaking => "Stop Speaking",
            PredefinedMenuItemType::StartDictation => "Start Dictation…",
            PredefinedMenuItemType::EmojiAndSymbols => "Emoji & Symbols",
        }
    }

    /// The label this kind carries when the caller supplies none.
    ///
    /// Mnemonic-encoded, like every other label in shared state: `&` marks the mnemonic and
    /// `&&` is a literal ampersand. Backends that have no mnemonics strip on the way out.
    ///
    /// `app_name` is consulted on macOS alone, where three of the kinds name the running
    /// application. It is passed in rather than read here because fetching it is a native
    /// call, and this table has to stay answerable without one.
    pub(crate) fn default_text(&self, app_name: Option<&str>) -> String {
        #[cfg(target_os = "macos")]
        {
            // An empty (or absent) name degrades to the bare verb, matching what
            // `format!("About {}", "").trim()` used to produce.
            let named = |verb: &str| match app_name {
                Some(name) if !name.trim().is_empty() => {
                    format!("{verb} {}", escape_mnemonic(name.trim()))
                }
                _ => verb.to_string(),
            };

            match self {
                PredefinedMenuItemType::About(_) => return named("About"),
                PredefinedMenuItemType::Hide => return named("Hide"),
                PredefinedMenuItemType::Quit => return named("Quit"),
                _ => {}
            }
        }
        #[cfg(not(target_os = "macos"))]
        let _ = app_name;

        self.text().to_string()
    }

    /// Whether this kind does anything on the platform being compiled for.
    ///
    /// An unsupported kind is not rejected at construction — it is created and left
    /// disabled, which is why this feeds `enabled` rather than an error.
    #[cfg(target_os = "windows")]
    pub(crate) fn is_supported(&self) -> bool {
        matches!(
            self,
            PredefinedMenuItemType::Separator
                | PredefinedMenuItemType::Copy
                | PredefinedMenuItemType::Cut
                | PredefinedMenuItemType::Paste
                | PredefinedMenuItemType::SelectAll
                | PredefinedMenuItemType::Undo
                | PredefinedMenuItemType::Redo
                | PredefinedMenuItemType::Minimize
                | PredefinedMenuItemType::Maximize
                | PredefinedMenuItemType::Hide
                | PredefinedMenuItemType::CloseWindow
                | PredefinedMenuItemType::Quit
                | PredefinedMenuItemType::About(_)
        )
    }

    #[cfg(all(
        any(
            target_os = "linux",
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd"
        ),
        not(any(feature = "gtk3", feature = "gtk4"))
    ))]
    pub(crate) fn is_supported(&self) -> bool {
        matches!(self, PredefinedMenuItemType::Separator)
    }

    #[cfg(all(
        any(
            target_os = "linux",
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd"
        ),
        feature = "gtk3",
        not(feature = "gtk4")
    ))]
    pub(crate) fn is_supported(&self) -> bool {
        matches!(
            self,
            PredefinedMenuItemType::Separator
                | PredefinedMenuItemType::Copy
                | PredefinedMenuItemType::Cut
                | PredefinedMenuItemType::Paste
                | PredefinedMenuItemType::SelectAll
                | PredefinedMenuItemType::About(_)
        )
    }

    #[cfg(all(
        any(
            target_os = "linux",
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd"
        ),
        feature = "gtk4"
    ))]
    pub(crate) fn is_supported(&self) -> bool {
        matches!(
            self,
            PredefinedMenuItemType::Separator
                | PredefinedMenuItemType::Minimize
                | PredefinedMenuItemType::Maximize
                | PredefinedMenuItemType::Fullscreen
                | PredefinedMenuItemType::Hide
                | PredefinedMenuItemType::CloseWindow
                | PredefinedMenuItemType::Quit
                | PredefinedMenuItemType::About(_)
        )
    }

    #[cfg(target_os = "macos")]
    pub(crate) fn is_supported(&self) -> bool {
        matches!(
            self,
            PredefinedMenuItemType::Separator
                | PredefinedMenuItemType::SectionHeader
                | PredefinedMenuItemType::Copy
                | PredefinedMenuItemType::Cut
                | PredefinedMenuItemType::Paste
                | PredefinedMenuItemType::PasteAndMatchStyle
                | PredefinedMenuItemType::Delete
                | PredefinedMenuItemType::SelectAll
                | PredefinedMenuItemType::Undo
                | PredefinedMenuItemType::Redo
                | PredefinedMenuItemType::Minimize
                | PredefinedMenuItemType::Maximize
                | PredefinedMenuItemType::ActualSize
                | PredefinedMenuItemType::ZoomIn
                | PredefinedMenuItemType::ZoomOut
                | PredefinedMenuItemType::Fullscreen
                | PredefinedMenuItemType::Hide
                | PredefinedMenuItemType::HideOthers
                | PredefinedMenuItemType::ShowAll
                | PredefinedMenuItemType::CloseWindow
                | PredefinedMenuItemType::Quit
                | PredefinedMenuItemType::About(_)
                | PredefinedMenuItemType::Services
                | PredefinedMenuItemType::BringAllToFront
                | PredefinedMenuItemType::StartSpeaking
                | PredefinedMenuItemType::StopSpeaking
                | PredefinedMenuItemType::StartDictation
                | PredefinedMenuItemType::EmojiAndSymbols
        )
    }

    /// Whether this kind should be enabled on the current platform.
    ///
    /// Section headers are supported on macOS 14+, but remain disabled because they are labels
    /// rather than actionable menu items.
    fn is_enabled(&self) -> bool {
        self.is_supported() && !matches!(self, PredefinedMenuItemType::SectionHeader)
    }

    pub(crate) fn accelerator(&self) -> Option<MenuAccelerator> {
        match self {
            PredefinedMenuItemType::Copy => Some(physical_accelerator(CMD_OR_CTRL, Code::KeyC)),
            PredefinedMenuItemType::Cut => Some(physical_accelerator(CMD_OR_CTRL, Code::KeyX)),
            PredefinedMenuItemType::Paste => Some(physical_accelerator(CMD_OR_CTRL, Code::KeyV)),
            #[cfg(target_os = "macos")]
            PredefinedMenuItemType::PasteAndMatchStyle => Some(physical_accelerator(
                CMD_OR_CTRL | Modifiers::ALT | Modifiers::SHIFT,
                Code::KeyV,
            )),
            PredefinedMenuItemType::Undo => Some(physical_accelerator(CMD_OR_CTRL, Code::KeyZ)),
            #[cfg(target_os = "macos")]
            PredefinedMenuItemType::Redo => Some(physical_accelerator(
                CMD_OR_CTRL | Modifiers::SHIFT,
                Code::KeyZ,
            )),
            #[cfg(not(target_os = "macos"))]
            PredefinedMenuItemType::Redo => Some(physical_accelerator(CMD_OR_CTRL, Code::KeyY)),
            PredefinedMenuItemType::SelectAll => {
                Some(physical_accelerator(CMD_OR_CTRL, Code::KeyA))
            }
            PredefinedMenuItemType::Minimize => Some(physical_accelerator(CMD_OR_CTRL, Code::KeyM)),
            #[cfg(target_os = "macos")]
            PredefinedMenuItemType::ActualSize => {
                Some(physical_accelerator(CMD_OR_CTRL, Code::Digit0))
            }
            #[cfg(target_os = "macos")]
            PredefinedMenuItemType::ZoomIn => Some(physical_accelerator(
                CMD_OR_CTRL | Modifiers::SHIFT,
                Code::Equal,
            )),
            #[cfg(target_os = "macos")]
            PredefinedMenuItemType::ZoomOut => Some(physical_accelerator(CMD_OR_CTRL, Code::Minus)),
            #[cfg(target_os = "macos")]
            PredefinedMenuItemType::Fullscreen => Some(physical_accelerator(
                Modifiers::META | Modifiers::CONTROL,
                Code::KeyF,
            )),
            PredefinedMenuItemType::Hide => Some(physical_accelerator(CMD_OR_CTRL, Code::KeyH)),
            PredefinedMenuItemType::HideOthers => Some(physical_accelerator(
                CMD_OR_CTRL | Modifiers::ALT,
                Code::KeyH,
            )),
            #[cfg(target_os = "macos")]
            PredefinedMenuItemType::CloseWindow => {
                Some(physical_accelerator(CMD_OR_CTRL, Code::KeyW))
            }
            #[cfg(not(target_os = "macos"))]
            PredefinedMenuItemType::CloseWindow => {
                Some(physical_accelerator(Modifiers::ALT, Code::F4))
            }
            #[cfg(target_os = "macos")]
            PredefinedMenuItemType::Quit => Some(physical_accelerator(CMD_OR_CTRL, Code::KeyQ)),
            #[cfg(target_os = "macos")]
            PredefinedMenuItemType::EmojiAndSymbols => Some(physical_accelerator(
                Modifiers::META | Modifiers::CONTROL,
                Code::Space,
            )),
            _ => None,
        }
    }
}

fn physical_accelerator(modifiers: Modifiers, key: Code) -> MenuAccelerator {
    MenuAccelerator::Physical(Accelerator::new(modifiers, key))
}

/// Make `text` survive mnemonic decoding unchanged.
///
/// Application names are not labels the caller wrote, so an ampersand in one is a literal
/// ampersand — `Foo & Bar` has to reach the user as `Foo & Bar`, not as `Foo  Bar` with the
/// space swallowed as a mnemonic marker.
#[cfg(target_os = "macos")]
fn escape_mnemonic(text: &str) -> String {
    text.replace('&', "&&")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_about_metadata() {
        assert_eq!(
            AboutMetadata {
                ..Default::default()
            }
            .full_version(),
            None
        );

        assert_eq!(
            AboutMetadata {
                version: Some("Version: 1.inner".into()),
                ..Default::default()
            }
            .full_version(),
            Some("Version: 1.inner".into())
        );

        assert_eq!(
            AboutMetadata {
                version: Some("Version: 1.inner".into()),
                short_version: Some("Universal".into()),
                ..Default::default()
            }
            .full_version(),
            Some("Version: 1.inner (Universal)".into())
        );
    }
}