You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
注意:虽然应用菜单和托盘菜单都是菜单,但除 CustomMenuItem 与 Menu 共用外,其他 API 不可以混用。
托盘菜单项
SystemTrayMenu::new()
add_item - 将自定义菜单项添加到系统托盘菜单
use tauri::{SystemTrayMenu,CustomMenuItem};SystemTrayMenu::new().add_item(CustomMenuItem::new("quit".to_string(),"Quit")).add_item(CustomMenuItem::new("close".to_string(),"Close"));
add_native_item - 将原生菜单项添加到系统托盘菜单
use tauri::{SystemTrayMenu,SystemTrayMenuItem,CustomMenuItem};SystemTrayMenu::new().add_item(CustomMenuItem::new("quit".to_string(),"Quit")).add_native_item(SystemTrayMenuItem::Separator)// ✅ 菜单分割线.add_item(CustomMenuItem::new("close".to_string(),"Close"));
add_submenu - 添加一个带有子菜单的条目
use tauri::{SystemTrayMenu,SystemTraySubmenu,SystemTrayMenuItem,CustomMenuItem};SystemTrayMenu::new().add_submenu(SystemTraySubmenu::new("File",SystemTrayMenu::new().add_item(CustomMenuItem::new("new_file".to_string(),"New File")).add_item(CustomMenuItem::new("edit_file".to_string(),"Edit File")),)).add_native_item(SystemTrayMenuItem::Separator).add_item(CustomMenuItem::new("hide".to_string(),"Hide")).add_item(CustomMenuItem::new("show".to_string(),"Show")).add_native_item(SystemTrayMenuItem::Separator).add_item(CustomMenuItem::new("quit".to_string(),"Quit"));
创建托盘菜单
新建 src-tauri/src/tray.rs 文件
use tauri::{AppHandle,SystemTrayEvent,SystemTray,CustomMenuItem,SystemTrayMenu,SystemTrayMenuItem,SystemTraySubmenu};// 托盘菜单pubfnmenu() -> SystemTray{let tray_menu = SystemTrayMenu::new().add_submenu(SystemTraySubmenu::new(// 子菜单"File",// 子菜单名称SystemTrayMenu::new().add_item(CustomMenuItem::new("new_file".to_string(),"New File"))// 子菜单项(新增).add_item(CustomMenuItem::new("edit_file".to_string(),"Edit File")),// 子菜单项(编辑))).add_native_item(SystemTrayMenuItem::Separator)// 分割线.add_item(CustomMenuItem::new("hide".to_string(),"Hide"))// 隐藏应用窗口.add_item(CustomMenuItem::new("show".to_string(),"Show"))// 显示应用窗口.add_native_item(SystemTrayMenuItem::Separator)// 分割线.add_item(CustomMenuItem::new("quit".to_string(),"Quit"));// 退出// 设置在右键单击系统托盘时显示菜单SystemTray::new().with_menu(tray_menu)}// 菜单事件pubfnhandler(app:&AppHandle,event:SystemTrayEvent){// 获取应用窗口let window = app.get_window("main").unwrap();let parent_window = Some(&window);// 匹配点击事件match event {// 左键点击SystemTrayEvent::LeftClick{position: _,size: _,
..
} => {println!("system tray received a left click");}// 右键点击SystemTrayEvent::RightClick{position: _,size: _,
..
} => {println!("system tray received a right click");}// 双击,macOS / Linux 不支持SystemTrayEvent::DoubleClick{position: _,size: _,
..
} => {println!("system tray received a double click");}// 根据菜单 id 进行事件匹配SystemTrayEvent::MenuItemClick{ id, .. } => match id.as_str(){"edit_file" => {message(parent_window,"Eidt File","TODO");}"new_file" => {message(parent_window,"New File","TODO");}"quit" => {
std::process::exit(0);}"show" => {
window.show().unwrap();}"hide" => {
window.hide().unwrap();}
_ => {}},
_ => {}}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
系统托盘允许用户通过系统状态栏中的托盘图标便捷访问应用或其特定菜单项。
基本配置
编辑
src-tauri/tauri.conf.json
文件iconAsTemplate
是一个布尔值,用于确定图像是否表示 macOS 上的模板图像(为true
时图像应仅包含黑色和清晰的颜色,为false
时图片显示原色)。在 Linux 上,则需要安装软件包对其进行支持,从官方文档了解 Linux 设置。编辑
src-tauri/src/main.rs
文件注意:点击托盘图标无响应,是因为并未对其添加菜单和事件。
托盘菜单 & 事件
菜单 API
SystemTrayMenu
- 创建一个新菜单SystemTraySubmenu
- 使用给定的标题和菜单项创建一个新的子菜单CustomMenuItem
- 自定义菜单项,与Menu
中的CustomMenuItem
用法一致SystemTrayMenuItem
- 原生菜单项,目前仅支持菜单分割符SystemTrayMenuItem::Separator
注意:虽然应用菜单和托盘菜单都是菜单,但除
CustomMenuItem
与 Menu 共用外,其他 API 不可以混用。托盘菜单项
add_item
- 将自定义菜单项添加到系统托盘菜单add_native_item
- 将原生菜单项添加到系统托盘菜单add_submenu
- 添加一个带有子菜单的条目创建托盘菜单
新建
src-tauri/src/tray.rs
文件编辑
src-tauri/src/main.rs
文件注意:只创建菜单项,而不添加事件处理,点击菜单依然会无响应。
更新系统托盘
AppHandle
上有一个tray_handle
方法,用于返回系统托盘的句柄,可以用来更新托盘图标和上下文菜单项。更新
src-tauri/src/tray.rs
文件编辑
src-tauri/Cargo.toml
文件,tauri 新增icon-png
,直接使用tauri::Icon
API 设置托盘图标会报错,具体原因可以查看此 issues Path variant of tauri::Icon cannot be found, despite being a documented variantBeta Was this translation helpful? Give feedback.
All reactions