1 个不稳定版本
0.13.1 | 2024年4月8日 |
---|
#696 在 GUI
79KB
1.5K SLoC
tray-icon 允许您为桌面应用程序创建托盘图标。
支持的平台
- Windows
- macOS
- Linux (仅支持 gtk)
平台特定说明
- 在 Windows 和 Linux 上,必须在线程上运行事件循环,在 Windows 上是 win32 事件循环,在 Linux 上是 gtk 事件循环。它不需要在主线程上运行,但您必须在事件循环相同的线程上创建托盘图标。
- 在 macOS 上,必须在主线程上运行事件循环,因此您还必须在主线程上创建托盘图标。
Cargo 功能
common-controls-v6
:在 Windows 上使用来自ComCtl32.dll
v6 的TaskDialogIndirect
API 来显示预定义的About
菜单项对话框。libxdo
:启用链接到libxdo
,它用于预定义的Copy
、Cut
、Paste
和SelectAll
菜单项,请参阅 https://github.com/tauri-apps/muda#cargo-featuresserde
:启用 de/serializing derive。
依赖项(仅限 Linux)
在 Linux 上,使用 gtk
和 libxdo
来使预定义的 Copy
、Cut
、Paste
和 SelectAll
菜单项工作,并使用 libappindicator
或 libayatnat-appindicator
来创建托盘图标,因此请确保在您的系统上安装它们。
Arch Linux / Manjaro
pacman -S gtk3 xdotool libappindicator-gtk3 #or libayatana-appindicator
Debian / Ubuntu
sudo apt install libgtk-3-dev libxdo-dev libappindicator3-dev #or libayatana-appindicator3-dev
示例
创建不带菜单的托盘图标。
use tray_icon::TrayIconBuilder;
let tray_icon = TrayIconBuilder::new()
.with_tooltip("system-tray - tray icon library!")
.with_icon(icon)
.build()
.unwrap();
创建带菜单的托盘图标。
use tray_icon::{TrayIconBuilder, menu::Menu};
let tray_menu = Menu::new();
let tray_icon = TrayIconBuilder::new()
.with_menu(Box::new(tray_menu))
.with_tooltip("system-tray - tray icon library!")
.with_icon(icon)
.build()
.unwrap();
处理托盘事件
您可以使用 TrayIconEvent::receiver
来获取对 TrayIconEventReceiver
的引用,您可以使用它来监听托盘图标上点击时发生的事件。
use tray_icon::TrayIconEvent;
if let Ok(event) = TrayIconEvent::receiver().try_recv() {
println!("{:?}", event);
}
您还可以使用 MenuEvent::receiver
来监听菜单事件,以获取托盘上下文菜单的事件。
use tray_icon::{TrayIconEvent, menu::{MenuEvent}};
if let Ok(event) = TrayIconEvent::receiver().try_recv() {
println!("tray event: {:?}", event);
}
if let Ok(event) = MenuEvent::receiver().try_recv() {
println!("menu event: {:?}", event);
}
许可证
Apache-2.0/MIT
依赖项
~1–14MB
~166K SLoC