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

103 lines
2.5 KiB
Rust
Raw Normal View History

2022-09-11 15:58:55 +03:00
use crate::{core::Core, data::Data, utils::init, utils::server};
2021-12-26 21:29:28 +03:00
use tauri::{App, AppHandle, Manager};
/// handle something when start app
pub fn resolve_setup(app: &App) {
// init app config
init::init_app(app.package_info());
2022-10-10 19:57:34 +03:00
let silent_start = {
2022-09-11 15:58:55 +03:00
let global = Data::global();
let verge = global.verge.lock();
let singleton = verge.app_singleton_port.clone();
// setup a simple http server for singleton
server::embed_server(&app.handle(), singleton);
2022-10-10 19:57:34 +03:00
verge.enable_silent_start.clone().unwrap_or(false)
};
2022-09-11 15:58:55 +03:00
// core should be initialized after init_app fix #122
2022-09-11 19:45:19 +03:00
let core = Core::global();
2022-04-19 09:38:59 +03:00
core.init(app.app_handle());
2021-12-26 21:29:28 +03:00
if !silent_start {
2022-10-10 19:57:34 +03:00
create_window(&app.app_handle());
}
2021-12-26 21:29:28 +03:00
}
/// reset system proxy
2022-09-11 15:58:55 +03:00
pub fn resolve_reset() {
let core = Core::global();
2022-04-19 20:44:47 +03:00
let mut sysopt = core.sysopt.lock();
2022-09-11 15:58:55 +03:00
crate::log_if_err!(sysopt.reset_sysproxy());
2022-04-25 15:00:11 +03:00
drop(sysopt);
let mut service = core.service.lock();
crate::log_if_err!(service.stop());
2021-12-26 21:29:28 +03:00
}
2022-02-20 18:46:13 +03:00
2022-05-25 11:45:18 +03:00
/// create main window
pub fn create_window(app_handle: &AppHandle) {
if let Some(window) = app_handle.get_window("main") {
let _ = window.unminimize();
let _ = window.show();
let _ = window.set_focus();
return;
}
let builder = tauri::window::WindowBuilder::new(
app_handle,
"main".to_string(),
tauri::WindowUrl::App("index.html".into()),
)
.title("Clash Verge")
.center()
.fullscreen(false)
.min_inner_size(600.0, 520.0);
#[cfg(target_os = "windows")]
{
use crate::utils::winhelp;
2022-08-14 20:37:26 +03:00
use std::time::Duration;
use tokio::time::sleep;
2022-05-25 11:45:18 +03:00
use window_shadows::set_shadow;
use window_vibrancy::apply_blur;
2022-05-31 20:04:46 +03:00
match builder
.decorations(false)
.transparent(true)
.inner_size(800.0, 636.0)
.build()
{
Ok(_) => {
let app_handle = app_handle.clone();
tauri::async_runtime::spawn(async move {
2022-08-14 20:37:26 +03:00
sleep(Duration::from_secs(1)).await;
if let Some(window) = app_handle.get_window("main") {
2022-06-18 12:28:37 +03:00
let _ = window.show();
let _ = set_shadow(&window, true);
if !winhelp::is_win11() {
let _ = apply_blur(&window, None);
}
}
});
2022-05-25 11:45:18 +03:00
}
2022-07-17 12:39:44 +03:00
Err(err) => log::error!(target: "app", "{err}"),
2022-05-25 11:45:18 +03:00
}
}
#[cfg(target_os = "macos")]
2022-10-10 19:57:34 +03:00
crate::log_if_err!(builder.decorations(true).inner_size(800.0, 642.0).build());
2022-05-25 11:45:18 +03:00
#[cfg(target_os = "linux")]
2022-05-31 20:04:46 +03:00
crate::log_if_err!(builder
.decorations(false)
.transparent(true)
.inner_size(800.0, 636.0)
.build());
2022-05-25 11:45:18 +03:00
}