#icons #tray #menu #freedesktop #system #access #api-access

stray

freedesktop StatusNotifierWatcher 实现

4 个版本

0.1.3 2023年1月22日
0.1.2 2022年10月16日
0.1.1 2022年5月3日
0.1.0 2022年4月27日

#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 下载量
2 crates 中使用

MIT 许可证

50KB
960 行代码

Stray

Stray 是一个最小的 SystemNotifierWatcher 实现,其目标是提供一个简洁的 API 来访问托盘图标和菜单。

示例

启动系统托盘并监听更改

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}");
            }
        }
    }
}

向系统托盘发送菜单激活请求

 // 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 示例

对于详细的真实示例,您可以查看 gtk-tray

git clone [email protected]:oknozor/stray.git
cd stray/gtk-tray
cargo run

依赖关系

~14–27MB
~407K SLoC