system_tray/
system_tray.rs1extern crate native_windows_gui as nwg;
7use nwg::NativeUi;
8
9
10#[derive(Default)]
11pub struct SystemTray {
12 window: nwg::MessageWindow,
13 icon: nwg::Icon,
14 tray: nwg::TrayNotification,
15 tray_menu: nwg::Menu,
16 tray_item1: nwg::MenuItem,
17 tray_item2: nwg::MenuItem,
18 tray_item3: nwg::MenuItem,
19}
20
21impl SystemTray {
22
23 fn show_menu(&self) {
24 let (x, y) = nwg::GlobalCursor::position();
25 self.tray_menu.popup(x, y);
26 }
27
28 fn hello1(&self) {
29 nwg::modal_info_message(&self.window, "Hello", "Hello World!");
30 }
31
32 fn hello2(&self) {
33 let flags = nwg::TrayNotificationFlags::USER_ICON | nwg::TrayNotificationFlags::LARGE_ICON;
34 self.tray.show("Hello World", Some("Welcome to my application"), Some(flags), Some(&self.icon));
35 }
36
37 fn exit(&self) {
38 nwg::stop_thread_dispatch();
39 }
40
41}
42
43
44mod system_tray_ui {
48 use native_windows_gui as nwg;
49 use super::*;
50 use std::rc::Rc;
51 use std::cell::RefCell;
52 use std::ops::Deref;
53
54 pub struct SystemTrayUi {
55 inner: Rc<SystemTray>,
56 default_handler: RefCell<Vec<nwg::EventHandler>>
57 }
58
59 impl nwg::NativeUi<SystemTrayUi> for SystemTray {
60 fn build_ui(mut data: SystemTray) -> Result<SystemTrayUi, nwg::NwgError> {
61 use nwg::Event as E;
62
63 nwg::Icon::builder()
65 .source_file(Some("./test_rc/cog.ico"))
66 .build(&mut data.icon)?;
67
68 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 let ui = SystemTrayUi {
100 inner: Rc::new(data),
101 default_handler: Default::default(),
102 };
103
104 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 }
132 }
133
134 impl Drop for SystemTrayUi {
135 fn drop(&mut self) {
137 let mut handlers = self.default_handler.borrow_mut();
138 for handler in handlers.drain(0..) {
139 nwg::unbind_event_handler(&handler);
140 }
141 }
142 }
143
144 impl Deref for SystemTrayUi {
145 type Target = SystemTray;
146
147 fn deref(&self) -> &SystemTray {
148 &self.inner
149 }
150 }
151
152}
153
154
155fn main() {
156 nwg::init().expect("Failed to init Native Windows GUI");
157 let _ui = SystemTray::build_ui(Default::default()).expect("Failed to build UI");
158 nwg::dispatch_thread_events();
159}