feat: add lightweight mode entry and related hotkey support

This commit is contained in:
Tunglies
2025-03-23 03:10:48 +08:00
parent 69347160e9
commit d669650758
9 changed files with 39 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
use crate::{config::Config, core::handle, feat, log_err, utils::resolve};
use crate::{config::Config, core::handle, feat, log_err, module::lightweight, utils::resolve};
use anyhow::{bail, Result};
use once_cell::sync::OnceCell;
use parking_lot::Mutex;
@@ -147,6 +147,7 @@ impl Hotkey {
"clash_mode_direct" => || feat::change_clash_mode("direct".into()),
"toggle_system_proxy" => || feat::toggle_system_proxy(),
"toggle_tun_mode" => || feat::toggle_tun_mode(None),
"entry_lightweight_mode" => || feat::lightweight_mode(),
"quit" => || feat::quit(Some(0)),
#[cfg(target_os = "macos")]
"hide" => || feat::hide(),

View File

@@ -6,7 +6,7 @@ use crate::{
cmd,
config::Config,
feat,
module::mihomo::Rate,
module::{lightweight, mihomo::Rate},
resolve,
utils::{dirs, i18n::t, resolve::VERSION},
};
@@ -94,6 +94,7 @@ impl Tray {
tray.on_tray_icon_event(|_, event| {
let tray_event = { Config::verge().latest().tray_event.clone() };
let tray_event: String = tray_event.unwrap_or("main_window".into());
log::debug!(target: "app","tray event: {:?}", tray_event);
if let TrayIconEvent::Click {
button: MouseButton::Left,
@@ -525,6 +526,15 @@ fn create_tray_menu(
)
.unwrap();
let lighteweight_mode = &MenuItem::with_id(
app_handle,
"entry_lightweight_mode",
t("Lightweight Mode"),
true,
hotkeys.get("entry_lightweight_mode").map(|s| s.as_str()),
)
.unwrap();
let copy_env =
&MenuItem::with_id(app_handle, "copy_env", t("Copy Env"), true, None::<&str>).unwrap();
@@ -617,6 +627,8 @@ fn create_tray_menu(
separator,
system_proxy,
tun_mode,
separator,
lighteweight_mode,
copy_env,
open_dir,
more,
@@ -644,6 +656,7 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
"open_logs_dir" => crate::log_err!(cmd::open_logs_dir()),
"restart_clash" => feat::restart_clash_core(),
"restart_app" => feat::restart_app(),
"entry_lightweight_mode" => feat::lightweight_mode(),
"quit" => {
println!("quit");
feat::quit(Some(0));

View File

@@ -0,0 +1,6 @@
use crate::module::lightweight::entry_lightweight_mode;
pub fn lightweight_mode() {
log::info!(target: "app","Lightweight mode enabled");
entry_lightweight_mode();
}

View File

@@ -1,6 +1,7 @@
mod backup;
mod clash;
mod config;
mod lightweight;
mod profile;
mod proxy;
mod window;
@@ -9,6 +10,7 @@ mod window;
pub use backup::*;
pub use clash::*;
pub use config::*;
pub use lightweight::*;
pub use profile::*;
pub use proxy::*;
pub use window::*;

View File

@@ -26,12 +26,14 @@ pub fn disable_auto_light_weight_mode() {
pub fn entry_lightweight_mode() {
if let Some(window) = handle::Handle::global().get_window() {
if window.is_visible().unwrap_or(false) {
let _ = window.hide();
}
if let Some(webview) = window.get_webview_window("main") {
let _ = webview.destroy();
let _ = window.hide();
println!("[lightweight_mode] 轻量模式已开启");
log::info!(target: "app", "[lightweight_mode] 轻量模式已开启");
}
println!("[lightweight_mode] 轻量模式已开启");
log::info!(target: "app", "[lightweight_mode] 轻量模式已开启");
}
let _ = cancel_light_weight_timer();
}