pub struct MenuItem {
pub handle: ControlHandle,
}
Expand description
A windows menu item. Can be added to a menubar or another menu.
Requires the menu
feature.
Builder parameters:
- text: The text of the menu, including access key and shortcut label
- disabled: If the item can be selected by the user
- check: If the item should have a check mark next to it.
- parent: A top level window or a menu. With a top level window, the menu item is added to the menu bar.
Control events:
- OnMenuItemSelected: When a menu item is selected. This can be done by clicking or using the hot-key.
- OnMenuHover: When the user hovers the menu
Menu Access Keys
Just like Menus, menu items can have access keys. An access key is an underlined letter in the text of a menu item. When a menu is active, the user can select a menu item by pressing the key that corresponds to the item’s underlined letter. The user makes the menu bar active by pressing the ALT key to highlight the first item on the menu bar. A menu is active when it is displayed.
To create an access key for a menu item, precede any character in the item’s text string with an ampersand. For example, the text string “&Move” causes the system to underline the letter “M”.
use native_windows_gui as nwg;
fn menu_item(item: &mut nwg::MenuItem, menu: &nwg::Menu) -> Result<(), nwg::NwgError> {
nwg::MenuItem::builder()
.text("&Hello")
.disabled(true)
.parent(menu)
.build(item)
}
Shortcut Label
A shortcut label like “Ctrl+O” can be added with the text
field. By prefixing the shortcut with a tab character \t
and
adding it as a suffix to the text label, it will be rendered right-aligned in the menu item.
For example, an “Exit” menu item could have both an access key “E” and a shortcut label “Alt+F4” with text: "&Exit\tAlt+F4"
.
note: This will only add a text label to the menu item, the keyboard handling must be done through other means.
use native_windows_gui as nwg;
fn menu(menu: &mut nwg::Menu, window: &nwg::Window) -> Result<(), nwg::NwgError> {
nwg::Menu::builder()
.text("&Exit\tAlt+F4")
.disabled(false)
.parent(window)
.build(menu)
}
Fields§
§handle: ControlHandle
Implementations§
Source§impl MenuItem
impl MenuItem
Sourcepub fn builder<'a>() -> MenuItemBuilder<'a>
pub fn builder<'a>() -> MenuItemBuilder<'a>
Examples found in repository?
60 fn build_ui(mut data: SystemTray) -> Result<SystemTrayUi, nwg::NwgError> {
61 use nwg::Event as E;
62
63 // Resources
64 nwg::Icon::builder()
65 .source_file(Some("./test_rc/cog.ico"))
66 .build(&mut data.icon)?;
67
68 // Controls
69 nwg::MessageWindow::builder()
70 .build(&mut data.window)?;
71
72 nwg::TrayNotification::builder()
73 .parent(&data.window)
74 .icon(Some(&data.icon))
75 .tip(Some("Hello"))
76 .build(&mut data.tray)?;
77
78 nwg::Menu::builder()
79 .popup(true)
80 .parent(&data.window)
81 .build(&mut data.tray_menu)?;
82
83 nwg::MenuItem::builder()
84 .text("Hello")
85 .parent(&data.tray_menu)
86 .build(&mut data.tray_item1)?;
87
88 nwg::MenuItem::builder()
89 .text("Popup")
90 .parent(&data.tray_menu)
91 .build(&mut data.tray_item2)?;
92
93 nwg::MenuItem::builder()
94 .text("Exit")
95 .parent(&data.tray_menu)
96 .build(&mut data.tray_item3)?;
97
98 // Wrap-up
99 let ui = SystemTrayUi {
100 inner: Rc::new(data),
101 default_handler: Default::default(),
102 };
103
104 // Events
105 let evt_ui = Rc::downgrade(&ui.inner);
106 let handle_events = move |evt, _evt_data, handle| {
107 if let Some(evt_ui) = evt_ui.upgrade() {
108 match evt {
109 E::OnContextMenu =>
110 if &handle == &evt_ui.tray {
111 SystemTray::show_menu(&evt_ui);
112 }
113 E::OnMenuItemSelected =>
114 if &handle == &evt_ui.tray_item1 {
115 SystemTray::hello1(&evt_ui);
116 } else if &handle == &evt_ui.tray_item2 {
117 SystemTray::hello2(&evt_ui);
118 } else if &handle == &evt_ui.tray_item3 {
119 SystemTray::exit(&evt_ui);
120 },
121 _ => {}
122 }
123 }
124 };
125
126 ui.default_handler.borrow_mut().push(
127 nwg::full_bind_event_handler(&ui.window.handle, handle_events)
128 );
129
130 return Ok(ui);
131 }
Sourcepub fn enabled(&self) -> bool
pub fn enabled(&self) -> bool
Return true if the control user can interact with the control, return false otherwise
Sourcepub fn set_enabled(&self, v: bool)
pub fn set_enabled(&self, v: bool)
Enable or disable the control
Sourcepub fn set_checked(&self, check: bool)
pub fn set_checked(&self, check: bool)
Sets the check state of a menu item