From d6a48deb5a5ce470e1ce1bb71b3038bca64ef8ae Mon Sep 17 00:00:00 2001 From: Tunglies Date: Sat, 15 Mar 2025 18:42:57 +0800 Subject: [PATCH] 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. --- src-tauri/src/feat/config.rs | 105 +++++++++++++++++------------------ 1 file changed, 51 insertions(+), 54 deletions(-) diff --git a/src-tauri/src/feat/config.rs b/src-tauri/src/feat/config.rs index c3627cad..1d992d03 100644 --- a/src-tauri/src/feat/config.rs +++ b/src-tauri/src/feat/config.rs @@ -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 pub async fn patch_verge(patch: IVerge, not_save_file: bool) -> Result<()> { 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 res: std::result::Result<(), anyhow::Error> = { - let mut should_restart_core = false; - let mut should_update_clash_config = false; - 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; + // Initialize with no flags set + let mut update_flags: i32 = UpdateFlags::None as i32; if tun_mode.is_some() { - should_update_clash_config = true; - should_update_systray_menu = true; - should_update_systray_tooltip = true; - should_update_systray_icon = true; + update_flags |= UpdateFlags::ClashConfig as i32; + update_flags |= UpdateFlags::SystrayMenu as i32; + update_flags |= UpdateFlags::SystrayTooltip as i32; + update_flags |= UpdateFlags::SystrayIcon as i32; } - if enable_global_hotkey.is_some() { - should_update_verge_config = true; - } - if home_cards.is_some() { - should_update_verge_config = true; + if enable_global_hotkey.is_some() || home_cards.is_some() { + update_flags |= UpdateFlags::VergeConfig as i32; } #[cfg(not(target_os = "windows"))] if redir_enabled.is_some() || redir_port.is_some() { - should_restart_core = true; + update_flags |= UpdateFlags::RestartCore as i32; } #[cfg(target_os = "linux")] if tproxy_enabled.is_some() || tproxy_port.is_some() { - should_restart_core = true; + update_flags |= UpdateFlags::RestartCore as i32; } if socks_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() || mixed_port.is_some() { - should_restart_core = true; + update_flags |= UpdateFlags::RestartCore as i32; } if auto_launch.is_some() { - should_update_launch = true; + update_flags |= UpdateFlags::Launch as i32; } if system_proxy.is_some() { - should_update_sysproxy = true; - should_update_systray_menu = true; - should_update_systray_tooltip = true; - should_update_systray_icon = true; + update_flags |= UpdateFlags::SysProxy as i32; + update_flags |= UpdateFlags::SystrayMenu as i32; + update_flags |= UpdateFlags::SystrayTooltip as i32; + update_flags |= UpdateFlags::SystrayIcon as i32; } 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() { - should_update_systray_menu = true; + update_flags |= UpdateFlags::SystrayMenu as i32; } if common_tray_icon.is_some() || sysproxy_tray_icon.is_some() || tun_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() { - should_update_hotkey = true; - should_update_systray_menu = true; - } - - if enable_tray_speed.is_some() { - should_update_systray_icon = true; + update_flags |= UpdateFlags::Hotkey as i32; + update_flags |= UpdateFlags::SystrayMenu as i32; } 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?; } - if should_update_clash_config { + if (update_flags & (UpdateFlags::ClashConfig as i32)) != 0 { CoreManager::global().update_config().await?; 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; handle::Handle::refresh_verge(); } - if should_update_launch { + if (update_flags & (UpdateFlags::Launch as i32)) != 0 { sysopt::Sysopt::global().update_launch()?; } - - if should_update_sysproxy { + if (update_flags & (UpdateFlags::SysProxy as i32)) != 0 { sysopt::Sysopt::global().update_sysproxy().await?; } - - if should_update_hotkey { + if (update_flags & (UpdateFlags::Hotkey as i32)) != 0 { hotkey::Hotkey::global().update(patch.hotkeys.unwrap())?; } - - if should_update_systray_menu { + if (update_flags & (UpdateFlags::SystrayMenu as i32)) != 0 { tray::Tray::global().update_menu()?; } - - if should_update_systray_icon { + if (update_flags & (UpdateFlags::SystrayIcon as i32)) != 0 { tray::Tray::global().update_icon(None)?; } - - if should_update_systray_tooltip { + if (update_flags & (UpdateFlags::SystrayTooltip as i32)) != 0 { tray::Tray::global().update_tooltip()?; } - - if should_update_systray_click_behavior { + if (update_flags & (UpdateFlags::SystrayClickBehavior as i32)) != 0 { tray::Tray::global().update_click_behavior()?; } - // 处理轻量模式切换 + // Handle lite mode switch if lite_mode.is_some() { if let Some(window) = handle::Handle::global().get_window() { if lite_mode.unwrap() {