Skip to main content

system_tray/
system_tray.rs

1/*!
2    An application that runs in the system tray.
3
4    Requires the following features: `cargo run --example system_tray --features "tray-notification message-window menu cursor"`
5*/
6extern crate native_windows_gui2 as nwg;
7use nwg::NativeUi;
8
9#[derive(Default)]
10pub struct SystemTray {
11    window: nwg::MessageWindow,
12    icon: nwg::Icon,
13    tray: nwg::TrayNotification,
14    tray_menu: nwg::Menu,
15    tray_item1: nwg::MenuItem,
16    tray_item2: nwg::MenuItem,
17    tray_item3: nwg::MenuItem,
18}
19
20impl SystemTray {
21    fn show_menu(&self) {
22        let (x, y) = nwg::GlobalCursor::position();
23        self.tray_menu.popup(x, y);
24    }
25
26    fn hello1(&self) {
27        nwg::modal_info_message(&self.window, "Hello", "Hello World!");
28    }
29
30    fn hello2(&self) {
31        let flags = nwg::TrayNotificationFlags::USER_ICON | nwg::TrayNotificationFlags::LARGE_ICON;
32        self.tray.show(
33            "Hello World",
34            Some("Welcome to my application"),
35            Some(flags),
36            Some(&self.icon),
37        );
38    }
39
40    fn exit(&self) {
41        nwg::stop_thread_dispatch();
42    }
43}
44
45//
46// ALL of this stuff is handled by native-windows-derive
47//
48mod system_tray_ui {
49    use super::*;
50    use native_windows_gui2 as nwg;
51    use std::cell::RefCell;
52    use std::ops::Deref;
53    use std::rc::Rc;
54
55    pub struct SystemTrayUi {
56        inner: Rc<SystemTray>,
57        default_handler: RefCell<Vec<nwg::EventHandler>>,
58    }
59
60    impl nwg::NativeUi<SystemTrayUi> for SystemTray {
61        fn build_ui(mut data: SystemTray) -> Result<SystemTrayUi, nwg::NwgError> {
62            use nwg::Event as E;
63
64            // Resources
65            nwg::Icon::builder()
66                .source_file(Some("./test_rc/cog.ico"))
67                .build(&mut data.icon)?;
68
69            // Controls
70            nwg::MessageWindow::builder().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                        }
114                        E::OnMenuItemSelected => {
115                            if &handle == &evt_ui.tray_item1 {
116                                SystemTray::hello1(&evt_ui);
117                            } else if &handle == &evt_ui.tray_item2 {
118                                SystemTray::hello2(&evt_ui);
119                            } else if &handle == &evt_ui.tray_item3 {
120                                SystemTray::exit(&evt_ui);
121                            }
122                        }
123                        _ => {}
124                    }
125                }
126            };
127
128            ui.default_handler
129                .borrow_mut()
130                .push(nwg::full_bind_event_handler(
131                    &ui.window.handle,
132                    handle_events,
133                ));
134
135            return Ok(ui);
136        }
137    }
138
139    impl Drop for SystemTrayUi {
140        /// To make sure that everything is freed without issues, the default handler must be unbound.
141        fn drop(&mut self) {
142            let mut handlers = self.default_handler.borrow_mut();
143            for handler in handlers.drain(0..) {
144                nwg::unbind_event_handler(&handler);
145            }
146        }
147    }
148
149    impl Deref for SystemTrayUi {
150        type Target = SystemTray;
151
152        fn deref(&self) -> &SystemTray {
153            &self.inner
154        }
155    }
156}
157
158fn main() {
159    nwg::init().expect("Failed to init Native Windows GUI");
160    let _ui = SystemTray::build_ui(Default::default()).expect("Failed to build UI");
161    nwg::dispatch_thread_events();
162}