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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! System tray icon with context menu and activation signals.
//!
//! Wraps [`QSystemTrayIcon`](https://doc.qt.io/qt-6/qsystemtrayicon.html).
//!
//! # Note
//!
//! `QSystemTrayIcon` is a `QObject` subclass, **not** a `QWidget`. It does
//! **not** implement [`AsWidget`](crate::widget::AsWidget). Signal closures
//! are always reclaimed on [`Drop`]; the C++ object is always deleted.
use cxx::let_cxx_string;
use crate::ffi;
use crate::signal::{self, SignalHandle};
use crate::menu::Menu;
/// Activation reason constants matching Qt's `QSystemTrayIcon::ActivationReason`.
pub const UNKNOWN: i32 = 0;
pub const CONTEXT: i32 = 1;
pub const DOUBLE_CLICK: i32 = 2;
pub const TRIGGER: i32 = 3;
pub const MIDDLE_CLICK: i32 = 4;
/// A system tray icon with tooltip, context menu, and activation signals.
///
/// `SystemTrayIcon` uses a **builder pattern**: call [`SystemTrayIcon::new`]
/// to obtain a [`Builder`], chain configuration, then call `.build()`.
///
/// # Signals
///
/// | Method | Qt signal | Callback receives |
/// |---|---|---|
/// | [`Builder::on_activated`] / [`SystemTrayIcon::connect_activated`] | `QSystemTrayIcon::activated` | `i32` (reason — `UNKNOWN`, `CONTEXT`, `DOUBLE_CLICK`, `TRIGGER`, `MIDDLE_CLICK`) |
///
/// # Example
///
/// ```no_run
/// use qtrs::SystemTrayIcon;
///
/// let tray = SystemTrayIcon::new("/path/to/icon.png")
/// .tool_tip("My App")
/// .on_activated(|reason| println!("clicked reason={}", reason))
/// .build();
/// tray.show();
/// ```
pub struct SystemTrayIcon {
ptr: *mut ffi::QSystemTrayIcon,
signal_handles: Vec<SignalHandle>,
}
impl SystemTrayIcon {
/// Start building a new system tray icon.
pub fn new(icon_path: impl Into<String>) -> Builder {
Builder::new(icon_path.into())
}
// --- Properties ---
/// Set the tray icon from a file path.
pub fn set_icon(&self, icon_path: &str) {
debug_assert!(!self.ptr.is_null());
let_cxx_string!(c_path = icon_path);
unsafe { ffi::QSystemTrayIcon_setIcon(self.ptr, &c_path); }
}
/// Set the tooltip text.
pub fn set_tool_tip(&self, tip: &str) {
debug_assert!(!self.ptr.is_null());
let_cxx_string!(c_tip = tip);
unsafe { ffi::QSystemTrayIcon_setToolTip(self.ptr, &c_tip); }
}
/// Show the tray icon.
pub fn show(&self) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QSystemTrayIcon_show(self.ptr); }
}
/// Hide the tray icon.
pub fn hide(&self) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QSystemTrayIcon_hide(self.ptr); }
}
/// Returns `true` if the tray icon is currently visible.
pub fn is_visible(&self) -> bool {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QSystemTrayIcon_isVisible(self.ptr) }
}
/// Set the context menu.
pub fn set_context_menu(&self, menu: &mut Menu) {
debug_assert!(!self.ptr.is_null());
unsafe { ffi::QSystemTrayIcon_setContextMenu(self.ptr, menu.ptr); }
}
// --- Runtime signal connections ---
/// Connect a callback when the tray icon is activated.
/// Receives a reason constant (`UNKNOWN`, `CONTEXT`, `DOUBLE_CLICK`, `TRIGGER`, `MIDDLE_CLICK`).
pub fn connect_activated<F: Fn(i32)>(&mut self, f: F) {
debug_assert!(!self.ptr.is_null());
let handle = signal::leak_int(f);
unsafe { ffi::QSystemTrayIcon_onActivated(self.ptr, handle.token); }
self.signal_handles.push(handle);
}
}
impl Drop for SystemTrayIcon {
fn drop(&mut self) {
if self.ptr.is_null() { return; }
for h in self.signal_handles.drain(..) {
unsafe { h.reclaim(); }
}
unsafe { ffi::QSystemTrayIcon_delete(self.ptr); }
self.ptr = std::ptr::null_mut();
}
}
/// Builder for [`SystemTrayIcon`].
pub struct Builder {
icon_path: String,
tool_tip: Option<String>,
on_activated: Option<Box<dyn Fn(i32)>>,
}
impl Builder {
fn new(icon_path: String) -> Self {
Self {
icon_path,
tool_tip: None,
on_activated: None,
}
}
/// Set the tooltip text.
pub fn tool_tip(mut self, tip: impl Into<String>) -> Self {
self.tool_tip = Some(tip.into());
self
}
/// Called when the tray icon is activated. Receives a reason constant.
pub fn on_activated<F: Fn(i32) + 'static>(mut self, f: F) -> Self {
self.on_activated = Some(Box::new(f));
self
}
/// Create the C++ `QSystemTrayIcon` and return the Rust wrapper.
pub fn build(self) -> SystemTrayIcon {
let_cxx_string!(c_icon = &self.icon_path);
let ptr = unsafe {
ffi::QSystemTrayIcon_new(&c_icon, std::ptr::null_mut())
};
debug_assert!(!ptr.is_null());
let mut tray = SystemTrayIcon { ptr, signal_handles: Vec::new() };
if let Some(tip) = &self.tool_tip {
let_cxx_string!(c_tip = tip);
unsafe { ffi::QSystemTrayIcon_setToolTip(ptr, &c_tip); }
}
if let Some(f) = self.on_activated {
let h = signal::leak_int(f);
unsafe { ffi::QSystemTrayIcon_onActivated(ptr, h.token); }
tray.signal_handles.push(h);
}
tray
}
}