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
use crate::dbus::DBusProps;
use crate::error::{Error, Result};
use serde::{Deserialize, Serialize};
use std::fmt::{Debug, Formatter};
use zbus::zvariant::{Array, Structure};
/// Represents an item to display inside the tray.
/// <https://www.freedesktop.org/wiki/Specifications/StatusNotifierItem/StatusNotifierItem/>
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct StatusNotifierItem {
/// A name that should be unique for this application and consistent between sessions, such as the application name itself.
pub id: String,
/// The category of this item.
///
/// The allowed values for the Category property are:
///
/// - `ApplicationStatus`: The item describes the status of a generic application, for instance the current state of a media player.
/// In the case where the category of the item can not be known, such as when the item is being proxied from another incompatible or emulated system,
/// `ApplicationStatus` can be used a sensible default fallback.
/// - `Communications`: The item describes the status of communication oriented applications, like an instant messenger or an email client.
/// - `SystemServices`: The item describes services of the system not seen as a stand alone application by the user, such as an indicator for the activity of a disk indexing service.
/// - `Hardware`: The item describes the state and control of a particular hardware, such as an indicator of the battery charge or sound card volume control.
pub category: Category,
/// A name that describes the application, it can be more descriptive than Id.
pub title: Option<String>,
/// Describes the status of this item or of the associated application.
///
/// The allowed values for the Status property are:
///
/// - Passive: The item doesn't convey important information to the user, it can be considered an "idle" status and is likely that visualizations will chose to hide it.
/// - Active: The item is active, is more important that the item will be shown in some way to the user.
/// - `NeedsAttention`: The item carries really important information for the user, such as battery charge running out and is wants to incentive the direct user intervention.
/// Visualizations should emphasize in some way the items with `NeedsAttention` status.
pub status: Status,
/// The windowing-system dependent identifier for a window, the application can choose one of its windows to be available through this property or just set 0 if it's not interested.
pub window_id: u32,
pub icon_theme_path: Option<String>,
/// The `StatusNotifierItem` can carry an icon that can be used by the visualization to identify the item.
///
/// An icon can either be identified by its Freedesktop-compliant icon name, carried by this property of by the icon data itself, carried by the property `IconPixmap`.
/// Visualizations are encouraged to prefer icon names over icon pixmaps if both are available
/// (still not very defined: could be the pixmap used as fallback if an icon name is not found?)
pub icon_name: Option<String>,
/// Carries an ARGB32 binary representation of the icon, the format of icon data used in this specification is described in Section Icons
///
/// # Icons
///
/// All the icons can be transferred over the bus by a particular serialization of their data,
/// capable of representing multiple resolutions of the same image or a brief aimation of images of the same size.
///
/// Icons are transferred in an array of raw image data structures of signature a(iiay) whith each one describing the width, height, and image data respectively.
/// The data is represented in ARGB32 format and is in the network byte order, to make easy the communication over the network between little and big endian machines.
pub icon_pixmap: Option<Vec<IconPixmap>>,
/// The Freedesktop-compliant name of an icon.
/// This can be used by the visualization to indicate extra state information, for instance as an overlay for the main icon.
pub overlay_icon_name: Option<String>,
/// ARGB32 binary representation of the overlay icon described in the previous paragraph.
pub overlay_icon_pixmap: Option<Vec<IconPixmap>>,
/// The Freedesktop-compliant name of an icon. this can be used by the visualization to indicate that the item is in `RequestingAttention` state.
pub attention_icon_name: Option<String>,
/// ARGB32 binary representation of the requesting attention icon describe in the previous paragraph.
pub attention_icon_pixmap: Option<Vec<IconPixmap>>,
/// An item can also specify an animation associated to the `RequestingAttention` state.
/// This should be either a Freedesktop-compliant icon name or a full path.
/// The visualization can choose between the movie or `AttentionIconPixmap` (or using neither of those) at its discretion.
pub attention_movie_name: Option<String>,
/// Data structure that describes extra information associated to this item, that can be visualized for instance by a tooltip
/// (or by any other mean the visualization consider appropriate.
pub tool_tip: Option<Tooltip>,
/// The item only support the context menu, the visualization should prefer showing the menu or sending `ContextMenu()` instead of `Activate()`
pub item_is_menu: bool,
/// `DBus` path to an object which should implement the `com.canonical.dbusmenu` interface
pub menu: Option<String>,
}
#[derive(Debug, Clone, Copy, Deserialize, Serialize, Default, Eq, PartialEq, Hash)]
pub enum Category {
#[default]
ApplicationStatus,
Communications,
SystemServices,
Hardware,
}
impl From<&str> for Category {
fn from(value: &str) -> Self {
match value {
"Communications" => Self::Communications,
"SystemServices" => Self::SystemServices,
"Hardware" => Self::Hardware,
_ => Self::ApplicationStatus,
}
}
}
#[derive(Debug, Clone, Copy, Deserialize, Serialize, Default, Eq, PartialEq, Hash)]
pub enum Status {
#[default]
Unknown,
Passive,
Active,
NeedsAttention,
}
impl From<&str> for Status {
fn from(value: &str) -> Self {
match value {
"Passive" => Self::Passive,
"Active" => Self::Active,
"NeedsAttention" => Self::NeedsAttention,
_ => Self::Unknown,
}
}
}
#[derive(Deserialize, Serialize, Clone, Eq, PartialEq, Hash)]
pub struct IconPixmap {
pub width: i32,
pub height: i32,
pub pixels: Vec<u8>,
}
impl Debug for IconPixmap {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("IconPixmap")
.field("width", &self.width)
.field("height", &self.height)
.field("pixels", &format!("<length: {}>", self.pixels.len()))
.finish()
}
}
impl IconPixmap {
/// Attempts to create a `Vec<IconPixmap>` from a provided `ZBus` `Array` type.
/// This assumes the array is actually pixmap data.
///
/// # Errors
///
/// An error will return if any of the fields are missing are of the wrong type,
/// indicating usually that the array passed in was likely not a `Pixmap`.
pub fn from_array(array: &Array) -> Result<Vec<Self>> {
array
.iter()
.map(|pixmap| {
let structure = pixmap.downcast_ref::<&Structure>()?;
let fields = structure.fields();
let width = fields
.first()
.ok_or(Error::InvalidData("invalid or missing width"))?
.downcast_ref::<i32>()?;
let height = fields
.first()
.ok_or(Error::InvalidData("invalid or missing width"))?
.downcast_ref::<i32>()?;
let pixel_values = fields
.get(2)
.ok_or(Error::InvalidData("invalid or missing pixel values"))?
.downcast_ref::<&Array>()?;
let pixels = pixel_values
.iter()
.map(|p| p.downcast_ref::<u8>().map_err(Into::into))
.collect::<Result<_>>()?;
Ok(IconPixmap {
width,
height,
pixels,
})
})
.collect()
}
}
/// Data structure that describes extra information associated to this item, that can be visualized for instance by a tooltip
/// (or by any other mean the visualization consider appropriate.
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Tooltip {
pub icon_name: String,
pub icon_data: Vec<IconPixmap>,
pub title: String,
pub description: String,
}
impl TryFrom<&Structure<'_>> for Tooltip {
type Error = Error;
fn try_from(value: &Structure) -> Result<Self> {
let fields = value.fields();
Ok(Self {
icon_name: fields
.first()
.ok_or(Error::InvalidData("icon_name"))?
.downcast_ref::<&str>()
.map(ToString::to_string)?,
icon_data: fields
.get(1)
.ok_or(Error::InvalidData("icon_data"))?
.downcast_ref::<&Array>()
.map_err(Into::into)
.and_then(IconPixmap::from_array)?,
title: fields
.get(2)
.ok_or(Error::InvalidData("title"))?
.downcast_ref::<&str>()
.map(ToString::to_string)?,
description: fields
.get(3)
.ok_or(Error::InvalidData("description"))?
.downcast_ref::<&str>()
.map(ToString::to_string)?,
})
}
}
impl TryFrom<DBusProps> for StatusNotifierItem {
type Error = Error;
fn try_from(props: DBusProps) -> Result<Self> {
if let Some(id) = props.get_string("Id") {
let id = id?;
Ok(Self {
id,
title: props.get_string("Title").transpose()?,
status: props.get_status()?,
window_id: props
.get::<i32>("WindowId")
.transpose()?
.copied()
.unwrap_or_default() as u32,
icon_theme_path: props.get_string("IconThemePath").transpose()?,
icon_name: props.get_string("IconName").transpose()?,
icon_pixmap: props.get_icon_pixmap("IconPixmap").transpose()?,
overlay_icon_name: props.get_string("OverlayIconName").transpose()?,
overlay_icon_pixmap: props.get_icon_pixmap("OverlayIconPixmap").transpose()?,
attention_icon_name: props.get_string("AttentionIconName").transpose()?,
attention_icon_pixmap: props.get_icon_pixmap("AttentionIconPixmap").transpose()?,
attention_movie_name: props.get_string("AttentionMovieName").transpose()?,
tool_tip: props.get_tooltip().transpose()?,
item_is_menu: props
.get("ItemIsMenu")
.transpose()?
.copied()
.unwrap_or_default(),
category: props.get_category()?,
menu: props.get_object_path("Menu").transpose()?,
})
} else {
Err(Error::MissingProperty("Id"))
}
}
}
impl DBusProps {
fn get_category(&self) -> Result<Category> {
Ok(self
.get::<str>("Category")
.transpose()?
.map(Category::from)
.unwrap_or_default())
}
fn get_status(&self) -> Result<Status> {
Ok(self
.get::<str>("Status")
.transpose()?
.map(Status::from)
.unwrap_or_default())
}
fn get_icon_pixmap(&self, key: &str) -> Option<Result<Vec<IconPixmap>>> {
self.get::<Array>(key)
.map(|arr| arr.and_then(IconPixmap::from_array))
}
fn get_tooltip(&self) -> Option<Result<Tooltip>> {
self.get::<Structure>("ToolTip")
.map(|t| t.and_then(Tooltip::try_from))
}
}