mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-01-29 00:35:38 +08:00
fix: resolve lightweight mode state detection issues and improve logging #3814
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
- 修复和优化服务检查流程
|
- 修复和优化服务检查流程
|
||||||
- 修复2.4.1引入的订阅地址重定向报错问题
|
- 修复2.4.1引入的订阅地址重定向报错问题
|
||||||
- 修复 rpm/deb 包名称问题
|
- 修复 rpm/deb 包名称问题
|
||||||
|
- 修复托盘轻量模式状态检测异常
|
||||||
|
|
||||||
### 👙 界面样式
|
### 👙 界面样式
|
||||||
|
|
||||||
|
|||||||
@@ -996,11 +996,11 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if crate::module::lightweight::is_in_lightweight_mode() {
|
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
|
crate::module::lightweight::exit_lightweight_mode().await; // Await async function
|
||||||
}
|
}
|
||||||
let result = WindowManager::show_main_window().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" => {
|
"system_proxy" => {
|
||||||
feat::toggle_system_proxy().await; // Await async function
|
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
|
crate::module::lightweight::exit_lightweight_mode().await; // Await async function
|
||||||
use crate::utils::window_manager::WindowManager;
|
use crate::utils::window_manager::WindowManager;
|
||||||
let result = WindowManager::show_main_window().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:?}");
|
||||||
} else {
|
} else {
|
||||||
crate::module::lightweight::entry_lightweight_mode().await; // Remove .await as it's not async
|
crate::module::lightweight::entry_lightweight_mode().await; // Remove .await as it's not async
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -81,8 +81,7 @@ mod app_init {
|
|||||||
{
|
{
|
||||||
builder = builder.plugin(tauri_plugin_devtools::init());
|
builder = builder.plugin(tauri_plugin_devtools::init());
|
||||||
}
|
}
|
||||||
|
builder
|
||||||
builder.manage(Mutex::new(state::lightweight::LightWeightState::default()))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Setup deep link handling
|
/// Setup deep link handling
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ use crate::{
|
|||||||
core::{handle, timer::Timer, tray::Tray},
|
core::{handle, timer::Timer, tray::Tray},
|
||||||
log_err, logging,
|
log_err, logging,
|
||||||
process::AsyncHandler,
|
process::AsyncHandler,
|
||||||
state::lightweight::LightWeightState,
|
utils::{logging::Type, window_manager::WindowManager},
|
||||||
utils::logging::Type,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(target_os = "macos")]
|
#[cfg(target_os = "macos")]
|
||||||
@@ -12,7 +11,6 @@ use crate::logging_error;
|
|||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use delay_timer::prelude::TaskBuilder;
|
use delay_timer::prelude::TaskBuilder;
|
||||||
use parking_lot::Mutex;
|
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
use std::sync::atomic::{AtomicBool, Ordering};
|
||||||
use tauri::{Listener, Manager};
|
use tauri::{Listener, Manager};
|
||||||
|
|
||||||
@@ -21,23 +19,20 @@ const LIGHT_WEIGHT_TASK_UID: &str = "light_weight_task";
|
|||||||
// 添加退出轻量模式的锁,防止并发调用
|
// 添加退出轻量模式的锁,防止并发调用
|
||||||
static EXITING_LIGHTWEIGHT: AtomicBool = AtomicBool::new(false);
|
static EXITING_LIGHTWEIGHT: AtomicBool = AtomicBool::new(false);
|
||||||
|
|
||||||
fn with_lightweight_status<F, R>(f: F) -> Option<R>
|
static IS_IN_LIGHTWEIGHT: AtomicBool = AtomicBool::new(false);
|
||||||
where
|
|
||||||
F: FnOnce(&mut LightWeightState) -> R,
|
fn inner_set_lightweight_mode(value: bool) -> bool {
|
||||||
{
|
if value {
|
||||||
if let Some(app_handle) = handle::Handle::global().app_handle() {
|
logging!(info, Type::Lightweight, true, "轻量模式已开启");
|
||||||
// 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 {
|
} else {
|
||||||
// State not managed yet, return None
|
logging!(info, Type::Lightweight, true, "轻量模式已关闭");
|
||||||
None
|
|
||||||
}
|
}
|
||||||
} else {
|
IS_IN_LIGHTWEIGHT.store(value, Ordering::SeqCst);
|
||||||
// App handle not available yet
|
value
|
||||||
None
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn inner_get_lightweight_mode() -> bool {
|
||||||
|
IS_IN_LIGHTWEIGHT.load(Ordering::SeqCst) || !WindowManager::is_main_window_exists()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn run_once_auto_lightweight() {
|
pub async fn run_once_auto_lightweight() {
|
||||||
@@ -68,29 +63,14 @@ 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 {
|
if let Err(e) = Tray::global().update_part().await {
|
||||||
log::warn!("Failed to update tray: {e}");
|
log::warn!("Failed to update tray: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn auto_lightweight_mode_init() -> Result<()> {
|
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 =
|
let is_silent_start =
|
||||||
{ Config::verge().await.latest_ref().enable_silent_start }.unwrap_or(false);
|
{ Config::verge().await.latest_ref().enable_silent_start }.unwrap_or(false);
|
||||||
let enable_auto = {
|
let enable_auto = {
|
||||||
@@ -117,23 +97,19 @@ pub async fn auto_lightweight_mode_init() -> Result<()> {
|
|||||||
return Err(e);
|
return Err(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// 检查是否处于轻量模式
|
// 检查是否处于轻量模式
|
||||||
pub fn is_in_lightweight_mode() -> bool {
|
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) {
|
pub async fn set_lightweight_mode(value: bool) {
|
||||||
if with_lightweight_status(|state| {
|
if inner_get_lightweight_mode() != value {
|
||||||
state.set_lightweight_mode(value);
|
inner_set_lightweight_mode(value);
|
||||||
})
|
|
||||||
.is_some()
|
|
||||||
{
|
|
||||||
// 只有在状态可用时才触发托盘更新
|
// 只有在状态可用时才触发托盘更新
|
||||||
if let Err(e) = Tray::global().update_part().await {
|
if let Err(e) = Tray::global().update_part().await {
|
||||||
log::warn!("Failed to update tray: {e}");
|
log::warn!("Failed to update tray: {e}");
|
||||||
@@ -180,7 +156,7 @@ pub async fn entry_lightweight_mode() {
|
|||||||
let _ = cancel_light_weight_timer();
|
let _ = cancel_light_weight_timer();
|
||||||
|
|
||||||
// 更新托盘显示
|
// 更新托盘显示
|
||||||
let _tray = crate::core::tray::Tray::global();
|
logging_error!(Type::Lightweight, true, Tray::global().update_part().await);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 添加从轻量模式恢复的函数
|
// 添加从轻量模式恢复的函数
|
||||||
|
|||||||
@@ -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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
// Tauri Manager 会进行 Arc 管理,无需额外 Arc
|
// Tauri Manager 会进行 Arc 管理,无需额外 Arc
|
||||||
// https://tauri.app/develop/state-management/#do-you-need-arc
|
// https://tauri.app/develop/state-management/#do-you-need-arc
|
||||||
|
|
||||||
pub mod lightweight;
|
|
||||||
pub mod proxy;
|
pub mod proxy;
|
||||||
|
|||||||
@@ -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 {
|
pub fn is_main_window_visible() -> bool {
|
||||||
Self::get_main_window()
|
Self::get_main_window()
|
||||||
|
|||||||
Reference in New Issue
Block a user