4 releases

0.1.3 Jan 22, 2023
0.1.2 Oct 16, 2022
0.1.1 May 3, 2022
0.1.0 Apr 27, 2022

#731 in GUI

Download history 29/week @ 2024-03-11 28/week @ 2024-03-18 152/week @ 2024-03-25 31/week @ 2024-04-01 11/week @ 2024-04-08 5/week @ 2024-04-15 7/week @ 2024-04-22 22/week @ 2024-04-29 18/week @ 2024-05-06 26/week @ 2024-05-13 13/week @ 2024-05-20 17/week @ 2024-05-27 23/week @ 2024-06-03 16/week @ 2024-06-10 10/week @ 2024-06-17 23/week @ 2024-06-24

73 downloads per month
Used in 2 crates

MIT license

50KB
960 lines

Stray

Stray is a minimal SystemNotifierWatcher implementation which goal is to provide a minimalistic API to access tray icons and menu.

Examples

Start the system tray and listen for changes

use stray::{SystemTray};
use tokio_stream::StreamExt;
use stray::message::NotifierItemMessage;
use stray::message::NotifierItemCommand;

#[tokio::main]
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}");
            }
        }
    }
}

Send menu activation request to the system tray

 // Assuming we stored our menu items in some UI state we can send menu item activation request:
 use stray::message::NotifierItemCommand;

 ui_tx.clone().try_send(NotifierItemCommand::MenuItemClicked {
    // The submenu to activate
    submenu_id: 32,
    // dbus menu path, available in the `StatusNotifierItem`
    menu_path: "/org/ayatana/NotificationItem/Element1/Menu".to_string(),
    // the notifier address we previously got from `NotifierItemMessage::Update`
    notifier_address: ":1.2161".to_string(),
 }).unwrap();

Gtk example

For a detailed, real life example, you can take a look at the gtk-tray.

git clone git@github.com:oknozor/stray.git
cd stray/gtk-tray
cargo run

Dependencies

~14–27MB
~407K SLoC