refactor: wip

This commit is contained in:
GyDi 2022-11-15 01:33:50 +08:00
parent c8e6f3a627
commit ee68d80d0a
No known key found for this signature in database
GPG Key ID: 9C3AD40F1F99880A
4 changed files with 77 additions and 40 deletions

View File

@ -0,0 +1,20 @@
use super::{Draft, IVerge};
use once_cell::sync::OnceCell;
pub struct Config {
verge_config: Draft<IVerge>,
}
impl Config {
pub fn global() -> &'static Config {
static CONFIG: OnceCell<Config> = OnceCell::new();
CONFIG.get_or_init(|| Config {
verge_config: Draft::from(IVerge::new()),
})
}
pub fn verge() -> Draft<IVerge> {
Self::global().verge_config.clone()
}
}

View File

@ -3,7 +3,7 @@ use parking_lot::{MappedMutexGuard, Mutex, MutexGuard};
use serde_yaml::Mapping; use serde_yaml::Mapping;
use std::sync::Arc; use std::sync::Arc;
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct Draft<T: Clone + ToOwned> { pub struct Draft<T: Clone + ToOwned> {
inner: Arc<Mutex<(T, Option<T>)>>, inner: Arc<Mutex<(T, Option<T>)>>,
} }

View File

@ -1,9 +1,13 @@
mod clash; mod clash;
mod config;
mod draft;
mod prfitem; mod prfitem;
mod profiles; mod profiles;
mod verge; mod verge;
pub use self::clash::*; pub use self::clash::*;
pub use self::config::*;
pub use self::draft::*;
pub use self::prfitem::*; pub use self::prfitem::*;
pub use self::profiles::*; pub use self::profiles::*;
pub use self::verge::*; pub use self::verge::*;

View File

@ -23,51 +23,15 @@ impl VergeN {
/// Save IVerge App Config /// Save IVerge App Config
pub fn save_file(&self) -> Result<()> { pub fn save_file(&self) -> Result<()> {
let config = self.config.lock(); self.config.lock().save_file()
config::save_yaml(
dirs::verge_path(),
&*config,
Some("# The Config for Clash IVerge App\n\n"),
)
} }
/// patch verge config /// patch verge config
/// only save to file /// only save to file
pub fn patch_config(&self, patch: IVerge) -> Result<()> { pub fn patch_config(&self, patch: IVerge) -> Result<()> {
let mut config = self.config.lock(); {
self.config.lock().patch_config(patch);
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);
drop(config);
self.save_file() self.save_file()
} }
@ -165,3 +129,52 @@ pub struct IVergeTheme {
pub font_family: Option<String>, pub font_family: Option<String>,
pub css_injection: Option<String>, pub css_injection: Option<String>,
} }
impl IVerge {
pub fn new() -> Self {
config::read_yaml::<IVerge>(dirs::verge_path())
}
/// Save IVerge App Config
pub fn save_file(&self) -> Result<()> {
config::save_yaml(
dirs::verge_path(),
&self,
Some("# The Config for Clash IVerge App\n\n"),
)
}
/// patch verge config
/// only save to file
pub fn patch_config(&mut self, patch: IVerge) {
macro_rules! patch {
($key: tt) => {
if patch.$key.is_some() {
self.$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);
}
}