capri-ui: add tray module

This commit is contained in:
nocode committed 2026-11-11 20:20:49 +01:00
1 parent 62e6b5c645
commit 9e52153eaa
1 file changed
+90
+90
View File
@@ -0,0 +1,90 @@
//! Tray icon and menu as a StatusNotifierItem: native on KDE Plasma; on i3 it needs an
//! SNI host (`snixembed` bridging to i3bar's XEmbed tray, or a bar with SNI support).
use std::sync::Arc;
use ksni::menu::{CheckmarkItem, StandardItem};
use ksni::{Icon, MenuItem, ToolTip, TrayMethods};
use parking_lot::Mutex;
use crate::hooks::{Command, UiHandle};
use crate::i18n::t;
pub struct Tray {
ui: UiHandle,
icon: Vec<Icon>,
}
fn item(label: &str, command: impl Fn() -> Command + Send + Sync + 'static) -> MenuItem<Tray> {
StandardItem { label: t(label).replace('_', "__"), activate: Box::new(move |tray: &mut Tray| tray.ui.command(command())), ..Default::default() }.into()
}
fn check(label: &str, checked: bool, command: impl Fn(bool) -> Command + Send + Sync + 'static) -> MenuItem<Tray> {
CheckmarkItem { label: t(label), checked, activate: Box::new(move |tray: &mut Tray| tray.ui.command(command(!checked))), ..Default::default() }.into()
}
impl ksni::Tray for Tray {
fn id(&self) -> String {
"capri".into()
}
fn title(&self) -> String {
"Capri".into()
}
fn icon_pixmap(&self) -> Vec<Icon> {
self.icon.clone()
}
fn tool_tip(&self) -> ToolTip {
ToolTip { title: t("Capri desktop companion"), ..Default::default() }
}
fn activate(&mut self, _x: i32, _y: i32) {
self.ui.show();
}
fn menu(&self) -> Vec<MenuItem<Self>> {
let (detached, focus, on_top) = self.ui.0.state.lock().tray;
vec![
item("Show Capri", || Command::Show),
item("Settings", || Command::Settings(None)),
item("Teleport Capri", || Command::Teleport),
StandardItem { label: t("Return to frame"), visible: detached, activate: Box::new(|tray: &mut Tray| tray.ui.command(Command::Return)), ..Default::default() }.into(),
check("Focus mode", focus, |_| Command::ToggleFocus),
item("Reset to bottom-right corner", || Command::ResetPosition),
check("Always on top", on_top, Command::AlwaysOnTop),
MenuItem::Separator,
item("Hide to tray", || Command::Hide),
MenuItem::Separator,
item("Quit and unload models", || Command::Quit),
]
}
}
/// ARGB32 big-endian pixmap from a PNG.
fn icon(png: &[u8]) -> Vec<Icon> {
let Ok(image) = image::load_from_memory(png) else { return vec![] };
let image = image.resize(64, 64, image::imageops::FilterType::Triangle).into_rgba8();
let data = image.pixels().flat_map(|p| [p[3], p[0], p[1], p[2]]).collect();
vec![Icon { width: image.width() as i32, height: image.height() as i32, data }]
}
pub type TrayHandle = Arc<Mutex<Option<ksni::Handle<Tray>>>>;
/// Starts the tray; `png` is Capri's icon when installed, else the project symbol.
pub fn spawn(rt: &tokio::runtime::Handle, ui: UiHandle, png: &[u8]) -> TrayHandle {
let handle: TrayHandle = Default::default();
let tray = Tray { ui, icon: icon(png) };
let slot = handle.clone();
rt.spawn(async move {
match tray.spawn().await {
Ok(h) => *slot.lock() = Some(h),
Err(e) => tracing::warn!("tray icon unavailable (no StatusNotifierItem host?): {e}"),
}
});
handle
}
/// Rebuilds the menu after the checkbox state changed.
pub fn refresh(rt: &tokio::runtime::Handle, handle: &TrayHandle) {
if let Some(h) = handle.lock().clone() {
rt.spawn(async move {
h.update(|_| {}).await;
});
}
}