Stray is a minimal SystemNotifierWatcher implementation which goal is to provide a minimalistic API to access tray icons and menu.
```rust, ignore use stray::{SystemTray}; use tokio_stream::StreamExt; use stray::message::NotifierItemMessage; use stray::message::NotifierItemCommand;
async fn main() {
// A mpsc channel to send menu activation requests later
let (ui_tx, ui_rx) = tokio::sync::mpsc::channel(32);
let mut tray = SystemTray::new(ui_rx).await;
while let Some(message) = tray.next().await {
match message {
NotifierItemMessage::Update { address: id, item, menu } => {
println!("NotifierItem updated :
id = {id},
item = {item:?},
menu = {menu:?}"
)
}
NotifierItemMessage::Remove { address: id } => {
println!("NotifierItem removed : id = {id}");
}
}
}
} ```
```rust, ignore // Assuming we stored our menu items in some UI state we can send menu item activation request: use stray::message::NotifierItemCommand;
uitx.clone().trysend(NotifierItemCommand::MenuItemClicked {
// The submenu to activate
submenuid: 32,
// dbus menu path, available in the StatusNotifierItem
menupath: "/org/ayatana/NotificationItem/Element1/Menu".tostring(),
// the notifier address we previously got from NotifierItemMessage::Update
notifieraddress: ":1.2161".to_string(),
}).unwrap();
```
For a detailed, real life example, you can take a look at the gtk-tray.
shell
git clone git@github.com:oknozor/stray.git
cd stray/gtk-tray
cargo run