diff --git a/src-tauri/src/core/handle.rs b/src-tauri/src/core/handle.rs index a37d97a94..38f87eeec 100644 --- a/src-tauri/src/core/handle.rs +++ b/src-tauri/src/core/handle.rs @@ -294,6 +294,10 @@ impl Handle { } } + pub fn is_initialized(&self) -> bool { + self.app_handle().is_some() + } + /// 获取 AppHandle pub fn app_handle(&self) -> Option { self.app_handle.read().clone() @@ -522,3 +526,54 @@ impl Handle { *self.is_exiting.read() } } + +#[cfg(target_os = "macos")] +impl Handle { + pub fn set_activation_policy(&self, policy: tauri::ActivationPolicy) -> Result<(), String> { + let app_handle = self.app_handle(); + if let Some(app_handle) = app_handle.as_ref() { + app_handle + .set_activation_policy(policy) + .map_err(|e| e.to_string()) + } else { + Err("AppHandle not initialized".to_string()) + } + } + + pub fn set_activation_policy_regular(&self) { + if let Err(e) = self.set_activation_policy(tauri::ActivationPolicy::Regular) { + logging!( + warn, + Type::Setup, + true, + "Failed to set regular activation policy: {}", + e + ); + } + } + + pub fn set_activation_policy_accessory(&self) { + if let Err(e) = self.set_activation_policy(tauri::ActivationPolicy::Accessory) { + logging!( + warn, + Type::Setup, + true, + "Failed to set accessory activation policy: {}", + e + ); + } + } + + #[allow(dead_code)] + pub fn set_activation_policy_prohibited(&self) { + if let Err(e) = self.set_activation_policy(tauri::ActivationPolicy::Prohibited) { + logging!( + warn, + Type::Setup, + true, + "Failed to set prohibited activation policy: {}", + e + ); + } + } +} diff --git a/src-tauri/src/feat/window.rs b/src-tauri/src/feat/window.rs index fe38e96da..370bc2a0d 100644 --- a/src-tauri/src/feat/window.rs +++ b/src-tauri/src/feat/window.rs @@ -1,5 +1,3 @@ -#[cfg(target_os = "macos")] -use crate::AppHandleManager; use crate::{ config::Config, core::{handle, sysopt, CoreManager}, @@ -259,5 +257,5 @@ pub async fn hide() { let _ = window.hide(); } } - AppHandleManager::global().set_activation_policy_accessory(); + handle::Handle::global().set_activation_policy_accessory(); } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 8a686b15a..a14f56b51 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -26,120 +26,10 @@ use tauri_plugin_deep_link::DeepLinkExt; use tokio::time::{timeout, Duration}; use utils::logging::Type; -/// A global singleton handle to the application. -pub struct AppHandleManager { - handle: Mutex>, -} - -impl AppHandleManager { - /// Create a new AppHandleManager instance - fn new() -> Self { - Self { - handle: Mutex::new(None), - } - } - - /// Initialize the app handle manager with an app handle. - pub fn init(&self, handle: AppHandle) { - let mut app_handle = self.handle.lock(); - if app_handle.is_none() { - *app_handle = Some(handle); - logging!( - info, - Type::Setup, - true, - "AppHandleManager initialized with handle" - ); - } - } - - /// Get the app handle if it has been initialized. - fn get(&self) -> Option { - self.handle.lock().clone() - } - - /// Get the app handle, panics if it hasn't been initialized. - pub fn get_handle(&self) -> AppHandle { - if let Some(handle) = self.get() { - handle - } else { - logging!( - error, - Type::Setup, - "AppHandle not initialized - ensure init() was called first" - ); - std::process::exit(1) - } - } - - /// Check if the app handle has been initialized. - pub fn is_initialized(&self) -> bool { - self.handle.lock().is_some() - } - - #[cfg(target_os = "macos")] - pub fn set_activation_policy(&self, policy: tauri::ActivationPolicy) -> Result<(), String> { - let app_handle = self.handle.lock(); - if let Some(app_handle) = app_handle.as_ref() { - app_handle - .set_activation_policy(policy) - .map_err(|e| e.to_string()) - } else { - Err("AppHandle not initialized".to_string()) - } - } - - pub fn set_activation_policy_regular(&self) { - #[cfg(target_os = "macos")] - { - if let Err(e) = self.set_activation_policy(tauri::ActivationPolicy::Regular) { - logging!( - warn, - Type::Setup, - true, - "Failed to set regular activation policy: {}", - e - ); - } - } - } - - pub fn set_activation_policy_accessory(&self) { - #[cfg(target_os = "macos")] - { - if let Err(e) = self.set_activation_policy(tauri::ActivationPolicy::Accessory) { - logging!( - warn, - Type::Setup, - true, - "Failed to set accessory activation policy: {}", - e - ); - } - } - } - - pub fn set_activation_policy_prohibited(&self) { - #[cfg(target_os = "macos")] - { - if let Err(e) = self.set_activation_policy(tauri::ActivationPolicy::Prohibited) { - logging!( - warn, - Type::Setup, - true, - "Failed to set prohibited activation policy: {}", - e - ); - } - } - } -} - -// Use unified singleton macro -singleton_with_logging!(AppHandleManager, INSTANCE, "AppHandleManager"); - /// Application initialization helper functions mod app_init { + use crate::core::handle; + use super::*; /// Initialize singleton monitoring for other instances @@ -150,8 +40,8 @@ mod app_init { Ok(result) => { if result.is_err() { logging!(info, Type::Setup, true, "检测到已有应用实例运行"); - if let Some(app_handle) = AppHandleManager::global().get() { - app_handle.exit(0); + if handle::Handle::global().is_initialized() { + handle::Handle::global().app_handle().unwrap().exit(0); } else { std::process::exit(0); } @@ -271,9 +161,6 @@ mod app_init { /// Initialize core components synchronously pub async fn init_core_sync(app_handle: &AppHandle) -> Result<(), Box> { - logging!(info, Type::Setup, true, "初始化AppHandleManager..."); - AppHandleManager::global().init(app_handle.clone()); - logging!(info, Type::Setup, true, "初始化核心句柄..."); core::handle::Handle::global().init(app_handle.clone()); @@ -509,12 +396,14 @@ pub fn run() { /// Event handling helper functions mod event_handlers { + use crate::core::handle; + use super::*; /// Handle application ready/resumed events pub fn handle_ready_resumed(app_handle: &AppHandle) { logging!(info, Type::System, true, "应用就绪或恢复"); - AppHandleManager::global().init(app_handle.clone()); + handle::Handle::global().init(app_handle.clone()); #[cfg(target_os = "macos")] { @@ -536,11 +425,11 @@ pub fn run() { has_visible_windows ); - AppHandleManager::global().init(app_handle.clone()); + handle::Handle::global().init(app_handle.clone()); if !has_visible_windows { // 当没有可见窗口时,设置为 regular 模式并显示主窗口 - AppHandleManager::global().set_activation_policy_regular(); + handle::Handle::global().set_activation_policy_regular(); logging!(info, Type::System, true, "没有可见窗口,尝试显示主窗口"); @@ -560,7 +449,7 @@ pub fn run() { /// Handle window close requests pub fn handle_window_close(api: &tauri::WindowEvent) { #[cfg(target_os = "macos")] - AppHandleManager::global().set_activation_policy_accessory(); + handle::Handle::global().set_activation_policy_accessory(); if core::handle::Handle::global().is_exiting() { return; diff --git a/src-tauri/src/module/lightweight.rs b/src-tauri/src/module/lightweight.rs index 115ffb762..48821e8ae 100644 --- a/src-tauri/src/module/lightweight.rs +++ b/src-tauri/src/module/lightweight.rs @@ -9,8 +9,6 @@ use crate::{ #[cfg(target_os = "macos")] use crate::logging_error; -#[cfg(target_os = "macos")] -use crate::AppHandleManager; use anyhow::{Context, Result}; use delay_timer::prelude::TaskBuilder; @@ -173,7 +171,7 @@ pub async fn entry_lightweight_mode() { let _ = webview.destroy(); } #[cfg(target_os = "macos")] - AppHandleManager::global().set_activation_policy_accessory(); + handle::Handle::global().set_activation_policy_accessory(); } set_lightweight_mode(true).await; let _ = cancel_light_weight_timer(); @@ -213,7 +211,7 @@ pub async fn exit_lightweight_mode() { // macOS激活策略 #[cfg(target_os = "macos")] - AppHandleManager::global().set_activation_policy_regular(); + handle::Handle::global().set_activation_policy_regular(); // 重置UI就绪状态 crate::utils::resolve::reset_ui_ready(); diff --git a/src-tauri/src/utils/resolve.rs b/src-tauri/src/utils/resolve.rs index b16aaec77..e2c85a3ed 100644 --- a/src-tauri/src/utils/resolve.rs +++ b/src-tauri/src/utils/resolve.rs @@ -1,5 +1,3 @@ -#[cfg(target_os = "macos")] -use crate::AppHandleManager; use crate::{ config::{Config, PrfItem}, core::*, @@ -185,9 +183,7 @@ pub async fn resolve_setup_async(app_handle: &AppHandle) { #[cfg(target_os = "macos")] { if is_silent_start { - use crate::AppHandleManager; - - AppHandleManager::global().set_activation_policy_accessory(); + handle::Handle::global().set_activation_policy_accessory(); } } create_window(!is_silent_start).await; @@ -273,9 +269,7 @@ pub async fn create_window(is_show: bool) -> bool { let _ = window.set_focus(); #[cfg(target_os = "macos")] - { - AppHandleManager::global().set_activation_policy_regular(); - } + handle::Handle::global().set_activation_policy_regular(); } return true; } @@ -432,9 +426,7 @@ pub async fn create_window(is_show: bool) -> bool { let _ = window_clone.set_focus(); logging!(info, Type::Window, true, "窗口已立即显示"); #[cfg(target_os = "macos")] - { - AppHandleManager::global().set_activation_policy_regular(); - } + handle::Handle::global().set_activation_policy_regular(); let timeout_seconds = if crate::module::lightweight::is_in_lightweight_mode() { 3 diff --git a/src-tauri/src/utils/window_manager.rs b/src-tauri/src/utils/window_manager.rs index 45f6ae7af..c1b287f9e 100644 --- a/src-tauri/src/utils/window_manager.rs +++ b/src-tauri/src/utils/window_manager.rs @@ -1,9 +1,6 @@ use crate::{core::handle, logging, utils::logging::Type}; use tauri::{Manager, WebviewWindow, Wry}; -#[cfg(target_os = "macos")] -use crate::AppHandleManager; - use once_cell::sync::OnceCell; use parking_lot::Mutex; use scopeguard; @@ -283,7 +280,7 @@ impl WindowManager { #[cfg(target_os = "macos")] { logging!(info, Type::Window, true, "应用 macOS 特定的激活策略"); - AppHandleManager::global().set_activation_policy_regular(); + handle::Handle::global().set_activation_policy_regular(); } #[cfg(target_os = "windows")]