From 224096abce57b1942fba09fbda5dc85c4bf1cb3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B8=8C=E4=BA=9A=E7=9A=84=E8=A5=BF=E7=BA=A2=E6=9F=BF?= Date: Wed, 16 Apr 2025 02:19:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20auto=20enable=20light=20mode=20config=20?= =?UTF-8?q?does=20not=20take=20effect=20when=20silent=20s=E2=80=A6=20(#334?= =?UTF-8?q?4)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: auto enable light mode config does not take effect when silent start is enabled * refactor: extract run_once_lightweight logic into lightweight.rs as a fn --- src-tauri/src/module/lightweight.rs | 27 +++++++++++++++++++++++---- src-tauri/src/utils/resolve.rs | 8 ++++++-- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src-tauri/src/module/lightweight.rs b/src-tauri/src/module/lightweight.rs index d0e51fbf..4f55b494 100644 --- a/src-tauri/src/module/lightweight.rs +++ b/src-tauri/src/module/lightweight.rs @@ -1,7 +1,3 @@ -use anyhow::{Context, Result}; -use delay_timer::prelude::TaskBuilder; -use tauri::{Listener, Manager}; - use crate::{ config::Config, core::{handle, timer::Timer}, @@ -10,6 +6,13 @@ use crate::{ AppHandleManager, }; +use anyhow::{Context, Result}; +use delay_timer::prelude::TaskBuilder; +use once_cell::sync::OnceCell; +use tauri::{Listener, Manager}; + +pub static AUTO_LIGHT_WEIGHT_MODE_INIT: OnceCell<()> = OnceCell::new(); + const LIGHT_WEIGHT_TASK_UID: &str = "light_weight_task"; pub fn enable_auto_light_weight_mode() { @@ -143,3 +146,19 @@ fn cancel_light_weight_timer() -> Result<()> { Ok(()) } + +pub fn run_once_auto_lightweight() { + AUTO_LIGHT_WEIGHT_MODE_INIT.get_or_init(|| { + 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" + ); + enable_auto_light_weight_mode(); + } + }); +} diff --git a/src-tauri/src/utils/resolve.rs b/src-tauri/src/utils/resolve.rs index 379e03cf..dc1a7c5a 100644 --- a/src-tauri/src/utils/resolve.rs +++ b/src-tauri/src/utils/resolve.rs @@ -108,8 +108,9 @@ pub async fn resolve_setup(app: &mut App) { logging_error!(Type::System, true, timer::Timer::global().init()); - let enable_auto_light_weight_mode = { Config::verge().data().enable_auto_light_weight_mode }; - if enable_auto_light_weight_mode.unwrap_or(false) { + 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(); } @@ -295,6 +296,9 @@ pub fn create_window(is_showup: bool) { Ok(window) => { logging!(info, Type::Window, true, "Window created successfully"); + // 静默启动模式等窗口初始化再启动自动进入轻量模式的计时监听器,防止初始化的时候找不到窗口对象导致监听器挂载失败 + lightweight::run_once_auto_lightweight(); + // 标记前端UI已准备就绪,向前端发送启动完成事件 let app_handle_clone = app_handle.clone();