-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
system_tray_d.rs
67 lines (50 loc) · 1.88 KB
/
system_tray_d.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*!
An application that runs in the system tray.
Requires the following features: `cargo run --example system_tray_d --features "tray-notification message-window menu cursor"`
*/
extern crate native_windows_gui as nwg;
extern crate native_windows_derive as nwd;
use nwd::NwgUi;
use nwg::NativeUi;
#[derive(Default, NwgUi)]
pub struct SystemTray {
#[nwg_control]
window: nwg::MessageWindow,
#[nwg_resource(source_file: Some("./test_rc/cog.ico"))]
icon: nwg::Icon,
#[nwg_control(icon: Some(&data.icon), tip: Some("Hello"))]
#[nwg_events(MousePressLeftUp: [SystemTray::show_menu], OnContextMenu: [SystemTray::show_menu])]
tray: nwg::TrayNotification,
#[nwg_control(parent: window, popup: true)]
tray_menu: nwg::Menu,
#[nwg_control(parent: tray_menu, text: "Hello")]
#[nwg_events(OnMenuItemSelected: [SystemTray::hello1])]
tray_item1: nwg::MenuItem,
#[nwg_control(parent: tray_menu, text: "Popup")]
#[nwg_events(OnMenuItemSelected: [SystemTray::hello2])]
tray_item2: nwg::MenuItem,
#[nwg_control(parent: tray_menu, text: "Exit")]
#[nwg_events(OnMenuItemSelected: [SystemTray::exit])]
tray_item3: nwg::MenuItem,
}
impl SystemTray {
fn show_menu(&self) {
let (x, y) = nwg::GlobalCursor::position();
self.tray_menu.popup(x, y);
}
fn hello1(&self) {
nwg::simple_message("Hello", "Hello World!");
}
fn hello2(&self) {
let flags = nwg::TrayNotificationFlags::USER_ICON | nwg::TrayNotificationFlags::LARGE_ICON;
self.tray.show("Hello World", Some("Welcome to my application"), Some(flags), Some(&self.icon));
}
fn exit(&self) {
nwg::stop_thread_dispatch();
}
}
fn main() {
nwg::init().expect("Failed to init Native Windows GUI");
let _ui = SystemTray::build_ui(Default::default()).expect("Failed to build UI");
nwg::dispatch_thread_events();
}