clash-verge/src-tauri/src/main.rs

195 lines
5.6 KiB
Rust
Raw Normal View History

2021-12-04 09:31:26 +03:00
#![cfg_attr(
2022-07-12 19:54:47 +03:00
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
2021-12-04 09:31:26 +03:00
)]
mod cmds;
2022-08-10 21:55:10 +03:00
mod config;
2022-01-07 20:27:25 +03:00
mod core;
2021-12-14 11:07:15 +03:00
mod utils;
2021-12-04 09:31:26 +03:00
use crate::{
2022-07-12 19:54:47 +03:00
core::Verge,
utils::{resolve, server},
};
2021-12-08 18:40:52 +03:00
use tauri::{
2022-07-12 19:54:47 +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<()> {
let mut context = tauri::generate_context!();
let verge = Verge::new();
if server::check_singleton(verge.app_singleton_port).is_err() {
2022-07-12 19:54:47 +03:00
println!("app exists");
return Ok(());
}
for win in context.config_mut().tauri.windows.iter_mut() {
if verge.enable_silent_start.unwrap_or(false) {
win.visible = false;
}
}
2022-07-12 19:54:47 +03:00
#[cfg(target_os = "windows")]
unsafe {
use crate::utils::dirs;
dirs::init_portable_flag();
}
let tray_menu = SystemTrayMenu::new()
.add_item(CustomMenuItem::new("open_window", "Show"))
2022-07-12 20:05:53 +03:00
.add_native_item(SystemTrayMenuItem::Separator)
2022-07-12 19:54:47 +03:00
.add_item(CustomMenuItem::new("rule_mode", "Rule Mode"))
.add_item(CustomMenuItem::new("global_mode", "Global Mode"))
.add_item(CustomMenuItem::new("direct_mode", "Direct Mode"))
.add_item(CustomMenuItem::new("script_mode", "Script Mode"))
2022-07-12 20:05:53 +03:00
.add_native_item(SystemTrayMenuItem::Separator)
2022-07-12 19:54:47 +03:00
.add_item(CustomMenuItem::new("system_proxy", "System Proxy"))
.add_item(CustomMenuItem::new("tun_mode", "Tun Mode"))
.add_item(CustomMenuItem::new("restart_clash", "Restart Clash"))
2022-08-14 20:22:39 +03:00
.add_item(CustomMenuItem::new("restart_app", "Restart App"))
2022-07-12 19:54:47 +03:00
.add_native_item(SystemTrayMenuItem::Separator)
.add_item(CustomMenuItem::new("quit", "Quit").accelerator("CmdOrControl+Q"));
#[allow(unused_mut)]
let mut builder = tauri::Builder::default()
.setup(|app| Ok(resolve::resolve_setup(app)))
.system_tray(SystemTray::new().with_menu(tray_menu))
.on_system_tray_event(move |app_handle, event| match event {
SystemTrayEvent::MenuItemClick { id, .. } => match id.as_str() {
"open_window" => {
resolve::create_window(app_handle);
}
2022-07-12 21:26:54 +03:00
mode @ ("rule_mode" | "global_mode" | "direct_mode" | "script_mode") => {
let mode = &mode[0..mode.len() - 5];
2022-07-12 19:54:47 +03:00
let core = app_handle.state::<core::Core>();
2022-07-12 21:26:54 +03:00
crate::log_if_err!(core.update_mode(app_handle, mode));
2022-07-12 19:54:47 +03:00
}
"system_proxy" => {
let core = app_handle.state::<core::Core>();
let new_value = {
let verge = core.verge.lock();
!verge.enable_system_proxy.clone().unwrap_or(false)
};
let patch = Verge {
enable_system_proxy: Some(new_value),
..Verge::default()
};
crate::log_if_err!(core.patch_verge(patch, app_handle));
}
"tun_mode" => {
let core = app_handle.state::<core::Core>();
let new_value = {
let verge = core.verge.lock();
!verge.enable_tun_mode.clone().unwrap_or(false)
};
let patch = Verge {
enable_tun_mode: Some(new_value),
..Verge::default()
};
crate::log_if_err!(core.patch_verge(patch, app_handle));
}
"restart_clash" => {
let core = app_handle.state::<core::Core>();
crate::log_if_err!(core.restart_clash());
}
2022-08-14 20:22:39 +03:00
"restart_app" => {
api::process::restart(&app_handle.env());
}
2022-07-12 19:54:47 +03:00
"quit" => {
resolve::resolve_reset(app_handle);
app_handle.exit(0);
}
_ => {}
},
#[cfg(target_os = "windows")]
SystemTrayEvent::LeftClick { .. } => {
resolve::create_window(app_handle);
}
_ => {}
})
.invoke_handler(tauri::generate_handler![
// common
cmds::get_sys_proxy,
cmds::get_cur_proxy,
cmds::open_app_dir,
cmds::open_logs_dir,
2022-08-06 16:56:54 +03:00
cmds::open_web_url,
2022-07-12 19:54:47 +03:00
cmds::kill_sidecar,
cmds::restart_sidecar,
// clash
cmds::get_clash_info,
2022-09-05 15:30:39 +03:00
cmds::get_clash_logs,
2022-07-12 19:54:47 +03:00
cmds::patch_clash_config,
cmds::change_clash_core,
2022-08-11 22:20:55 +03:00
cmds::get_runtime_config,
cmds::get_runtime_yaml,
cmds::get_runtime_exists,
cmds::get_runtime_logs,
2022-07-12 19:54:47 +03:00
// verge
cmds::get_verge_config,
cmds::patch_verge_config,
// profile
cmds::view_profile,
cmds::patch_profile,
cmds::create_profile,
cmds::import_profile,
cmds::update_profile,
cmds::delete_profile,
cmds::select_profile,
cmds::get_profiles,
cmds::enhance_profiles,
cmds::change_profile_chain,
cmds::change_profile_valid,
cmds::read_profile_file,
cmds::save_profile_file,
// service mode
cmds::service::start_service,
cmds::service::stop_service,
cmds::service::check_service,
cmds::service::install_service,
cmds::service::uninstall_service,
]);
#[cfg(target_os = "macos")]
{
use tauri::{Menu, MenuItem, Submenu};
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),
);
builder = builder.menu(Menu::new().add_submenu(submenu_file));
}
builder
2022-08-09 09:14:06 +03:00
.build(context)
2022-07-12 19:54:47 +03:00
.expect("error while running tauri application")
.run(|app_handle, e| match e {
tauri::RunEvent::ExitRequested { api, .. } => {
api.prevent_exit();
}
tauri::RunEvent::Exit => {
resolve::resolve_reset(app_handle);
api::process::kill_children();
}
_ => {}
});
Ok(())
2021-12-04 09:31:26 +03:00
}