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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//! The tray icon: the product's own icon in the platform's status area — the macOS menu bar and the
//! Windows notification area — with a click that brings the window up and a native one-item menu
//! whose item quits. It is the only way back to a window a close hid, and the product's only
//! in-app quit. Linux has no tray, and nothing here is compiled there. Nothing runs periodically:
//! each platform hands a gesture to the handlers [`install`] sets, and they only enqueue it. A tray
//! that cannot be created is not fatal — it is reported, the daemon runs without it, and a window
//! then never hides (a hidden window with no tray to bring it back would leave no way to quit).
use std::sync::atomic::{AtomicBool, Ordering};
use tray_icon::TrayIconBuilder;
use tray_icon::menu::{Menu, MenuEvent, MenuItem};
use crate::window_requests;
/// The menu's single item: the id its event is matched against, and its wording.
const QUIT_ITEM: &str = "quit";
const QUIT_ITEM_LABEL: &str = "Quit mahbot";
/// The icon's own name, set as the tooltip — the only label it carries.
const TOOLTIP: &str = "mahbot";
/// How long [`remove`] waits for the thread that owns the icon to take it away. Bounded because an
/// exiting process must not wait on it: a late removal leaves a stale icon on Windows, never a
/// hang.
const REMOVAL_WAIT: std::time::Duration = std::time::Duration::from_secs(2);
/// Whether the icon exists — the product's way back to a hidden window; false when creation failed.
static AVAILABLE: AtomicBool = AtomicBool::new(false);
/// Whether the tray icon is there: a window may only be hidden while it is (see `gui`).
pub(crate) fn available() -> bool {
AVAILABLE.load(Ordering::Relaxed)
}
/// Install the icon, once per process, before the iced application runs. The menu is the same on
/// both platforms; each supplies its own click. Gestures go to [`crate::window_requests`], whose
/// channel [`crate::window_requests::init`] fills before this runs — an early gesture waits there.
pub fn install() {
MenuEvent::set_event_handler(Some(|event: MenuEvent| {
if event.id.as_ref() == QUIT_ITEM {
window_requests::quit();
}
}));
#[cfg(target_os = "macos")]
macos::install();
#[cfg(target_os = "windows")]
windows::install();
}
// Each platform's own `remove` is documented there: the re-export is what both ends call — after
// the iced runtime returns (`main`), and the self-update tail, whose `exit(0)` runs no destructors.
#[cfg(target_os = "macos")]
pub use macos::remove;
#[cfg(target_os = "windows")]
pub use windows::remove;
/// The builder both platforms share: the native menu with its one item, the tooltip, and the click
/// that opens
/// the menu kept to a secondary click — a plain click belongs to the window it brings up.
fn builder() -> Result<TrayIconBuilder, String> {
let menu = Menu::new();
let quit = MenuItem::with_id(QUIT_ITEM, QUIT_ITEM_LABEL, true, None);
menu.append(&quit)
.map_err(|e| format!("its menu could not be built: {e}"))?;
Ok(TrayIconBuilder::new()
.with_menu(Box::new(menu))
.with_menu_on_left_click(false)
.with_tooltip(TOOLTIP))
}
fn report(reason: &str) {
crate::boot::boot_diagnostic(format!("the tray icon was not created: {reason}"));
}
#[cfg(target_os = "macos")]
mod macos {
use super::{AVAILABLE, REMOVAL_WAIT, builder, report};
use crate::window_requests;
use block2::RcBlock;
use dispatch2::DispatchQueue;
use objc2::MainThreadMarker;
use objc2::rc::Retained;
use objc2::runtime::AnyObject;
use objc2_app_kit::{NSEvent, NSEventMask, NSStatusItem};
use std::cell::RefCell;
use std::ptr::NonNull;
use std::sync::atomic::Ordering;
use tray_icon::TrayIcon;
/// What the main thread keeps for as long as the icon is in the menu bar.
struct Installed {
/// The tray itself; dropping it takes the status item out of the menu bar.
tray: TrayIcon,
/// The event-stream monitor reading the plain click, taken out with the item.
monitor: Retained<AnyObject>,
}
thread_local! {
/// The icon, on the thread that owns it: an AppKit status item belongs to the main thread.
static ICON: RefCell<Option<Installed>> = const { RefCell::new(None) };
}
/// The status item can only be created on the main thread, with the application's own event
/// loop running:
/// click handling, menu tracking and every AppKit call behind them are that thread's work.
/// [`install`](super::install) runs before iced's loop starts, so creation is queued onto the
/// main thread.
pub(super) fn install() {
DispatchQueue::main().exec_async(|| match create() {
Ok(installed) => {
AVAILABLE.store(true, Ordering::Relaxed);
ICON.with(|icon| *icon.borrow_mut() = Some(installed));
}
Err(reason) => report(&reason),
});
}
fn create() -> Result<Installed, String> {
let icon = crate::app_icon::tray_icon_image().ok_or("its image could not be built")?;
let tray = builder()?
.with_icon_templated(icon)
.build()
.map_err(|e| e.to_string())?;
let marker = MainThreadMarker::new().ok_or("it can only be created on the main thread")?;
let item = tray
.ns_status_item()
.ok_or("its status item is gone")?
.clone();
// The backing-store image replaces the one above at the same size in points: the item does
// not move.
if let (Some(image), Some(button)) =
(crate::app_icon::tray_status_image(), item.button(marker))
{
button.setImage(Some(&image));
}
// A plain click never reaches the crate's own view — the status-bar button handles the
// press itself and
// keeps the release, while a secondary click reaches the view and opens the menu — so the
// press is read from the event stream instead, before it is dispatched to a view.
let watched = item.clone();
let handler = RcBlock::new(move |event: NonNull<NSEvent>| -> *mut NSEvent {
// SAFETY: the monitor hands over a live event for the duration of the call.
if on_icon(&watched, unsafe { event.as_ref() }) {
window_requests::show();
}
// Handed back untouched: the icon's own handling still needs it.
event.as_ptr()
});
// SAFETY: the block hands back the pointer to the `NSEvent` it was given.
let monitor = unsafe {
NSEvent::addLocalMonitorForEventsMatchingMask_handler(
NSEventMask::LeftMouseDown,
&handler,
)
};
// The monitor is the whole click-to-show path, and a window is only ever hidden while this
// icon is the way back: an icon whose click cannot be read is reported as no icon at all,
// so the window then never hides (`gui`).
let monitor = monitor.ok_or("its click could not be watched")?;
Ok(Installed { tray, monitor })
}
/// Whether the event landed on this item's own button. A monitor sees every mouse event this
/// application gets, and the status bar gives each item a window of its own, so the window name
/// is the answer.
fn on_icon(item: &NSStatusItem, event: &NSEvent) -> bool {
let Some(marker) = MainThreadMarker::new() else {
return false;
};
let (Some(button), Some(clicked)) = (item.button(marker), event.window(marker)) else {
return false;
};
button.window().is_some_and(|window| {
std::ptr::eq(Retained::as_ptr(&window), Retained::as_ptr(&clicked))
})
}
/// Take the icon out of the menu bar: safe from any thread, idempotent, bounded by
/// [`REMOVAL_WAIT`].
pub fn remove() {
if MainThreadMarker::new().is_some() {
take_icon();
return;
}
// The exit may run on any thread, and the item can only go on the main one.
let (sent, removed) = std::sync::mpsc::channel();
DispatchQueue::main().exec_async(move || {
take_icon();
let _ = sent.send(());
});
let _ = removed.recv_timeout(REMOVAL_WAIT);
}
fn take_icon() {
let Some(Installed { tray, monitor }) = ICON.with(|icon| icon.borrow_mut().take()) else {
return;
};
// SAFETY: a monitor this thread installed, removed once.
unsafe { NSEvent::removeMonitor(&monitor) };
// Dropping the icon takes the status item out of the menu bar.
drop(tray);
}
}
#[cfg(target_os = "windows")]
mod windows {
use super::{AVAILABLE, REMOVAL_WAIT, builder, report};
use crate::util::UnwrapPoison;
use crate::window_requests;
use std::sync::Mutex;
use std::sync::atomic::Ordering;
use std::sync::mpsc::Receiver;
use tray_icon::{MouseButton, MouseButtonState, TrayIcon, TrayIconEvent};
use windows_sys::Win32::System::Threading::GetCurrentThreadId;
use windows_sys::Win32::UI::WindowsAndMessaging::{
DispatchMessageW, GetMessageW, PostThreadMessageW, TranslateMessage, WM_QUIT,
};
/// The notification area's icon: the thread that owns it and how to learn it is gone.
struct Owned {
/// The owning thread's id, set once it has built the icon; gone once an exit has claimed
/// the slot.
thread: Option<u32>,
/// Answers once the icon has been dropped: a disconnected channel is the icon's removal.
removed: Receiver<()>,
}
/// `install` fills the slot before the thread starts, so `remove` always finds one to claim and
/// wait for.
static OWNED: Mutex<Option<Owned>> = Mutex::new(None);
/// The notification area hands a click to the thread that registered the icon and needs a
/// message loop on
/// it, so the tray gets a thread of its own. Nothing of ours runs on that loop beyond the
/// dispatch, and an open menu never blocks the iced event loop.
pub(super) fn install() {
TrayIconEvent::set_event_handler(Some(|event: TrayIconEvent| {
// The release, not the press: the gesture one click reports once.
if let TrayIconEvent::Click {
button: MouseButton::Left,
button_state: MouseButtonState::Up,
..
} = event
{
window_requests::show();
}
}));
let (held, waiting) = std::sync::mpsc::channel();
*OWNED.lock().unwrap_poison() = Some(Owned {
thread: None,
removed: waiting,
});
let spawned = std::thread::Builder::new()
.name("tray".to_owned())
.spawn(move || {
run();
// Losing the last sender is what an exit waiting on the channel sees.
drop(held);
});
if let Err(e) = spawned {
// Nothing was started, so the slot would only make an exit wait for a signal that never
// comes.
*OWNED.lock().unwrap_poison() = None;
report(&format!("its thread could not be started: {e}"));
}
}
fn create() -> Result<TrayIcon, String> {
let icon = crate::app_icon::tray_icon_image().ok_or("its image could not be built")?;
builder()?
.with_icon(icon)
.build()
.map_err(|e| e.to_string())
}
fn run() {
let tray = match create() {
Ok(tray) => tray,
Err(reason) => {
report(&reason);
return;
}
};
// Registering under the lock `remove` claims makes the two orders exhaustive: either the
// exit claimed the
// slot first and this icon is dropped below (this thread will not pump it), or this thread
// is registered
// first and the exit's `WM_QUIT` ends the loop. An exit waits for the channel closing
// either way.
let registered = {
let mut slot = OWNED.lock().unwrap_poison();
match slot.as_mut() {
Some(owned) => {
owned.thread = Some(unsafe { GetCurrentThreadId() });
true
}
None => false,
}
};
if registered {
// The icon is in the notification area and will be pumped from here: the way back to a
// hidden window.
AVAILABLE.store(true, Ordering::Relaxed);
pump();
}
drop(tray);
}
/// The platform's messages for the icon — its clicks, and the restart of the taskbar,
/// which re-registers it — are dispatched here, on the thread that registered it. A
/// null window pumps this thread's own queue, and nothing of ours schedules work on
/// it: it sits blocked until a message arrives.
fn pump() {
let mut message = std::mem::MaybeUninit::zeroed();
while unsafe { GetMessageW(message.as_mut_ptr(), 0, 0, 0) } > 0 {
let message = unsafe { message.assume_init_ref() };
unsafe {
TranslateMessage(message);
DispatchMessageW(message);
}
}
}
/// Take the icon out of the notification area: safe from any thread, idempotent, bounded by
/// [`REMOVAL_WAIT`].
pub fn remove() {
let Some(owned) = OWNED.lock().unwrap_poison().take() else {
return;
};
if let Some(thread) = owned.thread {
// The loop returns on `WM_QUIT`, which is what takes the icon away.
unsafe { PostThreadMessageW(thread, WM_QUIT, 0, 0) };
}
// Returns once the thread dropped the icon, or on the timeout — a stale icon, never a hang.
let _ = owned.removed.recv_timeout(REMOVAL_WAIT);
}
}