2022-11-17 20:19:40 +08:00
|
|
|
use crate::log_err;
|
2022-11-14 01:26:33 +08:00
|
|
|
use once_cell::sync::OnceCell;
|
2024-09-23 23:15:51 +08:00
|
|
|
use parking_lot::RwLock;
|
2022-11-14 01:26:33 +08:00
|
|
|
use std::sync::Arc;
|
2024-09-12 15:35:08 +08:00
|
|
|
use tauri::{AppHandle, Emitter, Manager, WebviewWindow};
|
2025-03-03 03:34:34 +08:00
|
|
|
use tauri_plugin_shell::process::CommandChild;
|
2022-09-11 20:58:55 +08:00
|
|
|
|
|
|
|
|
#[derive(Debug, Default, Clone)]
|
|
|
|
|
pub struct Handle {
|
2024-09-23 23:15:51 +08:00
|
|
|
pub app_handle: Arc<RwLock<Option<AppHandle>>>,
|
2024-11-20 00:27:53 +08:00
|
|
|
pub is_exiting: Arc<RwLock<bool>>,
|
2025-03-03 03:34:34 +08:00
|
|
|
pub core_process: Arc<RwLock<Option<CommandChild>>>,
|
2022-09-11 20:58:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Handle {
|
2022-11-14 01:26:33 +08:00
|
|
|
pub fn global() -> &'static Handle {
|
|
|
|
|
static HANDLE: OnceCell<Handle> = OnceCell::new();
|
|
|
|
|
|
|
|
|
|
HANDLE.get_or_init(|| Handle {
|
2024-09-23 23:15:51 +08:00
|
|
|
app_handle: Arc::new(RwLock::new(None)),
|
2024-11-20 00:27:53 +08:00
|
|
|
is_exiting: Arc::new(RwLock::new(false)),
|
2025-03-03 03:34:34 +08:00
|
|
|
core_process: Arc::new(RwLock::new(None)),
|
2022-11-14 01:26:33 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-02 19:33:17 +08:00
|
|
|
pub fn init(&self, app_handle: &AppHandle) {
|
2024-09-23 23:15:51 +08:00
|
|
|
let mut handle = self.app_handle.write();
|
|
|
|
|
*handle = Some(app_handle.clone());
|
2022-11-12 11:37:23 +08:00
|
|
|
}
|
2022-09-11 20:58:55 +08:00
|
|
|
|
2024-09-23 16:31:58 +08:00
|
|
|
pub fn app_handle(&self) -> Option<AppHandle> {
|
2024-09-23 23:15:51 +08:00
|
|
|
self.app_handle.read().clone()
|
2024-09-23 16:31:58 +08:00
|
|
|
}
|
|
|
|
|
|
2024-09-02 19:33:17 +08:00
|
|
|
pub fn get_window(&self) -> Option<WebviewWindow> {
|
2024-10-30 06:27:29 +08:00
|
|
|
let app_handle = self.app_handle().unwrap();
|
|
|
|
|
let window: Option<WebviewWindow> = app_handle.get_webview_window("main");
|
|
|
|
|
if window.is_none() {
|
|
|
|
|
log::debug!(target:"app", "main window not found");
|
|
|
|
|
}
|
|
|
|
|
window
|
2022-11-12 11:37:23 +08:00
|
|
|
}
|
2022-09-11 20:58:55 +08:00
|
|
|
|
2022-11-14 01:26:33 +08:00
|
|
|
pub fn refresh_clash() {
|
|
|
|
|
if let Some(window) = Self::global().get_window() {
|
2022-11-17 20:19:40 +08:00
|
|
|
log_err!(window.emit("verge://refresh-clash-config", "yes"));
|
2022-11-12 11:37:23 +08:00
|
|
|
}
|
2022-09-11 20:58:55 +08:00
|
|
|
}
|
|
|
|
|
|
2022-11-14 01:26:33 +08:00
|
|
|
pub fn refresh_verge() {
|
|
|
|
|
if let Some(window) = Self::global().get_window() {
|
2022-11-17 20:19:40 +08:00
|
|
|
log_err!(window.emit("verge://refresh-verge-config", "yes"));
|
2022-11-12 11:37:23 +08:00
|
|
|
}
|
2022-09-11 20:58:55 +08:00
|
|
|
}
|
|
|
|
|
|
2022-11-12 11:37:23 +08:00
|
|
|
#[allow(unused)]
|
2022-11-14 01:26:33 +08:00
|
|
|
pub fn refresh_profiles() {
|
|
|
|
|
if let Some(window) = Self::global().get_window() {
|
2022-11-17 20:19:40 +08:00
|
|
|
log_err!(window.emit("verge://refresh-profiles-config", "yes"));
|
2022-11-12 11:37:23 +08:00
|
|
|
}
|
2022-09-11 20:58:55 +08:00
|
|
|
}
|
|
|
|
|
|
2022-11-14 01:26:33 +08:00
|
|
|
pub fn notice_message<S: Into<String>, M: Into<String>>(status: S, msg: M) {
|
|
|
|
|
if let Some(window) = Self::global().get_window() {
|
2022-11-17 20:19:40 +08:00
|
|
|
log_err!(window.emit("verge://notice-message", (status.into(), msg.into())));
|
2022-11-12 11:37:23 +08:00
|
|
|
}
|
2022-09-26 20:46:29 +08:00
|
|
|
}
|
|
|
|
|
|
2024-11-20 00:27:53 +08:00
|
|
|
pub fn set_is_exiting(&self) {
|
|
|
|
|
let mut is_exiting = self.is_exiting.write();
|
|
|
|
|
*is_exiting = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-03 03:34:34 +08:00
|
|
|
pub fn set_core_process(&self, process: CommandChild) {
|
|
|
|
|
let mut core_process = self.core_process.write();
|
|
|
|
|
*core_process = Some(process);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn take_core_process(&self) -> Option<CommandChild> {
|
|
|
|
|
let mut core_process = self.core_process.write();
|
|
|
|
|
core_process.take()
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-03 14:42:31 +08:00
|
|
|
/// 检查是否有运行中的核心进程
|
|
|
|
|
pub fn has_core_process(&self) -> bool {
|
|
|
|
|
self.core_process.read().is_some()
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-20 00:27:53 +08:00
|
|
|
pub fn is_exiting(&self) -> bool {
|
|
|
|
|
*self.is_exiting.read()
|
|
|
|
|
}
|
2022-09-11 20:58:55 +08:00
|
|
|
}
|