From 92418de6b647e7f79bf63588f87a0491b8205888 Mon Sep 17 00:00:00 2001 From: Yezzi Hsueh Date: Fri, 1 Nov 2024 07:31:23 +0800 Subject: [PATCH] feat: set system tray --- src-tauri/Cargo.toml | 2 +- src-tauri/src/lib.rs | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 2a9f12a..c073b8d 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-shell = "2" serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index f91b35e..156040d 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -6,7 +6,32 @@ fn greet(name: &str) -> String { #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { + use tauri::{ + menu::{Menu, MenuItem}, + tray::TrayIconBuilder, + }; + tauri::Builder::default() + .setup(|app| { + let quit_i = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?; + let menu = Menu::with_items(app, &[&quit_i])?; + + let _tray: tauri::tray::TrayIcon = TrayIconBuilder::new() + .icon(app.default_window_icon().unwrap().clone()) + .menu(&menu) + .menu_on_left_click(true) + .on_menu_event(|app, event| match event.id.as_ref() { + "quit" => { + println!("quit menu item was clicked"); + app.exit(0); + } + _ => { + println!("menu item {:?} not handled", event.id); + } + }) + .build(app)?; + Ok(()) + }) .plugin(tauri_plugin_shell::init()) .invoke_handler(tauri::generate_handler![greet]) .run(tauri::generate_context!())