use crate::{App, Image, MenuItem, SharedString, SvgRenderer};
use anyhow::Result;
use std::rc::Rc;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TrayIconEvent {
LeftClick,
RightClick,
DoubleClick,
}
#[derive(Debug, Clone)]
pub enum TrayMenuItem {
Action {
label: SharedString,
id: SharedString,
},
Separator,
Submenu {
label: SharedString,
items: Vec<TrayMenuItem>,
},
Toggle {
label: SharedString,
checked: bool,
id: SharedString,
},
}
#[derive(Clone)]
pub struct Tray {
pub tooltip: Option<SharedString>,
pub icon: Option<Rc<Image>>,
pub icon_data: Option<TrayIconData>,
pub menu_builder: Option<Rc<dyn Fn(&mut App) -> Vec<MenuItem>>>,
pub visible: bool,
}
impl Tray {
pub fn render_icon(&mut self, svg_renderer: SvgRenderer) -> Result<()> {
if let Some(icon) = &self.icon {
let image = icon.to_image_data(svg_renderer)?;
let bytes = image.as_bytes(0).unwrap_or_default();
let size = image.size(0);
self.icon_data = Some(TrayIconData {
data: Rc::new(bytes.to_vec()),
width: size.width.0 as u32,
height: size.height.0 as u32,
})
}
Ok(())
}
}
#[derive(Clone)]
pub struct TrayIconData {
pub data: Rc<Vec<u8>>,
pub width: u32,
pub height: u32,
}
impl Tray {
pub fn new() -> Self {
Self {
tooltip: None,
icon: None,
icon_data: None,
menu_builder: None,
visible: true,
}
}
pub fn tooltip(mut self, tooltip: impl Into<SharedString>) -> Self {
self.tooltip = Some(tooltip.into());
self
}
pub fn icon(mut self, icon: impl Into<Image>) -> Self {
self.icon = Some(Rc::new(icon.into()));
self
}
pub fn menu<F>(mut self, builder: F) -> Self
where
F: Fn(&mut App) -> Vec<MenuItem> + 'static,
{
self.menu_builder = Some(Rc::new(builder));
self
}
pub fn visible(mut self, visible: bool) -> Self {
self.visible = visible;
self
}
}