1use std::fmt;
11
12use crate::menus::MenuId;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
16pub struct TrayIconId(pub u64);
17
18impl TrayIconId {
19 pub const fn get(self) -> u64 {
21 self.0
22 }
23}
24
25impl fmt::Display for TrayIconId {
26 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27 write!(f, "tray icon {}", self.0)
28 }
29}
30
31#[derive(Debug, Clone, PartialEq, Eq)]
33pub struct TrayIconSpec {
34 pub id: TrayIconId,
36 pub tooltip: String,
38 pub menu: Option<MenuId>,
40 pub visible: bool,
42 pub icon_bytes: Option<Vec<u8>>,
52}
53
54impl TrayIconSpec {
55 pub fn new(id: TrayIconId, tooltip: impl Into<String>) -> Self {
57 Self {
58 id,
59 tooltip: tooltip.into(),
60 menu: None,
61 visible: true,
62 icon_bytes: None,
63 }
64 }
65
66 pub fn with_menu(mut self, menu: MenuId) -> Self {
68 self.menu = Some(menu);
69 self
70 }
71
72 pub fn with_visible(mut self, visible: bool) -> Self {
74 self.visible = visible;
75 self
76 }
77
78 pub fn with_icon_bytes(mut self, icon_bytes: Vec<u8>) -> Self {
84 self.icon_bytes = Some(icon_bytes);
85 self
86 }
87}
88
89#[derive(Debug, Clone, PartialEq, Eq)]
91pub enum TrayEvent {
92 Clicked { id: TrayIconId },
94 DoubleClicked { id: TrayIconId },
96 RightClicked { id: TrayIconId },
98}
99
100impl TrayEvent {
101 pub fn id(&self) -> TrayIconId {
103 match self {
104 Self::Clicked { id } | Self::DoubleClicked { id } | Self::RightClicked { id } => *id,
105 }
106 }
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112
113 #[test]
114 fn tray_icon_id_display_and_helpers() {
115 let id = TrayIconId(42);
116 assert_eq!(id.get(), 42);
117 assert_eq!(format!("{id}"), "tray icon 42");
118 }
119
120 #[test]
121 fn tray_icon_spec_builder() {
122 let id = TrayIconId(1);
123 let spec = TrayIconSpec::new(id, "Test Tooltip")
124 .with_menu(MenuId(99))
125 .with_visible(false)
126 .with_icon_bytes(vec![1, 2, 3, 4]);
127
128 assert_eq!(spec.id, id);
129 assert_eq!(spec.tooltip, "Test Tooltip");
130 assert_eq!(spec.menu, Some(MenuId(99)));
131 assert!(!spec.visible);
132 assert_eq!(spec.icon_bytes, Some(vec![1, 2, 3, 4]));
133 }
134
135 #[test]
136 fn tray_event_helpers() {
137 let id = TrayIconId(7);
138 let ev1 = TrayEvent::Clicked { id };
139 let ev2 = TrayEvent::DoubleClicked { id };
140 let ev3 = TrayEvent::RightClicked { id };
141
142 assert_eq!(ev1.id(), id);
143 assert_eq!(ev2.id(), id);
144 assert_eq!(ev3.id(), id);
145 }
146}