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

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

use crate::{
    platform_impl::PlatformMenu,
    util::{self, AddOp},
    ContextMenu, IsMenuItem, MenuId, MenuItemKind, StateCell, UnsafeMenuItemKind,
};

#[cfg(feature = "snapshot")]
use crate::MenuSnapshotHandle;

#[cfg(any(
    target_os = "windows",
    target_os = "macos",
    all(
        any(
            target_os = "linux",
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd"
        ),
        any(all(feature = "gtk3", not(feature = "gtk4")), feature = "gtk4")
    )
))]
use crate::dpi::Position;

/// A root menu that can be added to a window on Windows, GTK 3, or GTK 4
/// and used as the app global menu on macOS.
#[derive(Clone)]
pub struct Menu {
    id: Rc<MenuId>,
    state: StateCell<MenuState>,
    platform: Rc<RefCell<PlatformMenu>>,
}

/// Shared state of a root [`Menu`].
pub(crate) struct MenuState {
    pub children: Vec<UnsafeMenuItemKind>,
}

impl Drop for Menu {
    fn drop(&mut self) {
        if Rc::strong_count(&self.id) == 1 {
            let children: Vec<MenuItemKind> = std::mem::take(&mut self.state.borrow_mut().children)
                .into_iter()
                .map(|child| {
                    // SAFETY: the last thread-bound `Menu` is being dropped on the thread where
                    // its children were wrapped, so they can be recovered and destroyed here.
                    unsafe { child.unwrap() }
                })
                .collect();

            #[cfg(any(
                target_os = "macos",
                all(
                    any(
                        target_os = "linux",
                        target_os = "dragonfly",
                        target_os = "freebsd",
                        target_os = "netbsd",
                        target_os = "openbsd"
                    ),
                    any(all(feature = "gtk3", not(feature = "gtk4")), feature = "gtk4")
                )
            ))]
            self.platform.borrow_mut().destroy(&children);

            drop(children);
        }
    }
}

impl Default for Menu {
    fn default() -> Self {
        Self::new()
    }
}

impl Menu {
    /// Creates a new menu.
    pub fn new() -> Self {
        Self::new_inner(None)
    }

    /// Creates a new menu with the specified id.
    pub fn with_id<I: Into<MenuId>>(id: I) -> Self {
        Self::new_inner(Some(id.into()))
    }

    fn new_inner(id: Option<MenuId>) -> Self {
        Self {
            id: Rc::new(util::next_id(id)),
            state: StateCell::new(MenuState {
                children: Vec::new(),
            }),
            platform: Rc::new(RefCell::new(PlatformMenu::new())),
        }
    }

    /// Creates a new menu with given `items`. It calls [`Menu::new`] and [`Menu::append_items`] internally.
    pub fn with_items(items: &[&dyn IsMenuItem]) -> crate::Result<Self> {
        let menu = Self::new();
        menu.append_items(items)?;
        Ok(menu)
    }

    /// Creates a new menu with the specified id and given `items`. It calls [`Menu::new`] and [`Menu::append_items`] internally.
    pub fn with_id_and_items<I: Into<MenuId>>(
        id: I,
        items: &[&dyn IsMenuItem],
    ) -> crate::Result<Self> {
        let menu = Self::with_id(id);
        menu.append_items(items)?;
        Ok(menu)
    }

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

    /// Add a menu item to the end of this menu.
    ///
    /// ## Platform-specific:
    ///
    /// - **macOS:** Only [`Submenu`] can be added to the menu
    ///
    /// [`Submenu`]: crate::Submenu
    pub fn append(&self, item: &dyn IsMenuItem) -> crate::Result<()> {
        self.add_menu_item(item, AddOp::Append)
    }

    /// Add menu items to the end of this menu. It calls [`Menu::append`] in a loop internally.
    ///
    /// ## Platform-specific:
    ///
    /// - **macOS:** Only [`Submenu`] can be added to the menu
    ///
    /// [`Submenu`]: crate::Submenu
    pub fn append_items(&self, items: &[&dyn IsMenuItem]) -> crate::Result<()> {
        for item in items {
            self.append(*item)?
        }

        Ok(())
    }

    /// Add a menu item to the beginning of this menu.
    ///
    /// ## Platform-specific:
    ///
    /// - **macOS:** Only [`Submenu`] can be added to the menu
    ///
    /// [`Submenu`]: crate::Submenu
    pub fn prepend(&self, item: &dyn IsMenuItem) -> crate::Result<()> {
        self.add_menu_item(item, AddOp::Insert(0))
    }

    /// Add menu items to the beginning of this menu. It calls [`Menu::insert_items`] with position of `0` internally.
    ///
    /// ## Platform-specific:
    ///
    /// - **macOS:** Only [`Submenu`] can be added to the menu
    ///
    /// [`Submenu`]: crate::Submenu
    pub fn prepend_items(&self, items: &[&dyn IsMenuItem]) -> crate::Result<()> {
        self.insert_items(items, 0)
    }

    /// Insert a menu item at the specified `position` in the menu.
    ///
    /// ## Platform-specific:
    ///
    /// - **macOS:** Only [`Submenu`] can be added to the menu
    ///
    /// [`Submenu`]: crate::Submenu
    pub fn insert(&self, item: &dyn IsMenuItem, position: usize) -> crate::Result<()> {
        self.add_menu_item(item, AddOp::Insert(position))
    }

    /// Insert menu items at the specified `position` in the menu.
    ///
    /// ## Platform-specific:
    ///
    /// - **macOS:** Only [`Submenu`] can be added to the menu
    ///
    /// [`Submenu`]: crate::Submenu
    pub fn insert_items(&self, items: &[&dyn IsMenuItem], position: usize) -> crate::Result<()> {
        for (i, item) in items.iter().enumerate() {
            self.insert(*item, position + i)?
        }

        Ok(())
    }

    fn add_menu_item(&self, item: &dyn IsMenuItem, op: AddOp) -> crate::Result<()> {
        let kind = item.kind();

        {
            let mut platform = self.platform.borrow_mut();
            platform.attach(&kind, op)?;
        }

        let kind = UnsafeMenuItemKind::new(kind);
        let mut state = self.state.borrow_mut();
        match op {
            AddOp::Append => state.children.push(kind),
            AddOp::Insert(position) => state.children.insert(position, kind),
        }

        Ok(())
    }

    /// Remove a menu item from this menu.
    /// Remove all occurrences of a menu item from this menu.
    pub fn remove(&self, item: &dyn IsMenuItem) -> crate::Result<()> {
        let positions = positions_of(&self.state.borrow().children, item.id());

        if positions.is_empty() {
            return Err(crate::Error::NotAChildOfThisMenu);
        }

        // Back to front, so that each removal leaves the positions still to come untouched.
        for position in positions.into_iter().rev() {
            self.remove_at(position);
        }

        Ok(())
    }

    /// Remove the menu item at the specified position from this menu and returns it.
    pub fn remove_at(&self, position: usize) -> Option<MenuItemKind> {
        let kind = {
            let mut state = self.state.borrow_mut();
            if position >= state.children.len() {
                return None;
            }
            state.children.remove(position)
        };

        // SAFETY: this thread-bound `Menu` can mutate its children only on the thread where their
        // `MenuItemKind` values were wrapped.
        let kind = unsafe { kind.unwrap() };

        self.platform.borrow_mut().remove_at(position, &kind);

        Some(kind)
    }

    /// Returns a list of menu items that has been added to this menu.
    pub fn items(&self) -> Vec<MenuItemKind> {
        self.state
            .borrow()
            .children
            .iter()
            .map(|child| {
                // SAFETY: the thread-bound `Menu` remains on the thread where its children were
                // wrapped, and the returned clones remain on that thread.
                unsafe { child.clone() }
            })
            .collect()
    }

    /// Adds this menu to a [`gtk::Window`].
    ///
    /// With the `gtk3` feature this creates a `gtk::MenuBar`. With the `gtk4` feature this
    /// creates a `gtk::PopoverMenuBar`.
    ///
    /// - `container`: this optional parameter specifies the container that receives the menu bar.
    ///   Passing a container is highly recommended; otherwise the menu bar is added directly to the
    ///   window, which is usually not the desired behavior. Supported containers are [`gtk::Box`],
    ///   [`gtk::Fixed`], and [`gtk::Stack`].
    ///
    /// With the `gtk4` feature, the window must belong to a [`gtk::Application`].
    ///
    /// ## Example:
    /// ```no_run
    /// # #[cfg(feature = "gtk4")]
    /// # use gtk4 as gtk;
    /// let window = gtk::Window::builder().build();
    /// let vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);
    /// let menu = muda::Menu::new();
    /// // -- snip, add your menu items --
    /// menu.init_for_gtk_window(&window, Some(&vbox));
    /// // then proceed to add your widgets to the `vbox`
    /// ```
    ///
    /// ## Panics:
    ///
    /// Panics if the gtk event loop hasn't been initialized on the thread.
    #[cfg(all(
        any(
            target_os = "linux",
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd"
        ),
        any(all(feature = "gtk3", not(feature = "gtk4")), feature = "gtk4")
    ))]
    pub fn init_for_gtk_window<W, C>(&self, window: &W, container: Option<&C>) -> crate::Result<()>
    where
        W: gtk::prelude::IsA<gtk::Window>,
        W: gtk::prelude::IsA<gtk::Widget>,
        C: gtk::prelude::IsA<gtk::Widget>,
    {
        let children = self.items();
        self.platform
            .borrow_mut()
            .init_for_gtk_window(&children, window, container)
    }

    /// Adds this menu to a win32 window.
    ///
    /// # Safety
    ///
    /// The `hwnd` must be a valid window HWND.
    ///
    /// ##  Note about accelerators:
    ///
    /// For accelerators to work, the event loop needs to call
    /// [`TranslateAcceleratorW`](windows_sys::Win32::UI::WindowsAndMessaging::TranslateAcceleratorW)
    /// with the [`HACCEL`](windows_sys::Win32::UI::WindowsAndMessaging::HACCEL) returned from [`Menu::haccel`]
    ///
    /// #### Example:
    /// ```no_run
    /// # use muda::Menu;
    /// # use windows_sys::Win32::UI::WindowsAndMessaging::{MSG, GetMessageW, TranslateMessage, DispatchMessageW, TranslateAcceleratorW};
    /// let menu = Menu::new();
    /// unsafe {
    ///     let mut msg: MSG = std::mem::zeroed();
    ///     while GetMessageW(&mut msg, std::ptr::null_mut(), 0, 0) == 1 {
    ///         let translated = TranslateAcceleratorW(msg.hwnd, menu.haccel() as _, &msg as *const _);
    ///         if translated != 1{
    ///             TranslateMessage(&msg);
    ///             DispatchMessageW(&msg);
    ///         }
    ///     }
    /// }
    /// ```
    #[cfg(target_os = "windows")]
    pub unsafe fn init_for_hwnd(&self, hwnd: isize) -> crate::Result<()> {
        self.platform.borrow_mut().init_for_hwnd(hwnd)
    }

    /// Adds this menu to a win32 window using the specified theme.
    ///
    /// See [Menu::init_for_hwnd] for more info.
    ///
    /// Note that the theme only affects the menu bar itself and not submenus or context menu.
    ///
    /// # Safety
    ///
    /// The `hwnd` must be a valid window HWND.
    #[cfg(target_os = "windows")]
    pub unsafe fn init_for_hwnd_with_theme(
        &self,
        hwnd: isize,
        theme: MenuTheme,
    ) -> crate::Result<()> {
        self.platform
            .borrow_mut()
            .init_for_hwnd_with_theme(hwnd, theme)
    }

    /// Set a theme for the menu bar on this window.
    ///
    /// Note that the theme only affects the menu bar itself and not submenus or context menu.
    ///
    /// # Safety
    ///
    /// The `hwnd` must be a valid window HWND.
    #[cfg(target_os = "windows")]
    pub unsafe fn set_theme_for_hwnd(&self, hwnd: isize, theme: MenuTheme) -> crate::Result<()> {
        self.platform.borrow().set_theme_for_hwnd(hwnd, theme)
    }

    /// Returns The [`HACCEL`](windows_sys::Win32::UI::WindowsAndMessaging::HACCEL) associated with this menu
    /// It can be used with [`TranslateAcceleratorW`](windows_sys::Win32::UI::WindowsAndMessaging::TranslateAcceleratorW)
    /// in the event loop to enable accelerators
    ///
    /// The returned [`HACCEL`](windows_sys::Win32::UI::WindowsAndMessaging::HACCEL) is valid as long as the [Menu] is.
    #[cfg(target_os = "windows")]
    pub fn haccel(&self) -> isize {
        self.platform.borrow().haccel()
    }

    /// Removes this menu from a [`gtk::Window`]
    #[cfg(all(
        any(
            target_os = "linux",
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd"
        ),
        any(all(feature = "gtk3", not(feature = "gtk4")), feature = "gtk4")
    ))]
    pub fn remove_for_gtk_window<W>(&self, window: &W) -> crate::Result<()>
    where
        W: gtk::prelude::IsA<gtk::Window>,
        W: gtk::prelude::IsA<gtk::Widget>,
    {
        let children = self.items();
        self.platform
            .borrow_mut()
            .remove_for_gtk_window(&children, window)
    }

    /// Removes this menu from a win32 window
    ///
    /// # Safety
    ///
    /// The `hwnd` must be a valid window HWND.
    #[cfg(target_os = "windows")]
    pub unsafe fn remove_for_hwnd(&self, hwnd: isize) -> crate::Result<()> {
        self.platform.borrow_mut().remove_for_hwnd(hwnd)
    }

    /// Hides this menu from a [`gtk::Window`]
    #[cfg(all(
        any(
            target_os = "linux",
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd"
        ),
        any(all(feature = "gtk3", not(feature = "gtk4")), feature = "gtk4")
    ))]
    pub fn hide_for_gtk_window<W>(&self, window: &W) -> crate::Result<()>
    where
        W: gtk::prelude::IsA<gtk::Window>,
    {
        self.platform.borrow_mut().hide_for_gtk_window(window)
    }

    /// Hides this menu from a win32 window
    ///
    /// # Safety
    ///
    /// The `hwnd` must be a valid window HWND.
    #[cfg(target_os = "windows")]
    pub unsafe fn hide_for_hwnd(&self, hwnd: isize) -> crate::Result<()> {
        self.platform.borrow().hide_for_hwnd(hwnd)
    }

    /// Shows this menu on a [`gtk::Window`]
    #[cfg(all(
        any(
            target_os = "linux",
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd"
        ),
        any(all(feature = "gtk3", not(feature = "gtk4")), feature = "gtk4")
    ))]
    pub fn show_for_gtk_window<W>(&self, window: &W) -> crate::Result<()>
    where
        W: gtk::prelude::IsA<gtk::Window>,
    {
        self.platform.borrow_mut().show_for_gtk_window(window)
    }

    /// Shows this menu on a win32 window
    ///
    /// # Safety
    ///
    /// The `hwnd` must be a valid window HWND.
    #[cfg(target_os = "windows")]
    pub unsafe fn show_for_hwnd(&self, hwnd: isize) -> crate::Result<()> {
        self.platform.borrow().show_for_hwnd(hwnd)
    }

    /// Returns whether this menu visible on a [`gtk::Window`]
    #[cfg(all(
        any(
            target_os = "linux",
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd"
        ),
        any(all(feature = "gtk3", not(feature = "gtk4")), feature = "gtk4")
    ))]
    pub fn is_visible_on_gtk_window<W>(&self, window: &W) -> bool
    where
        W: gtk::prelude::IsA<gtk::Window>,
    {
        self.platform.borrow().is_visible_on_gtk_window(window)
    }

    #[cfg(all(
        any(
            target_os = "linux",
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd"
        ),
        feature = "gtk3",
        not(feature = "gtk4")
    ))]
    /// Returns the [`gtk::MenuBar`] that is associated with this window if it exists.
    /// This is useful to get information about the menubar for example its height.
    pub fn gtk_menubar_for_gtk_window<W>(self, window: &W) -> Option<gtk::MenuBar>
    where
        W: gtk::prelude::IsA<gtk::Window>,
    {
        self.platform.borrow().gtk_menubar_for_gtk_window(window)
    }

    #[cfg(all(
        any(
            target_os = "linux",
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd"
        ),
        feature = "gtk4"
    ))]
    /// Returns the [`gtk::PopoverMenuBar`] that is associated with this window if it exists.
    /// This is useful to get information about the menubar for example its height.
    pub fn gtk_menubar_for_gtk_window<W>(self, window: &W) -> Option<gtk::PopoverMenuBar>
    where
        W: gtk::prelude::IsA<gtk::Window>,
    {
        self.platform.borrow().gtk_menubar_for_gtk_window(window)
    }

    /// Returns whether this menu visible on a on a win32 window
    ///
    /// # Safety
    ///
    /// The `hwnd` must be a valid window HWND.
    #[cfg(target_os = "windows")]
    pub unsafe fn is_visible_on_hwnd(&self, hwnd: isize) -> bool {
        self.platform.borrow().is_visible_on_hwnd(hwnd)
    }

    /// Adds this menu to an NSApp.
    #[cfg(target_os = "macos")]
    pub fn init_for_nsapp(&self) {
        self.platform.borrow_mut().init_for_nsapp()
    }

    /// Removes this menu from an NSApp.
    #[cfg(target_os = "macos")]
    pub fn remove_for_nsapp(&self) {
        self.platform.borrow_mut().remove_for_nsapp()
    }
}

impl crate::sealed::Sealed for Menu {}
impl ContextMenu for Menu {
    #[cfg(target_os = "windows")]
    fn hpopupmenu(&self) -> isize {
        self.platform.borrow().hpopupmenu()
    }

    #[cfg(target_os = "windows")]
    unsafe fn show_context_menu_for_hwnd(&self, hwnd: isize, position: Option<Position>) -> bool {
        let selected = self.platform.borrow().show_context_menu(hwnd, position);
        crate::platform_impl::dispatch_selection(hwnd, selected)
    }

    #[cfg(target_os = "windows")]
    unsafe fn attach_menu_subclass_for_hwnd(&self, hwnd: isize) {
        self.platform.borrow().attach_menu_subclass_for_hwnd(hwnd)
    }

    #[cfg(target_os = "windows")]
    unsafe fn detach_menu_subclass_from_hwnd(&self, hwnd: isize) {
        self.platform.borrow().detach_menu_subclass_from_hwnd(hwnd)
    }

    #[cfg(all(
        any(
            target_os = "linux",
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd"
        ),
        any(all(feature = "gtk3", not(feature = "gtk4")), feature = "gtk4")
    ))]
    fn show_context_menu_for_gtk_window(
        &self,
        window: &gtk::Window,
        position: Option<Position>,
    ) -> bool {
        let children = self.items();
        self.platform
            .borrow_mut()
            .show_context_menu_for_gtk_window(&children, window, position)
    }

    #[cfg(all(
        any(
            target_os = "linux",
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd"
        ),
        feature = "gtk3",
        not(feature = "gtk4")
    ))]
    fn gtk_context_menu(&self) -> gtk::Menu {
        let children = self.items();
        self.platform.borrow_mut().gtk_context_menu(&children)
    }

    #[cfg(all(
        any(
            target_os = "linux",
            target_os = "dragonfly",
            target_os = "freebsd",
            target_os = "netbsd",
            target_os = "openbsd"
        ),
        feature = "gtk4"
    ))]
    fn gtk_context_menu(&self) -> gtk::PopoverMenu {
        let children = self.items();
        self.platform.borrow_mut().gtk_context_menu(&children)
    }

    #[cfg(target_os = "macos")]
    unsafe fn show_context_menu_for_nsview(
        &self,
        view: *const std::ffi::c_void,
        position: Option<Position>,
    ) -> bool {
        self.platform
            .borrow_mut()
            .show_context_menu_for_nsview(view, position)
    }

    #[cfg(target_os = "macos")]
    fn ns_menu(&self) -> *mut std::ffi::c_void {
        self.platform.borrow().ns_menu()
    }

    fn as_menu(&self) -> Option<&Menu> {
        Some(self)
    }

    #[cfg(feature = "snapshot")]
    fn snapshot_handle(&self) -> MenuSnapshotHandle {
        MenuSnapshotHandle::from_menu(self.state.clone())
    }
}

/// The window menu bar theme
#[cfg(windows)]
#[repr(usize)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum MenuTheme {
    Dark = 0,
    Light = 1,
    Auto = 2,
}

pub(crate) fn positions_of(children: &[UnsafeMenuItemKind], id: &MenuId) -> Vec<usize> {
    children
        .iter()
        .enumerate()
        .filter_map(|(index, child)| {
            // SAFETY: callers hold a thread-bound `Menu` or `Submenu`, so this runs on the thread
            // where the child was wrapped.
            (unsafe { child.borrow() }.id() == id).then_some(index)
        })
        .collect()
}