From 7aef9d2a5a5fd7bbb815a24f7c8fe8b2be7f54d2 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Tue, 2 Sep 2025 08:00:53 +0800 Subject: [PATCH] fix: resolve lightweight mode state detection issues and improve logging #3814 --- UPDATELOG.md | 1 + src-tauri/src/core/tray/mod.rs | 6 +- src-tauri/src/lib.rs | 3 +- src-tauri/src/module/lightweight.rs | 108 ++++++++++---------------- src-tauri/src/state/lightweight.rs | 36 --------- src-tauri/src/state/mod.rs | 1 - src-tauri/src/utils/window_manager.rs | 5 ++ 7 files changed, 52 insertions(+), 108 deletions(-) delete mode 100644 src-tauri/src/state/lightweight.rs diff --git a/UPDATELOG.md b/UPDATELOG.md index 363bb0c5..06320bae 100644 --- a/UPDATELOG.md +++ b/UPDATELOG.md @@ -10,6 +10,7 @@ - 修复和优化服务检查流程 - 修复2.4.1引入的订阅地址重定向报错问题 - 修复 rpm/deb 包名称问题 +- 修复托盘轻量模式状态检测异常 ### 👙 界面样式 diff --git a/src-tauri/src/core/tray/mod.rs b/src-tauri/src/core/tray/mod.rs index 32e099ee..4fa7c1ac 100644 --- a/src-tauri/src/core/tray/mod.rs +++ b/src-tauri/src/core/tray/mod.rs @@ -996,11 +996,11 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) { } if crate::module::lightweight::is_in_lightweight_mode() { - log::info!(target: "app", "当前在轻量模式,正在退出"); + logging!(info, Type::Lightweight, true, "Exiting Lightweight Mode"); crate::module::lightweight::exit_lightweight_mode().await; // Await async function } let result = WindowManager::show_main_window().await; // Await async function - log::info!(target: "app", "窗口显示结果: {result:?}"); + logging!(info, Type::Window, true, "Show Main Window: {result:?}"); } "system_proxy" => { feat::toggle_system_proxy().await; // Await async function @@ -1030,7 +1030,7 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) { crate::module::lightweight::exit_lightweight_mode().await; // Await async function use crate::utils::window_manager::WindowManager; let result = WindowManager::show_main_window().await; // Await async function - log::info!(target: "app", "退出轻量模式后显示主窗口: {result:?}"); + logging!(info, Type::Window, true, "Show Main Window: {result:?}"); } else { crate::module::lightweight::entry_lightweight_mode().await; // Remove .await as it's not async } diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index bc28a318..3b1694f3 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -81,8 +81,7 @@ mod app_init { { builder = builder.plugin(tauri_plugin_devtools::init()); } - - builder.manage(Mutex::new(state::lightweight::LightWeightState::default())) + builder } /// Setup deep link handling diff --git a/src-tauri/src/module/lightweight.rs b/src-tauri/src/module/lightweight.rs index b2424ffe..05d9628b 100644 --- a/src-tauri/src/module/lightweight.rs +++ b/src-tauri/src/module/lightweight.rs @@ -3,8 +3,7 @@ use crate::{ core::{handle, timer::Timer, tray::Tray}, log_err, logging, process::AsyncHandler, - state::lightweight::LightWeightState, - utils::logging::Type, + utils::{logging::Type, window_manager::WindowManager}, }; #[cfg(target_os = "macos")] @@ -12,7 +11,6 @@ use crate::logging_error; use anyhow::{Context, Result}; use delay_timer::prelude::TaskBuilder; -use parking_lot::Mutex; use std::sync::atomic::{AtomicBool, Ordering}; use tauri::{Listener, Manager}; @@ -21,23 +19,20 @@ const LIGHT_WEIGHT_TASK_UID: &str = "light_weight_task"; // 添加退出轻量模式的锁,防止并发调用 static EXITING_LIGHTWEIGHT: AtomicBool = AtomicBool::new(false); -fn with_lightweight_status(f: F) -> Option -where - F: FnOnce(&mut LightWeightState) -> R, -{ - if let Some(app_handle) = handle::Handle::global().app_handle() { - // Try to get state, but don't panic if it's not managed yet - if let Some(state) = app_handle.try_state::>() { - let mut guard = state.lock(); - Some(f(&mut guard)) - } else { - // State not managed yet, return None - None - } +static IS_IN_LIGHTWEIGHT: AtomicBool = AtomicBool::new(false); + +fn inner_set_lightweight_mode(value: bool) -> bool { + if value { + logging!(info, Type::Lightweight, true, "轻量模式已开启"); } else { - // App handle not available yet - None + logging!(info, Type::Lightweight, true, "轻量模式已关闭"); } + IS_IN_LIGHTWEIGHT.store(value, Ordering::SeqCst); + value +} + +fn inner_get_lightweight_mode() -> bool { + IS_IN_LIGHTWEIGHT.load(Ordering::SeqCst) || !WindowManager::is_main_window_exists() } pub async fn run_once_auto_lightweight() { @@ -68,54 +63,38 @@ pub async fn run_once_auto_lightweight() { "在静默启动的情况下,创建窗口再添加自动进入轻量模式窗口监听器" ); - if with_lightweight_status(|_| ()).is_some() { - set_lightweight_mode(false).await; - enable_auto_light_weight_mode().await; + enable_auto_light_weight_mode().await; - if let Err(e) = Tray::global().update_part().await { - log::warn!("Failed to update tray: {e}"); - } + if let Err(e) = Tray::global().update_part().await { + log::warn!("Failed to update tray: {e}"); } } pub async fn auto_lightweight_mode_init() -> Result<()> { - if let Some(app_handle) = handle::Handle::global().app_handle() { - // Check if state is available before accessing it - if app_handle.try_state::>().is_none() { - logging!( - warn, - Type::Lightweight, - true, - "LightWeightState 尚未初始化,跳过自动轻量模式初始化" - ); - return Err(anyhow::anyhow!("LightWeightState has not been initialized")); - } + let is_silent_start = + { Config::verge().await.latest_ref().enable_silent_start }.unwrap_or(false); + let enable_auto = { + Config::verge() + .await + .latest_ref() + .enable_auto_light_weight_mode + } + .unwrap_or(false); - let is_silent_start = - { Config::verge().await.latest_ref().enable_silent_start }.unwrap_or(false); - let enable_auto = { - Config::verge() - .await - .latest_ref() - .enable_auto_light_weight_mode - } - .unwrap_or(false); + if enable_auto && !is_silent_start { + logging!( + info, + Type::Lightweight, + true, + "非静默启动直接挂载自动进入轻量模式监听器!" + ); + set_lightweight_mode(true).await; + enable_auto_light_weight_mode().await; - if enable_auto && !is_silent_start { - logging!( - info, - Type::Lightweight, - true, - "非静默启动直接挂载自动进入轻量模式监听器!" - ); - set_lightweight_mode(true).await; - enable_auto_light_weight_mode().await; - - // 确保托盘状态更新 - if let Err(e) = Tray::global().update_part().await { - log::warn!("Failed to update tray: {e}"); - return Err(e); - } + // 确保托盘状态更新 + if let Err(e) = Tray::global().update_part().await { + log::warn!("Failed to update tray: {e}"); + return Err(e); } } @@ -124,16 +103,13 @@ pub async fn auto_lightweight_mode_init() -> Result<()> { // 检查是否处于轻量模式 pub fn is_in_lightweight_mode() -> bool { - with_lightweight_status(|state| state.is_lightweight).unwrap_or(false) + IS_IN_LIGHTWEIGHT.load(Ordering::SeqCst) } // 设置轻量模式状态 pub async fn set_lightweight_mode(value: bool) { - if with_lightweight_status(|state| { - state.set_lightweight_mode(value); - }) - .is_some() - { + if inner_get_lightweight_mode() != value { + inner_set_lightweight_mode(value); // 只有在状态可用时才触发托盘更新 if let Err(e) = Tray::global().update_part().await { log::warn!("Failed to update tray: {e}"); @@ -180,7 +156,7 @@ pub async fn entry_lightweight_mode() { let _ = cancel_light_weight_timer(); // 更新托盘显示 - let _tray = crate::core::tray::Tray::global(); + logging_error!(Type::Lightweight, true, Tray::global().update_part().await); } // 添加从轻量模式恢复的函数 diff --git a/src-tauri/src/state/lightweight.rs b/src-tauri/src/state/lightweight.rs deleted file mode 100644 index 5898b265..00000000 --- a/src-tauri/src/state/lightweight.rs +++ /dev/null @@ -1,36 +0,0 @@ -use std::sync::{Arc, Once, OnceLock}; - -use crate::{logging, utils::logging::Type}; - -#[derive(Clone)] -pub struct LightWeightState { - #[allow(unused)] - once: Arc, - pub is_lightweight: bool, -} - -impl LightWeightState { - pub fn new() -> Self { - Self { - once: Arc::new(Once::new()), - is_lightweight: false, - } - } - - pub fn set_lightweight_mode(&mut self, value: bool) -> &Self { - self.is_lightweight = value; - if value { - logging!(info, Type::Lightweight, true, "轻量模式已开启"); - } else { - logging!(info, Type::Lightweight, true, "轻量模式已关闭"); - } - self - } -} - -impl Default for LightWeightState { - fn default() -> Self { - static INSTANCE: OnceLock = OnceLock::new(); - INSTANCE.get_or_init(LightWeightState::new).clone() - } -} diff --git a/src-tauri/src/state/mod.rs b/src-tauri/src/state/mod.rs index d8acadef..94f453c3 100644 --- a/src-tauri/src/state/mod.rs +++ b/src-tauri/src/state/mod.rs @@ -1,5 +1,4 @@ // Tauri Manager 会进行 Arc 管理,无需额外 Arc // https://tauri.app/develop/state-management/#do-you-need-arc -pub mod lightweight; pub mod proxy; diff --git a/src-tauri/src/utils/window_manager.rs b/src-tauri/src/utils/window_manager.rs index 5d7b1cbd..c19b8079 100644 --- a/src-tauri/src/utils/window_manager.rs +++ b/src-tauri/src/utils/window_manager.rs @@ -338,6 +338,11 @@ impl WindowManager { } } + /// 检查窗口是否存在 + pub fn is_main_window_exists() -> bool { + Self::get_main_window().is_some() + } + /// 检查窗口是否可见 pub fn is_main_window_visible() -> bool { Self::get_main_window()