zsui 0.3.0-alpha.2

Rust-first feature-gated native UI framework for Win32, AppKit, and Linux.
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#[derive(Debug, Clone, PartialEq, Eq)]
struct WindowsWin32StatusItemRouteRecord {
    owner: isize,
    callback_message: u32,
    tray_id: u32,
    tooltip: Option<String>,
    icon: Option<isize>,
    menu: MenuSpec,
}

impl WindowsWin32StatusItemRouteRecord {
    fn notify_data(&self) -> NOTIFYICONDATAW {
        tray_notify_data(
            self.owner as HWND,
            self.tray_id,
            self.tooltip.as_deref(),
            self.icon.map(|icon| icon as HICON),
            self.callback_message,
        )
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct WindowsWin32StatusItemCallbackTarget {
    tray_id: u32,
    event_message: u32,
    menu: MenuSpec,
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum WindowsWin32StatusItemCallbackDispatch {
    Ignored,
    Menu(NativeStatusMenuCommandResult),
    Failed(String),
}

static WINDOWS_WIN32_STATUS_ITEM_ROUTES: OnceLock<Mutex<Vec<WindowsWin32StatusItemRouteRecord>>> =
    OnceLock::new();
static WINDOWS_WIN32_TASKBAR_CREATED_MESSAGE: OnceLock<u32> = OnceLock::new();

pub struct WindowsWin32OwnedTrayIcon {
    data: NOTIFYICONDATAW,
    icon: Option<WindowsWin32OwnedIcon>,
    tooltip: Option<String>,
    menu: MenuSpec,
    menu_command_table: WindowsWin32StatusMenuCommandTable,
    active: bool,
}

impl WindowsWin32OwnedTrayIcon {
    pub fn create(
        owner: HWND,
        id: u32,
        request: NativeStatusItemRequest,
        callback_message: u32,
    ) -> ZsuiResult<Self> {
        if owner.is_null() {
            return Err(ZsuiError::invalid_spec(
                "status_item.owner",
                "Win32 tray icon owner window cannot be null",
            ));
        }
        let icon = request
            .icon_path
            .as_deref()
            .map(|path| {
                WindowsWin32OwnedIcon::from_icon_path(
                    path,
                    system_metric(SM_CXSMICON),
                    system_metric(SM_CYSMICON),
                )
            })
            .transpose()?;
        let mut data = tray_notify_data(
            owner,
            id,
            request.tooltip.as_deref(),
            icon.as_ref().map(WindowsWin32OwnedIcon::handle),
            callback_message,
        );
        let created = unsafe { Shell_NotifyIconW(NIM_ADD, &mut data) != 0 };
        if !created {
            return Err(ZsuiError::host(
                "windows_win32_create_tray_icon",
                "Shell_NotifyIconW(NIM_ADD) failed",
            ));
        }
        let menu_command_table = WindowsWin32StatusMenuCommandTable::from_menu(&request.menu);
        let item = Self {
            data,
            icon,
            tooltip: request.tooltip,
            menu: request.menu,
            menu_command_table,
            active: true,
        };
        set_windows_win32_status_item_route(item.route_record());
        Ok(item)
    }

    pub const fn id(&self) -> u32 {
        self.data.uID
    }

    pub const fn owner(&self) -> HWND {
        self.data.hWnd
    }

    pub const fn callback_message(&self) -> u32 {
        self.data.uCallbackMessage
    }

    pub fn menu(&self) -> &MenuSpec {
        &self.menu
    }

    pub fn menu_command_table(&self) -> &WindowsWin32StatusMenuCommandTable {
        &self.menu_command_table
    }

    pub fn has_icon(&self) -> bool {
        self.icon.is_some()
    }

    pub fn set_tooltip(&mut self, tooltip: Option<&str>) -> bool {
        self.try_set_tooltip(tooltip).is_ok()
    }

    /// Updates native and retained tooltip state atomically, returning host failure.
    pub fn try_set_tooltip(&mut self, tooltip: Option<&str>) -> ZsuiResult<()> {
        if !self.active {
            return Err(ZsuiError::host(
                "windows_tray_tooltip",
                "tray icon is closed",
            ));
        }
        let mut data = self.data;
        clear_wide_buffer(&mut data.szTip);
        data.uFlags |= NIF_TIP;
        if let Some(tooltip) = tooltip {
            copy_wide_truncated(tooltip, &mut data.szTip);
        }
        if unsafe { Shell_NotifyIconW(NIM_MODIFY, &mut data) } == 0 {
            return Err(ZsuiError::host(
                "windows_tray_tooltip",
                "Shell_NotifyIconW(NIM_MODIFY) failed",
            ));
        }
        self.data = data;
        self.tooltip = tooltip.map(str::to_owned);
        set_windows_win32_status_item_route(self.route_record());
        Ok(())
    }

    pub fn set_menu(&mut self, menu: MenuSpec) {
        self.menu_command_table = WindowsWin32StatusMenuCommandTable::from_menu(&menu);
        self.menu = menu;
        set_windows_win32_status_item_route(self.route_record());
    }

    pub fn dispatch_native_menu_command(&self, native_id: u32) -> NativeStatusMenuCommandResult {
        self.menu_command_table.resolve_native_command_id(native_id)
    }

    pub fn create_popup_menu(&self) -> ZsuiResult<WindowsWin32OwnedPopupMenu> {
        WindowsWin32OwnedPopupMenu::from_menu(&self.menu)
    }

    pub fn delete(self) -> bool {
        self.try_delete().is_ok()
    }

    /// Explicit cleanup reports failure and never retries the same native deletion in Drop.
    pub fn try_delete(mut self) -> ZsuiResult<()> {
        if self.delete_active() {
            Ok(())
        } else {
            Err(ZsuiError::host(
                "windows_tray_delete",
                "Shell_NotifyIconW(NIM_DELETE) failed",
            ))
        }
    }

    fn delete_active(&mut self) -> bool {
        if !std::mem::replace(&mut self.active, false) {
            return true;
        }
        unsafe { Shell_NotifyIconW(NIM_DELETE, &mut self.data) != 0 }
    }

    fn route_record(&self) -> WindowsWin32StatusItemRouteRecord {
        WindowsWin32StatusItemRouteRecord {
            owner: self.owner() as isize,
            callback_message: self.callback_message(),
            tray_id: self.id(),
            tooltip: self.tooltip.clone(),
            icon: self.icon.as_ref().map(|icon| icon.handle() as isize),
            menu: self.menu.clone(),
        }
    }
}

impl Drop for WindowsWin32OwnedTrayIcon {
    fn drop(&mut self) {
        clear_windows_win32_status_item_route(self.owner(), self.callback_message(), self.id());
        if !self.delete_active() {
            {
                use std::io::Write as _;
                let _ = writeln!(
                    std::io::stderr(),
                    "ZSUI tray cleanup failed: Shell_NotifyIconW(NIM_DELETE)"
                );
            }
        }
    }
}

pub struct WindowsWin32StatusItemHost {
    owner: HWND,
    callback_message: u32,
    next_id: u32,
    items: Vec<WindowsWin32OwnedTrayIcon>,
    operation_log: Vec<NativeStatusItemHostOperation>,
    status_menu_operation_log: Vec<NativeStatusMenuCommandHostOperation>,
    last_error: Option<String>,
}

impl WindowsWin32StatusItemHost {
    pub fn new(owner: HWND) -> Self {
        Self::with_callback_message(owner, ZSUI_WIN32_TRAY_CALLBACK_MESSAGE)
    }

    pub fn with_callback_message(owner: HWND, callback_message: u32) -> Self {
        Self {
            owner,
            callback_message,
            next_id: 1,
            items: Vec::new(),
            operation_log: Vec::new(),
            status_menu_operation_log: Vec::new(),
            last_error: None,
        }
    }

    pub const fn owner(&self) -> HWND {
        self.owner
    }

    pub fn operation_log(&self) -> &[NativeStatusItemHostOperation] {
        &self.operation_log
    }

    pub fn status_menu_operation_log(&self) -> &[NativeStatusMenuCommandHostOperation] {
        &self.status_menu_operation_log
    }

    pub fn item_count(&self) -> usize {
        self.items.len()
    }

    pub fn first_native_menu_command_id(&self, status_item_index: usize) -> Option<u32> {
        self.items
            .get(status_item_index)
            .and_then(|item| item.menu_command_table().first_native_id())
    }

    pub fn native_menu_command_count(&self, status_item_index: usize) -> usize {
        self.items
            .get(status_item_index)
            .map(|item| item.menu_command_table().entry_count())
            .unwrap_or(0)
    }

    pub fn dispatch_native_menu_command(
        &mut self,
        status_item_index: usize,
        native_command_id: u32,
    ) -> NativeStatusMenuCommandResult {
        self.status_menu_operation_log
            .push(NativeStatusMenuCommandHostOperation::DispatchStatusMenuCommand);
        self.items
            .get(status_item_index)
            .map(|item| item.dispatch_native_menu_command(native_command_id))
            .unwrap_or(NativeStatusMenuCommandResult::NotFound)
    }

    pub fn create_popup_menu_for_status_item(
        &self,
        status_item_index: usize,
    ) -> ZsuiResult<WindowsWin32OwnedPopupMenu> {
        self.items
            .get(status_item_index)
            .ok_or_else(|| {
                ZsuiError::invalid_spec(
                    "status_item_index",
                    "Win32 status item index does not exist",
                )
            })?
            .create_popup_menu()
    }

    pub fn present_status_item_menu_at(
        &mut self,
        status_item_index: usize,
        x: i32,
        y: i32,
    ) -> ZsuiResult<NativeStatusMenuCommandResult> {
        self.status_menu_operation_log
            .push(NativeStatusMenuCommandHostOperation::DispatchStatusMenuCommand);
        let popup = self.create_popup_menu_for_status_item(status_item_index)?;
        popup.present_at(self.owner, x, y)
    }

    pub fn present_status_item_menu_at_cursor(
        &mut self,
        status_item_index: usize,
    ) -> ZsuiResult<NativeStatusMenuCommandResult> {
        let mut point = POINT { x: 0, y: 0 };
        let ok = unsafe { GetCursorPos(&mut point) != 0 };
        if !ok {
            return Err(ZsuiError::host(
                "windows_win32_status_popup_menu_position",
                "GetCursorPos failed",
            ));
        }
        self.present_status_item_menu_at(status_item_index, point.x, point.y)
    }

    pub fn last_error(&self) -> Option<&str> {
        self.last_error.as_deref()
    }

    fn record(&mut self, operation: NativeStatusItemHostOperation) {
        self.operation_log.push(operation);
    }
}

impl NativeStatusItemHost for WindowsWin32StatusItemHost {
    type Handle = u32;

    fn create_status_item(
        &mut self,
        request: NativeStatusItemRequest,
    ) -> NativeStatusItemPresentation<Self::Handle> {
        self.record(NativeStatusItemHostOperation::CreateStatusItem);
        self.last_error = None;
        let id = self.next_id;
        self.next_id = self.next_id.saturating_add(1).max(1);
        match WindowsWin32OwnedTrayIcon::create(self.owner, id, request, self.callback_message) {
            Ok(item) => {
                self.items.push(item);
                NativeStatusItemPresentation::Created(id)
            }
            Err(err) => {
                self.last_error = Some(err.to_string());
                NativeStatusItemPresentation::Failed
            }
        }
    }

    fn set_status_item_tooltip(&mut self, handle: Self::Handle, tooltip: Option<String>) {
        self.record(NativeStatusItemHostOperation::SetStatusItemTooltip);
        self.last_error = match self.items.iter_mut().find(|item| item.id() == handle) {
            Some(item) => item
                .try_set_tooltip(tooltip.as_deref())
                .err()
                .map(|error| error.to_string()),
            None => Some(format!("unknown tray icon {handle}")),
        };
    }

    fn set_status_item_menu(&mut self, handle: Self::Handle, menu: MenuSpec) {
        self.record(NativeStatusItemHostOperation::SetStatusItemMenu);
        if let Some(item) = self.items.iter_mut().find(|item| item.id() == handle) {
            item.set_menu(menu);
        }
    }

    fn destroy_status_item(&mut self, handle: Self::Handle) {
        self.record(NativeStatusItemHostOperation::DestroyStatusItem);
        self.last_error =
            if let Some(index) = self.items.iter().position(|item| item.id() == handle) {
                self.items
                    .remove(index)
                    .try_delete()
                    .err()
                    .map(|error| error.to_string())
            } else {
                Some(format!("unknown tray icon {handle}"))
            };
    }
}

impl NativeStatusMenuCommandHost for WindowsWin32StatusItemHost {
    fn dispatch_status_menu_command(
        &mut self,
        request: NativeStatusMenuCommandRequest,
    ) -> NativeStatusMenuCommandResult {
        self.status_menu_operation_log
            .push(NativeStatusMenuCommandHostOperation::DispatchStatusMenuCommand);
        let Some(item) = self.items.get(request.status_item_index) else {
            return NativeStatusMenuCommandResult::NotFound;
        };
        native_status_menu_command_from_menu(item.menu(), &request)
    }
}

fn windows_win32_status_item_routes() -> &'static Mutex<Vec<WindowsWin32StatusItemRouteRecord>> {
    WINDOWS_WIN32_STATUS_ITEM_ROUTES.get_or_init(|| Mutex::new(Vec::new()))
}

fn set_windows_win32_status_item_route(route: WindowsWin32StatusItemRouteRecord) {
    let mut routes = windows_win32_status_item_routes()
        .lock()
        .expect("Win32 status item route registry should not be poisoned");
    routes.retain(|candidate| {
        candidate.owner != route.owner
            || candidate.callback_message != route.callback_message
            || candidate.tray_id != route.tray_id
    });
    routes.push(route);
}

fn clear_windows_win32_status_item_route(owner: HWND, callback_message: u32, tray_id: u32) {
    let owner = owner as isize;
    windows_win32_status_item_routes()
        .lock()
        .expect("Win32 status item route registry should not be poisoned")
        .retain(|route| {
            route.owner != owner
                || route.callback_message != callback_message
                || route.tray_id != tray_id
        });
}

fn clear_windows_win32_status_item_routes_for_owner(owner: HWND) {
    let owner = owner as isize;
    windows_win32_status_item_routes()
        .lock()
        .expect("Win32 status item route registry should not be poisoned")
        .retain(|route| route.owner != owner);
}

fn clear_windows_win32_status_item_routes() {
    windows_win32_status_item_routes()
        .lock()
        .expect("Win32 status item route registry should not be poisoned")
        .clear();
}

fn windows_win32_status_item_callback_target(
    owner: HWND,
    callback_message: u32,
    wparam: WPARAM,
    lparam: LPARAM,
) -> Option<WindowsWin32StatusItemCallbackTarget> {
    if owner.is_null() {
        return None;
    }
    let owner = owner as isize;
    let tray_id = wparam as u32;
    let event_message = lparam as u32;
    windows_win32_status_item_routes()
        .lock()
        .expect("Win32 status item route registry should not be poisoned")
        .iter()
        .find(|route| {
            route.owner == owner
                && route.callback_message == callback_message
                && route.tray_id == tray_id
        })
        .map(|route| WindowsWin32StatusItemCallbackTarget {
            tray_id,
            event_message,
            menu: route.menu.clone(),
        })
}

fn dispatch_windows_win32_status_item_callback(
    owner: HWND,
    callback_message: u32,
    wparam: WPARAM,
    lparam: LPARAM,
) -> Option<WindowsWin32StatusItemCallbackDispatch> {
    let target =
        windows_win32_status_item_callback_target(owner, callback_message, wparam, lparam)?;
    if !matches!(target.event_message, WM_RBUTTONUP | WM_CONTEXTMENU) {
        return Some(WindowsWin32StatusItemCallbackDispatch::Ignored);
    }
    let popup = match WindowsWin32OwnedPopupMenu::from_menu(&target.menu) {
        Ok(popup) => popup,
        Err(err) => {
            return Some(WindowsWin32StatusItemCallbackDispatch::Failed(
                err.to_string(),
            ));
        }
    };
    let mut point = POINT { x: 0, y: 0 };
    if unsafe { GetCursorPos(&mut point) } == 0 {
        return Some(WindowsWin32StatusItemCallbackDispatch::Failed(
            "GetCursorPos failed".to_string(),
        ));
    }
    match popup.present_at(owner, point.x, point.y) {
        Ok(result) => {
            if let NativeStatusMenuCommandResult::Dispatched(command) = &result {
                dispatch_windows_win32_app_command(owner, command.clone());
            }
            Some(WindowsWin32StatusItemCallbackDispatch::Menu(result))
        }
        Err(err) => Some(WindowsWin32StatusItemCallbackDispatch::Failed(
            err.to_string(),
        )),
    }
}

fn windows_win32_taskbar_created_message() -> u32 {
    *WINDOWS_WIN32_TASKBAR_CREATED_MESSAGE.get_or_init(|| {
        let message_name = wide_null("TaskbarCreated");
        unsafe { RegisterWindowMessageW(message_name.as_ptr()) }
    })
}

fn restore_windows_win32_status_items(owner: HWND) -> usize {
    if owner.is_null() {
        return 0;
    }
    let routes = windows_win32_status_item_routes()
        .lock()
        .expect("Win32 status item route registry should not be poisoned")
        .iter()
        .filter(|route| route.owner == owner as isize)
        .cloned()
        .collect::<Vec<_>>();
    routes
        .into_iter()
        .filter(|route| {
            let mut data = route.notify_data();
            unsafe { Shell_NotifyIconW(NIM_ADD, &mut data) != 0 }
        })
        .count()
}

fn tray_notify_data(
    owner: HWND,
    id: u32,
    tooltip: Option<&str>,
    icon: Option<HICON>,
    callback_message: u32,
) -> NOTIFYICONDATAW {
    let mut data: NOTIFYICONDATAW = unsafe { zeroed() };
    data.cbSize = size_of::<NOTIFYICONDATAW>() as u32;
    data.hWnd = owner;
    data.uID = id;
    data.uFlags = NIF_MESSAGE;
    data.uCallbackMessage = callback_message;
    if let Some(tooltip) = tooltip {
        data.uFlags |= NIF_TIP;
        copy_wide_truncated(tooltip, &mut data.szTip);
    }
    if let Some(icon) = icon {
        data.uFlags |= NIF_ICON;
        data.hIcon = icon;
    }
    data
}

fn copy_wide_truncated(value: &str, target: &mut [u16]) {
    if target.is_empty() {
        return;
    }
    clear_wide_buffer(target);
    let max_chars = target.len().saturating_sub(1);
    for (slot, value) in target.iter_mut().take(max_chars).zip(value.encode_utf16()) {
        *slot = value;
    }
}

fn clear_wide_buffer(target: &mut [u16]) {
    for value in target {
        *value = 0;
    }
}

#[cfg(test)]
mod tray_error_tests {
    use super::*;
    #[test]
    fn explicit_tray_failures_preserve_state_and_cleanup_is_idempotent() {
        let menu = MenuSpec::new();
        let mut icon = WindowsWin32OwnedTrayIcon {
            data: unsafe { zeroed() },
            icon: None,
            tooltip: Some("retained".into()),
            menu_command_table: WindowsWin32StatusMenuCommandTable::from_menu(&menu),
            menu,
            active: true,
        };
        assert!(icon.try_set_tooltip(Some("rejected")).is_err());
        assert_eq!(icon.tooltip.as_deref(), Some("retained"));
        assert!(!icon.delete_active());
        assert!(!icon.active);
        assert!(icon.delete_active());
        let mut host = WindowsWin32StatusItemHost::new(null_mut());
        host.set_status_item_tooltip(99, Some("missing".into()));
        assert!(host.last_error().unwrap().contains("99"));
        host.destroy_status_item(99);
        assert!(host.last_error().unwrap().contains("99"));
    }
}