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
//! Portable system tray service model.
//!
//! Provides the shared, backend-agnostic specification and identifiers for
//! system tray (notification area / status item) icons.
//!
//! Tray icons are application-level services owned directly by [`Ui`](crate::Ui),
//! outside the hierarchical layout tree: they do not have a `WidgetId` and do
//! not participate in hierarchical layout.
use std::fmt;
use crate::menus::MenuId;
/// Stable identifier for a system tray / status bar icon.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TrayIconId(pub u64);
impl TrayIconId {
/// Returns the raw numeric identifier.
pub const fn get(self) -> u64 {
self.0
}
}
impl fmt::Display for TrayIconId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "tray icon {}", self.0)
}
}
/// Specification for a system tray icon.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TrayIconSpec {
/// Stable identifier for the tray icon.
pub id: TrayIconId,
/// Hover tooltip text.
pub tooltip: String,
/// Optional context menu bound to the tray icon.
pub menu: Option<MenuId>,
/// Whether the tray icon is visible in the status area.
pub visible: bool,
/// Optional raw icon pixels.
///
/// The shared encoding contract is a **square straight-alpha RGBA
/// raster**: `side * side * 4` bytes in row-major order. A lane whose
/// native image loader recognises container formats may additionally
/// accept an encoded image (Cocoa decodes PNG/TIFF/ICNS/JPEG/GIF/BMP and
/// the ISO base media family); a payload that fits neither shape is
/// refused with a typed error. `None` leaves the icon on the platform
/// default image.
pub icon_bytes: Option<Vec<u8>>,
}
impl TrayIconSpec {
/// Creates a visible tray icon specification with default settings.
pub fn new(id: TrayIconId, tooltip: impl Into<String>) -> Self {
Self {
id,
tooltip: tooltip.into(),
menu: None,
visible: true,
icon_bytes: None,
}
}
/// Sets the context menu for the tray icon.
pub fn with_menu(mut self, menu: MenuId) -> Self {
self.menu = Some(menu);
self
}
/// Sets whether the tray icon is visible.
pub fn with_visible(mut self, visible: bool) -> Self {
self.visible = visible;
self
}
/// Sets the raw icon pixels for the tray icon.
///
/// See [`TrayIconSpec::icon_bytes`] for the shared encoding contract
/// (square straight-alpha RGBA, or an encoded container the lane's loader
/// recognises).
pub fn with_icon_bytes(mut self, icon_bytes: Vec<u8>) -> Self {
self.icon_bytes = Some(icon_bytes);
self
}
}
/// Interaction events occurring on a system tray icon.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TrayEvent {
/// Primary or single click on the tray icon.
Clicked { id: TrayIconId },
/// Double-click on the tray icon.
DoubleClicked { id: TrayIconId },
/// Secondary or right click on the tray icon.
RightClicked { id: TrayIconId },
}
impl TrayEvent {
/// Returns the target [`TrayIconId`] associated with this event.
pub fn id(&self) -> TrayIconId {
match self {
Self::Clicked { id } | Self::DoubleClicked { id } | Self::RightClicked { id } => *id,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn tray_icon_id_display_and_helpers() {
let id = TrayIconId(42);
assert_eq!(id.get(), 42);
assert_eq!(format!("{id}"), "tray icon 42");
}
#[test]
fn tray_icon_spec_builder() {
let id = TrayIconId(1);
let spec = TrayIconSpec::new(id, "Test Tooltip")
.with_menu(MenuId(99))
.with_visible(false)
.with_icon_bytes(vec![1, 2, 3, 4]);
assert_eq!(spec.id, id);
assert_eq!(spec.tooltip, "Test Tooltip");
assert_eq!(spec.menu, Some(MenuId(99)));
assert!(!spec.visible);
assert_eq!(spec.icon_bytes, Some(vec![1, 2, 3, 4]));
}
#[test]
fn tray_event_helpers() {
let id = TrayIconId(7);
let ev1 = TrayEvent::Clicked { id };
let ev2 = TrayEvent::DoubleClicked { id };
let ev3 = TrayEvent::RightClicked { id };
assert_eq!(ev1.id(), id);
assert_eq!(ev2.id(), id);
assert_eq!(ev3.id(), id);
}
}