2021-12-04 09:31:26 +03:00
|
|
|
#![cfg_attr(
|
|
|
|
all(not(debug_assertions), target_os = "windows"),
|
|
|
|
windows_subsystem = "windows"
|
|
|
|
)]
|
|
|
|
|
|
|
|
extern crate tauri;
|
|
|
|
|
2021-12-17 21:01:02 +03:00
|
|
|
mod cmds;
|
2021-12-12 21:29:48 +03:00
|
|
|
mod config;
|
2021-12-14 13:37:03 +03:00
|
|
|
mod events;
|
2021-12-14 11:07:15 +03:00
|
|
|
mod utils;
|
2021-12-04 09:31:26 +03:00
|
|
|
|
2021-12-19 18:56:16 +03:00
|
|
|
use crate::{
|
|
|
|
events::state,
|
2021-12-26 21:29:28 +03:00
|
|
|
utils::{resolve, server},
|
2021-12-19 18:56:16 +03:00
|
|
|
};
|
2021-12-08 18:40:52 +03:00
|
|
|
use tauri::{
|
2021-12-14 13:37:03 +03:00
|
|
|
api, CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem,
|
2021-12-08 18:40:52 +03:00
|
|
|
};
|
2021-12-04 09:31:26 +03:00
|
|
|
|
|
|
|
fn main() -> std::io::Result<()> {
|
2021-12-26 21:29:28 +03:00
|
|
|
if server::check_singleton().is_err() {
|
2021-12-22 20:35:17 +03:00
|
|
|
println!("app exists");
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
2021-12-08 18:40:52 +03:00
|
|
|
let menu = SystemTrayMenu::new()
|
|
|
|
.add_item(CustomMenuItem::new("open_window", "显示应用"))
|
|
|
|
.add_native_item(SystemTrayMenuItem::Separator)
|
|
|
|
.add_item(CustomMenuItem::new("quit", "退出").accelerator("CmdOrControl+Q"));
|
2021-12-04 09:31:26 +03:00
|
|
|
|
2021-12-25 21:31:18 +03:00
|
|
|
tauri::Builder::default()
|
2021-12-26 21:29:28 +03:00
|
|
|
.manage(state::VergeConfLock::default())
|
|
|
|
.manage(state::ClashInfoState::default())
|
|
|
|
.manage(state::SomthingState::default())
|
|
|
|
.manage(state::ProfileLock::default())
|
|
|
|
.setup(|app| Ok(resolve::resolve_setup(app)))
|
2021-12-08 18:40:52 +03:00
|
|
|
.system_tray(SystemTray::new().with_menu(menu))
|
2021-12-26 21:29:28 +03:00
|
|
|
.on_system_tray_event(move |app_handle, event| match event {
|
2021-12-04 09:31:26 +03:00
|
|
|
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
|
2021-12-08 18:40:52 +03:00
|
|
|
"open_window" => {
|
2021-12-26 21:29:28 +03:00
|
|
|
let window = app_handle.get_window("main").unwrap();
|
2021-12-06 05:31:17 +03:00
|
|
|
window.show().unwrap();
|
|
|
|
window.set_focus().unwrap();
|
|
|
|
}
|
2021-12-08 18:40:52 +03:00
|
|
|
"quit" => {
|
2021-12-14 13:37:03 +03:00
|
|
|
api::process::kill_children();
|
2021-12-26 21:29:28 +03:00
|
|
|
resolve::resolve_reset(app_handle);
|
|
|
|
app_handle.exit(0);
|
2021-12-04 09:31:26 +03:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
2021-12-08 18:40:52 +03:00
|
|
|
SystemTrayEvent::LeftClick { .. } => {
|
2021-12-26 21:29:28 +03:00
|
|
|
let window = app_handle.get_window("main").unwrap();
|
2021-12-08 18:40:52 +03:00
|
|
|
window.show().unwrap();
|
|
|
|
window.set_focus().unwrap();
|
|
|
|
}
|
2021-12-04 09:31:26 +03:00
|
|
|
_ => {}
|
|
|
|
})
|
2021-12-08 18:40:52 +03:00
|
|
|
.invoke_handler(tauri::generate_handler![
|
2021-12-17 21:01:02 +03:00
|
|
|
cmds::some::restart_sidecar,
|
|
|
|
cmds::some::set_sys_proxy,
|
2021-12-19 18:56:16 +03:00
|
|
|
cmds::some::get_sys_proxy,
|
|
|
|
cmds::some::get_clash_info,
|
|
|
|
cmds::some::patch_clash_config,
|
|
|
|
cmds::some::get_verge_config,
|
|
|
|
cmds::some::patch_verge_config,
|
2021-12-17 21:01:02 +03:00
|
|
|
cmds::profile::import_profile,
|
|
|
|
cmds::profile::update_profile,
|
|
|
|
cmds::profile::get_profiles,
|
|
|
|
cmds::profile::set_profiles,
|
|
|
|
cmds::profile::put_profiles,
|
2021-12-08 18:40:52 +03:00
|
|
|
])
|
2021-12-04 09:31:26 +03:00
|
|
|
.build(tauri::generate_context!())
|
2021-12-25 21:31:18 +03:00
|
|
|
.expect("error while running tauri application")
|
|
|
|
.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 => {
|
2021-12-26 21:29:28 +03:00
|
|
|
resolve::resolve_reset(app_handle);
|
2021-12-25 21:31:18 +03:00
|
|
|
api::process::kill_children();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
});
|
2021-12-04 09:31:26 +03:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|