CI / Docker image (pull_request) Successful in 14s
CI / CLI (pull_request) Successful in 16s
CI / Server (pull_request) Successful in 24s
CI / CLI (linux-arm64, static) (pull_request) Successful in 50s
CI / CLI (linux-x86-64, static) (pull_request) Successful in 58s
CI / CLI (windows) (pull_request) Successful in 2m16s
tray-icon 0.27 pulls muda 0.22, where MenuEventHandler is Arc<dyn Fn(MenuEvent)> instead of a generic F: Fn(MenuEvent). Some(closure) no longer coerces through Option, so the handler is built as a typed Arc first.
90 lines
3.7 KiB
Rust
90 lines
3.7 KiB
Rust
use anyhow::Result;
|
|
use std::sync::mpsc::Sender;
|
|
use tokio::runtime::Handle;
|
|
use super::{Repaint, Wake};
|
|
|
|
const ICON_SIZE: u32 = 32;
|
|
const ICON_RGBA: &[u8] = include_bytes!("../../assets/icon-32.rgba");
|
|
|
|
pub struct Tray { _keep: Box<dyn std::any::Any> }
|
|
|
|
#[derive(Clone)]
|
|
struct Notifier { wake: Sender<Wake>, repaint: Repaint }
|
|
|
|
impl Notifier {
|
|
fn send(&self, wake: Wake) {
|
|
let _ = self.wake.send(wake);
|
|
self.repaint.ping();
|
|
}
|
|
}
|
|
|
|
#[cfg(target_os = "linux")]
|
|
pub fn start(wake: Sender<Wake>, repaint: Repaint, runtime: &Handle) -> Result<Tray> {
|
|
use ksni::{menu::{MenuItem, StandardItem}, Icon, ToolTip, TrayMethods};
|
|
|
|
struct FrislitTray { notify: Notifier }
|
|
|
|
impl ksni::Tray for FrislitTray {
|
|
fn id(&self) -> String { "frislit".into() }
|
|
fn title(&self) -> String { "frislit".into() }
|
|
fn tool_tip(&self) -> ToolTip { ToolTip { title: "frislit".into(), ..Default::default() } }
|
|
fn icon_pixmap(&self) -> Vec<Icon> {
|
|
let mut data = Vec::with_capacity(ICON_RGBA.len());
|
|
for pixel in ICON_RGBA.chunks(4) { data.extend_from_slice(&[pixel[3], pixel[0], pixel[1], pixel[2]]); }
|
|
vec![Icon { width: ICON_SIZE as i32, height: ICON_SIZE as i32, data }]
|
|
}
|
|
fn activate(&mut self, _x: i32, _y: i32) { self.notify.send(Wake::Toggle); }
|
|
fn menu(&self) -> Vec<MenuItem<Self>> {
|
|
vec![
|
|
StandardItem { label: "Open frislit".into(), activate: Box::new(|tray: &mut Self| tray.notify.send(Wake::Show)), ..Default::default() }.into(),
|
|
MenuItem::Separator,
|
|
StandardItem { label: "Quit".into(), activate: Box::new(|tray: &mut Self| tray.notify.send(Wake::Quit)), ..Default::default() }.into(),
|
|
]
|
|
}
|
|
}
|
|
|
|
let tray = FrislitTray { notify: Notifier { wake, repaint } };
|
|
let handle = runtime.block_on(tray.spawn()).map_err(|err| anyhow::anyhow!("No tray available: {err}"))?;
|
|
Ok(Tray { _keep: Box::new(handle) })
|
|
}
|
|
|
|
#[cfg(any(windows, target_os = "macos"))]
|
|
pub fn start(wake: Sender<Wake>, repaint: Repaint, _runtime: &Handle) -> Result<Tray> {
|
|
use tray_icon::{menu::{Menu, MenuEvent, MenuItem, PredefinedMenuItem}, Icon, MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent};
|
|
|
|
let notify = Notifier { wake, repaint };
|
|
|
|
let menu = Menu::new();
|
|
let open = MenuItem::new("Open frislit", true, None);
|
|
let quit = MenuItem::new("Quit", true, None);
|
|
menu.append_items(&[&open, &PredefinedMenuItem::separator(), &quit])?;
|
|
let (open_id, quit_id) = (open.id().clone(), quit.id().clone());
|
|
|
|
let icon = Icon::from_rgba(ICON_RGBA.to_vec(), ICON_SIZE, ICON_SIZE)?;
|
|
let tray = TrayIconBuilder::new()
|
|
.with_menu(Box::new(menu))
|
|
.with_tooltip("frislit")
|
|
.with_icon(icon)
|
|
.with_icon_as_template(cfg!(target_os = "macos"))
|
|
.with_menu_on_left_click(false)
|
|
.build()?;
|
|
|
|
let clicks = notify.clone();
|
|
TrayIconEvent::set_event_handler(Some(move |event: TrayIconEvent| {
|
|
if let TrayIconEvent::Click { button: MouseButton::Left, button_state: MouseButtonState::Up, .. } = event {
|
|
clicks.send(Wake::Toggle);
|
|
}
|
|
}));
|
|
let on_menu: tray_icon::menu::MenuEventHandler = std::sync::Arc::new(move |event: MenuEvent| {
|
|
if *event.id() == open_id { notify.send(Wake::Show); }
|
|
else if *event.id() == quit_id { notify.send(Wake::Quit); }
|
|
});
|
|
MenuEvent::set_event_handler(Some(on_menu));
|
|
Ok(Tray { _keep: Box::new(tray) })
|
|
}
|
|
|
|
#[cfg(not(any(windows, target_os = "macos", target_os = "linux")))]
|
|
pub fn start(_wake: Sender<Wake>, _repaint: Repaint, _runtime: &Handle) -> Result<Tray> {
|
|
anyhow::bail!("No tray on this platform")
|
|
}
|