use std::sync::Arc;
use crate::color::Color;
use crate::component::ComponentId;
use crate::geometry::PointF;
use crate::ui::Ui;
#[derive(Clone, Debug, PartialEq)]
pub struct TraySpec {
pub icons: Vec<Arc<Vec<u8>>>,
pub interval_ms: u32,
pub tooltip: String,
pub badge: String,
pub badge_color: Color,
pub visible: bool,
pub guid: Option<u128>,
pub uid: u32,
}
pub fn parse_guid(text: &str) -> Option<u128> {
let t = text.trim();
let t = t.strip_prefix('{').and_then(|t| t.strip_suffix('}')).unwrap_or(t);
let hyphenated = t.len() == 36 && [8, 13, 18, 23].iter().all(|&i| t.as_bytes()[i] == b'-');
let hex: String = if hyphenated { t.chars().filter(|&c| c != '-').collect() } else { t.to_string() };
if hex.len() != 32 || !hex.bytes().all(|b| b.is_ascii_hexdigit()) {
return None;
}
u128::from_str_radix(&hex, 16).ok()
}
impl Default for TraySpec {
fn default() -> Self {
Self { icons: Vec::new(), interval_ms: 0, tooltip: String::new(), badge: String::new(), badge_color: Color::rgb(0xE5, 0x39, 0x35), visible: true, guid: None, uid: 0 }
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TrayEventKind {
Click,
DoubleClick,
RightClick,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TrayEvent {
pub kind: TrayEventKind,
pub screen: PointF,
pub scale: f32,
}
impl Ui {
pub fn trays(&self) -> Vec<(ComponentId, TraySpec)> {
self.components().filter_map(|id| self.component(id).and_then(|c| c.platform_hooks()).and_then(|h| h.tray()).map(|spec| (id, spec))).collect()
}
}
#[cfg(test)]
mod tests {
use super::parse_guid;
#[test]
fn guid_text_is_read_in_text_order() {
let n = 0x0F8A_1B2C_3D4E_5F60_7182_93A4_B5C6_D7E8u128;
assert_eq!(parse_guid("{0F8A1B2C-3D4E-5F60-7182-93A4B5C6D7E8}"), Some(n));
assert_eq!(parse_guid("0f8a1b2c-3d4e-5f60-7182-93a4b5c6d7e8"), Some(n));
assert_eq!(parse_guid(" 0F8A1B2C3D4E5F60718293A4B5C6D7E8 "), Some(n));
assert_eq!(parse_guid(""), None);
assert_eq!(parse_guid("0F8A1B2C-3D4E-5F60-7182"), None);
assert_eq!(parse_guid("0F8A1B2C-3D4E-5F6-07182-93A4B5C6D7E8"), None);
assert_eq!(parse_guid("{0F8A1B2C-3D4E-5F60-7182-93A4B5C6D7EZ}"), None);
}
}