pub struct TrayNotification {
pub handle: ControlHandle,
}
Expand description
A control that handle system tray notification. A TrayNotification wraps a single icon in the Windows system tray.
An application can have many TrayNotification, but each window (aka parent) can only have a single traynotification.
It is possible to create system tray only application with the MessageOnlyWindow
control.
A system tray will receive events if callback
is set to true in the builder (the default behaviour).
The control will generate mouse events such as OnMouseMove
when the user interact with the tray icon or the message popup.
A system tray will also receive a OnContextMenu
when the user right click the icon. It is highly recommended handle this message and display a popup menu
You can’t get information on the state of a tray notification (such as visibility) because Windows don’t want you to.
Builder parameters:
* parent
: Required. The tray notification parent container.
* icon
: Required. The icon to display in the system tray
* tips
: Display a simple tooltip when hovering the icon in the system tray
* flags
: A combination of the TrayNotificationFlags values.
* visible
: If the icon should be visible in the system tray
* realtime
: If the balloon notification cannot be displayed immediately, discard it.
* info
: Display a fancy tooltip when the system tray icon is hovered (replaces tip)
* balloon_icon
: The icon to display in the fancy tooltip
* info_title
: The title of the fancy tooltip
Control events:
* OnContextMenu
: When the user right clicks on the system tray icon
* MousePressLeftUp
: When the user left click the system tray icon
* OnTrayNotificationShow
: When a TrayNotification info popup (not the tooltip) is shown
* OnTrayNotificationHide
: When a TrayNotification info popup (not the tooltip) is hidden
* OnTrayNotificationTimeout
: When a TrayNotification is closed due to a timeout
* OnTrayNotificationUserClose
: When a TrayNotification is closed due to a user click
§Example
use native_windows_gui as nwg;
fn notice_user(tray: &nwg::TrayNotification, image: &nwg::Icon) {
let flags = nwg::TrayNotificationFlags::USER_ICON | nwg::TrayNotificationFlags::LARGE_ICON;
tray.show("Hello World", Some("Welcome to my application"), Some(flags), Some(image));
}
use native_windows_gui as nwg;
fn build_tray(tray: &mut nwg::TrayNotification, window: &nwg::Window, icon: &nwg::Icon) {
nwg::TrayNotification::builder()
.parent(window)
.icon(Some(icon))
.tip(Some("Hello"))
.build(tray);
}
Winapi docs: https://docs.microsoft.com/en-us/windows/win32/shell/notification-area
Fields§
§handle: ControlHandle
Implementations§
Source§impl TrayNotification
impl TrayNotification
Sourcepub fn builder<'a>() -> TrayNotificationBuilder<'a>
pub fn builder<'a>() -> TrayNotificationBuilder<'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 set_visibility(&self, v: bool)
pub fn set_visibility(&self, v: bool)
Set the visibility of the icon in the system tray
Sourcepub fn set_tip<'a>(&self, tip: &'a str)
pub fn set_tip<'a>(&self, tip: &'a str)
Set the tooltip for the tray notification. Note: tip will be truncated to 127 characters
Sourcepub fn show<'a>(
&self,
text: &'a str,
title: Option<&'a str>,
flags: Option<TrayNotificationFlags>,
icon: Option<&'a Icon>,
)
pub fn show<'a>( &self, text: &'a str, title: Option<&'a str>, flags: Option<TrayNotificationFlags>, icon: Option<&'a Icon>, )
Shows a popup message on top of the system tray
Parameters:
- text: The text in the popup
- title: The title of the popup
- flags: Flags that specify how the popup is shown. Default is NO_ICON | QUIET.
- icon: Icon to display in the popup. Only used if
USER_ICON
is set in flags.
Note 1: text will be truncated to 255 characters Note 2: title will be truncated to 63 characters