跳至主要內容

系統托盤

原生應用程式系統托盤。

設定

設定 tauri.conf.json 上的 systemTray 物件

{
"tauri": {
"systemTray": {
"iconPath": "icons/icon.png",
"iconAsTemplate": true
}
}
}

iconAsTemplate 是布林值,用於判斷圖像是否代表 macOS 上的 範本圖像

Linux 設定

在 Linux 上,您需要安裝 libayatana-appindicatorlibappindicator3 套件之一。Tauri 會在執行階段判斷要使用哪個套件,如果兩個都已安裝,則優先使用 libayatana

預設情況下,Debian 套件 (.deb 檔案) 會新增對 libayatana-appindicator3-1 的依賴關係。若要建立目標為 libappindicator3 的 Debian 套件,請將 TAURI_TRAY 環境變數設定為 libappindicator3

AppImage 軟體包會自動嵌入已安裝的托盤程式庫,您也可以使用 TAURI_TRAY 環境變數手動選擇它。

資訊

libappindicator3 已停止維護,且在某些發行版(例如 debian11)上不存在,但 libayatana-appindicator 在較舊的版本上不存在。

建立系統托盤

要建立原生系統托盤,請匯入 SystemTray 類型

use tauri::SystemTray;

初始化新的托盤實例

let tray = SystemTray::new();

設定系統托盤內容選單

您可選擇在右鍵按一下托盤圖示時顯示內容選單。匯入 SystemTrayMenuSystemTrayMenuItemCustomMenuItem 類型

use tauri::{CustomMenuItem, SystemTrayMenu, SystemTrayMenuItem};

建立 SystemTrayMenu

// here `"quit".to_string()` defines the menu item id, and the second parameter is the menu item label.
let quit = CustomMenuItem::new("quit".to_string(), "Quit");
let hide = CustomMenuItem::new("hide".to_string(), "Hide");
let tray_menu = SystemTrayMenu::new()
.add_item(quit)
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(hide);

將托盤選單新增至 SystemTray 實例

let tray = SystemTray::new().with_menu(tray_menu);

設定應用程式系統托盤

已建立的 SystemTray 實例可以使用 tauri::Builder 結構上的 system_tray API 設定

use tauri::{CustomMenuItem, SystemTray, SystemTrayMenu};

fn main() {
let tray_menu = SystemTrayMenu::new(); // insert the menu items here
let system_tray = SystemTray::new()
.with_menu(tray_menu);
tauri::Builder::default()
.system_tray(system_tray)
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

聆聽系統托盤事件

每個 CustomMenuItem 在按一下時會觸發事件。此外,Tauri 會發出托盤圖示按一下事件。使用 on_system_tray_event API 處理這些事件

use tauri::{CustomMenuItem, SystemTray, SystemTrayMenu, SystemTrayEvent};
use tauri::Manager;

fn main() {
let tray_menu = SystemTrayMenu::new(); // insert the menu items here
tauri::Builder::default()
.system_tray(SystemTray::new().with_menu(tray_menu))
.on_system_tray_event(|app, event| match event {
SystemTrayEvent::LeftClick {
position: _,
size: _,
..
} => {
println!("system tray received a left click");
}
SystemTrayEvent::RightClick {
position: _,
size: _,
..
} => {
println!("system tray received a right click");
}
SystemTrayEvent::DoubleClick {
position: _,
size: _,
..
} => {
println!("system tray received a double click");
}
SystemTrayEvent::MenuItemClick { id, .. } => {
match id.as_str() {
"quit" => {
std::process::exit(0);
}
"hide" => {
let window = app.get_window("main").unwrap();
window.hide().unwrap();
}
_ => {}
}
}
_ => {}
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

更新系統托盤

AppHandle 結構有一個 tray_handle 方法,它會傳回系統托盤的控制代碼,允許更新托盤圖示和內容選單項目

更新內容選單項目

use tauri::{CustomMenuItem, SystemTray, SystemTrayMenu, SystemTrayEvent};
use tauri::Manager;

fn main() {
let tray_menu = SystemTrayMenu::new(); // insert the menu items here
tauri::Builder::default()
.system_tray(SystemTray::new().with_menu(tray_menu))
.on_system_tray_event(|app, event| match event {
SystemTrayEvent::MenuItemClick { id, .. } => {
// get a handle to the clicked menu item
// note that `tray_handle` can be called anywhere,
// just get an `AppHandle` instance with `app.handle()` on the setup hook
// and move it to another function or thread
let item_handle = app.tray_handle().get_item(&id);
match id.as_str() {
"hide" => {
let window = app.get_window("main").unwrap();
window.hide().unwrap();
// you can also `set_selected`, `set_enabled` and `set_native_image` (macOS only).
item_handle.set_title("Show").unwrap();
}
_ => {}
}
}
_ => {}
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

更新托盤圖示

請注意,你需要在 Cargo.toml 中的 tauri 相依性中加入 icon-icoicon-png 功能標記,才能使用 Icon::Raw

app.tray_handle().set_icon(tauri::Icon::Raw(include_bytes!("../path/to/myicon.ico").to_vec())).unwrap();

防止應用程式關閉

預設情況下,當最後一個視窗關閉時,Tauri 會關閉應用程式。你可以簡單地呼叫 api.prevent_close() 來防止這種情況。

根據你的需求,你可以使用以下兩個選項之一

讓後端在背景中執行

如果你的後端應該在背景中執行,你可以這樣呼叫 api.prevent_exit()

tauri::Builder::default()
.build(tauri::generate_context!())
.expect("error while building tauri application")
.run(|_app_handle, event| match event {
tauri::RunEvent::ExitRequested { api, .. } => {
api.prevent_exit();
}
_ => {}
});

讓前端在背景中執行

如果你需要讓前端在背景中執行,可以這樣實現

tauri::Builder::default().on_window_event(|event| match event.event() {
tauri::WindowEvent::CloseRequested { api, .. } => {
event.window().hide().unwrap();
api.prevent_close();
}
_ => {}
})
.run(tauri::generate_context!())
.expect("error while running tauri application");