diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 2d55bf14..261c79b5 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -956,7 +956,6 @@ name = "clash-verge" version = "2.0.0" dependencies = [ "anyhow", - "auto-launch", "boa_engine", "chrono", "deelevate", @@ -982,6 +981,7 @@ dependencies = [ "sysproxy", "tauri", "tauri-build", + "tauri-plugin-autostart", "tauri-plugin-clipboard-manager", "tauri-plugin-deep-link", "tauri-plugin-devtools", @@ -2730,7 +2730,7 @@ dependencies = [ "httpdate", "itoa 1.0.11", "pin-project-lite", - "socket2 0.4.10", + "socket2 0.5.7", "tokio", "tower-service", "tracing", @@ -6257,6 +6257,21 @@ dependencies = [ "walkdir", ] +[[package]] +name = "tauri-plugin-autostart" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bba6bb936e0fd0a58ed958b49e2e423dd40949c9d9425cc991be996959e3838e" +dependencies = [ + "auto-launch", + "log", + "serde", + "serde_json", + "tauri", + "tauri-plugin", + "thiserror 1.0.69", +] + [[package]] name = "tauri-plugin-clipboard-manager" version = "2.0.2" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index fe1d62c4..ca865e07 100755 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -30,7 +30,6 @@ once_cell = "1.19" port_scanner = "0.1.5" delay_timer = "0.11" parking_lot = "0.12" -auto-launch = "0.5.0" percent-encoding = "2.3.1" window-shadows = { version = "0.2.2" } tokio = { version = "1", features = ["full"] } @@ -67,6 +66,7 @@ url = "2.5.2" users = "0.11.0" [target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies] +tauri-plugin-autostart = "2.0.0-rc" tauri-plugin-global-shortcut = "2.0.1" tauri-plugin-updater = "2.0.2" tauri-plugin-window-state = "2.0.2" diff --git a/src-tauri/capabilities/desktop.json b/src-tauri/capabilities/desktop.json index f50c5730..b483d98d 100755 --- a/src-tauri/capabilities/desktop.json +++ b/src-tauri/capabilities/desktop.json @@ -8,6 +8,7 @@ "updater:default", "deep-link:default", "window-state:default", - "window-state:default" + "window-state:default", + "autostart:default" ] } diff --git a/src-tauri/package.json b/src-tauri/package.json new file mode 100644 index 00000000..cfc05b54 --- /dev/null +++ b/src-tauri/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "@tauri-apps/plugin-autostart": "^2.0.0-rc" + } +} diff --git a/src-tauri/pnpm-lock.yaml b/src-tauri/pnpm-lock.yaml new file mode 100644 index 00000000..70bcb60a --- /dev/null +++ b/src-tauri/pnpm-lock.yaml @@ -0,0 +1,32 @@ +lockfileVersion: "9.0" + +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false + +importers: + .: + dependencies: + "@tauri-apps/plugin-autostart": + specifier: ^2.0.0-rc + version: 2.0.0 + +packages: + "@tauri-apps/api@2.0.3": + resolution: + { + integrity: sha512-840qk6n8rbXBXMA5/aAgTYsg5JAubKO0nXw5wf7IzGnUuYKGbB4oFBIZtXOIWy+E0kNTDI3qhq5iqsoMJfwp8g==, + } + + "@tauri-apps/plugin-autostart@2.0.0": + resolution: + { + integrity: sha512-NEwOQWVasZ8RczXkMLNJokRDujneuMH/UFA5t84DLkbNZUmiD3G7HZWhgSd1YQ0BFU9h9w+h2B/py3y6bzWg4Q==, + } + +snapshots: + "@tauri-apps/api@2.0.3": {} + + "@tauri-apps/plugin-autostart@2.0.0": + dependencies: + "@tauri-apps/api": 2.0.3 diff --git a/src-tauri/src/core/sysopt.rs b/src-tauri/src/core/sysopt.rs index 0674d5b4..525d03ea 100644 --- a/src-tauri/src/core/sysopt.rs +++ b/src-tauri/src/core/sysopt.rs @@ -1,22 +1,22 @@ +use crate::core::handle::Handle; use crate::{ config::{Config, IVerge}, log_err, }; -use anyhow::{anyhow, Result}; -use auto_launch::{AutoLaunch, AutoLaunchBuilder}; +use anyhow::Result; use once_cell::sync::OnceCell; use parking_lot::Mutex; -use std::env::current_exe; use std::sync::Arc; use sysproxy::{Autoproxy, Sysproxy}; use tauri::async_runtime::Mutex as TokioMutex; +use tauri_plugin_autostart::ManagerExt; use tokio::time::{sleep, Duration}; pub struct Sysopt { update_sysproxy: Arc>, reset_sysproxy: Arc>, /// helps to auto launch the app - auto_launch: Arc>>, + auto_launch: Arc>, /// record whether the guard async is running or not guard_state: Arc>, } @@ -56,7 +56,7 @@ impl Sysopt { SYSOPT.get_or_init(|| Sysopt { update_sysproxy: Arc::new(TokioMutex::new(false)), reset_sysproxy: Arc::new(TokioMutex::new(false)), - auto_launch: Arc::new(Mutex::new(None)), + auto_launch: Arc::new(Mutex::new(false)), guard_state: Arc::new(false.into()), }) } @@ -214,76 +214,17 @@ impl Sysopt { Ok(()) } - /// init the auto launch - pub fn init_launch(&self) -> Result<()> { - let app_exe = current_exe()?; - // let app_exe = dunce::canonicalize(app_exe)?; - let app_name = app_exe - .file_stem() - .and_then(|f| f.to_str()) - .ok_or(anyhow!("failed to get file stem"))?; - - let app_path = app_exe - .as_os_str() - .to_str() - .ok_or(anyhow!("failed to get app_path"))? - .to_string(); - - // fix issue #26 - #[cfg(target_os = "windows")] - let app_path = format!("\"{app_path}\""); - - // use the /Applications/Clash Verge.app path - #[cfg(target_os = "macos")] - let app_path = (|| -> Option { - let path = std::path::PathBuf::from(&app_path); - let path = path.parent()?.parent()?.parent()?; - let extension = path.extension()?.to_str()?; - match extension == "app" { - true => Some(path.as_os_str().to_str()?.to_string()), - false => None, - } - })() - .unwrap_or(app_path); - - #[cfg(target_os = "linux")] - let app_path = { - use crate::core::handle::Handle; - use tauri::Manager; - - let app_handle = Handle::global().app_handle(); - match app_handle { - Some(app_handle) => { - let appimage = app_handle.env().appimage; - appimage - .and_then(|p| p.to_str().map(|s| s.to_string())) - .unwrap_or(app_path) - } - None => app_path, - } - }; - - let auto = AutoLaunchBuilder::new() - .set_app_name(app_name) - .set_app_path(&app_path) - .build()?; - - *self.auto_launch.lock() = Some(auto); - - Ok(()) - } - /// update the startup pub fn update_launch(&self) -> Result<()> { - let auto_launch = self.auto_launch.lock(); - + let _lock = self.auto_launch.lock(); let enable = { Config::verge().latest().enable_auto_launch }; let enable = enable.unwrap_or(false); - let auto_launch = auto_launch.as_ref().unwrap(); - + let app_handle = Handle::global().app_handle().unwrap(); + let autostart_manager = app_handle.autolaunch(); + println!("enable: {}", enable); match enable { - true => auto_launch.enable()?, - false => log_err!(auto_launch.disable()), // 忽略关闭的错误 + true => log_err!(autostart_manager.enable()), + false => log_err!(autostart_manager.disable()), }; Ok(()) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 845b1e7c..d7748eb2 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -4,11 +4,11 @@ mod core; mod enhance; mod feat; mod utils; - use crate::core::hotkey; use crate::utils::{resolve, resolve::resolve_scheme, server}; #[cfg(target_os = "macos")] use tauri::Listener; +use tauri_plugin_autostart::MacosLauncher; pub fn run() { // 单例检测 @@ -32,6 +32,10 @@ pub fn run() { #[allow(unused_mut)] let mut builder = tauri::Builder::default() + .plugin(tauri_plugin_autostart::init( + MacosLauncher::LaunchAgent, + None, + )) .plugin(tauri_plugin_window_state::Builder::new().build()) .plugin(tauri_plugin_updater::Builder::new().build()) .plugin(tauri_plugin_clipboard_manager::init()) diff --git a/src-tauri/src/utils/resolve.rs b/src-tauri/src/utils/resolve.rs index fdf46c6d..034704a9 100644 --- a/src-tauri/src/utils/resolve.rs +++ b/src-tauri/src/utils/resolve.rs @@ -100,7 +100,6 @@ pub async fn resolve_setup(app: &mut App) { create_window(); } - log_err!(sysopt::Sysopt::global().init_launch()); log_err!(sysopt::Sysopt::global().update_sysproxy().await); log_err!(sysopt::Sysopt::global().init_guard_sysproxy());