Skip to content

Commit

Permalink
feat: add version on tray
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzgydi committed Nov 1, 2022
1 parent eb28ec8 commit 56fe7b3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
27 changes: 21 additions & 6 deletions src-tauri/src/core/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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", "打开面板"))
Expand All @@ -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 {
Expand All @@ -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(())
}
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions src-tauri/src/utils/resolve.rs
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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)
};
Expand Down

0 comments on commit 56fe7b3

Please sign in to comment.