From 18ca4fd6759907bf8b7d95c4db3d863b2be0e21d Mon Sep 17 00:00:00 2001 From: selcarpa Date: Thu, 30 Oct 2025 13:16:40 +0800 Subject: [PATCH] feat(tray): Adds a system tray icon and exit menu - Adds the `tray-icon` attribute to `tauri` in `Cargo.toml` - Imports system tray-related menu and builder modules --- src-tauri/Cargo.toml | 2 +- src-tauri/src/lib.rs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 2b2da85..ccfab10 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -18,7 +18,7 @@ crate-type = ["staticlib", "cdylib", "rlib"] tauri-build = { version = "2", features = [] } [dependencies] -tauri = { version = "2", features = [] } +tauri = { version = "2", features = ["tray-icon"] } tauri-plugin-opener = "2" serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 7d96862..e94fad3 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -1,7 +1,10 @@ // Learn more about Tauri commands at https://tauri.app/develop/calling-rust/ use crate::spary::spary_switch; +use tauri::menu::{Menu, MenuItem}; +use tauri::tray::TrayIconBuilder; use tauri_plugin_sql::{Migration, MigrationKind}; + mod spary; #[cfg_attr(mobile, tauri::mobile_entry_point)] @@ -26,6 +29,7 @@ pub fn run() { kind:MigrationKind::Up, } ]; + tauri::Builder::default() .plugin(tauri_plugin_process::init()) .plugin(tauri_plugin_shell::init()) @@ -38,6 +42,20 @@ pub fn run() { ) .plugin(tauri_plugin_opener::init()) .invoke_handler(tauri::generate_handler![spary_switch]) + .setup(|app| { + let quit_item = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?; + let menu = Menu::with_items(app, &[&quit_item])?; + let tray = TrayIconBuilder::new() + .tooltip("spary") + .on_menu_event(move |app, event| { + if event.id().as_ref() == "quit" { + app.exit(0); + } + }) + .build(app)?; + tray.set_menu(Some(menu))?; + Ok(()) + }) .run(tauri::generate_context!()) .expect("error while running tauri application"); }