feat: implement lightweight mode state management and initialization #3344, d05952e945
This commit is contained in:
@@ -36,6 +36,7 @@
|
||||
- 修复切换语言时设置页面可能加载失败
|
||||
- 修复编辑器中连字符处理问题
|
||||
- 修复提权漏洞,改用带认证的 IPC 通信机制
|
||||
- 修复静默启动无法使用自动轻量模式
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -206,6 +206,7 @@ pub fn run() {
|
||||
}
|
||||
|
||||
app.manage(Mutex::new(state::proxy::CmdProxyState::default()));
|
||||
app.manage(Mutex::new(state::lightweight::LightWeightState::default()));
|
||||
|
||||
logging!(info, Type::Setup, true, "初始化完成,继续执行");
|
||||
Ok(())
|
||||
|
||||
@@ -2,6 +2,7 @@ use crate::{
|
||||
config::Config,
|
||||
core::{handle, timer::Timer},
|
||||
log_err, logging,
|
||||
state::lightweight::LightWeightState,
|
||||
utils::logging::Type,
|
||||
};
|
||||
|
||||
@@ -12,40 +13,70 @@ use crate::AppHandleManager;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use delay_timer::prelude::TaskBuilder;
|
||||
use once_cell::sync::OnceCell;
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use std::{
|
||||
sync::Arc,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use std::sync::Mutex;
|
||||
use tauri::{Listener, Manager};
|
||||
|
||||
const LIGHT_WEIGHT_TASK_UID: &str = "light_weight_task";
|
||||
|
||||
// 轻量模式状态标志
|
||||
static IS_LIGHTWEIGHT_MODE: OnceCell<Arc<RwLock<bool>>> = OnceCell::new();
|
||||
|
||||
// 添加一个锁来防止并发退出轻量模式
|
||||
static EXIT_LOCK: OnceCell<Mutex<(bool, Instant)>> = OnceCell::new();
|
||||
|
||||
fn get_lightweight_mode() -> &'static Arc<RwLock<bool>> {
|
||||
IS_LIGHTWEIGHT_MODE.get_or_init(|| Arc::new(RwLock::new(false)))
|
||||
fn with_lightweight_status<F, R>(f: F) -> R
|
||||
where
|
||||
F: FnOnce(&mut LightWeightState) -> R,
|
||||
{
|
||||
let app_handle = handle::Handle::global().app_handle().unwrap();
|
||||
let state = app_handle.state::<Mutex<LightWeightState>>();
|
||||
let mut guard = state.lock().unwrap();
|
||||
f(&mut *guard)
|
||||
}
|
||||
|
||||
fn get_exit_lock() -> &'static Mutex<(bool, Instant)> {
|
||||
EXIT_LOCK.get_or_init(|| Mutex::new((false, Instant::now())))
|
||||
pub fn run_once_auto_lightweight() {
|
||||
LightWeightState::default().run_once_time(|| {
|
||||
let is_silent_start = Config::verge().data().enable_silent_start.unwrap_or(false);
|
||||
let enable_auto = Config::verge()
|
||||
.data()
|
||||
.enable_auto_light_weight_mode
|
||||
.unwrap_or(false);
|
||||
if enable_auto && is_silent_start {
|
||||
logging!(
|
||||
info,
|
||||
Type::Lightweight,
|
||||
true,
|
||||
"Add timer listener when creating window in silent start mode"
|
||||
);
|
||||
set_lightweight_mode(true);
|
||||
enable_auto_light_weight_mode();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn auto_lightweight_mode_init() {
|
||||
if let Some(app_handle) = handle::Handle::global().app_handle() {
|
||||
// 通过 app_handle.state 保证同步
|
||||
let _ = app_handle.state::<Mutex<LightWeightState>>();
|
||||
let is_silent_start = { Config::verge().data().enable_silent_start }.unwrap_or(false);
|
||||
let enable_auto = { Config::verge().data().enable_auto_light_weight_mode }.unwrap_or(false);
|
||||
if enable_auto && is_silent_start {
|
||||
logging!(
|
||||
info,
|
||||
Type::Lightweight,
|
||||
true,
|
||||
"Add timer listener when creating window in silent start mode"
|
||||
);
|
||||
set_lightweight_mode(true);
|
||||
enable_auto_light_weight_mode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否处于轻量模式
|
||||
pub fn is_in_lightweight_mode() -> bool {
|
||||
*get_lightweight_mode().read()
|
||||
with_lightweight_status(|state| state.is_lightweight)
|
||||
}
|
||||
|
||||
// 设置轻量模式状态
|
||||
fn set_lightweight_mode(value: bool) {
|
||||
let mut mode = get_lightweight_mode().write();
|
||||
*mode = value;
|
||||
logging!(info, Type::Lightweight, true, "轻量模式状态: {}", value);
|
||||
with_lightweight_status(|state| {
|
||||
state.set_lightweight_mode(value);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn enable_auto_light_weight_mode() {
|
||||
@@ -73,47 +104,23 @@ pub fn entry_lightweight_mode() {
|
||||
AppHandleManager::global().set_activation_policy_accessory();
|
||||
logging!(info, Type::Lightweight, true, "轻量模式已开启");
|
||||
}
|
||||
// 标记已进入轻量模式
|
||||
set_lightweight_mode(true);
|
||||
let _ = cancel_light_weight_timer();
|
||||
}
|
||||
|
||||
// 添加从轻量模式恢复的函数
|
||||
pub fn exit_lightweight_mode() {
|
||||
// 获取锁,检查是否已经有退出操作在进行中
|
||||
let mut exit_lock = get_exit_lock().lock();
|
||||
let (is_exiting, last_exit_time) = *exit_lock;
|
||||
let now = Instant::now();
|
||||
|
||||
// 如果已经有一个退出操作在进行,并且距离上次退出时间不超过2秒,跳过本次退出
|
||||
if is_exiting && now.duration_since(last_exit_time) < Duration::from_secs(2) {
|
||||
logging!(
|
||||
warn,
|
||||
Type::Lightweight,
|
||||
true,
|
||||
"已有退出轻量模式操作正在进行中,跳过本次请求"
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
*exit_lock = (true, now);
|
||||
|
||||
// 确保当前确实处于轻量模式才执行退出操作
|
||||
if !is_in_lightweight_mode() {
|
||||
logging!(info, Type::Lightweight, true, "当前不在轻量模式,无需退出");
|
||||
exit_lock.0 = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// 标记退出轻量模式
|
||||
set_lightweight_mode(false);
|
||||
logging!(info, Type::Lightweight, true, "正在退出轻量模式");
|
||||
|
||||
// 重置UI就绪状态
|
||||
crate::utils::resolve::reset_ui_ready();
|
||||
|
||||
// 释放锁
|
||||
exit_lock.0 = false;
|
||||
}
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
|
||||
42
src-tauri/src/state/lightweight.rs
Normal file
42
src-tauri/src/state/lightweight.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use std::sync::Once;
|
||||
|
||||
use crate::{logging, utils::logging::Type};
|
||||
|
||||
pub struct LightWeightState {
|
||||
#[allow(unused)]
|
||||
once: Once,
|
||||
pub is_lightweight: bool,
|
||||
}
|
||||
|
||||
impl LightWeightState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
once: Once::new(),
|
||||
is_lightweight: false,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn run_once_time<F>(&self, f: F)
|
||||
where
|
||||
F: FnOnce() + Send + 'static,
|
||||
{
|
||||
self.once.call_once(f);
|
||||
}
|
||||
|
||||
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 {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
// Tauri Manager 会进行 Arc 管理,无需额外 Arc
|
||||
// https://tauri.app/develop/state-management/#do-you-need-arc
|
||||
|
||||
pub mod lightweight;
|
||||
pub mod proxy;
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::{
|
||||
config::{Config, IVerge, PrfItem},
|
||||
core::*,
|
||||
logging, logging_error,
|
||||
module::lightweight,
|
||||
module::lightweight::{self, auto_lightweight_mode_init},
|
||||
process::AsyncHandler,
|
||||
utils::{init, logging::Type, server},
|
||||
wrap_err,
|
||||
@@ -223,11 +223,7 @@ pub async fn resolve_setup_async(app_handle: &AppHandle) {
|
||||
logging_error!(Type::System, true, timer::Timer::global().init());
|
||||
|
||||
// 自动进入轻量模式
|
||||
let enable_auto_light_weight_mode =
|
||||
{ Config::verge().data().enable_auto_light_weight_mode }.unwrap_or(false);
|
||||
if enable_auto_light_weight_mode && !is_silent_start {
|
||||
lightweight::enable_auto_light_weight_mode();
|
||||
}
|
||||
auto_lightweight_mode_init();
|
||||
|
||||
logging_error!(Type::Tray, true, tray::Tray::global().update_part());
|
||||
|
||||
@@ -335,6 +331,8 @@ pub fn create_window(is_show: bool) -> bool {
|
||||
let _ = window_clone.set_focus();
|
||||
logging!(debug, Type::Window, true, "窗口已尝试显示和聚焦");
|
||||
|
||||
lightweight::run_once_auto_lightweight();
|
||||
|
||||
tokio::time::sleep(Duration::from_millis(100)).await; // Crucial delay
|
||||
|
||||
logging!(
|
||||
|
||||
Reference in New Issue
Block a user