clash-verge/src-tauri/src/utils/resolve.rs

89 lines
2.3 KiB
Rust
Raw Normal View History

2022-01-07 18:29:20 +03:00
use super::{init, server};
2022-03-01 03:58:47 +03:00
use crate::{core::Profiles, log_if_err, states};
2021-12-26 21:29:28 +03:00
use tauri::{App, AppHandle, Manager};
/// handle something when start app
pub fn resolve_setup(app: &App) {
2022-02-20 18:46:13 +03:00
resolve_window(app);
2022-01-11 21:27:29 +03:00
2021-12-26 21:29:28 +03:00
// setup a simple http server for singleton
server::embed_server(&app.handle());
// init app config
init::init_app(app.package_info());
2022-01-07 18:29:20 +03:00
// init states
let clash_state = app.state::<states::ClashState>();
let verge_state = app.state::<states::VergeState>();
let profiles_state = app.state::<states::ProfilesState>();
2021-12-26 21:29:28 +03:00
2022-01-07 18:29:20 +03:00
let mut clash = clash_state.0.lock().unwrap();
let mut verge = verge_state.0.lock().unwrap();
let mut profiles = profiles_state.0.lock().unwrap();
2021-12-26 21:29:28 +03:00
2022-03-01 03:58:47 +03:00
log_if_err!(clash.run_sidecar());
2022-01-05 18:30:18 +03:00
*profiles = Profiles::read_file();
2022-01-05 18:30:18 +03:00
2022-03-06 10:40:16 +03:00
clash.set_window(app.get_window("main"));
log_if_err!(clash.activate(&profiles, true));
2022-03-02 20:52:02 +03:00
2022-01-07 18:29:20 +03:00
verge.init_sysproxy(clash.info.port.clone());
2022-02-24 21:09:39 +03:00
// enable tun mode
if verge.config.enable_tun_mode.clone().unwrap_or(false)
&& verge.cur_sysproxy.is_some()
&& verge.cur_sysproxy.as_ref().unwrap().enable
{
log::info!("enable tun mode");
clash.tun_mode(true).unwrap();
}
2022-03-16 13:44:25 +03:00
log_if_err!(verge.init_launch());
verge.config.enable_system_proxy.map(|enable| {
log_if_err!(app
.tray_handle()
.get_item("system_proxy")
.set_selected(enable));
});
2021-12-26 21:29:28 +03:00
}
/// reset system proxy
pub fn resolve_reset(app_handle: &AppHandle) {
2022-01-07 18:29:20 +03:00
let verge_state = app_handle.state::<states::VergeState>();
2022-01-08 17:23:48 +03:00
let mut verge = verge_state.0.lock().unwrap();
2022-01-07 18:29:20 +03:00
2022-01-08 17:23:48 +03:00
verge.reset_sysproxy();
2021-12-26 21:29:28 +03:00
}
2022-02-20 18:46:13 +03:00
/// customize the window theme
fn resolve_window(app: &App) {
let window = app.get_window("main").unwrap();
#[cfg(target_os = "windows")]
{
2022-03-03 15:37:06 +03:00
use window_shadows::set_shadow;
use window_vibrancy::apply_blur;
2022-02-20 18:46:13 +03:00
window.set_decorations(false).unwrap();
2022-03-03 15:37:06 +03:00
set_shadow(&window, true).unwrap();
2022-03-08 05:44:41 +03:00
apply_blur(&window, None).unwrap();
2022-02-20 18:46:13 +03:00
}
#[cfg(target_os = "macos")]
{
use tauri::LogicalSize;
use tauri::Size::Logical;
window.set_decorations(true).unwrap();
window
.set_size(Logical(LogicalSize {
width: 800.0,
height: 610.0,
}))
.unwrap();
// use tauri_plugin_vibrancy::MacOSVibrancy;
// #[allow(deprecated)]
// window.apply_vibrancy(MacOSVibrancy::AppearanceBased);
}
}