chore: something

This commit is contained in:
GyDi 2021-12-26 02:31:18 +08:00
parent a35cd28562
commit 7d878d2551

View File

@ -21,7 +21,6 @@ use crate::{
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use tauri::{ use tauri::{
api, CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem, api, CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem,
SystemTraySubmenu,
}; };
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
@ -30,23 +29,34 @@ fn main() -> std::io::Result<()> {
return Ok(()); return Ok(());
} }
let sub_menu = SystemTraySubmenu::new(
"出站规则",
SystemTrayMenu::new()
.add_item(CustomMenuItem::new("rway_global", "全局连接"))
.add_item(CustomMenuItem::new("rway_rule", "规则连接").selected())
.add_item(CustomMenuItem::new("rway_direct", "直接连接")),
);
let menu = SystemTrayMenu::new() let menu = SystemTrayMenu::new()
.add_submenu(sub_menu)
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("syste_proxy", "设置为系统代理"))
.add_item(CustomMenuItem::new("self_startup", "开机启动").selected())
.add_item(CustomMenuItem::new("open_window", "显示应用")) .add_item(CustomMenuItem::new("open_window", "显示应用"))
.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"));
let app = tauri::Builder::default() tauri::Builder::default()
.setup(|app| {
// a simple http server
embed_server(&app.handle());
// init app config
utils::init::init_app(app.package_info());
// run clash sidecar
let info = utils::clash::run_clash_bin(&app.handle());
// update the profile
let info_copy = info.clone();
tauri::async_runtime::spawn(async move {
match put_clash_profile(&info_copy).await {
Ok(_) => {}
Err(err) => log::error!("failed to put config for `{}`", err),
};
});
app.manage(state::VergeConfLock(Arc::new(Mutex::new(read_verge()))));
app.manage(state::ClashInfoState(Arc::new(Mutex::new(info))));
app.manage(state::ProfileLock::default());
Ok(())
})
.system_tray(SystemTray::new().with_menu(menu)) .system_tray(SystemTray::new().with_menu(menu))
.on_system_tray_event(move |app, event| match event { .on_system_tray_event(move |app, event| match event {
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() { SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
@ -83,42 +93,21 @@ fn main() -> std::io::Result<()> {
cmds::profile::put_profiles, cmds::profile::put_profiles,
]) ])
.build(tauri::generate_context!()) .build(tauri::generate_context!())
.expect("error while running tauri application"); .expect("error while running tauri application")
.run(|app_handle, e| match e {
// a simple http server tauri::Event::CloseRequested { label, api, .. } => {
embed_server(&app.handle()); let app_handle = app_handle.clone();
api.prevent_close();
// init app config app_handle.get_window(&label).unwrap().hide().unwrap();
utils::init::init_app(app.package_info()); }
// run clash sidecar tauri::Event::ExitRequested { api, .. } => {
let info = utils::clash::run_clash_bin(&app.handle()); api.prevent_exit();
// update the profile }
let info_copy = info.clone(); tauri::Event::Exit => {
tauri::async_runtime::spawn(async move { api::process::kill_children();
match put_clash_profile(&info_copy).await { }
Ok(_) => {} _ => {}
Err(err) => log::error!("failed to put config for `{}`", err), });
};
});
app.manage(state::VergeConfLock(Arc::new(Mutex::new(read_verge()))));
app.manage(state::ClashInfoState(Arc::new(Mutex::new(info))));
app.manage(state::ProfileLock::default());
app.run(|app_handle, e| match e {
tauri::Event::CloseRequested { label, api, .. } => {
let app_handle = app_handle.clone();
api.prevent_close();
app_handle.get_window(&label).unwrap().hide().unwrap();
}
tauri::Event::ExitRequested { api, .. } => {
api.prevent_exit();
}
tauri::Event::Exit => {
api::process::kill_children();
}
_ => {}
});
Ok(()) Ok(())
} }