refactor: backend (#7102)

* refactor(tray): move tray log opening to utils helper

* refactor(enhance): move owned config through pipeline

* refactor(enhance): move sequence values in use_seq

* refactor(enhance): move values while sorting fields

* refactor(enhance): move values when lowercasing owned config

* refactor(backend): move owned config values in utilities

* refactor(tray): borrow hotkey accelerators

* refactor(core): borrow proxy config strings

* fix(backend): preserve config patch semantics

* refactor(enhance): clarify config transform names
This commit is contained in:
Tunglies
2026-05-26 10:47:03 +00:00
committed by GitHub
parent a287d13b4d
commit 761209bbbc
11 changed files with 198 additions and 157 deletions
+12 -18
View File
@@ -60,23 +60,16 @@ static DEFAULT_BYPASS: &str =
"127.0.0.1,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,localhost,*.local,*.crashlytics.com,<local>";
async fn get_bypass() -> String {
let use_default = Config::verge().await.latest_arc().use_default_bypass.unwrap_or(true);
let res = {
let verge = Config::verge().await;
let verge = verge.latest_arc();
verge.system_proxy_bypass.clone()
};
let custom_bypass = match res {
Some(bypass) => bypass,
None => "".into(),
};
let verge = Config::verge().await.latest_arc();
let use_default = verge.use_default_bypass.unwrap_or(true);
let custom_bypass = verge.system_proxy_bypass.as_deref().unwrap_or("");
if custom_bypass.is_empty() {
DEFAULT_BYPASS.into()
} else if use_default {
format!("{DEFAULT_BYPASS},{custom_bypass}").into()
} else {
custom_bypass
custom_bypass.into()
}
}
@@ -141,18 +134,19 @@ impl Sysopt {
None => Config::clash().await.latest_arc().get_mixed_port(),
};
let pac_port = IVerge::get_singleton_port();
let (sys_enable, pac_enable, proxy_host, proxy_guard) = (
verge.enable_system_proxy.unwrap_or_default(),
verge.proxy_auto_config.unwrap_or_default(),
verge.proxy_host.clone().unwrap_or_else(|| String::from("127.0.0.1")),
verge.enable_proxy_guard.unwrap_or_default(),
);
// 先 await, 避免持有锁导致的 Send 问题
let bypass = get_bypass().await;
let (sys_enable, pac_enable, proxy_host, proxy_guard) = (
verge.enable_system_proxy.unwrap_or_default(),
verge.proxy_auto_config.unwrap_or_default(),
verge.proxy_host.as_deref().unwrap_or("127.0.0.1"),
verge.enable_proxy_guard.unwrap_or_default(),
);
let (sys, auto, guard_type) = {
let (sys, auto) = &mut *self.inner_proxy.write();
sys.host = proxy_host.clone().into();
sys.host = proxy_host.into();
sys.port = port;
sys.bypass = bypass.into();
auto.url = format!("http://{proxy_host}:{pac_port}/commands/pac");
+18 -15
View File
@@ -6,8 +6,11 @@ use crate::process::AsyncHandler;
use crate::singleton;
use crate::utils::window_manager::WindowManager;
use crate::{
Type, cmd, config::Config, feat, logging, module::lightweight::is_in_lightweight_mode,
utils::dirs::find_target_icons,
Type, cmd,
config::Config,
feat, logging,
module::lightweight::is_in_lightweight_mode,
utils::{dirs::find_target_icons, help},
};
use clash_verge_limiter::{Limiter, SystemClock, SystemLimiter};
use clash_verge_logging::logging_error;
@@ -399,7 +402,7 @@ impl Tray {
}
}
fn create_hotkeys(hotkeys: &Option<Vec<String>>) -> HashMap<String, String> {
fn create_hotkeys(hotkeys: &Option<Vec<String>>) -> HashMap<&str, &str> {
hotkeys
.as_ref()
.map(|h| {
@@ -412,13 +415,13 @@ fn create_hotkeys(hotkeys: &Option<Vec<String>>) -> HashMap<String, String> {
if key.to_uppercase().contains("NUMPAD") {
None
} else {
Some((func.into(), key.into()))
Some((func, key))
}
}
_ => None,
}
})
.collect::<std::collections::HashMap<String, String>>()
.collect::<HashMap<&str, &str>>()
})
.unwrap_or_default()
}
@@ -655,7 +658,7 @@ async fn create_tray_menu(
MenuIds::DASHBOARD,
&texts.dashboard,
true,
hotkeys.get("open_or_close_dashboard").map(|s| s.as_str()),
hotkeys.get("open_or_close_dashboard").copied(),
)?;
let rule_mode = &CheckMenuItem::with_id(
@@ -664,7 +667,7 @@ async fn create_tray_menu(
&texts.rule_mode,
true,
current_proxy_mode == "rule",
hotkeys.get("clash_mode_rule").map(|s| s.as_str()),
hotkeys.get("clash_mode_rule").copied(),
)?;
let global_mode = &CheckMenuItem::with_id(
@@ -673,7 +676,7 @@ async fn create_tray_menu(
&texts.global_mode,
true,
current_proxy_mode == "global",
hotkeys.get("clash_mode_global").map(|s| s.as_str()),
hotkeys.get("clash_mode_global").copied(),
)?;
let direct_mode = &CheckMenuItem::with_id(
@@ -682,7 +685,7 @@ async fn create_tray_menu(
&texts.direct_mode,
true,
current_proxy_mode == "direct",
hotkeys.get("clash_mode_direct").map(|s| s.as_str()),
hotkeys.get("clash_mode_direct").copied(),
)?;
let outbound_modes = if show_outbound_modes_inline {
@@ -730,7 +733,7 @@ async fn create_tray_menu(
&texts.system_proxy,
true,
system_proxy_enabled,
hotkeys.get("toggle_system_proxy").map(|s| s.as_str()),
hotkeys.get("toggle_system_proxy").copied(),
)?;
let tun_mode = &CheckMenuItem::with_id(
@@ -739,7 +742,7 @@ async fn create_tray_menu(
&texts.tun_mode,
tun_mode_available,
tun_mode_enabled,
hotkeys.get("toggle_tun_mode").map(|s| s.as_str()),
hotkeys.get("toggle_tun_mode").copied(),
)?;
let close_all_connections = &MenuItem::with_id(
@@ -756,7 +759,7 @@ async fn create_tray_menu(
&texts.lightweight_mode,
true,
is_lightweight_mode,
hotkeys.get("entry_lightweight_mode").map(|s| s.as_str()),
hotkeys.get("entry_lightweight_mode").copied(),
)?;
let copy_env = &MenuItem::with_id(app_handle, MenuIds::COPY_ENV, &texts.copy_env, true, None::<&str>)?;
@@ -811,7 +814,7 @@ async fn create_tray_menu(
],
)?;
let quit_accelerator = hotkeys.get("quit").map(|s| s.as_str());
let quit_accelerator = hotkeys.get("quit").copied();
#[cfg(target_os = "macos")]
let quit_accelerator = quit_accelerator.or(Some("Cmd+Q"));
@@ -953,10 +956,10 @@ fn on_menu_event(_: &AppHandle, event: MenuEvent) {
let _ = cmd::open_logs_dir().await;
}
MenuIds::APP_LOG => {
let _ = cmd::open_app_log().await;
let _ = help::open_app_latest_log();
}
MenuIds::CORE_LOG => {
let _ = cmd::open_core_log().await;
let _ = help::open_core_latest_log();
}
MenuIds::RESTART_CLASH => feat::restart_clash_core().await,
MenuIds::RESTART_APP => feat::restart_app().await,