Files
clash-proxy/src-tauri/src/main.rs

127 lines
3.8 KiB
Rust
Raw Normal View History

2021-12-04 14:31:26 +08:00
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
mod cmds;
2022-01-08 01:27:25 +08:00
mod core;
2022-01-07 23:29:20 +08:00
mod states;
2021-12-14 16:07:15 +08:00
mod utils;
2021-12-04 14:31:26 +08:00
2022-01-07 23:29:20 +08:00
use crate::utils::{resolve, server};
2021-12-08 23:40:52 +08:00
use tauri::{
2022-02-27 01:29:57 +08:00
api, CustomMenuItem, Manager, Menu, MenuItem, Submenu, SystemTray, SystemTrayEvent,
SystemTrayMenu, SystemTrayMenuItem,
2021-12-08 23:40:52 +08:00
};
2021-12-04 14:31:26 +08:00
fn main() -> std::io::Result<()> {
2021-12-27 02:29:28 +08:00
if server::check_singleton().is_err() {
println!("app exists");
return Ok(());
}
2022-02-27 01:29:57 +08:00
let submenu_file = Submenu::new(
"File",
Menu::new()
.add_native_item(MenuItem::Undo)
.add_native_item(MenuItem::Redo)
.add_native_item(MenuItem::Copy)
.add_native_item(MenuItem::Paste)
.add_native_item(MenuItem::Cut)
.add_native_item(MenuItem::SelectAll),
);
let tray_menu = SystemTrayMenu::new()
2022-01-08 02:54:37 +08:00
.add_item(CustomMenuItem::new("open_window", "Show"))
.add_item(CustomMenuItem::new("restart_clash", "Restart Clash"))
2021-12-08 23:40:52 +08:00
.add_native_item(SystemTrayMenuItem::Separator)
2022-01-08 02:54:37 +08:00
.add_item(CustomMenuItem::new("quit", "Quit").accelerator("CmdOrControl+Q"));
2021-12-04 14:31:26 +08:00
2021-12-26 02:31:18 +08:00
tauri::Builder::default()
2022-01-07 23:29:20 +08:00
.manage(states::VergeState::default())
.manage(states::ClashState::default())
.manage(states::ProfilesState::default())
2021-12-27 02:29:28 +08:00
.setup(|app| Ok(resolve::resolve_setup(app)))
2022-02-27 01:29:57 +08:00
.menu(Menu::new().add_submenu(submenu_file))
.system_tray(SystemTray::new().with_menu(tray_menu))
2021-12-27 02:29:28 +08:00
.on_system_tray_event(move |app_handle, event| match event {
2021-12-04 14:31:26 +08:00
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
2021-12-08 23:40:52 +08:00
"open_window" => {
2021-12-27 02:29:28 +08:00
let window = app_handle.get_window("main").unwrap();
2022-01-16 03:09:36 +08:00
window.unminimize().unwrap();
2021-12-06 10:31:17 +08:00
window.show().unwrap();
window.set_focus().unwrap();
}
"restart_clash" => {
2022-01-07 23:29:20 +08:00
let clash_state = app_handle.state::<states::ClashState>();
2022-01-21 02:31:44 +08:00
let profiles_state = app_handle.state::<states::ProfilesState>();
let mut clash = clash_state.0.lock().unwrap();
2022-01-21 02:31:44 +08:00
let mut profiles = profiles_state.0.lock().unwrap();
match clash.restart_sidecar(&mut profiles) {
Ok(_) => {
let window = app_handle.get_window("main").unwrap();
window.emit("restart_clash", "yes").unwrap();
}
Err(err) => log::error!("{}", err),
}
}
2021-12-08 23:40:52 +08:00
"quit" => {
2021-12-27 02:29:28 +08:00
resolve::resolve_reset(app_handle);
2022-02-13 18:45:03 +08:00
api::process::kill_children();
std::process::exit(0);
2021-12-04 14:31:26 +08:00
}
_ => {}
},
2021-12-08 23:40:52 +08:00
SystemTrayEvent::LeftClick { .. } => {
2022-01-31 23:34:58 +08:00
if cfg![target_os = "windows"] {
let window = app_handle.get_window("main").unwrap();
window.unminimize().unwrap();
window.show().unwrap();
window.set_focus().unwrap();
}
2021-12-08 23:40:52 +08:00
}
2021-12-04 14:31:26 +08:00
_ => {}
})
2021-12-08 23:40:52 +08:00
.invoke_handler(tauri::generate_handler![
2022-01-08 22:23:48 +08:00
// common
2022-01-07 23:29:20 +08:00
cmds::restart_sidecar,
cmds::get_sys_proxy,
cmds::get_cur_proxy,
2022-02-17 02:10:25 +08:00
cmds::kill_sidecars,
2022-02-16 03:21:34 +08:00
cmds::open_app_dir,
cmds::open_logs_dir,
2022-01-08 22:23:48 +08:00
// clash
2022-01-07 23:29:20 +08:00
cmds::get_clash_info,
cmds::patch_clash_config,
2022-01-08 22:23:48 +08:00
// verge
2022-01-07 23:29:20 +08:00
cmds::get_verge_config,
cmds::patch_verge_config,
2022-01-08 22:23:48 +08:00
// profile
2022-02-07 17:26:05 +08:00
cmds::new_profile,
2022-01-19 23:58:34 +08:00
cmds::view_profile,
2022-01-17 02:16:17 +08:00
cmds::patch_profile,
2022-01-07 23:29:20 +08:00
cmds::import_profile,
cmds::update_profile,
cmds::delete_profile,
cmds::select_profile,
cmds::get_profiles,
2022-01-17 02:16:17 +08:00
cmds::sync_profiles,
2021-12-08 23:40:52 +08:00
])
2021-12-04 14:31:26 +08:00
.build(tauri::generate_context!())
2021-12-26 02:31:18 +08:00
.expect("error while running tauri application")
.run(|app_handle, e| match e {
2022-02-13 18:45:03 +08:00
tauri::RunEvent::CloseRequested { label, api, .. } => {
2021-12-26 02:31:18 +08:00
let app_handle = app_handle.clone();
api.prevent_close();
app_handle.get_window(&label).unwrap().hide().unwrap();
}
2022-02-13 18:45:03 +08:00
tauri::RunEvent::ExitRequested { .. } => {
2021-12-27 02:29:28 +08:00
resolve::resolve_reset(app_handle);
2021-12-26 02:31:18 +08:00
api::process::kill_children();
}
_ => {}
});
2021-12-04 14:31:26 +08:00
Ok(())
}