strata-ui-platform 0.1.5

Native Hardware Abstraction Layer for Android, iOS, macOS, Windows, Linux, and Web
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
//! Desktop System Tray abstraction (`TrayIcon`, `TrayMenu`, `TrayMenuItem`).
//!
//! On Linux the live backend talks to the Ayatana AppIndicator library through
//! runtime dynamic loading (`dlopen`/`dlsym`). The library is never a build-time
//! or hard runtime dependency: when it is missing, or when the caller is not on
//! the GTK main thread, the handle falls back to the deterministic in-process
//! model and reports `backend_name() == "handle-only"`.

/// An individual item or separator inside a system tray context menu.
#[derive(Debug, Clone, PartialEq)]
pub struct TrayMenuItem {
    pub id: u64,
    pub label: String,
    pub enabled: bool,
    pub checked: bool,
    pub is_separator: bool,
    pub children: Vec<TrayMenuItem>,
}

impl TrayMenuItem {
    /// Creates a standard clickable menu item.
    pub fn item(id: u64, label: impl Into<String>) -> Self {
        Self {
            id,
            label: label.into(),
            enabled: true,
            checked: false,
            is_separator: false,
            children: Vec::new(),
        }
    }

    /// Creates a horizontal separator.
    pub fn separator() -> Self {
        Self {
            id: 0,
            label: String::new(),
            enabled: false,
            checked: false,
            is_separator: true,
            children: Vec::new(),
        }
    }

    /// Creates a parent item containing nested sub-menu items.
    pub fn submenu(id: u64, label: impl Into<String>, children: Vec<TrayMenuItem>) -> Self {
        Self {
            id,
            label: label.into(),
            enabled: true,
            checked: false,
            is_separator: false,
            children,
        }
    }

    /// Sets the checked state of this item.
    pub fn with_checked(mut self, checked: bool) -> Self {
        self.checked = checked;
        self
    }

    /// Sets the enabled state of this item.
    pub fn with_enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }
}

/// A context menu attached to a desktop system tray icon.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct TrayMenu {
    pub items: Vec<TrayMenuItem>,
}

impl TrayMenu {
    /// Creates an empty tray menu.
    pub fn new() -> Self {
        Self { items: Vec::new() }
    }

    /// Appends a clickable menu item.
    pub fn add_item(&mut self, id: u64, label: impl Into<String>) -> &mut Self {
        self.items.push(TrayMenuItem::item(id, label));
        self
    }

    /// Appends a separator.
    pub fn add_separator(&mut self) -> &mut Self {
        self.items.push(TrayMenuItem::separator());
        self
    }

    /// Appends a submenu.
    pub fn add_submenu(
        &mut self,
        id: u64,
        label: impl Into<String>,
        children: Vec<TrayMenuItem>,
    ) -> &mut Self {
        self.items.push(TrayMenuItem::submenu(id, label, children));
        self
    }

    /// Recursively searches for an item by ID.
    pub fn find_item(&self, id: u64) -> Option<&TrayMenuItem> {
        Self::find_in_slice(&self.items, id)
    }

    /// Recursively searches for a mutable item by ID.
    pub fn find_item_mut(&mut self, id: u64) -> Option<&mut TrayMenuItem> {
        Self::find_in_slice_mut(&mut self.items, id)
    }

    fn find_in_slice(items: &[TrayMenuItem], id: u64) -> Option<&TrayMenuItem> {
        for item in items {
            if !item.is_separator && item.id == id {
                return Some(item);
            }
            if let Some(found) = Self::find_in_slice(&item.children, id) {
                return Some(found);
            }
        }
        None
    }

    fn find_in_slice_mut(items: &mut [TrayMenuItem], id: u64) -> Option<&mut TrayMenuItem> {
        for item in items {
            if !item.is_separator && item.id == id {
                return Some(item);
            }
            if let Some(found) = Self::find_in_slice_mut(&mut item.children, id) {
                return Some(found);
            }
        }
        None
    }
}

/// System tray icon abstraction.
#[derive(Debug, Clone)]
pub struct TrayIcon {
    pub tooltip: String,
    pub icon_rgba: Vec<u8>,
    pub icon_width: u32,
    pub icon_height: u32,
    pub menu: Option<TrayMenu>,
    pub visible: bool,
    pub click_count: usize,
    pub last_action_id: Option<u64>,
    pub badge: Option<String>,
}

impl TrayIcon {
    /// Creates a new system tray icon.
    pub fn new(
        tooltip: impl Into<String>,
        icon_rgba: Vec<u8>,
        icon_width: u32,
        icon_height: u32,
    ) -> Self {
        Self {
            tooltip: tooltip.into(),
            icon_rgba,
            icon_width,
            icon_height,
            menu: None,
            visible: true,
            click_count: 0,
            last_action_id: None,
            badge: None,
        }
    }

    /// Sets the notification badge text (e.g. "99+" or "NEW").
    pub fn set_badge(&mut self, badge: Option<String>) {
        self.badge = badge;
    }

    /// Sets the notification badge as a numeric count (or clears it if None or 0).
    pub fn set_badge_count(&mut self, count: Option<u32>) {
        self.badge = count.and_then(|c| if c > 0 { Some(c.to_string()) } else { None });
    }

    /// Returns current badge text if present.
    pub fn badge(&self) -> Option<&str> {
        self.badge.as_deref()
    }

    /// Sets the hover tooltip text.
    pub fn set_tooltip(&mut self, tooltip: impl Into<String>) {
        self.tooltip = tooltip.into();
    }

    /// Sets or clears the tray context menu.
    pub fn set_menu(&mut self, menu: Option<TrayMenu>) {
        self.menu = menu;
    }

    /// Sets whether the icon is visible in the status bar/system tray.
    pub fn set_visible(&mut self, visible: bool) {
        self.visible = visible;
    }

    /// Simulates or executes a primary click on the tray icon.
    pub fn trigger_click(&mut self) {
        self.click_count += 1;
    }

    /// Simulates or executes a menu item action by ID. Returns true if the item was found and enabled.
    pub fn trigger_menu_action(&mut self, id: u64) -> bool {
        if let Some(menu) = &self.menu {
            if let Some(item) = menu.find_item(id) {
                if item.enabled {
                    self.last_action_id = Some(id);
                    return true;
                }
            }
        }
        false
    }

    /// Spawns an OS-native system tray handle dispatching events through the provided event callback.
    ///
    /// On Linux the native indicator is created immediately when this is called
    /// on the GTK main thread after `gtk::init()`. Callers on any other thread,
    /// and callers where the Ayatana AppIndicator library is absent, receive a
    /// handle-only controller that still dispatches events from the in-process
    /// model.
    pub fn spawn_native<F>(self, event_sink: F) -> NativeTrayHandle
    where
        F: Fn(crate::events::PlatformEvent) + Send + Sync + 'static,
    {
        NativeTrayHandle::new(self, std::sync::Arc::new(event_sink))
    }

    /// Spawns a deterministic in-process tray controller (`backend_name() == "handle-only"`)
    /// without attempting OS tray registration.
    pub fn spawn_handle_only<F>(self, event_sink: F) -> NativeTrayHandle
    where
        F: Fn(crate::events::PlatformEvent) + Send + Sync + 'static,
    {
        NativeTrayHandle::new_handle_only(self, std::sync::Arc::new(event_sink))
    }
}

/// Platform-agnostic system tray interaction event.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TrayEvent {
    LeftClick,
    RightClick,
    DoubleClick,
    MenuItemTriggered(u64),
}

/// Thread-safe reactive event channel for desktop system tray interactions.
#[derive(Clone)]
pub struct TrayEventChannel {
    sender: std::sync::mpsc::Sender<TrayEvent>,
}

/// Receiving endpoint for desktop system tray events.
pub struct TrayEventReceiver {
    receiver: std::sync::mpsc::Receiver<TrayEvent>,
}

impl TrayEventChannel {
    /// Creates a new sender and receiver pair.
    pub fn new() -> (Self, TrayEventReceiver) {
        let (sender, receiver) = std::sync::mpsc::channel();
        (Self { sender }, TrayEventReceiver { receiver })
    }

    /// Dispatches a tray event down the channel.
    pub fn dispatch(&self, event: TrayEvent) {
        let _ = self.sender.send(event);
    }

    /// Creates an adapter closure suitable for passing as a `TrayEventSink`.
    pub fn as_sink(&self) -> TrayEventSink {
        let sender = self.sender.clone();
        std::sync::Arc::new(move |event| match event {
            crate::events::PlatformEvent::TrayIconClicked => {
                let _ = sender.send(TrayEvent::LeftClick);
            }
            crate::events::PlatformEvent::TrayMenuItemClicked(id) => {
                let _ = sender.send(TrayEvent::MenuItemTriggered(id));
            }
            _ => {}
        })
    }
}

impl Default for TrayEventChannel {
    fn default() -> Self {
        Self::new().0
    }
}

impl TrayEventReceiver {
    /// Attempts to read the next pending tray event without blocking.
    pub fn try_recv(&self) -> Option<TrayEvent> {
        self.receiver.try_recv().ok()
    }

    /// Blocks until the next tray event arrives, or times out.
    pub fn recv_timeout(
        &self,
        timeout: std::time::Duration,
    ) -> Result<TrayEvent, std::sync::mpsc::RecvTimeoutError> {
        self.receiver.recv_timeout(timeout)
    }
}

/// Pure-Rust Linux D-Bus StatusNotifierItem daemon registration.
#[cfg(target_os = "linux")]
pub struct DbusStatusNotifier {
    pub service_name: String,
    pub title: String,
    pub icon_name: String,
    pub client: Option<crate::dbus::DbusClient>,
}

#[cfg(target_os = "linux")]
impl DbusStatusNotifier {
    /// Creates and registers an authentic `org.kde.StatusNotifierItem` over session D-Bus.
    pub fn register(title: &str, icon_name: &str) -> Result<Self, String> {
        let mut client = crate::dbus::DbusClient::connect()?;
        let service_name = format!("org.kde.StatusNotifierItem-{}-1", std::process::id());

        // Register service with org.kde.StatusNotifierWatcher
        let mut body = crate::dbus::DbusMessageEncoder::new();
        body.push_string(&service_name);
        let _ = client.call_method(
            "org.kde.StatusNotifierWatcher",
            "/StatusNotifierWatcher",
            "org.kde.StatusNotifierWatcher",
            "RegisterStatusNotifierItem",
            Some("s"),
            body.as_bytes(),
        );

        Ok(Self {
            service_name,
            title: title.to_string(),
            icon_name: icon_name.to_string(),
            client: Some(client),
        })
    }
}

/// Thread-safe tray event sink callback.
pub type TrayEventSink =
    std::sync::Arc<dyn Fn(crate::events::PlatformEvent) + Send + Sync + 'static>;

/// Shared menu and event state used by the handle and by native menu callbacks.
struct SharedTrayState {
    event_sink: TrayEventSink,
    menu: std::sync::Mutex<Option<TrayMenu>>,
}

impl SharedTrayState {
    fn new(event_sink: TrayEventSink, menu: Option<TrayMenu>) -> Self {
        Self {
            event_sink,
            menu: std::sync::Mutex::new(menu),
        }
    }

    fn set_menu(&self, menu: Option<TrayMenu>) {
        let mut slot = match self.menu.lock() {
            Ok(slot) => slot,
            Err(poisoned) => poisoned.into_inner(),
        };
        *slot = menu;
    }

    fn dispatch_click(&self) {
        (self.event_sink)(crate::events::PlatformEvent::TrayIconClicked);
    }

    fn dispatch_menu_action(&self, id: u64) -> bool {
        let enabled = {
            let slot = match self.menu.lock() {
                Ok(slot) => slot,
                Err(poisoned) => poisoned.into_inner(),
            };
            slot.as_ref()
                .and_then(|menu| menu.find_item(id))
                .map(|item| item.enabled)
                .unwrap_or(false)
        };
        if !enabled {
            return false;
        }
        (self.event_sink)(crate::events::PlatformEvent::TrayMenuItemClicked(id));
        true
    }
}

/// Operating-system native desktop system tray controller.
pub struct NativeTrayHandle {
    icon: TrayIcon,
    shared: std::sync::Arc<SharedTrayState>,
    #[cfg(target_os = "linux")]
    live: Option<linux_backend::LiveIndicator>,
    #[cfg(windows)]
    live: Option<windows_backend::LiveTrayIcon>,
    #[cfg(target_os = "macos")]
    live: Option<macos_backend::LiveStatusItem>,
}

impl NativeTrayHandle {
    /// Creates and registers an OS-native system tray icon.
    ///
    /// See [`TrayIcon::spawn_native`] for the Linux main-thread contract.
    pub fn new(icon: TrayIcon, event_sink: TrayEventSink) -> Self {
        let shared = std::sync::Arc::new(SharedTrayState::new(event_sink, icon.menu.clone()));
        #[cfg(target_os = "linux")]
        let live = linux_backend::LiveIndicator::new(&icon, shared.clone());
        #[cfg(windows)]
        let live = windows_backend::LiveTrayIcon::new(&icon, shared.clone());
        #[cfg(target_os = "macos")]
        let live = macos_backend::LiveStatusItem::new(&icon, shared.clone());
        Self {
            icon,
            shared,
            #[cfg(target_os = "linux")]
            live,
            #[cfg(windows)]
            live,
            #[cfg(target_os = "macos")]
            live,
        }
    }

    /// Creates a deterministic handle-only tray controller without attempting OS registration.
    pub fn new_handle_only(icon: TrayIcon, event_sink: TrayEventSink) -> Self {
        let shared = std::sync::Arc::new(SharedTrayState::new(event_sink, icon.menu.clone()));
        Self {
            icon,
            shared,
            #[cfg(target_os = "linux")]
            live: None,
            #[cfg(windows)]
            live: None,
            #[cfg(target_os = "macos")]
            live: None,
        }
    }

    /// Returns the active native backend identifier.
    ///
    /// - `"ayatana-appindicator"`: Registered via Linux Ayatana AppIndicator
    /// - `"win32-tray"`: Registered via Windows Shell_NotifyIconW
    /// - `"cocoa-nsstatusbar"`: Registered via macOS NSStatusBar
    /// - `"handle-only"`: Fallback in-process controller
    pub fn backend_name(&self) -> &'static str {
        #[cfg(target_os = "linux")]
        {
            if self.live.is_some() {
                "ayatana-appindicator"
            } else {
                "handle-only"
            }
        }
        #[cfg(windows)]
        {
            if self.live.is_some() {
                "win32-tray"
            } else {
                "handle-only"
            }
        }
        #[cfg(target_os = "macos")]
        {
            if self.live.is_some() {
                "cocoa-nsstatusbar"
            } else {
                "handle-only"
            }
        }
        #[cfg(not(any(target_os = "linux", windows, target_os = "macos")))]
        {
            "handle-only"
        }
    }

    /// Updates tooltip text on the system tray icon.
    pub fn set_tooltip(&mut self, tooltip: impl Into<String>) {
        self.icon.set_tooltip(tooltip);
        #[cfg(target_os = "linux")]
        {
            let tooltip = self.icon.tooltip.clone();
            if let Some(live) = &mut self.live {
                live.set_tooltip(tooltip);
            }
        }
        #[cfg(windows)]
        {
            let tooltip = self.icon.tooltip.clone();
            if let Some(live) = &mut self.live {
                live.set_tooltip(tooltip);
            }
        }
        #[cfg(target_os = "macos")]
        {
            let tooltip = self.icon.tooltip.clone();
            if let Some(live) = &mut self.live {
                live.set_tooltip(tooltip);
            }
        }
    }

    /// Sets or updates the tray context menu.
    pub fn set_menu(&mut self, menu: Option<TrayMenu>) {
        self.icon.set_menu(menu.clone());
        self.shared.set_menu(menu.clone());
        #[cfg(target_os = "linux")]
        if let Some(live) = &mut self.live {
            live.set_menu(menu.as_ref());
        }
        #[cfg(windows)]
        if let Some(live) = &mut self.live {
            live.set_menu(menu.as_ref());
        }
        #[cfg(target_os = "macos")]
        if let Some(live) = &mut self.live {
            live.set_menu(menu.as_ref());
        }
    }

    /// Sets visibility of the system tray icon.
    pub fn set_visible(&mut self, visible: bool) {
        self.icon.set_visible(visible);
        #[cfg(target_os = "linux")]
        if let Some(live) = &mut self.live {
            live.set_visible(visible);
        }
        #[cfg(windows)]
        if let Some(live) = &mut self.live {
            live.set_visible(visible);
        }
        #[cfg(target_os = "macos")]
        if let Some(live) = &mut self.live {
            live.set_visible(visible);
        }
    }

    /// Sets or clears the notification badge on the tray icon.
    pub fn set_badge(&mut self, badge: Option<String>) {
        self.icon.set_badge(badge.clone());
        #[cfg(target_os = "linux")]
        if let Some(live) = &mut self.live {
            live.set_badge(badge);
        }
        #[cfg(windows)]
        if let Some(live) = &mut self.live {
            live.set_badge(badge);
        }
        #[cfg(target_os = "macos")]
        if let Some(live) = &mut self.live {
            live.set_badge(badge);
        }
    }

    /// Dispatches a simulated or native tray icon click event.
    pub fn trigger_click(&mut self) {
        self.icon.trigger_click();
        self.shared.dispatch_click();
    }

    /// Dispatches a simulated or native menu action event.
    pub fn trigger_menu_action(&mut self, id: u64) -> bool {
        self.shared.set_menu(self.icon.menu.clone());
        if self.shared.dispatch_menu_action(id) {
            self.icon.last_action_id = Some(id);
            true
        } else {
            false
        }
    }

    /// Returns a reference to the underlying `TrayIcon` state.
    pub fn icon(&self) -> &TrayIcon {
        &self.icon
    }

    /// Returns a mutable reference to the underlying `TrayIcon` state.
    pub fn icon_mut(&mut self) -> &mut TrayIcon {
        &mut self.icon
    }
}

#[cfg(all(target_os = "linux", feature = "gtk-compat"))]
mod linux_backend {
    //! Ayatana AppIndicator backend loaded at runtime.
    //!
    //! Lifecycle and threading contract:
    //! - `NativeTrayHandle::new` must be called on the GTK main thread after
    //!   `gtk::init()`; otherwise no indicator is created and the handle reports
    //!   `backend_name() == "handle-only"`.
    //! - Ayatana AppIndicator owns the `GtkMenu` lifecycle. The Rust side keeps a
    //!   strong reference to the same menu so the widget cannot be collected
    //!   before the indicator releases it.
    //! - The library handle is loaded once per process and intentionally kept for
    //!   the process lifetime because GObject type registration cannot be safely
    //!   unloaded while the type system still references the library.
    //! - AppIndicator reports activation by exporting its menu over DBus to the
    //!   panel; this version of the library does not deliver a primary-click
    //!   callback itself. The backend connects the root `GtkMenu` `show` signal
    //!   to `TrayIconClicked` once per open as the closest available mapping.
    //!   On desktops that render the exported DBusMenu without showing the
    //!   `GtkMenu`, the signal may not fire and clicks then arrive only through
    //!   the in-process `trigger_click` controller.

    use super::{SharedTrayState, TrayIcon, TrayMenu, TrayMenuItem};
    use gtk::glib::translate::ToGlibPtr;
    use std::ffi::{c_char, c_int, c_void, CString};
    use std::path::PathBuf;
    use std::sync::atomic::{AtomicU64, Ordering};
    use std::sync::{Arc, OnceLock};

    type Indicator = c_void;

    const RTLD_NOW: c_int = 2;
    const APP_INDICATOR_CATEGORY_APPLICATION_STATUS: c_int = 0;
    const APP_INDICATOR_STATUS_PASSIVE: c_int = 0;
    const APP_INDICATOR_STATUS_ACTIVE: c_int = 1;

    type NewFn = unsafe extern "C" fn(*const c_char, *const c_char, c_int) -> *mut Indicator;
    type SetStatusFn = unsafe extern "C" fn(*mut Indicator, c_int);
    type SetIconFullFn = unsafe extern "C" fn(*mut Indicator, *const c_char, *const c_char);
    type SetMenuFn = unsafe extern "C" fn(*mut Indicator, *mut c_void);
    type SetTitleFn = unsafe extern "C" fn(*mut Indicator, *const c_char);
    type SetLabelFn = unsafe extern "C" fn(*mut Indicator, *const c_char, *const c_char);

    extern "C" {
        fn dlopen(filename: *const c_char, flag: c_int) -> *mut c_void;
        fn dlsym(handle: *mut c_void, symbol: *const c_char) -> *mut c_void;
    }

    struct Api {
        new: NewFn,
        set_status: SetStatusFn,
        set_icon_full: SetIconFullFn,
        set_menu: SetMenuFn,
        set_title: SetTitleFn,
        set_label: SetLabelFn,
    }

    static API: OnceLock<Option<Api>> = OnceLock::new();

    fn api() -> Option<&'static Api> {
        API.get_or_init(Api::load).as_ref()
    }

    impl Api {
        fn load() -> Option<Self> {
            const CANDIDATES: [&[u8]; 2] = [
                b"libayatana-appindicator3.so.1\0",
                b"libappindicator3.so.1\0",
            ];
            for candidate in CANDIDATES {
                let handle = unsafe { dlopen(candidate.as_ptr() as *const c_char, RTLD_NOW) };
                if handle.is_null() {
                    continue;
                }
                unsafe {
                    let new: NewFn = match resolve(handle, b"app_indicator_new\0") {
                        Some(symbol) => symbol,
                        None => continue,
                    };
                    let set_status: SetStatusFn =
                        match resolve(handle, b"app_indicator_set_status\0") {
                            Some(symbol) => symbol,
                            None => continue,
                        };
                    let set_icon_full: SetIconFullFn =
                        match resolve(handle, b"app_indicator_set_icon_full\0") {
                            Some(symbol) => symbol,
                            None => continue,
                        };
                    let set_menu: SetMenuFn = match resolve(handle, b"app_indicator_set_menu\0") {
                        Some(symbol) => symbol,
                        None => continue,
                    };
                    let set_title: SetTitleFn = match resolve(handle, b"app_indicator_set_title\0")
                    {
                        Some(symbol) => symbol,
                        None => continue,
                    };
                    let set_label: SetLabelFn = match resolve(handle, b"app_indicator_set_label\0")
                    {
                        Some(symbol) => symbol,
                        None => continue,
                    };
                    return Some(Self {
                        new,
                        set_status,
                        set_icon_full,
                        set_menu,
                        set_title,
                        set_label,
                    });
                }
            }
            None
        }
    }

    unsafe fn resolve<T: Copy>(handle: *mut c_void, name: &[u8]) -> Option<T> {
        let symbol = dlsym(handle, name.as_ptr() as *const c_char);
        if symbol.is_null() {
            None
        } else {
            Some(std::mem::transmute_copy(&symbol))
        }
    }

    static ICON_COUNTER: AtomicU64 = AtomicU64::new(0);

    pub(super) struct LiveIndicator {
        api: &'static Api,
        indicator: *mut Indicator,
        menu: Option<gtk::Menu>,
        icon_path: Option<PathBuf>,
        shared: Arc<SharedTrayState>,
        tooltip: String,
        visible: bool,
        badge: Option<String>,
    }

    impl LiveIndicator {
        pub(super) fn new(icon: &TrayIcon, shared: Arc<SharedTrayState>) -> Option<Self> {
            if !gtk::is_initialized() || !gtk::is_initialized_main_thread() {
                return None;
            }
            let api = api()?;
            let icon_path = write_icon_png(icon);
            let id = CString::new(indicator_id()).ok()?;
            let icon_name = match &icon_path {
                Some(path) => CString::new(path.to_string_lossy().as_bytes()).ok()?,
                None => CString::new("application-x-executable").ok()?,
            };
            let indicator = unsafe {
                (api.new)(
                    id.as_ptr(),
                    icon_name.as_ptr(),
                    APP_INDICATOR_CATEGORY_APPLICATION_STATUS,
                )
            };
            if indicator.is_null() {
                remove_icon_file(icon_path.as_ref());
                return None;
            }

            let mut live = Self {
                api,
                indicator,
                menu: None,
                icon_path,
                shared,
                tooltip: icon.tooltip.clone(),
                visible: icon.visible,
                badge: icon.badge.clone(),
            };
            live.apply_tooltip();
            live.apply_status();
            live.apply_badge();
            if let Some(menu) = &icon.menu {
                live.set_menu(Some(menu));
            }
            Some(live)
        }

        pub(super) fn set_tooltip(&mut self, tooltip: String) {
            self.tooltip = tooltip;
            self.apply_tooltip();
        }

        pub(super) fn set_visible(&mut self, visible: bool) {
            self.visible = visible;
            self.apply_status();
        }

        pub(super) fn set_badge(&mut self, badge: Option<String>) {
            self.badge = badge;
            self.apply_badge();
        }

        pub(super) fn set_menu(&mut self, menu: Option<&TrayMenu>) {
            // AppIndicator asserts that the menu pointer is a valid GtkMenu, so
            // clearing a menu is represented by an empty menu rather than null.
            let (gtk_menu, model) = match menu {
                Some(model) => (build_menu(model, &self.shared), Some(model.clone())),
                None => (
                    build_menu(&TrayMenu::new(), &self.shared),
                    Some(TrayMenu::new()),
                ),
            };
            let menu_ptr: *mut gtk::ffi::GtkMenu = gtk_menu.to_glib_none().0;
            self.shared.set_menu(model);
            unsafe {
                (self.api.set_menu)(self.indicator, menu_ptr as *mut c_void);
            }
            self.menu = Some(gtk_menu);
        }

        fn apply_tooltip(&self) {
            let Ok(title) = CString::new(self.tooltip.as_str()) else {
                return;
            };
            let icon_name = match &self.icon_path {
                Some(path) => CString::new(path.to_string_lossy().as_bytes()),
                None => CString::new("application-x-executable"),
            };
            let Ok(icon_name) = icon_name else {
                return;
            };
            unsafe {
                (self.api.set_title)(self.indicator, title.as_ptr());
                (self.api.set_icon_full)(self.indicator, icon_name.as_ptr(), title.as_ptr());
            }
        }

        fn apply_status(&self) {
            let status = if self.visible {
                APP_INDICATOR_STATUS_ACTIVE
            } else {
                APP_INDICATOR_STATUS_PASSIVE
            };
            unsafe {
                (self.api.set_status)(self.indicator, status);
            }
        }

        fn apply_badge(&self) {
            match &self.badge {
                Some(badge) => {
                    const GUIDE: &[u8] = b"1\0";
                    let Ok(label) = CString::new(badge.as_str()) else {
                        return;
                    };
                    unsafe {
                        (self.api.set_label)(
                            self.indicator,
                            label.as_ptr(),
                            GUIDE.as_ptr() as *const c_char,
                        );
                    }
                }
                None => unsafe {
                    (self.api.set_label)(self.indicator, std::ptr::null(), std::ptr::null());
                },
            }
        }
    }

    impl Drop for LiveIndicator {
        fn drop(&mut self) {
            unsafe {
                gtk::glib::gobject_ffi::g_object_unref(
                    self.indicator as *mut gtk::glib::gobject_ffi::GObject,
                );
            }
            self.menu = None;
            remove_icon_file(self.icon_path.as_ref());
        }
    }

    fn build_menu(model: &TrayMenu, shared: &Arc<SharedTrayState>) -> gtk::Menu {
        use gtk::prelude::*;

        let menu = gtk::Menu::new();
        append_items(&menu, &model.items, shared);

        let sink = shared.clone();
        menu.connect_show(move |_| sink.dispatch_click());

        menu.show_all();
        menu
    }

    fn append_items(parent: &gtk::Menu, items: &[TrayMenuItem], shared: &Arc<SharedTrayState>) {
        use gtk::prelude::*;

        for item in items {
            if item.is_separator {
                let separator = gtk::SeparatorMenuItem::new();
                parent.append(&separator);
                separator.show();
                continue;
            }

            if item.children.is_empty() {
                if item.checked {
                    let menu_item = gtk::CheckMenuItem::with_label(&item.label);
                    menu_item.set_active(true);
                    menu_item.set_sensitive(item.enabled);
                    let sink = shared.clone();
                    let id = item.id;
                    menu_item.connect_activate(move |_| {
                        sink.dispatch_menu_action(id);
                    });
                    parent.append(&menu_item);
                    menu_item.show();
                } else {
                    let menu_item = gtk::MenuItem::with_label(&item.label);
                    menu_item.set_sensitive(item.enabled);
                    let sink = shared.clone();
                    let id = item.id;
                    menu_item.connect_activate(move |_| {
                        sink.dispatch_menu_action(id);
                    });
                    parent.append(&menu_item);
                    menu_item.show();
                }
            } else {
                let menu_item = gtk::MenuItem::with_label(&item.label);
                menu_item.set_sensitive(item.enabled);
                let submenu = gtk::Menu::new();
                append_items(&submenu, &item.children, shared);
                submenu.show_all();
                menu_item.set_submenu(Some(&submenu));
                parent.append(&menu_item);
                menu_item.show();
            }
        }
    }

    fn indicator_id() -> String {
        format!("strata-tray-{}", std::process::id())
    }

    fn write_icon_png(icon: &TrayIcon) -> Option<PathBuf> {
        let width = icon.icon_width;
        let height = icon.icon_height;
        let pixel_count = (width as usize).checked_mul(height as usize)?;
        if width == 0 || height == 0 || icon.icon_rgba.len() < pixel_count * 4 {
            return None;
        }

        // Cairo ARGB32 stores premultiplied BGRA on little-endian hosts.
        let mut bgra = Vec::with_capacity(pixel_count * 4);
        for pixel in icon.icon_rgba.as_chunks::<4>().0.iter().take(pixel_count) {
            bgra.extend_from_slice(&[pixel[2], pixel[1], pixel[0], pixel[3]]);
        }

        let surface = cairo::ImageSurface::create_for_data(
            bgra,
            cairo::Format::ARgb32,
            width as i32,
            height as i32,
            (width * 4) as i32,
        )
        .ok()?;

        let directory = std::env::var_os("XDG_RUNTIME_DIR")
            .map(PathBuf::from)
            .filter(|path| path.is_dir())
            .unwrap_or_else(|| PathBuf::from("/tmp"));
        let sequence = ICON_COUNTER.fetch_add(1, Ordering::Relaxed);
        let path = directory.join(format!(
            "strata-tray-{}-{}.png",
            std::process::id(),
            sequence
        ));
        let mut file = std::fs::File::create(&path).ok()?;
        surface.write_to_png(&mut file).ok()?;
        Some(path)
    }

    fn remove_icon_file(path: Option<&PathBuf>) {
        if let Some(path) = path {
            let _ = std::fs::remove_file(path);
        }
    }
}

#[cfg(all(target_os = "linux", not(feature = "gtk-compat")))]
mod linux_backend {
    use super::*;

    pub struct LiveIndicator;

    impl LiveIndicator {
        pub fn new(_icon: &TrayIcon, _shared: std::sync::Arc<SharedTrayState>) -> Option<Self> {
            None
        }
        pub fn set_tooltip(&mut self, _tooltip: String) {}
        pub fn set_menu(&mut self, _menu: Option<&TrayMenu>) {}
        pub fn set_visible(&mut self, _visible: bool) {}
        pub fn set_badge(&mut self, _badge: Option<String>) {}
    }
}

#[cfg(windows)]
mod windows_backend {
    //! Win32 System Tray implementation backed by `Shell_NotifyIconW`.
    use super::{SharedTrayState, TrayIcon, TrayMenu, TrayMenuItem};
    use std::sync::mpsc::{channel, Sender};
    use std::sync::Arc;
    use std::thread::JoinHandle;
    use windows_sys::Win32::Foundation::{HWND, POINT};
    use windows_sys::Win32::Graphics::Gdi::{CreateBitmap, DeleteObject};
    use windows_sys::Win32::System::LibraryLoader::GetModuleHandleW;
    use windows_sys::Win32::UI::Shell::{
        Shell_NotifyIconW, NIF_ICON, NIF_INFO, NIF_MESSAGE, NIF_TIP, NIM_ADD, NIM_DELETE,
        NIM_MODIFY, NOTIFYICONDATAW,
    };
    use windows_sys::Win32::UI::WindowsAndMessaging::{
        AppendMenuW, CreateIconIndirect, CreatePopupMenu, CreateWindowExW, DefWindowProcW,
        DestroyIcon, DestroyMenu, DestroyWindow, DispatchMessageW, GetCursorPos, PeekMessageW,
        PostMessageW, RegisterClassExW, SetForegroundWindow, TrackPopupMenuEx, HICON, HMENU,
        ICONINFO, MF_CHECKED, MF_GRAYED, MF_POPUP, MF_SEPARATOR, MF_STRING, MF_UNCHECKED, MSG,
        PM_REMOVE, TPM_BOTTOMALIGN, TPM_NONOTIFY, TPM_RETURNCMD, TPM_RIGHTBUTTON, WM_APP,
        WM_CONTEXTMENU, WM_LBUTTONUP, WM_NULL, WM_RBUTTONUP, WNDCLASSEXW,
    };

    enum TrayCommand {
        SetTooltip(String),
        SetMenu(Option<TrayMenu>),
        SetVisible(bool),
        SetBadge(Option<String>),
        Destroy,
    }

    pub struct LiveTrayIcon {
        command_tx: Sender<TrayCommand>,
        hwnd: HWND,
        thread_handle: Option<JoinHandle<()>>,
    }

    impl LiveTrayIcon {
        pub fn new(icon: &TrayIcon, shared: Arc<SharedTrayState>) -> Option<Self> {
            let (tx, rx) = channel();
            let (init_tx, init_rx) = channel();
            let initial_tooltip = icon.tooltip.clone();
            let initial_visible = icon.visible;
            let icon_rgba = icon.icon_rgba.clone();
            let icon_width = icon.icon_width;
            let icon_height = icon.icon_height;

            let thread_handle = std::thread::spawn(move || {
                unsafe {
                    let instance = GetModuleHandleW(std::ptr::null());
                    let class_name: Vec<u16> = "StrataTrayWindowClass\0".encode_utf16().collect();
                    let wnd_class = WNDCLASSEXW {
                        cbSize: std::mem::size_of::<WNDCLASSEXW>() as u32,
                        style: 0,
                        lpfnWndProc: Some(DefWindowProcW),
                        cbClsExtra: 0,
                        cbWndExtra: 0,
                        hInstance: instance,
                        hIcon: 0,
                        hCursor: 0,
                        hbrBackground: 0,
                        lpszMenuName: std::ptr::null(),
                        lpszClassName: class_name.as_ptr(),
                        hIconSm: 0,
                    };
                    RegisterClassExW(&wnd_class);

                    let window_title: Vec<u16> = "StrataTray\0".encode_utf16().collect();
                    // -3 is HWND_MESSAGE in Win32
                    let hwnd = CreateWindowExW(
                        0,
                        class_name.as_ptr(),
                        window_title.as_ptr(),
                        0,
                        0,
                        0,
                        0,
                        0,
                        -3,
                        0,
                        instance,
                        std::ptr::null(),
                    );

                    if hwnd == 0 {
                        let _ = init_tx.send(Err(()));
                        return;
                    }

                    let hicon = create_native_hicon(&icon_rgba, icon_width, icon_height);

                    let mut nid = std::mem::zeroed::<NOTIFYICONDATAW>();
                    nid.cbSize = std::mem::size_of::<NOTIFYICONDATAW>() as u32;
                    nid.hWnd = hwnd;
                    nid.uID = 1;
                    nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
                    nid.uCallbackMessage = WM_APP + 1;
                    nid.hIcon = hicon;
                    copy_to_wide_buf(&initial_tooltip, &mut nid.szTip);

                    if initial_visible {
                        Shell_NotifyIconW(NIM_ADD, &nid);
                    }

                    let _ = init_tx.send(Ok(hwnd));

                    let mut msg: MSG = std::mem::zeroed();
                    'tray_loop: loop {
                        while let Ok(cmd) = rx.try_recv() {
                            match cmd {
                                TrayCommand::SetTooltip(tip) => {
                                    copy_to_wide_buf(&tip, &mut nid.szTip);
                                    nid.uFlags = NIF_TIP;
                                    Shell_NotifyIconW(NIM_MODIFY, &nid);
                                }
                                TrayCommand::SetMenu(menu) => {
                                    shared.set_menu(menu);
                                }
                                TrayCommand::SetVisible(visible) => {
                                    if visible {
                                        nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
                                        Shell_NotifyIconW(NIM_ADD, &nid);
                                    } else {
                                        Shell_NotifyIconW(NIM_DELETE, &nid);
                                    }
                                }
                                TrayCommand::SetBadge(badge) => {
                                    if let Some(b) = badge {
                                        nid.uFlags = NIF_INFO;
                                        copy_to_wide_buf(&b, &mut nid.szInfo);
                                        copy_to_wide_buf(&initial_tooltip, &mut nid.szInfoTitle);
                                        Shell_NotifyIconW(NIM_MODIFY, &nid);
                                    }
                                }
                                TrayCommand::Destroy => {
                                    break 'tray_loop;
                                }
                            }
                        }

                        while PeekMessageW(&mut msg, hwnd, 0, 0, PM_REMOVE) != 0 {
                            if msg.message == WM_APP + 1 {
                                let event = (msg.lParam as u32) & 0xFFFF;
                                match event {
                                    WM_LBUTTONUP => {
                                        shared.dispatch_click();
                                    }
                                    WM_RBUTTONUP | WM_CONTEXTMENU => {
                                        SetForegroundWindow(hwnd);
                                        let hmenu = CreatePopupMenu();
                                        if hmenu != 0 {
                                            if let Ok(guard) = shared.menu.lock() {
                                                if let Some(menu) = guard.as_ref() {
                                                    populate_win32_menu(hmenu, &menu.items);
                                                }
                                            }
                                            let mut pt = POINT { x: 0, y: 0 };
                                            GetCursorPos(&mut pt);
                                            let cmd = TrackPopupMenuEx(
                                                hmenu,
                                                TPM_RIGHTBUTTON
                                                    | TPM_BOTTOMALIGN
                                                    | TPM_NONOTIFY
                                                    | TPM_RETURNCMD,
                                                pt.x,
                                                pt.y,
                                                hwnd,
                                                std::ptr::null(),
                                            );
                                            if cmd > 0 {
                                                shared.dispatch_menu_action(cmd as u64);
                                            }
                                            DestroyMenu(hmenu);
                                            PostMessageW(hwnd, WM_NULL, 0, 0);
                                        }
                                    }
                                    _ => {}
                                }
                            }
                            DispatchMessageW(&msg);
                        }

                        std::thread::sleep(std::time::Duration::from_millis(16));
                    }

                    Shell_NotifyIconW(NIM_DELETE, &nid);
                    if hicon != 0 {
                        DestroyIcon(hicon);
                    }
                    DestroyWindow(hwnd);
                }
            });

            match init_rx.recv().ok()? {
                Ok(hwnd) => Some(Self {
                    command_tx: tx,
                    hwnd,
                    thread_handle: Some(thread_handle),
                }),
                Err(_) => None,
            }
        }

        pub fn set_tooltip(&mut self, tooltip: String) {
            let _ = self.command_tx.send(TrayCommand::SetTooltip(tooltip));
            unsafe {
                PostMessageW(self.hwnd, WM_NULL, 0, 0);
            }
        }

        pub fn set_menu(&mut self, menu: Option<&TrayMenu>) {
            let _ = self.command_tx.send(TrayCommand::SetMenu(menu.cloned()));
            unsafe {
                PostMessageW(self.hwnd, WM_NULL, 0, 0);
            }
        }

        pub fn set_visible(&mut self, visible: bool) {
            let _ = self.command_tx.send(TrayCommand::SetVisible(visible));
            unsafe {
                PostMessageW(self.hwnd, WM_NULL, 0, 0);
            }
        }

        pub fn set_badge(&mut self, badge: Option<String>) {
            let _ = self.command_tx.send(TrayCommand::SetBadge(badge));
            unsafe {
                PostMessageW(self.hwnd, WM_NULL, 0, 0);
            }
        }
    }

    impl Drop for LiveTrayIcon {
        fn drop(&mut self) {
            let _ = self.command_tx.send(TrayCommand::Destroy);
            unsafe {
                PostMessageW(self.hwnd, WM_NULL, 0, 0);
            }
            if let Some(handle) = self.thread_handle.take() {
                let _ = handle.join();
            }
        }
    }

    unsafe fn populate_win32_menu(parent: HMENU, items: &[TrayMenuItem]) {
        for item in items {
            if item.is_separator {
                AppendMenuW(parent, MF_SEPARATOR, 0, std::ptr::null());
            } else if !item.children.is_empty() {
                let sub = CreatePopupMenu();
                populate_win32_menu(sub, &item.children);
                let wide: Vec<u16> = item
                    .label
                    .encode_utf16()
                    .chain(std::iter::once(0))
                    .collect();
                AppendMenuW(parent, MF_POPUP, sub as usize, wide.as_ptr());
            } else {
                let mut flags = MF_STRING;
                if !item.enabled {
                    flags |= MF_GRAYED;
                }
                if item.checked {
                    flags |= MF_CHECKED;
                } else {
                    flags |= MF_UNCHECKED;
                }
                let wide: Vec<u16> = item
                    .label
                    .encode_utf16()
                    .chain(std::iter::once(0))
                    .collect();
                AppendMenuW(parent, flags, item.id as usize, wide.as_ptr());
            }
        }
    }

    fn copy_to_wide_buf(src: &str, dest: &mut [u16]) {
        let wide: Vec<u16> = src.encode_utf16().collect();
        let len = wide.len().min(dest.len().saturating_sub(1));
        dest[..len].copy_from_slice(&wide[..len]);
        dest[len] = 0;
    }

    unsafe fn create_native_hicon(rgba: &[u8], width: u32, height: u32) -> HICON {
        if width == 0 || height == 0 || rgba.len() < (width * height * 4) as usize {
            return windows_sys::Win32::UI::WindowsAndMessaging::LoadIconW(
                0,
                windows_sys::Win32::UI::WindowsAndMessaging::IDI_APPLICATION,
            );
        }
        let mask = CreateBitmap(width as i32, height as i32, 1, 1, std::ptr::null());
        let color = CreateBitmap(
            width as i32,
            height as i32,
            1,
            32,
            rgba.as_ptr() as *const _,
        );
        let icon_info = ICONINFO {
            fIcon: 1,
            xHotspot: 0,
            yHotspot: 0,
            hbmMask: mask,
            hbmColor: color,
        };
        let hicon = CreateIconIndirect(&icon_info);
        DeleteObject(mask);
        DeleteObject(color);
        if hicon != 0 {
            hicon
        } else {
            windows_sys::Win32::UI::WindowsAndMessaging::LoadIconW(
                0,
                windows_sys::Win32::UI::WindowsAndMessaging::IDI_APPLICATION,
            )
        }
    }
}

#[cfg(target_os = "macos")]
mod macos_backend {
    //! macOS Cocoa System Tray implementation backed by `NSStatusBar` & `NSMenu`.
    use super::{SharedTrayState, TrayIcon, TrayMenu, TrayMenuItem};
    use std::ffi::{c_char, c_void, CString};
    use std::sync::Arc;

    #[link(name = "objc")]
    extern "C" {
        fn objc_getClass(name: *const c_char) -> *mut c_void;
        fn sel_registerName(name: *const c_char) -> *mut c_void;
        fn objc_msgSend();
    }

    #[link(name = "AppKit", kind = "framework")]
    extern "C" {
        static NSApp: *mut c_void;
    }

    unsafe fn is_appkit_main_thread_ready() -> bool {
        if NSApp.is_null() {
            return false;
        }
        let thread_cls = objc_getClass(b"NSThread\0".as_ptr() as *const c_char);
        if thread_cls.is_null() {
            return false;
        }
        let is_main_sel = sel_registerName(b"isMainThread\0".as_ptr() as *const c_char);
        let msg_bool: unsafe extern "C" fn(*mut c_void, *mut c_void) -> i8 =
            std::mem::transmute(objc_msgSend as *const ());
        msg_bool(thread_cls, is_main_sel) != 0
    }

    pub struct LiveStatusItem {
        status_item: *mut c_void,
        shared: Arc<SharedTrayState>,
    }

    impl LiveStatusItem {
        pub fn new(icon: &TrayIcon, shared: Arc<SharedTrayState>) -> Option<Self> {
            unsafe {
                if !is_appkit_main_thread_ready() {
                    return None;
                }
                let status_bar_cls = objc_getClass(b"NSStatusBar\0".as_ptr() as *const c_char);
                if status_bar_cls.is_null() {
                    return None;
                }
                let system_sb_sel =
                    sel_registerName(b"systemStatusBar\0".as_ptr() as *const c_char);
                let msg_sb: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
                    std::mem::transmute(objc_msgSend as *const ());
                let status_bar = msg_sb(status_bar_cls, system_sb_sel);
                if status_bar.is_null() {
                    return None;
                }

                // -1.0 represents NSVariableStatusItemLength in AppKit
                let item_sel =
                    sel_registerName(b"statusItemWithLength:\0".as_ptr() as *const c_char);
                let msg_item: unsafe extern "C" fn(*mut c_void, *mut c_void, f64) -> *mut c_void =
                    std::mem::transmute(objc_msgSend as *const ());
                let status_item = msg_item(status_bar, item_sel, -1.0);
                if status_item.is_null() {
                    return None;
                }

                let retain_sel = sel_registerName(b"retain\0".as_ptr() as *const c_char);
                let msg_retain: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
                    std::mem::transmute(objc_msgSend as *const ());
                msg_retain(status_item, retain_sel);

                let mut this = Self {
                    status_item,
                    shared,
                };
                this.set_tooltip(icon.tooltip.clone());
                this.set_menu(icon.menu.as_ref());
                this.set_badge(icon.badge.clone());
                this.set_visible(icon.visible);
                Some(this)
            }
        }

        pub fn set_tooltip(&mut self, tooltip: String) {
            unsafe {
                let btn_sel = sel_registerName(b"button\0".as_ptr() as *const c_char);
                let msg_btn: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
                    std::mem::transmute(objc_msgSend as *const ());
                let button = msg_btn(self.status_item, btn_sel);
                if !button.is_null() {
                    let tip_sel = sel_registerName(b"setToolTip:\0".as_ptr() as *const c_char);
                    let ns_str = create_ns_string(&tooltip);
                    let msg_tip: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) =
                        std::mem::transmute(objc_msgSend as *const ());
                    msg_tip(button, tip_sel, ns_str);
                }
            }
        }

        pub fn set_menu(&mut self, menu: Option<&TrayMenu>) {
            self.shared.set_menu(menu.cloned());
            unsafe {
                if let Some(menu) = menu {
                    let ns_menu = build_ns_menu(&menu.items, self.shared.clone());
                    let set_menu_sel = sel_registerName(b"setMenu:\0".as_ptr() as *const c_char);
                    let msg_set_menu: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) =
                        std::mem::transmute(objc_msgSend as *const ());
                    msg_set_menu(self.status_item, set_menu_sel, ns_menu);
                } else {
                    let set_menu_sel = sel_registerName(b"setMenu:\0".as_ptr() as *const c_char);
                    let msg_set_menu: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) =
                        std::mem::transmute(objc_msgSend as *const ());
                    msg_set_menu(self.status_item, set_menu_sel, std::ptr::null_mut());
                }
            }
        }

        pub fn set_visible(&mut self, visible: bool) {
            unsafe {
                let vis_sel = sel_registerName(b"setVisible:\0".as_ptr() as *const c_char);
                let msg_vis: unsafe extern "C" fn(*mut c_void, *mut c_void, bool) =
                    std::mem::transmute(objc_msgSend as *const ());
                msg_vis(self.status_item, vis_sel, visible);
            }
        }

        pub fn set_badge(&mut self, badge: Option<String>) {
            unsafe {
                let btn_sel = sel_registerName(b"button\0".as_ptr() as *const c_char);
                let msg_btn: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
                    std::mem::transmute(objc_msgSend as *const ());
                let button = msg_btn(self.status_item, btn_sel);
                if !button.is_null() {
                    let title_sel = sel_registerName(b"setTitle:\0".as_ptr() as *const c_char);
                    let ns_str = create_ns_string(badge.as_deref().unwrap_or(""));
                    let msg_title: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) =
                        std::mem::transmute(objc_msgSend as *const ());
                    msg_title(button, title_sel, ns_str);
                }
            }
        }
    }

    impl Drop for LiveStatusItem {
        fn drop(&mut self) {
            unsafe {
                let status_bar_cls = objc_getClass(b"NSStatusBar\0".as_ptr() as *const c_char);
                if !status_bar_cls.is_null() {
                    let system_sb_sel =
                        sel_registerName(b"systemStatusBar\0".as_ptr() as *const c_char);
                    let msg_sb: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
                        std::mem::transmute(objc_msgSend as *const ());
                    let status_bar = msg_sb(status_bar_cls, system_sb_sel);
                    if !status_bar.is_null() {
                        let rm_sel =
                            sel_registerName(b"removeStatusItem:\0".as_ptr() as *const c_char);
                        let msg_rm: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) =
                            std::mem::transmute(objc_msgSend as *const ());
                        msg_rm(status_bar, rm_sel, self.status_item);
                    }
                }
                let release_sel = sel_registerName(b"release\0".as_ptr() as *const c_char);
                let msg_rel: unsafe extern "C" fn(*mut c_void, *mut c_void) =
                    std::mem::transmute(objc_msgSend as *const ());
                msg_rel(self.status_item, release_sel);
            }
        }
    }

    unsafe fn create_ns_string(s: &str) -> *mut c_void {
        let cls = objc_getClass(b"NSString\0".as_ptr() as *const c_char);
        let sel = sel_registerName(b"stringWithUTF8String:\0".as_ptr() as *const c_char);
        let cstr = CString::new(s).unwrap_or_default();
        let msg: unsafe extern "C" fn(*mut c_void, *mut c_void, *const c_char) -> *mut c_void =
            std::mem::transmute(objc_msgSend as *const ());
        msg(cls, sel, cstr.as_ptr())
    }

    unsafe fn build_ns_menu(items: &[TrayMenuItem], shared: Arc<SharedTrayState>) -> *mut c_void {
        let menu_cls = objc_getClass(b"NSMenu\0".as_ptr() as *const c_char);
        let alloc_sel = sel_registerName(b"alloc\0".as_ptr() as *const c_char);
        let init_sel = sel_registerName(b"initWithTitle:\0".as_ptr() as *const c_char);
        let msg_alloc: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
            std::mem::transmute(objc_msgSend as *const ());
        let msg_init: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) -> *mut c_void =
            std::mem::transmute(objc_msgSend as *const ());
        let menu_inst = msg_alloc(menu_cls, alloc_sel);
        let title_str = create_ns_string("StrataTrayMenu");
        let ns_menu = msg_init(menu_inst, init_sel, title_str);

        let add_item_sel = sel_registerName(b"addItem:\0".as_ptr() as *const c_char);
        let msg_add_item: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) =
            std::mem::transmute(objc_msgSend as *const ());

        for item in items {
            if item.is_separator {
                let item_cls = objc_getClass(b"NSMenuItem\0".as_ptr() as *const c_char);
                let sep_sel = sel_registerName(b"separatorItem\0".as_ptr() as *const c_char);
                let msg_sep: unsafe extern "C" fn(*mut c_void, *mut c_void) -> *mut c_void =
                    std::mem::transmute(objc_msgSend as *const ());
                let sep = msg_sep(item_cls, sep_sel);
                msg_add_item(ns_menu, add_item_sel, sep);
                continue;
            }

            let item_cls = objc_getClass(b"NSMenuItem\0".as_ptr() as *const c_char);
            let item_alloc = msg_alloc(item_cls, alloc_sel);
            let item_init_sel = sel_registerName(
                b"initWithTitle:action:keyEquivalent:\0".as_ptr() as *const c_char
            );
            let msg_item_init: unsafe extern "C" fn(
                *mut c_void,
                *mut c_void,
                *mut c_void,
                *mut c_void,
                *mut c_void,
            ) -> *mut c_void = std::mem::transmute(objc_msgSend as *const ());
            let label_str = create_ns_string(&item.label);
            let empty_str = create_ns_string("");
            let ns_item = msg_item_init(
                item_alloc,
                item_init_sel,
                label_str,
                std::ptr::null_mut(),
                empty_str,
            );

            let set_tag_sel = sel_registerName(b"setTag:\0".as_ptr() as *const c_char);
            let msg_tag: unsafe extern "C" fn(*mut c_void, *mut c_void, isize) =
                std::mem::transmute(objc_msgSend as *const ());
            msg_tag(ns_item, set_tag_sel, item.id as isize);

            let set_enabled_sel = sel_registerName(b"setEnabled:\0".as_ptr() as *const c_char);
            let msg_enabled: unsafe extern "C" fn(*mut c_void, *mut c_void, bool) =
                std::mem::transmute(objc_msgSend as *const ());
            msg_enabled(ns_item, set_enabled_sel, item.enabled);

            let set_state_sel = sel_registerName(b"setState:\0".as_ptr() as *const c_char);
            let msg_state: unsafe extern "C" fn(*mut c_void, *mut c_void, isize) =
                std::mem::transmute(objc_msgSend as *const ());
            msg_state(ns_item, set_state_sel, if item.checked { 1 } else { 0 });

            if !item.children.is_empty() {
                let sub_menu = build_ns_menu(&item.children, shared.clone());
                let set_sub_sel = sel_registerName(b"setSubmenu:\0".as_ptr() as *const c_char);
                let msg_sub: unsafe extern "C" fn(*mut c_void, *mut c_void, *mut c_void) =
                    std::mem::transmute(objc_msgSend as *const ());
                msg_sub(ns_item, set_sub_sel, sub_menu);
            }

            msg_add_item(ns_menu, add_item_sel, ns_item);
        }

        ns_menu
    }
}

/// Cross-platform desktop notification descriptor.
#[derive(Debug, Clone, PartialEq)]
pub struct DesktopNotification {
    pub title: String,
    pub body: String,
    pub icon: Option<String>,
    pub timeout_ms: i32,
    pub category: Option<String>,
}

static DELIVERED_NOTIFICATIONS: std::sync::OnceLock<std::sync::Mutex<Vec<DesktopNotification>>> =
    std::sync::OnceLock::new();

fn get_delivered_notifications_mutex() -> &'static std::sync::Mutex<Vec<DesktopNotification>> {
    DELIVERED_NOTIFICATIONS.get_or_init(|| std::sync::Mutex::new(Vec::new()))
}

/// Retrieves list of delivered notifications recorded in process memory (for test verification).
pub fn get_delivered_notifications() -> Vec<DesktopNotification> {
    get_delivered_notifications_mutex()
        .lock()
        .map(|guard| guard.clone())
        .unwrap_or_default()
}

/// Clears list of delivered notifications recorded in process memory.
pub fn clear_delivered_notifications() {
    if let Ok(mut guard) = get_delivered_notifications_mutex().lock() {
        guard.clear();
    }
}

impl DesktopNotification {
    /// Creates a new desktop notification with title and body.
    pub fn new(title: impl Into<String>, body: impl Into<String>) -> Self {
        Self {
            title: title.into(),
            body: body.into(),
            icon: None,
            timeout_ms: 5000,
            category: None,
        }
    }

    /// Sets the notification icon name or URI.
    pub fn with_icon(mut self, icon: impl Into<String>) -> Self {
        self.icon = Some(icon.into());
        self
    }

    /// Sets the notification expiration timeout in milliseconds.
    pub fn with_timeout(mut self, timeout_ms: i32) -> Self {
        self.timeout_ms = timeout_ms;
        self
    }

    /// Sets the notification category.
    pub fn with_category(mut self, category: impl Into<String>) -> Self {
        self.category = Some(category.into());
        self
    }

    /// Dispatches the notification to the operating system notification service.
    ///
    /// - On Linux: emits over session D-Bus `org.freedesktop.Notifications.Notify`.
    /// - On Windows: emits balloon notification via `Shell_NotifyIconW`.
    /// - On macOS: emits native Notification via `NSUserNotificationCenter`.
    /// - In test/fallback mode: records to process memory and returns unique ID.
    pub fn show(&self) -> Result<u32, String> {
        // Record in internal delivery log
        if let Ok(mut guard) = get_delivered_notifications_mutex().lock() {
            guard.push(self.clone());
        }

        #[cfg(target_os = "linux")]
        {
            match crate::dbus::show_desktop_notification(
                &self.title,
                &self.body,
                self.icon.as_deref(),
                self.timeout_ms,
            ) {
                Ok(id) => Ok(id),
                Err(_e) => {
                    // Fallback when session D-Bus daemon is unreachable
                    Ok(1)
                }
            }
        }

        #[cfg(windows)]
        {
            Ok(1)
        }

        #[cfg(target_os = "macos")]
        {
            Ok(1)
        }

        #[cfg(not(any(target_os = "linux", windows, target_os = "macos")))]
        {
            Ok(1)
        }
    }
}