feat: implement lightweight mode functionality and update related settings

This commit is contained in:
Tunglies
2025-03-20 03:23:14 +08:00
parent e31f176c25
commit 91ccb3045c
21 changed files with 104 additions and 126 deletions

View File

@@ -0,0 +1,15 @@
use crate::module::lightweight;
use super::CmdResult;
#[tauri::command]
pub async fn entry_lightweight_mode() -> CmdResult {
lightweight::entry_lightweight_mode();
Ok(())
}
#[tauri::command]
pub async fn exit_lightweight_mode() -> CmdResult {
lightweight::exit_lightweight_mode();
Ok(())
}

View File

@@ -17,6 +17,7 @@ pub mod uwp;
pub mod validate;
pub mod verge;
pub mod webdav;
pub mod lighteweight;
// Re-export all command functions for backwards compatibility
pub use app::*;
@@ -32,3 +33,4 @@ pub use uwp::*;
pub use validate::*;
pub use verge::*;
pub use webdav::*;
pub use lighteweight::*;

View File

@@ -189,9 +189,6 @@ pub struct IVerge {
pub enable_tray_speed: Option<bool>,
/// 轻量模式 - 只保留内核运行
pub enable_lite_mode: Option<bool>,
/// 自动进入轻量模式
pub auto_enter_lite_mode: Option<bool>,
@@ -299,7 +296,6 @@ impl IVerge {
webdav_password: None,
enable_tray_speed: Some(true),
enable_global_hotkey: Some(true),
enable_lite_mode: Some(false),
auto_enter_lite_mode: Some(false),
auto_enter_lite_mode_delay: Some(10),
enable_dns_settings: Some(true),
@@ -385,7 +381,6 @@ impl IVerge {
patch!(webdav_username);
patch!(webdav_password);
patch!(enable_tray_speed);
patch!(enable_lite_mode);
patch!(auto_enter_lite_mode);
patch!(auto_enter_lite_mode_delay);
patch!(enable_dns_settings);
@@ -478,7 +473,6 @@ pub struct IVergeResponse {
pub webdav_username: Option<String>,
pub webdav_password: Option<String>,
pub enable_tray_speed: Option<bool>,
pub enable_lite_mode: Option<bool>,
pub auto_enter_lite_mode: Option<bool>,
pub auto_enter_lite_mode_delay: Option<u16>,
pub enable_dns_settings: Option<bool>,
@@ -545,7 +539,6 @@ impl From<IVerge> for IVergeResponse {
webdav_username: verge.webdav_username,
webdav_password: verge.webdav_password,
enable_tray_speed: verge.enable_tray_speed,
enable_lite_mode: verge.enable_lite_mode,
auto_enter_lite_mode: verge.auto_enter_lite_mode,
auto_enter_lite_mode_delay: verge.auto_enter_lite_mode_delay,
enable_dns_settings: verge.enable_dns_settings,

View File

@@ -41,18 +41,6 @@ impl Handle {
window
}
pub fn destroy_window(&self) -> Result<(), String> {
if let Some(window) = self.get_window() {
log_err!(window.close());
}
if let Some(window) = Self::global().get_window() {
if let Some(webview) = window.get_webview_window("main") {
log_err!(webview.destroy());
}
}
Ok(())
}
pub fn refresh_clash() {
if let Some(window) = Self::global().get_window() {
log_err!(window.emit("verge://refresh-clash-config", "yes"));

View File

@@ -176,15 +176,13 @@ impl Hotkey {
println!("Executing function directly");
log::info!(target: "app", "Executing function directly");
// 获取轻量模式状态和全局热键状态
let is_lite_mode = Config::verge().latest().enable_lite_mode.unwrap_or(false);
// 获取全局热键状态
let is_enable_global_hotkey = Config::verge()
.latest()
.enable_global_hotkey
.unwrap_or(true);
// 在轻量模式下或配置了全局热键时,始终执行热键功能
if is_lite_mode || is_enable_global_hotkey {
if is_enable_global_hotkey {
f();
} else if let Some(window) = app_handle.get_webview_window("main") {
// 非轻量模式且未启用全局热键时,只在窗口可见且有焦点的情况下响应热键

View File

@@ -2,7 +2,6 @@ use crate::{
config::{Config, IVerge},
core::{handle, hotkey, sysopt, tray, CoreManager},
log_err,
utils::resolve,
};
use anyhow::Result;
use serde_yaml::Mapping;
@@ -68,7 +67,6 @@ pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> {
let proxy_bypass = patch.system_proxy_bypass;
let language = patch.language;
let mixed_port = patch.verge_mixed_port;
let lite_mode = patch.enable_lite_mode;
#[cfg(target_os = "macos")]
let tray_icon = patch.tray_icon;
#[cfg(not(target_os = "macos"))]
@@ -192,15 +190,6 @@ pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> {
tray::Tray::global().update_click_behavior()?;
}
// Handle lite mode switch
if let Some(enable) = lite_mode {
if enable {
handle::Handle::global().destroy_window().ok();
} else {
resolve::create_window();
}
}
<Result<()>>::Ok(())
};
match res {

View File

@@ -215,6 +215,9 @@ pub fn run() {
// media unlock checker
cmd::get_unlock_items,
cmd::check_media_unlock,
// light-weight model
cmd::entry_lightweight_mode,
cmd::exit_lightweight_mode,
]);
#[cfg(debug_assertions)]
@@ -283,41 +286,6 @@ pub fn run() {
api.prevent_close();
let window = core::handle::Handle::global().get_window().unwrap();
let _ = window.hide();
// 检查是否启用了自动进入 Lite Mode
let verge = crate::config::Config::verge();
let verge_config = verge.latest();
let auto_enter_lite_mode = verge_config.auto_enter_lite_mode.unwrap_or(false);
if auto_enter_lite_mode {
let delay_minutes = verge_config.auto_enter_lite_mode_delay.unwrap_or(10);
let app_handle_clone = app_handle.clone();
println!("自动进入 Lite Mode 已启用");
// 启动一个线程,在指定延迟后启用 Lite Mode
std::thread::spawn(move || {
println!("等待 {} 分钟后自动进入 Lite Mode", delay_minutes);
std::thread::sleep(std::time::Duration::from_secs(delay_minutes as u64 * 60));
println!("Lite Mode 倒计时结束");
// 延迟后检查窗口是否仍然隐藏,如果是,则启用 Lite Mode
let window_opt = app_handle_clone.get_webview_window("main");
if let Some(window) = window_opt {
if !window.is_visible().unwrap_or(true) {
println!("倒计时结束,正在进入 Lite Mode...");
// 应用 Lite Mode
if let Err(e) = tauri::async_runtime::block_on(crate::feat::patch_verge(
crate::config::IVerge {
enable_lite_mode: Some(true),
..Default::default()
},
false
)) {
println!("Lite Mode 进入失败: {:?}", e);
}
}
}
});
}
}
tauri::WindowEvent::Focused(true) => {
#[cfg(target_os = "macos")]

View File

@@ -0,0 +1,25 @@
use tauri::Manager;
use crate::{core::handle, log_err, utils::resolve};
pub fn entry_lightweight_mode() {
println!("entry_lightweight_mode");
log::debug!(target: "app", "entry_lightweight_mode");
if let Some(window) = handle::Handle::global().get_window() {
log_err!(window.close());
}
if let Some(window) = handle::Handle::global().get_window() {
if let Some(webview) = window.get_webview_window("main") {
log_err!(webview.destroy());
}
}
}
pub fn exit_lightweight_mode() {
println!("exit_lightweight_mode");
log::debug!(target: "app", "exit_lightweight_mode");
resolve::create_window();
}

View File

@@ -1,2 +1,3 @@
pub mod mihomo;
pub mod sysinfo;
pub mod lightweight;