feat: add version on tray

This commit is contained in:
GyDi 2022-11-01 23:29:59 +08:00
parent eb28ec866a
commit 56fe7b3596
No known key found for this signature in database
GPG Key ID: 58B15242BA8277A6
3 changed files with 33 additions and 9 deletions

View File

@ -2,18 +2,21 @@ use crate::{data::Data, feat, utils::resolve};
use anyhow::{Ok, Result}; use anyhow::{Ok, Result};
use tauri::{ use tauri::{
api, AppHandle, CustomMenuItem, Manager, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem, api, AppHandle, CustomMenuItem, Manager, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem,
SystemTraySubmenu,
}; };
pub struct Tray {} pub struct Tray {}
impl Tray { impl Tray {
pub fn tray_menu() -> SystemTrayMenu { pub fn tray_menu(app_handle: &AppHandle) -> SystemTrayMenu {
let data = Data::global(); let data = Data::global();
let zh = { let zh = {
let verge = data.verge.lock(); let verge = data.verge.lock();
verge.language == Some("zh".into()) verge.language == Some("zh".into())
}; };
let version = app_handle.package_info().version.to_string();
if zh { if zh {
SystemTrayMenu::new() SystemTrayMenu::new()
.add_item(CustomMenuItem::new("open_window", "打开面板")) .add_item(CustomMenuItem::new("open_window", "打开面板"))
@ -25,8 +28,13 @@ impl Tray {
.add_native_item(SystemTrayMenuItem::Separator) .add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("system_proxy", "系统代理")) .add_item(CustomMenuItem::new("system_proxy", "系统代理"))
.add_item(CustomMenuItem::new("tun_mode", "TUN 模式")) .add_item(CustomMenuItem::new("tun_mode", "TUN 模式"))
.add_item(CustomMenuItem::new("restart_clash", "重启 Clash")) .add_submenu(SystemTraySubmenu::new(
.add_item(CustomMenuItem::new("restart_app", "重启应用")) "更多",
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_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("quit", "退出").accelerator("CmdOrControl+Q")) .add_item(CustomMenuItem::new("quit", "退出").accelerator("CmdOrControl+Q"))
} else { } else {
@ -40,15 +48,22 @@ impl Tray {
.add_native_item(SystemTrayMenuItem::Separator) .add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("system_proxy", "System Proxy")) .add_item(CustomMenuItem::new("system_proxy", "System Proxy"))
.add_item(CustomMenuItem::new("tun_mode", "Tun Mode")) .add_item(CustomMenuItem::new("tun_mode", "Tun Mode"))
.add_item(CustomMenuItem::new("restart_clash", "Restart Clash")) .add_submenu(SystemTraySubmenu::new(
.add_item(CustomMenuItem::new("restart_app", "Restart App")) "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_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("quit", "Quit").accelerator("CmdOrControl+Q")) .add_item(CustomMenuItem::new("quit", "Quit").accelerator("CmdOrControl+Q"))
} }
} }
pub fn update_systray(app_handle: &AppHandle) -> Result<()> { 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)?; Tray::update_part(app_handle)?;
Ok(()) Ok(())
} }

View File

@ -31,7 +31,7 @@ fn main() -> std::io::Result<()> {
#[allow(unused_mut)] #[allow(unused_mut)]
let mut builder = tauri::Builder::default() let mut builder = tauri::Builder::default()
.setup(|app| Ok(resolve::resolve_setup(app))) .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) .on_system_tray_event(core::tray::Tray::on_system_tray_event)
.invoke_handler(tauri::generate_handler![ .invoke_handler(tauri::generate_handler![
// common // common

View File

@ -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}; use tauri::{App, AppHandle, Manager};
/// handle something when start app /// handle something when start app
pub fn resolve_setup(app: &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()); init::init_resources(app.package_info());
let silent_start = { let silent_start = {
@ -11,7 +20,7 @@ pub fn resolve_setup(app: &App) {
let singleton = verge.app_singleton_port.clone(); let singleton = verge.app_singleton_port.clone();
// setup a simple http server for singleton // 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) verge.enable_silent_start.clone().unwrap_or(false)
}; };