Struct Menu

Source
pub struct Menu {
    pub handle: ControlHandle,
}
Expand description

A windows menu. Can represent a menu in a window menubar, a context menu, or a submenu in another menu

Requires the menu feature.

Builder parameters:

  • text: The text of the menu
  • disabled: If the menu can be selected by the user
  • popup: The menu is a context menu
  • parent: A top level window, a menu or None. With a top level window, the menu is added to the menu bar if popup is set to false.

Control events:

  • OnMenuOpen: Sent when a drop-down menu or submenu is about to become active.
  • OnMenuHover: When the user hovers the menu
  • OnMenuEnter: When the user enters the menu. Technically, when the user enters the menu modal loop.
  • OnMenuExit: When the menu is closed. Technically, when the user exits the menu modal loop.

Menu Access Keys

Menu 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(menu: &mut nwg::Menu, window: &nwg::Window) -> Result<(), nwg::NwgError> {
    nwg::Menu::builder()
        .text("&Hello")
        .disabled(false)
        .parent(window)
        .build(menu)
}

Fields§

§handle: ControlHandle

Implementations§

Source§

impl Menu

Source

pub fn builder<'a>() -> MenuBuilder<'a>

Examples found in repository?
examples/system_tray.rs (line 78)
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        }
Source

pub fn enabled(&self) -> bool

Return true if the control user can interact with the control, return false otherwise

Source

pub fn set_enabled(&self, v: bool)

Enable or disable the control A popup menu cannot be disabled

Source

pub fn popup_with_flags(&self, x: i32, y: i32, flags: PopupMenuFlags)

Show a popup menu as the selected position. Do nothing for menubar menu.

Source

pub fn popup(&self, x: i32, y: i32)

Show a popup menu as the selected position. Do nothing for menubar menu.

Examples found in repository?
examples/system_tray.rs (line 25)
23    fn show_menu(&self) {
24        let (x, y) = nwg::GlobalCursor::position();
25        self.tray_menu.popup(x, y);
26    }

Trait Implementations§

Source§

impl Default for Menu

Source§

fn default() -> Menu

Returns the “default value” for a type. Read more
Source§

impl Drop for Menu

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl From<&Menu> for ControlHandle

Source§

fn from(control: &Menu) -> Self

Converts to this type from the input type.
Source§

impl From<&mut Menu> for ControlHandle

Source§

fn from(control: &mut Menu) -> Self

Converts to this type from the input type.
Source§

impl PartialEq<ControlHandle> for Menu

Source§

fn eq(&self, other: &ControlHandle) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<Menu> for ControlHandle

Source§

fn eq(&self, other: &Menu) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for Menu

Source§

fn eq(&self, other: &Menu) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Menu

Source§

impl StructuralPartialEq for 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, 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, 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.