fix: resolve lightweight mode state detection issues and improve logging #3814

This commit is contained in:
Tunglies
2025-09-02 08:00:53 +08:00
Unverified
parent 45fdebeaca
commit 7aef9d2a5a
7 changed files with 52 additions and 108 deletions

View File

@@ -10,6 +10,7 @@
- 修复和优化服务检查流程
- 修复2.4.1引入的订阅地址重定向报错问题
- 修复 rpm/deb 包名称问题
- 修复托盘轻量模式状态检测异常
### 👙 界面样式

View File

@@ -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
}

View File

@@ -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

View File

@@ -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, R>(f: F) -> Option<R>
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::<Mutex<LightWeightState>>() {
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::<Mutex<LightWeightState>>().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);
}
// 添加从轻量模式恢复的函数

View File

@@ -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<Once>,
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<LightWeightState> = OnceLock::new();
INSTANCE.get_or_init(LightWeightState::new).clone()
}
}

View File

@@ -1,5 +1,4 @@
// Tauri Manager 会进行 Arc 管理,无需额外 Arc
// https://tauri.app/develop/state-management/#do-you-need-arc
pub mod lightweight;
pub mod proxy;

View File

@@ -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()