druid_shell/menu.rs
1// Copyright 2019 The Druid Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::backend::menu as backend;
16use crate::hotkey::HotKey;
17
18/// A menu object.
19///
20/// This may be a window menu, an application menu (macOS) or a context (right-click)
21/// menu.
22///
23/// # Configuring menus
24///
25/// Currently, a menu and its items cannot be changed once created. If you need
26/// to change anything about a menu (for instance, disabling or selecting items)
27/// you need to create a new menu with the desired properties.
28pub struct Menu(pub(crate) backend::Menu);
29
30impl Menu {
31    /// Create a new empty window or application menu.
32    pub fn new() -> Menu {
33        Menu(backend::Menu::new())
34    }
35
36    /// Create a new empty context menu.
37    ///
38    /// Some platforms distinguish between these types of menus, and some
39    /// do not.
40    pub fn new_for_popup() -> Menu {
41        Menu(backend::Menu::new_for_popup())
42    }
43
44    /// Consume this `Menu`, returning the platform menu object.
45    pub(crate) fn into_inner(self) -> backend::Menu {
46        self.0
47    }
48
49    /// Add the provided `Menu` as a submenu of self, with the provided title.
50    pub fn add_dropdown(&mut self, menu: Menu, text: &str, enabled: bool) {
51        self.0.add_dropdown(menu.0, text, enabled)
52    }
53
54    /// Add an item to this menu.
55    ///
56    /// The `id` should uniquely identify this item. If the user selects this
57    /// item, the responsible [`WinHandler`]'s [`command`] method will
58    /// be called with this `id`. If the `enabled` argument is false, the menu
59    /// item will be grayed out; the hotkey will also be disabled.
60    /// If the `selected` argument is `true`, the menu will have a checkmark
61    /// or platform appropriate equivalent indicating that it is currently selected.
62    /// The `key` argument is an optional [`HotKey`] that will be registered
63    /// with the system.
64    ///
65    ///
66    /// [`WinHandler`]: crate::WinHandler
67    /// [`command`]: crate::WinHandler::command
68    pub fn add_item(
69        &mut self,
70        id: u32,
71        text: &str,
72        key: Option<&HotKey>,
73        selected: Option<bool>,
74        enabled: bool,
75    ) {
76        self.0.add_item(id, text, key, selected, enabled)
77    }
78
79    /// Add a separator to the menu.
80    pub fn add_separator(&mut self) {
81        self.0.add_separator()
82    }
83}