use crate::utils::{config, dirs}; use anyhow::Result; use once_cell::sync::OnceCell; use parking_lot::Mutex; use serde::{Deserialize, Serialize}; use std::sync::Arc; pub struct VergeN { pub config: Arc>, } impl VergeN { pub fn global() -> &'static VergeN { static DATA: OnceCell = OnceCell::new(); DATA.get_or_init(|| { let config = config::read_yaml::(dirs::verge_path()); VergeN { config: Arc::new(Mutex::new(config)), } }) } /// Save IVerge App Config pub fn save_file(&self) -> Result<()> { let config = self.config.lock(); config::save_yaml( dirs::verge_path(), &*config, Some("# The Config for Clash IVerge App\n\n"), ) } /// patch verge config /// only save to file pub fn patch_config(&self, patch: IVerge) -> Result<()> { let mut config = self.config.lock(); macro_rules! patch { ($key: tt) => { if patch.$key.is_some() { config.$key = patch.$key; } }; } patch!(language); patch!(theme_mode); patch!(theme_blur); patch!(traffic_graph); patch!(enable_tun_mode); patch!(enable_service_mode); patch!(enable_auto_launch); patch!(enable_silent_start); patch!(enable_system_proxy); patch!(enable_proxy_guard); patch!(system_proxy_bypass); patch!(proxy_guard_duration); patch!(theme_setting); patch!(web_ui_list); patch!(clash_core); patch!(hotkeys); patch!(auto_close_connection); patch!(default_latency_test); self.save_file() } /// 在初始化前尝试拿到单例端口的值 pub fn get_singleton_port() -> u16 { let config = config::read_yaml::(dirs::verge_path()); #[cfg(not(feature = "verge-dev"))] const SERVER_PORT: u16 = 33331; #[cfg(feature = "verge-dev")] const SERVER_PORT: u16 = 11233; config.app_singleton_port.unwrap_or(SERVER_PORT) } } /// ### `verge.yaml` schema #[derive(Default, Debug, Clone, Deserialize, Serialize)] pub struct IVerge { /// app listening port /// for app singleton pub app_singleton_port: Option, // i18n pub language: Option, /// `light` or `dark` or `system` pub theme_mode: Option, /// enable blur mode /// maybe be able to set the alpha pub theme_blur: Option, /// enable traffic graph default is true pub traffic_graph: Option, /// clash tun mode pub enable_tun_mode: Option, /// windows service mode #[serde(skip_serializing_if = "Option::is_none")] pub enable_service_mode: Option, /// can the app auto startup pub enable_auto_launch: Option, /// not show the window on launch pub enable_silent_start: Option, /// set system proxy pub enable_system_proxy: Option, /// enable proxy guard pub enable_proxy_guard: Option, /// set system proxy bypass pub system_proxy_bypass: Option, /// proxy guard duration pub proxy_guard_duration: Option, /// theme setting pub theme_setting: Option, /// web ui list pub web_ui_list: Option>, /// clash core path #[serde(skip_serializing_if = "Option::is_none")] pub clash_core: Option, /// hotkey map /// format: {func},{key} pub hotkeys: Option>, /// 切换代理时自动关闭连接 pub auto_close_connection: Option, /// 默认的延迟测试连接 pub default_latency_test: Option, } #[derive(Default, Debug, Clone, Deserialize, Serialize)] pub struct IVergeTheme { pub primary_color: Option, pub secondary_color: Option, pub primary_text: Option, pub secondary_text: Option, pub info_color: Option, pub error_color: Option, pub warning_color: Option, pub success_color: Option, pub font_family: Option, pub css_injection: Option, }