ldtray 0.1.1

Cross-platform tray icons with zero compile-time linkage to platform GUI libraries — every toolkit is loaded at runtime via libloading, so headless daemons degrade gracefully instead of failing to link.
Documentation
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//! Windows tray backend: a `Shell_NotifyIconW` icon owned by a hidden
//! message-only window, all driven through `libloading`-resolved Win32 calls.
//!
//! The window and the tray icon are created lazily on the first `pump` so they
//! live on whichever thread runs the event loop (`run` or `spawn`) — Win32
//! requires the icon's window and its message pump to share a thread. Shell
//! click callbacks arrive as our `WM_APP+1` message and become [`Event`]s; the
//! context menu is a native popup shown on right-click.

mod ffi;

use std::os::raw::c_void;
use std::sync::OnceLock;
use std::time::Duration;

use ffi::*;

use super::{Backend, Init};
use crate::error::{Error, Result};
use crate::event::Event;
use crate::icon::Icon;
use crate::menu::{Menu, MenuId, MenuItem};
use crate::notification::Notification;

/// Message id (in the `WM_APP` range) the shell uses for icon callbacks.
const CALLBACK_MSG: u32 = WM_APP + 1;
/// Our single tray icon's id within the window.
const ICON_UID: u32 = 1;

// Two Win32 entry points are needed inside the window procedure *before* the
// per-window state pointer is available (during window creation). They are the
// same for every tray, so we stash them globally when the first backend loads.
static DEF_WNDPROC: OnceLock<unsafe extern "system" fn(HWND, u32, WPARAM, LPARAM) -> LRESULT> =
    OnceLock::new();
static GET_WNDLONGPTR: OnceLock<unsafe extern "system" fn(HWND, i32) -> isize> = OnceLock::new();

pub(crate) fn new(init: Init) -> Result<Box<dyn Backend>> {
    Ok(Box::new(WindowsBackend::new(init)?))
}

struct State {
    win: Win,
    hinstance: HINSTANCE,
    hwnd: HWND,
    hicon: HICON,
    class_name: Vec<u16>,
    icon: (i32, i32, Vec<u8>),
    tooltip: String,
    menu: Option<Menu>,
    pending: Vec<Event>,
    started: bool,
}

// Touched only from the single event-loop thread (see module docs).
unsafe impl Send for State {}

pub(crate) struct WindowsBackend {
    state: Box<State>,
}

impl WindowsBackend {
    fn new(init: Init) -> Result<WindowsBackend> {
        let win = Win::load()?;
        let hinstance = unsafe { (win.GetModuleHandleW)(std::ptr::null()) };
        let icon = (
            init.icon.width as i32,
            init.icon.height as i32,
            init.icon.rgba.clone(),
        );
        let state = Box::new(State {
            win,
            hinstance,
            hwnd: std::ptr::null_mut(),
            hicon: std::ptr::null_mut(),
            class_name: Vec::new(),
            icon,
            tooltip: init.tooltip,
            menu: init.menu,
            pending: Vec::new(),
            started: false,
        });
        Ok(WindowsBackend { state })
    }

    fn ensure_started(&mut self) -> Result<()> {
        if !self.state.started {
            unsafe { self.state.start()? };
            self.state.started = true;
        }
        Ok(())
    }
}

impl State {
    /// Registers the window class, creates the message-only window, builds the
    /// icon, and adds it to the tray. Runs on the event-loop thread.
    unsafe fn start(&mut self) -> Result<()> {
        let _ = DEF_WNDPROC.set(self.win.DefWindowProcW);
        let _ = GET_WNDLONGPTR.set(self.win.GetWindowLongPtrW);

        let state_ptr = self as *mut State;
        self.class_name = wide(&format!("ldtray_wndclass_{:x}", state_ptr as usize));

        unsafe {
            let cursor = (self.win.LoadCursorW)(std::ptr::null_mut(), IDC_ARROW as *const u16);
            let mut class: WNDCLASSEXW = std::mem::zeroed();
            class.cbSize = std::mem::size_of::<WNDCLASSEXW>() as u32;
            class.lpfnWndProc = Some(wndproc);
            class.hInstance = self.hinstance;
            class.hCursor = cursor;
            class.lpszClassName = self.class_name.as_ptr();
            let atom = (self.win.RegisterClassExW)(&class);
            if atom == 0 {
                return Err(Error::Backend("RegisterClassExW failed".into()));
            }

            let name = wide("ldtray");
            let hwnd = (self.win.CreateWindowExW)(
                0,
                self.class_name.as_ptr(),
                name.as_ptr(),
                0,
                0,
                0,
                0,
                0,
                HWND_MESSAGE as HWND,
                std::ptr::null_mut(),
                self.hinstance,
                std::ptr::null_mut(),
            );
            if hwnd.is_null() {
                return Err(Error::Backend("CreateWindowExW failed".into()));
            }
            self.hwnd = hwnd;
            (self.win.SetWindowLongPtrW)(hwnd, GWLP_USERDATA, state_ptr as isize);

            self.hicon = create_hicon(&self.win, self.icon.0, self.icon.1, &self.icon.2);

            let mut data = self.base_notify_data();
            data.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP | NIF_SHOWTIP;
            data.uCallbackMessage = CALLBACK_MSG;
            data.hIcon = self.hicon;
            copy_wide(&mut data.szTip, &self.tooltip);
            if (self.win.Shell_NotifyIconW)(NIM_ADD, &mut data) == 0 {
                return Err(Error::Backend("Shell_NotifyIcon(NIM_ADD) failed".into()));
            }
            let mut version = self.base_notify_data();
            version.uVersion = NOTIFYICON_VERSION_4;
            (self.win.Shell_NotifyIconW)(NIM_SETVERSION, &mut version);
        }
        Ok(())
    }

    /// A zeroed `NOTIFYICONDATAW` addressed to our icon.
    fn base_notify_data(&self) -> NOTIFYICONDATAW {
        let mut data: NOTIFYICONDATAW = unsafe { std::mem::zeroed() };
        data.cbSize = std::mem::size_of::<NOTIFYICONDATAW>() as u32;
        data.hWnd = self.hwnd;
        data.uID = ICON_UID;
        data
    }

    /// Handles a shell callback message (version-4 packing: the event is in the
    /// low word of `lParam`).
    unsafe fn handle_callback(&mut self, _wparam: WPARAM, lparam: LPARAM) {
        let event = (lparam as u32) & 0xFFFF;
        match event {
            WM_LBUTTONUP => self.pending.push(Event::LeftClick),
            WM_LBUTTONDBLCLK => self.pending.push(Event::DoubleClick),
            WM_MBUTTONUP => self.pending.push(Event::MiddleClick),
            WM_RBUTTONUP | WM_CONTEXTMENU => {
                self.pending.push(Event::RightClick);
                unsafe { self.show_menu() };
            }
            _ => {}
        }
    }

    /// Pops up the native context menu and queues the chosen item's event.
    unsafe fn show_menu(&mut self) {
        let Some(menu) = self.menu.clone() else {
            return;
        };
        unsafe {
            let hmenu = build_hmenu(&self.win, menu.items());
            if hmenu.is_null() {
                return;
            }
            let mut pt = POINT { x: 0, y: 0 };
            (self.win.GetCursorPos)(&mut pt);
            // Required so the menu dismisses when the user clicks elsewhere.
            (self.win.SetForegroundWindow)(self.hwnd);
            let cmd = (self.win.TrackPopupMenu)(
                hmenu,
                TPM_RETURNCMD | TPM_RIGHTBUTTON | TPM_LEFTALIGN,
                pt.x,
                pt.y,
                0,
                self.hwnd,
                std::ptr::null(),
            );
            (self.win.DestroyMenu)(hmenu);
            if cmd > 0 {
                self.pending.push(Event::Menu(MenuId(cmd as u32)));
            }
        }
    }

    /// Re-applies the current icon to the tray (after `set_icon`).
    unsafe fn refresh_icon(&mut self) {
        unsafe {
            let old = self.hicon;
            self.hicon = create_hicon(&self.win, self.icon.0, self.icon.1, &self.icon.2);
            let mut data = self.base_notify_data();
            data.uFlags = NIF_ICON;
            data.hIcon = self.hicon;
            (self.win.Shell_NotifyIconW)(NIM_MODIFY, &mut data);
            if !old.is_null() {
                (self.win.DestroyIcon)(old);
            }
        }
    }

    unsafe fn refresh_tooltip(&mut self) {
        unsafe {
            let mut data = self.base_notify_data();
            data.uFlags = NIF_TIP | NIF_SHOWTIP;
            copy_wide(&mut data.szTip, &self.tooltip);
            (self.win.Shell_NotifyIconW)(NIM_MODIFY, &mut data);
        }
    }

    unsafe fn show_notification(&mut self, notification: &Notification) {
        unsafe {
            let mut data = self.base_notify_data();
            data.uFlags = NIF_INFO;
            copy_wide(&mut data.szInfoTitle, &notification.title);
            copy_wide(&mut data.szInfo, &notification.body);
            data.dwInfoFlags = NIIF_INFO;
            (self.win.Shell_NotifyIconW)(NIM_MODIFY, &mut data);
        }
    }
}

impl Drop for State {
    fn drop(&mut self) {
        if !self.started {
            return;
        }
        unsafe {
            let mut data = self.base_notify_data();
            (self.win.Shell_NotifyIconW)(NIM_DELETE, &mut data);
            if !self.hicon.is_null() {
                (self.win.DestroyIcon)(self.hicon);
            }
            if !self.hwnd.is_null() {
                (self.win.DestroyWindow)(self.hwnd);
            }
            if !self.class_name.is_empty() {
                (self.win.UnregisterClassW)(self.class_name.as_ptr(), self.hinstance);
            }
        }
    }
}

impl Backend for WindowsBackend {
    fn set_icon(&mut self, icon: &Icon) -> Result<()> {
        self.state.icon = (icon.width as i32, icon.height as i32, icon.rgba.clone());
        if self.state.started {
            unsafe { self.state.refresh_icon() };
        }
        Ok(())
    }

    fn set_tooltip(&mut self, text: &str) -> Result<()> {
        self.state.tooltip = text.to_string();
        if self.state.started {
            unsafe { self.state.refresh_tooltip() };
        }
        Ok(())
    }

    fn set_menu(&mut self, menu: Option<&Menu>) -> Result<()> {
        self.state.menu = menu.cloned();
        Ok(())
    }

    fn notify(&mut self, notification: &Notification) -> Result<()> {
        self.ensure_started()?;
        unsafe { self.state.show_notification(notification) };
        Ok(())
    }

    fn pump(&mut self, timeout: Duration, sink: &mut dyn FnMut(Event)) -> Result<()> {
        self.ensure_started()?;
        // Copy the fn pointers out so no borrow of `state` is held while the
        // window procedure re-enters through the shared state pointer.
        let msgwait = self.state.win.MsgWaitForMultipleObjectsEx;
        let peek = self.state.win.PeekMessageW;
        let translate = self.state.win.TranslateMessage;
        let dispatch = self.state.win.DispatchMessageW;
        let ms = timeout.as_millis().min(u32::MAX as u128) as u32;
        unsafe {
            msgwait(0, std::ptr::null(), ms, QS_ALLINPUT, MWMO_INPUTAVAILABLE);
            let mut msg: MSG = std::mem::zeroed();
            while peek(&mut msg, std::ptr::null_mut(), 0, 0, PM_REMOVE) != 0 {
                translate(&msg);
                dispatch(&msg);
            }
        }
        for event in self.state.pending.drain(..) {
            sink(event);
        }
        Ok(())
    }
}

/// The window procedure. Recovers the per-window [`State`] pointer and turns the
/// shell callback into queued events; everything else goes to `DefWindowProcW`.
unsafe extern "system" fn wndproc(hwnd: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
    let get_ptr = GET_WNDLONGPTR.get();
    let def = DEF_WNDPROC.get();
    let state_ptr = match get_ptr {
        Some(f) => (unsafe { f(hwnd, GWLP_USERDATA) }) as *mut State,
        None => std::ptr::null_mut(),
    };
    if state_ptr.is_null() {
        return match def {
            Some(f) => unsafe { f(hwnd, msg, wparam, lparam) },
            None => 0,
        };
    }
    let state = unsafe { &mut *state_ptr };
    if msg == CALLBACK_MSG {
        unsafe { state.handle_callback(wparam, lparam) };
        return 0;
    }
    if msg == WM_DESTROY {
        return 0;
    }
    unsafe { (state.win.DefWindowProcW)(hwnd, msg, wparam, lparam) }
}

/// Recursively builds a native popup menu from menu items. Button command ids
/// are the caller's [`MenuId`] values (which must be non-zero on Windows, since
/// `TrackPopupMenu` returns 0 for "nothing selected").
unsafe fn build_hmenu(win: &Win, items: &[MenuItem]) -> HMENU {
    unsafe {
        let hmenu = (win.CreatePopupMenu)();
        if hmenu.is_null() {
            return hmenu;
        }
        for item in items {
            match item {
                MenuItem::Separator => {
                    (win.AppendMenuW)(hmenu, MF_SEPARATOR, 0, std::ptr::null());
                }
                MenuItem::Button {
                    id,
                    label,
                    enabled,
                    checked,
                } => {
                    let mut flags = MF_STRING;
                    if !enabled {
                        flags |= MF_GRAYED;
                    }
                    if *checked == Some(true) {
                        flags |= MF_CHECKED;
                    }
                    let text = wide(label);
                    (win.AppendMenuW)(hmenu, flags, id.0 as usize, text.as_ptr());
                }
                MenuItem::Submenu {
                    label,
                    enabled,
                    items,
                } => {
                    let submenu = build_hmenu(win, items);
                    let mut flags = MF_POPUP;
                    if !enabled {
                        flags |= MF_GRAYED;
                    }
                    let text = wide(label);
                    (win.AppendMenuW)(hmenu, flags, submenu as usize, text.as_ptr());
                }
            }
        }
        hmenu
    }
}

/// Builds an `HICON` from RGBA pixels via a top-down 32bpp DIB section plus a
/// throwaway monochrome mask (the alpha channel is the real mask).
unsafe fn create_hicon(win: &Win, width: i32, height: i32, rgba: &[u8]) -> HICON {
    unsafe {
        let mut header: BITMAPINFOHEADER = std::mem::zeroed();
        header.biSize = std::mem::size_of::<BITMAPINFOHEADER>() as u32;
        header.biWidth = width;
        header.biHeight = -height; // negative = top-down
        header.biPlanes = 1;
        header.biBitCount = 32;
        header.biCompression = BI_RGB;

        let mut bits: *mut c_void = std::ptr::null_mut();
        let color = (win.CreateDIBSection)(
            std::ptr::null_mut(),
            &header,
            DIB_RGB_COLORS,
            &mut bits,
            std::ptr::null_mut(),
            0,
        );
        if color.is_null() || bits.is_null() {
            return std::ptr::null_mut();
        }
        // RGBA -> BGRA, the order the color bitmap expects.
        let dst = bits as *mut u8;
        let pixels = (width as usize) * (height as usize);
        for i in 0..pixels {
            let s = i * 4;
            *dst.add(s) = rgba[s + 2]; // B
            *dst.add(s + 1) = rgba[s + 1]; // G
            *dst.add(s + 2) = rgba[s]; // R
            *dst.add(s + 3) = rgba[s + 3]; // A
        }

        let mask = (win.CreateBitmap)(width, height, 1, 1, std::ptr::null());
        let mut info = ICONINFO {
            fIcon: 1,
            xHotspot: 0,
            yHotspot: 0,
            hbmMask: mask,
            hbmColor: color,
        };
        let icon = (win.CreateIconIndirect)(&mut info);
        (win.DeleteObject)(color);
        (win.DeleteObject)(mask);
        icon
    }
}

/// UTF-16, NUL-terminated — the form the wide Win32 APIs want.
fn wide(s: &str) -> Vec<u16> {
    s.encode_utf16().chain(std::iter::once(0)).collect()
}

/// Copies `s` (UTF-16) into a fixed buffer, always leaving a NUL terminator.
fn copy_wide(dst: &mut [u16], s: &str) {
    let limit = dst.len().saturating_sub(1);
    let mut n = 0;
    for u in s.encode_utf16().take(limit) {
        dst[n] = u;
        n += 1;
    }
    dst[n] = 0;
}