refactor(config): use bitflags for tracking update operations

Replace multiple boolean variables with a bitflag approach for tracking
required update operations in the patch_verge function. This improves
code maintainability and potentially performance by:

1. Using a single integer variable with bit operations instead of multiple booleans
2. Defining clear flags as enum variants for better code readability
3. Simplifying flag checks with bitwise operations

The UpdateFlags enum provides a clear and type-safe way to represent
different types of updates needed when patching Verge configuration.
This commit is contained in:
Tunglies 2025-03-15 18:42:57 +08:00
parent e98ce0c2ae
commit d6a48deb5a

View File

@ -41,6 +41,22 @@ pub async fn patch_clash(patch: Mapping) -> Result<()> {
} }
} }
// Define update flags as bitflags for better performance
#[derive(Clone, Copy)]
enum UpdateFlags {
None = 0,
RestartCore = 1 << 0,
ClashConfig = 1 << 1,
VergeConfig = 1 << 2,
Launch = 1 << 3,
SysProxy = 1 << 4,
SystrayIcon = 1 << 5,
Hotkey = 1 << 6,
SystrayMenu = 1 << 7,
SystrayTooltip = 1 << 8,
SystrayClickBehavior = 1 << 9,
}
/// Patch Verge configuration /// Patch Verge configuration
pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> { pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> {
Config::verge().draft().patch_config(patch.clone()); Config::verge().draft().patch_config(patch.clone());
@ -79,36 +95,25 @@ pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> {
let home_cards = patch.home_cards.clone(); let home_cards = patch.home_cards.clone();
let res: std::result::Result<(), anyhow::Error> = { let res: std::result::Result<(), anyhow::Error> = {
let mut should_restart_core = false; // Initialize with no flags set
let mut should_update_clash_config = false; let mut update_flags: i32 = UpdateFlags::None as i32;
let mut should_update_verge_config = false;
let mut should_update_launch = false;
let mut should_update_sysproxy = false;
let mut should_update_systray_icon = false;
let mut should_update_hotkey = false;
let mut should_update_systray_menu = false;
let mut should_update_systray_tooltip = false;
let mut should_update_systray_click_behavior = false;
if tun_mode.is_some() { if tun_mode.is_some() {
should_update_clash_config = true; update_flags |= UpdateFlags::ClashConfig as i32;
should_update_systray_menu = true; update_flags |= UpdateFlags::SystrayMenu as i32;
should_update_systray_tooltip = true; update_flags |= UpdateFlags::SystrayTooltip as i32;
should_update_systray_icon = true; update_flags |= UpdateFlags::SystrayIcon as i32;
} }
if enable_global_hotkey.is_some() { if enable_global_hotkey.is_some() || home_cards.is_some() {
should_update_verge_config = true; update_flags |= UpdateFlags::VergeConfig as i32;
}
if home_cards.is_some() {
should_update_verge_config = true;
} }
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
if redir_enabled.is_some() || redir_port.is_some() { if redir_enabled.is_some() || redir_port.is_some() {
should_restart_core = true; update_flags |= UpdateFlags::RestartCore as i32;
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
if tproxy_enabled.is_some() || tproxy_port.is_some() { if tproxy_enabled.is_some() || tproxy_port.is_some() {
should_restart_core = true; update_flags |= UpdateFlags::RestartCore as i32;
} }
if socks_enabled.is_some() if socks_enabled.is_some()
|| http_enabled.is_some() || http_enabled.is_some()
@ -116,87 +121,79 @@ pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> {
|| http_port.is_some() || http_port.is_some()
|| mixed_port.is_some() || mixed_port.is_some()
{ {
should_restart_core = true; update_flags |= UpdateFlags::RestartCore as i32;
} }
if auto_launch.is_some() { if auto_launch.is_some() {
should_update_launch = true; update_flags |= UpdateFlags::Launch as i32;
} }
if system_proxy.is_some() { if system_proxy.is_some() {
should_update_sysproxy = true; update_flags |= UpdateFlags::SysProxy as i32;
should_update_systray_menu = true; update_flags |= UpdateFlags::SystrayMenu as i32;
should_update_systray_tooltip = true; update_flags |= UpdateFlags::SystrayTooltip as i32;
should_update_systray_icon = true; update_flags |= UpdateFlags::SystrayIcon as i32;
} }
if proxy_bypass.is_some() || pac_content.is_some() || pac.is_some() { if proxy_bypass.is_some() || pac_content.is_some() || pac.is_some() {
should_update_sysproxy = true; update_flags |= UpdateFlags::SysProxy as i32;
} }
if language.is_some() { if language.is_some() {
should_update_systray_menu = true; update_flags |= UpdateFlags::SystrayMenu as i32;
} }
if common_tray_icon.is_some() if common_tray_icon.is_some()
|| sysproxy_tray_icon.is_some() || sysproxy_tray_icon.is_some()
|| tun_tray_icon.is_some() || tun_tray_icon.is_some()
|| tray_icon.is_some() || tray_icon.is_some()
|| enable_tray_speed.is_some()
{ {
should_update_systray_icon = true; update_flags |= UpdateFlags::SystrayIcon as i32;
} }
if patch.hotkeys.is_some() { if patch.hotkeys.is_some() {
should_update_hotkey = true; update_flags |= UpdateFlags::Hotkey as i32;
should_update_systray_menu = true; update_flags |= UpdateFlags::SystrayMenu as i32;
}
if enable_tray_speed.is_some() {
should_update_systray_icon = true;
} }
if tray_event.is_some() { if tray_event.is_some() {
should_update_systray_click_behavior = true; update_flags |= UpdateFlags::SystrayClickBehavior as i32;
} }
if should_restart_core { // Process updates based on flags
if (update_flags & (UpdateFlags::RestartCore as i32)) != 0 {
CoreManager::global().restart_core().await?; CoreManager::global().restart_core().await?;
} }
if should_update_clash_config { if (update_flags & (UpdateFlags::ClashConfig as i32)) != 0 {
CoreManager::global().update_config().await?; CoreManager::global().update_config().await?;
handle::Handle::refresh_clash(); handle::Handle::refresh_clash();
} }
if should_update_verge_config { if (update_flags & (UpdateFlags::VergeConfig as i32)) != 0 {
Config::verge().draft().enable_global_hotkey = enable_global_hotkey; Config::verge().draft().enable_global_hotkey = enable_global_hotkey;
handle::Handle::refresh_verge(); handle::Handle::refresh_verge();
} }
if should_update_launch { if (update_flags & (UpdateFlags::Launch as i32)) != 0 {
sysopt::Sysopt::global().update_launch()?; sysopt::Sysopt::global().update_launch()?;
} }
if (update_flags & (UpdateFlags::SysProxy as i32)) != 0 {
if should_update_sysproxy {
sysopt::Sysopt::global().update_sysproxy().await?; sysopt::Sysopt::global().update_sysproxy().await?;
} }
if (update_flags & (UpdateFlags::Hotkey as i32)) != 0 {
if should_update_hotkey {
hotkey::Hotkey::global().update(patch.hotkeys.unwrap())?; hotkey::Hotkey::global().update(patch.hotkeys.unwrap())?;
} }
if (update_flags & (UpdateFlags::SystrayMenu as i32)) != 0 {
if should_update_systray_menu {
tray::Tray::global().update_menu()?; tray::Tray::global().update_menu()?;
} }
if (update_flags & (UpdateFlags::SystrayIcon as i32)) != 0 {
if should_update_systray_icon {
tray::Tray::global().update_icon(None)?; tray::Tray::global().update_icon(None)?;
} }
if (update_flags & (UpdateFlags::SystrayTooltip as i32)) != 0 {
if should_update_systray_tooltip {
tray::Tray::global().update_tooltip()?; tray::Tray::global().update_tooltip()?;
} }
if (update_flags & (UpdateFlags::SystrayClickBehavior as i32)) != 0 {
if should_update_systray_click_behavior {
tray::Tray::global().update_click_behavior()?; tray::Tray::global().update_click_behavior()?;
} }
// 处理轻量模式切换 // Handle lite mode switch
if lite_mode.is_some() { if lite_mode.is_some() {
if let Some(window) = handle::Handle::global().get_window() { if let Some(window) = handle::Handle::global().get_window() {
if lite_mode.unwrap() { if lite_mode.unwrap() {