diff --git a/src-tauri/src/core/tray.rs b/src-tauri/src/core/tray.rs index 7bef9ae..6b3fb99 100644 --- a/src-tauri/src/core/tray.rs +++ b/src-tauri/src/core/tray.rs @@ -2,18 +2,21 @@ use crate::{data::Data, feat, utils::resolve}; use anyhow::{Ok, Result}; use tauri::{ api, AppHandle, CustomMenuItem, Manager, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem, + SystemTraySubmenu, }; pub struct Tray {} impl Tray { - pub fn tray_menu() -> SystemTrayMenu { + pub fn tray_menu(app_handle: &AppHandle) -> SystemTrayMenu { let data = Data::global(); let zh = { let verge = data.verge.lock(); verge.language == Some("zh".into()) }; + let version = app_handle.package_info().version.to_string(); + if zh { SystemTrayMenu::new() .add_item(CustomMenuItem::new("open_window", "打开面板")) @@ -25,8 +28,13 @@ impl Tray { .add_native_item(SystemTrayMenuItem::Separator) .add_item(CustomMenuItem::new("system_proxy", "系统代理")) .add_item(CustomMenuItem::new("tun_mode", "TUN 模式")) - .add_item(CustomMenuItem::new("restart_clash", "重启 Clash")) - .add_item(CustomMenuItem::new("restart_app", "重启应用")) + .add_submenu(SystemTraySubmenu::new( + "更多", + SystemTrayMenu::new() + .add_item(CustomMenuItem::new("restart_clash", "重启 Clash")) + .add_item(CustomMenuItem::new("restart_app", "重启应用")) + .add_item(CustomMenuItem::new("app_version", format!("Version {version}")).disabled()), + )) .add_native_item(SystemTrayMenuItem::Separator) .add_item(CustomMenuItem::new("quit", "退出").accelerator("CmdOrControl+Q")) } else { @@ -40,15 +48,22 @@ impl Tray { .add_native_item(SystemTrayMenuItem::Separator) .add_item(CustomMenuItem::new("system_proxy", "System Proxy")) .add_item(CustomMenuItem::new("tun_mode", "Tun Mode")) - .add_item(CustomMenuItem::new("restart_clash", "Restart Clash")) - .add_item(CustomMenuItem::new("restart_app", "Restart App")) + .add_submenu(SystemTraySubmenu::new( + "More", + SystemTrayMenu::new() + .add_item(CustomMenuItem::new("restart_clash", "Restart Clash")) + .add_item(CustomMenuItem::new("restart_app", "Restart App")) + .add_item(CustomMenuItem::new("app_version", format!("Version {version}")).disabled()), + )) .add_native_item(SystemTrayMenuItem::Separator) .add_item(CustomMenuItem::new("quit", "Quit").accelerator("CmdOrControl+Q")) } } pub fn update_systray(app_handle: &AppHandle) -> Result<()> { - app_handle.tray_handle().set_menu(Tray::tray_menu())?; + app_handle + .tray_handle() + .set_menu(Tray::tray_menu(app_handle))?; Tray::update_part(app_handle)?; Ok(()) } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 26cad7c..1d7d15a 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -31,7 +31,7 @@ fn main() -> std::io::Result<()> { #[allow(unused_mut)] let mut builder = tauri::Builder::default() .setup(|app| Ok(resolve::resolve_setup(app))) - .system_tray(SystemTray::new().with_menu(core::tray::Tray::tray_menu())) + .system_tray(SystemTray::new()) .on_system_tray_event(core::tray::Tray::on_system_tray_event) .invoke_handler(tauri::generate_handler![ // common diff --git a/src-tauri/src/utils/resolve.rs b/src-tauri/src/utils/resolve.rs index 2665a37..aa1ba32 100644 --- a/src-tauri/src/utils/resolve.rs +++ b/src-tauri/src/utils/resolve.rs @@ -1,8 +1,17 @@ -use crate::{core::Core, data::Data, utils::init, utils::server}; +use crate::{ + core::{tray, Core}, + data::Data, + utils::init, + utils::server, +}; use tauri::{App, AppHandle, Manager}; /// handle something when start app pub fn resolve_setup(app: &App) { + let _ = app + .tray_handle() + .set_menu(tray::Tray::tray_menu(&app.app_handle())); + init::init_resources(app.package_info()); let silent_start = { @@ -11,7 +20,7 @@ pub fn resolve_setup(app: &App) { let singleton = verge.app_singleton_port.clone(); // setup a simple http server for singleton - server::embed_server(&app.handle(), singleton); + server::embed_server(&app.app_handle(), singleton); verge.enable_silent_start.clone().unwrap_or(false) };