mirror of
https://github.com/clash-verge-rev/clash-verge-rev
synced 2025-05-05 05:03:45 +08:00
refactor: unify and simplify the call of app_handle
This commit is contained in:
parent
3d6faecaed
commit
1c894f3cfa
@ -14,8 +14,8 @@ type CmdResult<T = ()> = Result<T, String>;
|
|||||||
use tauri::Manager;
|
use tauri::Manager;
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn copy_clash_env(app_handle: tauri::AppHandle) -> CmdResult {
|
pub fn copy_clash_env() -> CmdResult {
|
||||||
feat::copy_clash_env(&app_handle);
|
feat::copy_clash_env();
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,7 +8,6 @@ use parking_lot::Mutex;
|
|||||||
use serde_yaml::Mapping;
|
use serde_yaml::Mapping;
|
||||||
use std::{sync::Arc, time::Duration};
|
use std::{sync::Arc, time::Duration};
|
||||||
use sysinfo::{ProcessRefreshKind, RefreshKind, System};
|
use sysinfo::{ProcessRefreshKind, RefreshKind, System};
|
||||||
use tauri::AppHandle;
|
|
||||||
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
|
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
|
||||||
use tauri_plugin_shell::ShellExt;
|
use tauri_plugin_shell::ShellExt;
|
||||||
|
|
||||||
@ -16,7 +15,6 @@ use tokio::time::sleep;
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct CoreManager {
|
pub struct CoreManager {
|
||||||
app_handle: Arc<Mutex<Option<AppHandle>>>,
|
|
||||||
sidecar: Arc<Mutex<Option<CommandChild>>>,
|
sidecar: Arc<Mutex<Option<CommandChild>>>,
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
use_service_mode: Arc<Mutex<bool>>,
|
use_service_mode: Arc<Mutex<bool>>,
|
||||||
@ -27,14 +25,12 @@ impl CoreManager {
|
|||||||
static CORE_MANAGER: OnceCell<CoreManager> = OnceCell::new();
|
static CORE_MANAGER: OnceCell<CoreManager> = OnceCell::new();
|
||||||
|
|
||||||
CORE_MANAGER.get_or_init(|| CoreManager {
|
CORE_MANAGER.get_or_init(|| CoreManager {
|
||||||
app_handle: Arc::new(Mutex::new(None)),
|
|
||||||
sidecar: Arc::new(Mutex::new(None)),
|
sidecar: Arc::new(Mutex::new(None)),
|
||||||
use_service_mode: Arc::new(Mutex::new(false)),
|
use_service_mode: Arc::new(Mutex::new(false)),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(&self, app_handle: &AppHandle) -> Result<()> {
|
pub fn init(&self) -> Result<()> {
|
||||||
*self.app_handle.lock() = Some(app_handle.clone());
|
|
||||||
tauri::async_runtime::spawn(async {
|
tauri::async_runtime::spawn(async {
|
||||||
log::trace!("run core start");
|
log::trace!("run core start");
|
||||||
// 启动clash
|
// 启动clash
|
||||||
@ -69,29 +65,24 @@ impl CoreManager {
|
|||||||
|
|
||||||
let test_dir = dirs::app_home_dir()?.join("test");
|
let test_dir = dirs::app_home_dir()?.join("test");
|
||||||
let test_dir = dirs::path_to_str(&test_dir)?;
|
let test_dir = dirs::path_to_str(&test_dir)?;
|
||||||
let app_handle_option = {
|
let app_handle = handle::Handle::global().app_handle().unwrap();
|
||||||
let lock = self.app_handle.lock();
|
|
||||||
lock.as_ref().cloned()
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(app_handle) = app_handle_option {
|
let output = app_handle
|
||||||
let output = app_handle
|
.shell()
|
||||||
.shell()
|
.sidecar(clash_core)?
|
||||||
.sidecar(clash_core)?
|
.args(["-t", "-d", test_dir, "-f", config_path])
|
||||||
.args(["-t", "-d", test_dir, "-f", config_path])
|
.output()
|
||||||
.output()
|
.await?;
|
||||||
.await?;
|
|
||||||
|
|
||||||
if !output.status.success() {
|
if !output.status.success() {
|
||||||
let stdout = String::from_utf8(output.stdout).unwrap_or_default();
|
let stdout = String::from_utf8(output.stdout).unwrap_or_default();
|
||||||
let error = clash_api::parse_check_output(stdout.clone());
|
let error = clash_api::parse_check_output(stdout.clone());
|
||||||
let error = match !error.is_empty() {
|
let error = match !error.is_empty() {
|
||||||
true => error,
|
true => error,
|
||||||
false => stdout.clone(),
|
false => stdout.clone(),
|
||||||
};
|
};
|
||||||
Logger::global().set_log(stdout.clone());
|
Logger::global().set_log(stdout.clone());
|
||||||
bail!("{error}");
|
bail!("{error}");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -173,39 +164,37 @@ impl CoreManager {
|
|||||||
|
|
||||||
let args = vec!["-d", app_dir, "-f", config_path];
|
let args = vec!["-d", app_dir, "-f", config_path];
|
||||||
|
|
||||||
let app_handle = self.app_handle.lock();
|
let app_handle = handle::Handle::global().app_handle().unwrap();
|
||||||
|
|
||||||
if let Some(app_handle) = app_handle.as_ref() {
|
let cmd = app_handle.shell().sidecar(clash_core)?;
|
||||||
let cmd = app_handle.shell().sidecar(clash_core)?;
|
let (mut rx, _) = cmd.args(args).spawn()?;
|
||||||
let (mut rx, _) = cmd.args(args).spawn()?;
|
|
||||||
|
|
||||||
tauri::async_runtime::spawn(async move {
|
tauri::async_runtime::spawn(async move {
|
||||||
while let Some(event) = rx.recv().await {
|
while let Some(event) = rx.recv().await {
|
||||||
match event {
|
match event {
|
||||||
CommandEvent::Stdout(line) => {
|
CommandEvent::Stdout(line) => {
|
||||||
let line = String::from_utf8(line).unwrap_or_default();
|
let line = String::from_utf8(line).unwrap_or_default();
|
||||||
log::info!(target: "app", "[mihomo]: {line}");
|
log::info!(target: "app", "[mihomo]: {line}");
|
||||||
Logger::global().set_log(line);
|
Logger::global().set_log(line);
|
||||||
}
|
|
||||||
CommandEvent::Stderr(err) => {
|
|
||||||
let err = String::from_utf8(err).unwrap_or_default();
|
|
||||||
log::error!(target: "app", "[mihomo]: {err}");
|
|
||||||
Logger::global().set_log(err);
|
|
||||||
}
|
|
||||||
CommandEvent::Error(err) => {
|
|
||||||
log::error!(target: "app", "[mihomo]: {err}");
|
|
||||||
Logger::global().set_log(err);
|
|
||||||
}
|
|
||||||
CommandEvent::Terminated(_) => {
|
|
||||||
log::info!(target: "app", "mihomo core terminated");
|
|
||||||
let _ = CoreManager::global().recover_core();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
|
CommandEvent::Stderr(err) => {
|
||||||
|
let err = String::from_utf8(err).unwrap_or_default();
|
||||||
|
log::error!(target: "app", "[mihomo]: {err}");
|
||||||
|
Logger::global().set_log(err);
|
||||||
|
}
|
||||||
|
CommandEvent::Error(err) => {
|
||||||
|
log::error!(target: "app", "[mihomo]: {err}");
|
||||||
|
Logger::global().set_log(err);
|
||||||
|
}
|
||||||
|
CommandEvent::Terminated(_) => {
|
||||||
|
log::info!(target: "app", "mihomo core terminated");
|
||||||
|
let _ = CoreManager::global().recover_core();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use super::tray::Tray;
|
use super::tray::Tray;
|
||||||
use crate::log_err;
|
use crate::log_err;
|
||||||
use anyhow::{bail, Result};
|
use anyhow::Result;
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@ -24,10 +24,12 @@ impl Handle {
|
|||||||
*self.app_handle.lock() = Some(app_handle.clone());
|
*self.app_handle.lock() = Some(app_handle.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn app_handle(&self) -> Option<AppHandle> {
|
||||||
|
self.app_handle.lock().clone()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_window(&self) -> Option<WebviewWindow> {
|
pub fn get_window(&self) -> Option<WebviewWindow> {
|
||||||
self.app_handle
|
self.app_handle()
|
||||||
.lock()
|
|
||||||
.clone()
|
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.and_then(|a| a.get_webview_window("main"))
|
.and_then(|a| a.get_webview_window("main"))
|
||||||
}
|
}
|
||||||
@ -59,11 +61,7 @@ impl Handle {
|
|||||||
|
|
||||||
/// update the system tray state
|
/// update the system tray state
|
||||||
pub fn update_systray_part() -> Result<()> {
|
pub fn update_systray_part() -> Result<()> {
|
||||||
let app_handle = Self::global().app_handle.lock().clone();
|
Tray::update_part()?;
|
||||||
if app_handle.is_none() {
|
|
||||||
bail!("update_systray unhandled error");
|
|
||||||
}
|
|
||||||
Tray::update_part(app_handle.as_ref().unwrap())?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use crate::core::handle;
|
||||||
use crate::{config::Config, feat, log_err};
|
use crate::{config::Config, feat, log_err};
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use once_cell::sync::OnceCell;
|
use once_cell::sync::OnceCell;
|
||||||
@ -7,7 +8,6 @@ use tauri::{AppHandle, Manager};
|
|||||||
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, ShortcutState};
|
use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, ShortcutState};
|
||||||
pub struct Hotkey {
|
pub struct Hotkey {
|
||||||
current: Arc<Mutex<Vec<String>>>, // 保存当前的热键设置
|
current: Arc<Mutex<Vec<String>>>, // 保存当前的热键设置
|
||||||
app_handle: Arc<Mutex<Option<AppHandle>>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hotkey {
|
impl Hotkey {
|
||||||
@ -16,12 +16,10 @@ impl Hotkey {
|
|||||||
|
|
||||||
HOTKEY.get_or_init(|| Hotkey {
|
HOTKEY.get_or_init(|| Hotkey {
|
||||||
current: Arc::new(Mutex::new(Vec::new())),
|
current: Arc::new(Mutex::new(Vec::new())),
|
||||||
app_handle: Arc::new(Mutex::new(None)),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(&self, app_handle: &AppHandle) -> Result<()> {
|
pub fn init(&self) -> Result<()> {
|
||||||
*self.app_handle.lock() = Some(app_handle.clone());
|
|
||||||
let verge = Config::verge();
|
let verge = Config::verge();
|
||||||
|
|
||||||
if let Some(hotkeys) = verge.latest().hotkeys.as_ref() {
|
if let Some(hotkeys) = verge.latest().hotkeys.as_ref() {
|
||||||
@ -48,11 +46,8 @@ impl Hotkey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn register(&self, hotkey: &str, func: &str) -> Result<()> {
|
pub fn register(&self, hotkey: &str, func: &str) -> Result<()> {
|
||||||
let app_handle = self.app_handle.lock();
|
let app_handle: AppHandle = handle::Handle::global().app_handle().unwrap();
|
||||||
if app_handle.is_none() {
|
let manager = app_handle.global_shortcut();
|
||||||
bail!("failed to get the hotkey manager");
|
|
||||||
}
|
|
||||||
let manager = app_handle.as_ref().unwrap().global_shortcut();
|
|
||||||
|
|
||||||
if manager.is_registered(hotkey) {
|
if manager.is_registered(hotkey) {
|
||||||
manager.unregister(hotkey)?;
|
manager.unregister(hotkey)?;
|
||||||
@ -88,11 +83,8 @@ impl Hotkey {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn unregister(&self, hotkey: &str) -> Result<()> {
|
pub fn unregister(&self, hotkey: &str) -> Result<()> {
|
||||||
let app_handle = self.app_handle.lock();
|
let app_handle: AppHandle = handle::Handle::global().app_handle().unwrap();
|
||||||
if app_handle.is_none() {
|
let manager = app_handle.global_shortcut();
|
||||||
bail!("failed to get the hotkey manager");
|
|
||||||
}
|
|
||||||
let manager = app_handle.as_ref().unwrap().global_shortcut();
|
|
||||||
manager.unregister(hotkey)?;
|
manager.unregister(hotkey)?;
|
||||||
|
|
||||||
log::info!(target: "app", "unregister hotkey {hotkey}");
|
log::info!(target: "app", "unregister hotkey {hotkey}");
|
||||||
@ -166,10 +158,9 @@ impl Hotkey {
|
|||||||
|
|
||||||
impl Drop for Hotkey {
|
impl Drop for Hotkey {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Some(app_handle) = self.app_handle.lock().as_ref() {
|
let app_handle: AppHandle = handle::Handle::global().app_handle().unwrap();
|
||||||
if let Err(e) = app_handle.global_shortcut().unregister_all() {
|
if let Err(e) = app_handle.global_shortcut().unregister_all() {
|
||||||
log::error!("Error unregistering all hotkeys: {:?}", e);
|
log::error!("Error unregistering all hotkeys: {:?}", e);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,14 +17,17 @@ use tauri::{
|
|||||||
Wry,
|
Wry,
|
||||||
};
|
};
|
||||||
use tauri::{AppHandle, Manager};
|
use tauri::{AppHandle, Manager};
|
||||||
|
|
||||||
|
use super::handle;
|
||||||
pub struct Tray {}
|
pub struct Tray {}
|
||||||
|
|
||||||
impl Tray {
|
impl Tray {
|
||||||
pub fn create_systray(app_handle: &AppHandle) -> Result<()> {
|
pub fn create_systray() -> Result<()> {
|
||||||
|
let app_handle: AppHandle = handle::Handle::global().app_handle().unwrap();
|
||||||
let tray_incon_id = TrayIconId::new("main");
|
let tray_incon_id = TrayIconId::new("main");
|
||||||
let tray = app_handle.tray_by_id(&tray_incon_id).unwrap();
|
let tray = app_handle.tray_by_id(&tray_incon_id).unwrap();
|
||||||
|
|
||||||
tray.on_tray_icon_event(|tray, event| {
|
tray.on_tray_icon_event(|_, event| {
|
||||||
let tray_event = { Config::verge().latest().tray_event.clone() };
|
let tray_event = { Config::verge().latest().tray_event.clone() };
|
||||||
let tray_event: String = tray_event.unwrap_or("main_window".into());
|
let tray_event: String = tray_event.unwrap_or("main_window".into());
|
||||||
|
|
||||||
@ -35,11 +38,10 @@ impl Tray {
|
|||||||
..
|
..
|
||||||
} = event
|
} = event
|
||||||
{
|
{
|
||||||
let app = tray.app_handle();
|
|
||||||
match tray_event.as_str() {
|
match tray_event.as_str() {
|
||||||
"system_proxy" => feat::toggle_system_proxy(),
|
"system_proxy" => feat::toggle_system_proxy(),
|
||||||
"tun_mode" => feat::toggle_tun_mode(),
|
"tun_mode" => feat::toggle_tun_mode(),
|
||||||
"main_window" => resolve::create_window(app),
|
"main_window" => resolve::create_window(),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -67,7 +69,8 @@ impl Tray {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_part(app_handle: &AppHandle) -> Result<()> {
|
pub fn update_part() -> Result<()> {
|
||||||
|
let app_handle: AppHandle = handle::Handle::global().app_handle().unwrap();
|
||||||
let use_zh = { Config::verge().latest().language == Some("zh".into()) };
|
let use_zh = { Config::verge().latest().language == Some("zh".into()) };
|
||||||
let version = VERSION.get().unwrap();
|
let version = VERSION.get().unwrap();
|
||||||
let mode = {
|
let mode = {
|
||||||
@ -91,7 +94,7 @@ impl Tray {
|
|||||||
let tray = app_handle.tray_by_id("main").unwrap();
|
let tray = app_handle.tray_by_id("main").unwrap();
|
||||||
|
|
||||||
let _ = tray.set_menu(Some(create_tray_menu(
|
let _ = tray.set_menu(Some(create_tray_menu(
|
||||||
app_handle,
|
&app_handle,
|
||||||
Some(mode.as_str()),
|
Some(mode.as_str()),
|
||||||
*system_proxy,
|
*system_proxy,
|
||||||
*tun_mode,
|
*tun_mode,
|
||||||
@ -421,10 +424,10 @@ fn on_menu_event(app_handle: &AppHandle, event: MenuEvent) {
|
|||||||
println!("change mode to: {}", mode);
|
println!("change mode to: {}", mode);
|
||||||
feat::change_clash_mode(mode.into());
|
feat::change_clash_mode(mode.into());
|
||||||
}
|
}
|
||||||
"open_window" => resolve::create_window(app_handle),
|
"open_window" => resolve::create_window(),
|
||||||
"system_proxy" => feat::toggle_system_proxy(),
|
"system_proxy" => feat::toggle_system_proxy(),
|
||||||
"tun_mode" => feat::toggle_tun_mode(),
|
"tun_mode" => feat::toggle_tun_mode(),
|
||||||
"copy_env" => feat::copy_clash_env(app_handle),
|
"copy_env" => feat::copy_clash_env(),
|
||||||
"open_app_dir" => crate::log_err!(cmds::open_app_dir()),
|
"open_app_dir" => crate::log_err!(cmds::open_app_dir()),
|
||||||
"open_core_dir" => crate::log_err!(cmds::open_core_dir()),
|
"open_core_dir" => crate::log_err!(cmds::open_core_dir()),
|
||||||
"open_logs_dir" => crate::log_err!(cmds::open_logs_dir()),
|
"open_logs_dir" => crate::log_err!(cmds::open_logs_dir()),
|
||||||
|
@ -10,22 +10,18 @@ use crate::log_err;
|
|||||||
use crate::utils::resolve;
|
use crate::utils::resolve;
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use serde_yaml::{Mapping, Value};
|
use serde_yaml::{Mapping, Value};
|
||||||
use tauri::{AppHandle, Manager};
|
use tauri::AppHandle;
|
||||||
use tauri_plugin_clipboard_manager::ClipboardExt;
|
use tauri_plugin_clipboard_manager::ClipboardExt;
|
||||||
|
|
||||||
// 打开面板
|
// 打开面板
|
||||||
pub fn open_or_close_dashboard() {
|
pub fn open_or_close_dashboard() {
|
||||||
let handle = handle::Handle::global();
|
if let Some(window) = handle::Handle::global().get_window() {
|
||||||
let app_handle = handle.app_handle.lock().clone();
|
if let Ok(true) = window.is_focused() {
|
||||||
if let Some(app_handle) = app_handle.as_ref() {
|
let _ = window.close();
|
||||||
if let Some(window) = app_handle.get_webview_window("main") {
|
return;
|
||||||
if let Ok(true) = window.is_focused() {
|
|
||||||
let _ = window.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
resolve::create_window(app_handle);
|
|
||||||
}
|
}
|
||||||
|
resolve::create_window();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重启clash
|
// 重启clash
|
||||||
@ -104,14 +100,11 @@ pub fn toggle_tun_mode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn quit() {
|
pub fn quit() {
|
||||||
let handle = handle::Handle::global();
|
let app_handle: AppHandle = handle::Handle::global().app_handle().unwrap();
|
||||||
let app_handle = handle.app_handle.lock().clone();
|
let _ = resolve::save_window_size_position(true);
|
||||||
if let Some(app_handle) = app_handle.as_ref() {
|
resolve::resolve_reset();
|
||||||
let _ = resolve::save_window_size_position(&app_handle, true);
|
app_handle.exit(0);
|
||||||
resolve::resolve_reset();
|
std::process::exit(0);
|
||||||
app_handle.exit(0);
|
|
||||||
std::process::exit(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 修改clash的订阅
|
/// 修改clash的订阅
|
||||||
@ -319,7 +312,8 @@ async fn update_core_config() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// copy env variable
|
/// copy env variable
|
||||||
pub fn copy_clash_env(app_handle: &AppHandle) {
|
pub fn copy_clash_env() {
|
||||||
|
let app_handle = handle::Handle::global().app_handle().unwrap();
|
||||||
let port = { Config::verge().latest().verge_mixed_port.unwrap_or(7897) };
|
let port = { Config::verge().latest().verge_mixed_port.unwrap_or(7897) };
|
||||||
let http_proxy = format!("http://127.0.0.1:{}", port);
|
let http_proxy = format!("http://127.0.0.1:{}", port);
|
||||||
let socks5_proxy = format!("socks5://127.0.0.1:{}", port);
|
let socks5_proxy = format!("socks5://127.0.0.1:{}", port);
|
||||||
|
@ -133,7 +133,7 @@ pub fn run() {
|
|||||||
.build(tauri::generate_context!())
|
.build(tauri::generate_context!())
|
||||||
.expect("error while running tauri application");
|
.expect("error while running tauri application");
|
||||||
|
|
||||||
app.run(|app_handle, e| match e {
|
app.run(|_, e| match e {
|
||||||
tauri::RunEvent::ExitRequested { api, .. } => {
|
tauri::RunEvent::ExitRequested { api, .. } => {
|
||||||
api.prevent_exit();
|
api.prevent_exit();
|
||||||
}
|
}
|
||||||
@ -141,13 +141,13 @@ pub fn run() {
|
|||||||
if label == "main" {
|
if label == "main" {
|
||||||
match event {
|
match event {
|
||||||
tauri::WindowEvent::Destroyed => {
|
tauri::WindowEvent::Destroyed => {
|
||||||
let _ = resolve::save_window_size_position(app_handle, true);
|
let _ = resolve::save_window_size_position(true);
|
||||||
}
|
}
|
||||||
tauri::WindowEvent::CloseRequested { .. } => {
|
tauri::WindowEvent::CloseRequested { .. } => {
|
||||||
let _ = resolve::save_window_size_position(app_handle, true);
|
let _ = resolve::save_window_size_position(true);
|
||||||
}
|
}
|
||||||
tauri::WindowEvent::Moved(_) | tauri::WindowEvent::Resized(_) => {
|
tauri::WindowEvent::Moved(_) | tauri::WindowEvent::Resized(_) => {
|
||||||
let _ = resolve::save_window_size_position(app_handle, false);
|
let _ = resolve::save_window_size_position(false);
|
||||||
}
|
}
|
||||||
tauri::WindowEvent::Focused(true) => {
|
tauri::WindowEvent::Focused(true) => {
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use crate::config::*;
|
use crate::config::*;
|
||||||
|
use crate::core::handle;
|
||||||
use crate::utils::{dirs, help};
|
use crate::utils::{dirs, help};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use chrono::{Local, TimeZone};
|
use chrono::{Local, TimeZone};
|
||||||
@ -297,7 +298,9 @@ pub fn init_scheme() -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn startup_script(app_handle: &AppHandle) -> Result<()> {
|
pub async fn startup_script() -> Result<()> {
|
||||||
|
let app_handle: AppHandle = handle::Handle::global().app_handle().unwrap();
|
||||||
|
|
||||||
let script_path = {
|
let script_path = {
|
||||||
let verge = Config::verge();
|
let verge = Config::verge();
|
||||||
let verge = verge.latest();
|
let verge = verge.latest();
|
||||||
|
@ -44,7 +44,7 @@ pub async fn resolve_setup(app: &mut App) {
|
|||||||
log_err!(init::init_config());
|
log_err!(init::init_config());
|
||||||
log_err!(init::init_resources());
|
log_err!(init::init_resources());
|
||||||
log_err!(init::init_scheme());
|
log_err!(init::init_scheme());
|
||||||
log_err!(init::startup_script(app.app_handle()).await);
|
log_err!(init::startup_script().await);
|
||||||
// 处理随机端口
|
// 处理随机端口
|
||||||
let enable_random_port = Config::verge().latest().enable_random_port.unwrap_or(false);
|
let enable_random_port = Config::verge().latest().enable_random_port.unwrap_or(false);
|
||||||
|
|
||||||
@ -78,25 +78,25 @@ pub async fn resolve_setup(app: &mut App) {
|
|||||||
log_err!(Config::init_config().await);
|
log_err!(Config::init_config().await);
|
||||||
|
|
||||||
log::trace!("launch core");
|
log::trace!("launch core");
|
||||||
log_err!(CoreManager::global().init(app.app_handle()));
|
log_err!(CoreManager::global().init());
|
||||||
|
|
||||||
// setup a simple http server for singleton
|
// setup a simple http server for singleton
|
||||||
log::trace!("launch embed server");
|
log::trace!("launch embed server");
|
||||||
server::embed_server(app.app_handle());
|
server::embed_server();
|
||||||
|
|
||||||
log::trace!("init system tray");
|
log::trace!("init system tray");
|
||||||
log_err!(tray::Tray::create_systray(&app.app_handle()));
|
log_err!(tray::Tray::create_systray());
|
||||||
|
|
||||||
let silent_start = { Config::verge().data().enable_silent_start };
|
let silent_start = { Config::verge().data().enable_silent_start };
|
||||||
if !silent_start.unwrap_or(false) {
|
if !silent_start.unwrap_or(false) {
|
||||||
create_window(&app.app_handle());
|
create_window();
|
||||||
}
|
}
|
||||||
|
|
||||||
log_err!(sysopt::Sysopt::global().init_launch());
|
log_err!(sysopt::Sysopt::global().init_launch());
|
||||||
log_err!(sysopt::Sysopt::global().init_sysproxy());
|
log_err!(sysopt::Sysopt::global().init_sysproxy());
|
||||||
|
|
||||||
log_err!(handle::Handle::update_systray_part());
|
log_err!(handle::Handle::update_systray_part());
|
||||||
log_err!(hotkey::Hotkey::global().init(app.app_handle()));
|
log_err!(hotkey::Hotkey::global().init());
|
||||||
log_err!(timer::Timer::global().init());
|
log_err!(timer::Timer::global().init());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,8 +110,10 @@ pub fn resolve_reset() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// create main window
|
/// create main window
|
||||||
pub fn create_window(app_handle: &AppHandle) {
|
pub fn create_window() {
|
||||||
if let Some(window) = app_handle.get_webview_window("main") {
|
let app_handle: AppHandle = handle::Handle::global().app_handle().unwrap();
|
||||||
|
|
||||||
|
if let Some(window) = handle::Handle::global().get_window() {
|
||||||
trace_err!(window.unminimize(), "set win unminimize");
|
trace_err!(window.unminimize(), "set win unminimize");
|
||||||
trace_err!(window.show(), "set win visible");
|
trace_err!(window.show(), "set win visible");
|
||||||
trace_err!(window.set_focus(), "set win focus");
|
trace_err!(window.set_focus(), "set win focus");
|
||||||
@ -119,7 +121,7 @@ pub fn create_window(app_handle: &AppHandle) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut builder = tauri::WebviewWindowBuilder::new(
|
let mut builder = tauri::WebviewWindowBuilder::new(
|
||||||
app_handle,
|
&app_handle,
|
||||||
"main".to_string(),
|
"main".to_string(),
|
||||||
tauri::WebviewUrl::App("index.html".into()),
|
tauri::WebviewUrl::App("index.html".into()),
|
||||||
)
|
)
|
||||||
@ -208,7 +210,8 @@ pub fn create_window(app_handle: &AppHandle) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// save window size and position
|
/// save window size and position
|
||||||
pub fn save_window_size_position(app_handle: &AppHandle, save_to_file: bool) -> Result<()> {
|
pub fn save_window_size_position(save_to_file: bool) -> Result<()> {
|
||||||
|
let app_handle: AppHandle = handle::Handle::global().app_handle().unwrap();
|
||||||
let verge = Config::verge();
|
let verge = Config::verge();
|
||||||
let mut verge = verge.latest();
|
let mut verge = verge.latest();
|
||||||
|
|
||||||
@ -236,6 +239,8 @@ pub fn save_window_size_position(app_handle: &AppHandle, save_to_file: bool) ->
|
|||||||
pub async fn resolve_scheme(param: String) -> Result<()> {
|
pub async fn resolve_scheme(param: String) -> Result<()> {
|
||||||
log::info!("received deep link: {}", param);
|
log::info!("received deep link: {}", param);
|
||||||
|
|
||||||
|
let app_handle: AppHandle = handle::Handle::global().app_handle().unwrap();
|
||||||
|
|
||||||
let param_str = if param.starts_with("[") && param.len() > 4 {
|
let param_str = if param.starts_with("[") && param.len() > 4 {
|
||||||
param
|
param
|
||||||
.get(2..param.len() - 2)
|
.get(2..param.len() - 2)
|
||||||
@ -269,35 +274,31 @@ pub async fn resolve_scheme(param: String) -> Result<()> {
|
|||||||
.decode_utf8_lossy()
|
.decode_utf8_lossy()
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
let handle = handle::Handle::global();
|
create_window();
|
||||||
let app_handle = handle.app_handle.lock().clone();
|
match PrfItem::from_url(url.as_ref(), name, None, None).await {
|
||||||
if let Some(app_handle) = app_handle.as_ref() {
|
Ok(item) => {
|
||||||
create_window(app_handle);
|
let uid = item.uid.clone().unwrap();
|
||||||
match PrfItem::from_url(url.as_ref(), name, None, None).await {
|
let _ = wrap_err!(Config::profiles().data().append_item(item));
|
||||||
Ok(item) => {
|
app_handle
|
||||||
let uid = item.uid.clone().unwrap();
|
.notification()
|
||||||
let _ = wrap_err!(Config::profiles().data().append_item(item));
|
.builder()
|
||||||
app_handle
|
.title("Clash Verge")
|
||||||
.notification()
|
.body("Import profile success")
|
||||||
.builder()
|
.show()
|
||||||
.title("Clash Verge")
|
.unwrap();
|
||||||
.body("Import profile success")
|
|
||||||
.show()
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
handle::Handle::notice_message("import_sub_url::ok", uid);
|
handle::Handle::notice_message("import_sub_url::ok", uid);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
app_handle
|
app_handle
|
||||||
.notification()
|
.notification()
|
||||||
.builder()
|
.builder()
|
||||||
.title("Clash Verge")
|
.title("Clash Verge")
|
||||||
.body(format!("Import profile failed: {e}"))
|
.body(format!("Import profile failed: {e}"))
|
||||||
.show()
|
.show()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
handle::Handle::notice_message("import_sub_url::error", e.to_string());
|
handle::Handle::notice_message("import_sub_url::error", e.to_string());
|
||||||
bail!("Failed to add subscriptions: {e}");
|
bail!("Failed to add subscriptions: {e}");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,6 @@ use crate::log_err;
|
|||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
use port_scanner::local_port_available;
|
use port_scanner::local_port_available;
|
||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
use tauri::AppHandle;
|
|
||||||
use warp::Filter;
|
use warp::Filter;
|
||||||
|
|
||||||
#[derive(serde::Deserialize, Debug)]
|
#[derive(serde::Deserialize, Debug)]
|
||||||
@ -42,13 +41,12 @@ pub async fn check_singleton() -> Result<()> {
|
|||||||
|
|
||||||
/// The embed server only be used to implement singleton process
|
/// The embed server only be used to implement singleton process
|
||||||
/// maybe it can be used as pac server later
|
/// maybe it can be used as pac server later
|
||||||
pub fn embed_server(app_handle: &AppHandle) {
|
pub fn embed_server() {
|
||||||
let port = IVerge::get_singleton_port();
|
let port = IVerge::get_singleton_port();
|
||||||
|
|
||||||
let handle = app_handle.clone();
|
|
||||||
tauri::async_runtime::spawn(async move {
|
tauri::async_runtime::spawn(async move {
|
||||||
let visible = warp::path!("commands" / "visible").map(move || {
|
let visible = warp::path!("commands" / "visible").map(move || {
|
||||||
resolve::create_window(&handle);
|
resolve::create_window();
|
||||||
"ok"
|
"ok"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user