Struct Menu

Source
pub struct Menu(/* private fields */);
Expand description

A menu object.

This may be a window menu, an application menu (macOS) or a context (right-click) menu.

§Configuring menus

Currently, a menu and its items cannot be changed once created. If you need to change anything about a menu (for instance, disabling or selecting items) you need to create a new menu with the desired properties.

Implementations§

Source§

impl Menu

Source

pub fn new() -> Menu

Create a new empty window or application menu.

Examples found in repository?
examples/quit.rs (line 73)
69fn main() {
70    tracing_subscriber::fmt().init();
71    let app = Application::new().unwrap();
72
73    let mut file_menu = Menu::new();
74    file_menu.add_item(
75        0x100,
76        "E&xit",
77        Some(&HotKey::new(SysMods::Cmd, "q")),
78        None,
79        true,
80    );
81
82    let mut menubar = Menu::new();
83    menubar.add_dropdown(file_menu, "Application", true);
84
85    let mut builder = WindowBuilder::new(app.clone());
86    builder.set_handler(Box::<QuitState>::default());
87    builder.set_title("Quit example");
88    builder.set_menu(menubar);
89
90    let window = builder.build().unwrap();
91    window.show();
92
93    app.run(None);
94}
More examples
Hide additional examples
examples/shello.rs (line 138)
136fn main() {
137    tracing_subscriber::fmt().init();
138    let mut file_menu = Menu::new();
139    file_menu.add_item(
140        0x100,
141        "E&xit",
142        Some(&HotKey::new(SysMods::Cmd, "q")),
143        None,
144        true,
145    );
146    file_menu.add_item(
147        0x101,
148        "O&pen",
149        Some(&HotKey::new(SysMods::Cmd, "o")),
150        None,
151        true,
152    );
153    file_menu.add_item(
154        0x102,
155        "S&ave",
156        Some(&HotKey::new(SysMods::Cmd, "s")),
157        None,
158        true,
159    );
160    let mut menubar = Menu::new();
161    menubar.add_dropdown(Menu::new(), "Application", true);
162    menubar.add_dropdown(file_menu, "&File", true);
163
164    let app = Application::new().unwrap();
165    let mut builder = WindowBuilder::new(app.clone());
166    builder.set_handler(Box::<HelloState>::default());
167    builder.set_title("Hello example");
168    builder.set_menu(menubar);
169
170    let window = builder.build().unwrap();
171    window.show();
172
173    app.run(None);
174}
Source

pub fn new_for_popup() -> Menu

Create a new empty context menu.

Some platforms distinguish between these types of menus, and some do not.

Source

pub fn add_dropdown(&mut self, menu: Menu, text: &str, enabled: bool)

Add the provided Menu as a submenu of self, with the provided title.

Examples found in repository?
examples/quit.rs (line 83)
69fn main() {
70    tracing_subscriber::fmt().init();
71    let app = Application::new().unwrap();
72
73    let mut file_menu = Menu::new();
74    file_menu.add_item(
75        0x100,
76        "E&xit",
77        Some(&HotKey::new(SysMods::Cmd, "q")),
78        None,
79        true,
80    );
81
82    let mut menubar = Menu::new();
83    menubar.add_dropdown(file_menu, "Application", true);
84
85    let mut builder = WindowBuilder::new(app.clone());
86    builder.set_handler(Box::<QuitState>::default());
87    builder.set_title("Quit example");
88    builder.set_menu(menubar);
89
90    let window = builder.build().unwrap();
91    window.show();
92
93    app.run(None);
94}
More examples
Hide additional examples
examples/shello.rs (line 161)
136fn main() {
137    tracing_subscriber::fmt().init();
138    let mut file_menu = Menu::new();
139    file_menu.add_item(
140        0x100,
141        "E&xit",
142        Some(&HotKey::new(SysMods::Cmd, "q")),
143        None,
144        true,
145    );
146    file_menu.add_item(
147        0x101,
148        "O&pen",
149        Some(&HotKey::new(SysMods::Cmd, "o")),
150        None,
151        true,
152    );
153    file_menu.add_item(
154        0x102,
155        "S&ave",
156        Some(&HotKey::new(SysMods::Cmd, "s")),
157        None,
158        true,
159    );
160    let mut menubar = Menu::new();
161    menubar.add_dropdown(Menu::new(), "Application", true);
162    menubar.add_dropdown(file_menu, "&File", true);
163
164    let app = Application::new().unwrap();
165    let mut builder = WindowBuilder::new(app.clone());
166    builder.set_handler(Box::<HelloState>::default());
167    builder.set_title("Hello example");
168    builder.set_menu(menubar);
169
170    let window = builder.build().unwrap();
171    window.show();
172
173    app.run(None);
174}
Source

pub fn add_item( &mut self, id: u32, text: &str, key: Option<&HotKey>, selected: Option<bool>, enabled: bool, )

Add an item to this menu.

The id should uniquely identify this item. If the user selects this item, the responsible WinHandler’s command method will be called with this id. If the enabled argument is false, the menu item will be grayed out; the hotkey will also be disabled. If the selected argument is true, the menu will have a checkmark or platform appropriate equivalent indicating that it is currently selected. The key argument is an optional HotKey that will be registered with the system.

Examples found in repository?
examples/quit.rs (lines 74-80)
69fn main() {
70    tracing_subscriber::fmt().init();
71    let app = Application::new().unwrap();
72
73    let mut file_menu = Menu::new();
74    file_menu.add_item(
75        0x100,
76        "E&xit",
77        Some(&HotKey::new(SysMods::Cmd, "q")),
78        None,
79        true,
80    );
81
82    let mut menubar = Menu::new();
83    menubar.add_dropdown(file_menu, "Application", true);
84
85    let mut builder = WindowBuilder::new(app.clone());
86    builder.set_handler(Box::<QuitState>::default());
87    builder.set_title("Quit example");
88    builder.set_menu(menubar);
89
90    let window = builder.build().unwrap();
91    window.show();
92
93    app.run(None);
94}
More examples
Hide additional examples
examples/shello.rs (lines 139-145)
136fn main() {
137    tracing_subscriber::fmt().init();
138    let mut file_menu = Menu::new();
139    file_menu.add_item(
140        0x100,
141        "E&xit",
142        Some(&HotKey::new(SysMods::Cmd, "q")),
143        None,
144        true,
145    );
146    file_menu.add_item(
147        0x101,
148        "O&pen",
149        Some(&HotKey::new(SysMods::Cmd, "o")),
150        None,
151        true,
152    );
153    file_menu.add_item(
154        0x102,
155        "S&ave",
156        Some(&HotKey::new(SysMods::Cmd, "s")),
157        None,
158        true,
159    );
160    let mut menubar = Menu::new();
161    menubar.add_dropdown(Menu::new(), "Application", true);
162    menubar.add_dropdown(file_menu, "&File", true);
163
164    let app = Application::new().unwrap();
165    let mut builder = WindowBuilder::new(app.clone());
166    builder.set_handler(Box::<HelloState>::default());
167    builder.set_title("Hello example");
168    builder.set_menu(menubar);
169
170    let window = builder.build().unwrap();
171    window.show();
172
173    app.run(None);
174}
Source

pub fn add_separator(&mut self)

Add a separator to the menu.

Auto Trait Implementations§

§

impl Freeze for Menu

§

impl RefUnwindSafe for Menu

§

impl !Send for Menu

§

impl !Sync for Menu

§

impl Unpin for Menu

§

impl UnwindSafe for Menu

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> RoundFrom<T> for T

Source§

fn round_from(x: T) -> T

Performs the conversion.
Source§

impl<T, U> RoundInto<U> for T
where U: RoundFrom<T>,

Source§

fn round_into(self) -> U

Performs the conversion.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more