use tray_icon::Icon;
const TRAY_ICON_SIZE: u32 = 32;
pub fn load_tray_icon_from_png(bytes: &[u8]) -> Result<Icon, String> {
let image = image::load_from_memory(bytes)
.map_err(|error| format!("Failed to decode tray icon: {error}"))?
.into_rgba8();
let image = if image.width() > TRAY_ICON_SIZE || image.height() > TRAY_ICON_SIZE {
image::imageops::resize(
&image,
TRAY_ICON_SIZE,
TRAY_ICON_SIZE,
image::imageops::FilterType::Lanczos3,
)
} else {
image
};
let (width, height) = (image.width(), image.height());
Icon::from_rgba(image.into_raw(), width, height)
.map_err(|error| format!("Failed to create tray icon: {error}"))
}
pub fn pump_tray_events() {
#[cfg(windows)]
pump_windows_events();
#[cfg(target_os = "linux")]
pump_gtk_events();
}
#[cfg(windows)]
fn pump_windows_events() {
use std::mem::zeroed;
use windows_sys::Win32::UI::WindowsAndMessaging::{
DispatchMessageW, PeekMessageW, TranslateMessage, MSG, PM_REMOVE,
};
unsafe {
let mut msg: MSG = zeroed();
while PeekMessageW(&mut msg, std::ptr::null_mut(), 0, 0, PM_REMOVE) != 0 {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
}
#[cfg(target_os = "linux")]
fn pump_gtk_events() {
while gtk::events_pending() {
gtk::main_iteration();
}
}