Files
clash-verge-rev/src-tauri/src/core/verge.rs
T

125 lines
3.1 KiB
Rust
Raw Normal View History

2022-04-20 01:44:47 +08:00
use crate::utils::{config, dirs};
2022-04-20 11:17:54 +08:00
use anyhow::Result;
2021-12-14 15:55:29 +08:00
use serde::{Deserialize, Serialize};
/// ### `verge.yaml` schema
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
2022-04-20 11:17:54 +08:00
pub struct Verge {
2022-03-12 23:07:45 +08:00
// i18n
pub language: Option<String>,
2021-12-19 23:56:16 +08:00
/// `light` or `dark`
pub theme_mode: Option<String>,
2022-01-12 02:27:29 +08:00
/// enable blur mode
/// maybe be able to set the alpha
pub theme_blur: Option<bool>,
2022-02-19 18:14:40 +08:00
/// enable traffic graph default is true
pub traffic_graph: Option<bool>,
2022-02-25 02:09:39 +08:00
/// clash tun mode
pub enable_tun_mode: Option<bool>,
2021-12-19 23:56:16 +08:00
/// can the app auto startup
pub enable_auto_launch: Option<bool>,
2021-12-19 23:56:16 +08:00
2022-03-26 18:56:16 +08:00
/// not show the window on launch
pub enable_silent_start: Option<bool>,
2021-12-19 23:56:16 +08:00
/// set system proxy
pub enable_system_proxy: Option<bool>,
2021-12-27 02:29:28 +08:00
2022-02-20 18:53:21 +08:00
/// enable proxy guard
pub enable_proxy_guard: Option<bool>,
2021-12-27 02:29:28 +08:00
/// set system proxy bypass
pub system_proxy_bypass: Option<String>,
2022-02-20 20:11:39 +08:00
/// proxy guard duration
pub proxy_guard_duration: Option<u64>,
2022-03-31 23:11:50 +08:00
/// theme setting
pub theme_setting: Option<VergeTheme>,
}
#[derive(Default, Debug, Clone, Deserialize, Serialize)]
pub struct VergeTheme {
pub primary_color: Option<String>,
pub secondary_color: Option<String>,
2022-03-31 23:34:36 +08:00
pub primary_text: Option<String>,
pub secondary_text: Option<String>,
2022-03-31 23:11:50 +08:00
pub info_color: Option<String>,
pub error_color: Option<String>,
pub warning_color: Option<String>,
pub success_color: Option<String>,
pub font_family: Option<String>,
2022-04-01 02:08:42 +08:00
pub css_injection: Option<String>,
2021-12-14 15:55:29 +08:00
}
2022-01-07 23:29:20 +08:00
2022-04-20 11:17:54 +08:00
impl Verge {
2022-01-07 23:29:20 +08:00
pub fn new() -> Self {
2022-04-20 11:17:54 +08:00
config::read_yaml::<Verge>(dirs::verge_path())
2022-01-07 23:29:20 +08:00
}
/// Save Verge App Config
2022-02-28 01:34:25 +08:00
pub fn save_file(&self) -> Result<()> {
2022-01-07 23:29:20 +08:00
config::save_yaml(
2022-02-28 01:34:25 +08:00
dirs::verge_path(),
2022-01-07 23:29:20 +08:00
self,
Some("# The Config for Clash Verge App\n\n"),
)
}
2022-01-11 02:24:43 +08:00
/// patch verge config
2022-04-20 01:44:47 +08:00
/// only save to file
2022-04-20 11:17:54 +08:00
pub fn patch_config(&mut self, patch: Verge) -> Result<()> {
2022-01-11 02:24:43 +08:00
// only change it
2022-03-12 23:07:45 +08:00
if patch.language.is_some() {
2022-04-20 11:17:54 +08:00
self.language = patch.language;
2022-03-12 23:07:45 +08:00
}
2022-01-11 02:24:43 +08:00
if patch.theme_mode.is_some() {
2022-04-20 11:17:54 +08:00
self.theme_mode = patch.theme_mode;
2022-01-11 02:24:43 +08:00
}
2022-01-12 02:27:29 +08:00
if patch.theme_blur.is_some() {
2022-04-20 11:17:54 +08:00
self.theme_blur = patch.theme_blur;
2022-01-12 02:27:29 +08:00
}
2022-03-31 23:11:50 +08:00
if patch.theme_setting.is_some() {
2022-04-20 11:17:54 +08:00
self.theme_setting = patch.theme_setting;
2022-03-31 23:11:50 +08:00
}
2022-04-20 01:44:47 +08:00
if patch.traffic_graph.is_some() {
2022-04-20 11:17:54 +08:00
self.traffic_graph = patch.traffic_graph;
2022-04-20 01:44:47 +08:00
}
2022-01-11 02:24:43 +08:00
2022-04-20 01:44:47 +08:00
// system setting
if patch.enable_silent_start.is_some() {
2022-04-20 11:17:54 +08:00
self.enable_silent_start = patch.enable_silent_start;
2022-04-20 01:44:47 +08:00
}
if patch.enable_auto_launch.is_some() {
2022-04-20 11:17:54 +08:00
self.enable_auto_launch = patch.enable_auto_launch;
2022-01-11 02:24:43 +08:00
}
2022-04-20 01:44:47 +08:00
// proxy
2022-01-11 02:24:43 +08:00
if patch.enable_system_proxy.is_some() {
2022-04-20 11:17:54 +08:00
self.enable_system_proxy = patch.enable_system_proxy;
2022-01-11 02:24:43 +08:00
}
if patch.system_proxy_bypass.is_some() {
2022-04-20 11:17:54 +08:00
self.system_proxy_bypass = patch.system_proxy_bypass;
2022-01-11 02:24:43 +08:00
}
2022-02-20 18:53:21 +08:00
if patch.enable_proxy_guard.is_some() {
2022-04-20 11:17:54 +08:00
self.enable_proxy_guard = patch.enable_proxy_guard;
2022-02-20 18:53:21 +08:00
}
2022-02-20 20:11:39 +08:00
if patch.proxy_guard_duration.is_some() {
2022-04-20 11:17:54 +08:00
self.proxy_guard_duration = patch.proxy_guard_duration;
2022-02-20 20:11:39 +08:00
}
2022-02-20 18:53:21 +08:00
2022-04-20 01:44:47 +08:00
// tun mode
2022-02-25 02:09:39 +08:00
if patch.enable_tun_mode.is_some() {
2022-04-20 11:17:54 +08:00
self.enable_tun_mode = patch.enable_tun_mode;
2022-02-25 02:09:39 +08:00
}
2022-04-20 11:17:54 +08:00
self.save_file()
2022-01-11 02:24:43 +08:00
}
2022-02-20 18:53:21 +08:00
}