diff --git a/src-tauri/src/cmds/some.rs b/src-tauri/src/cmds/some.rs index 2cf8bfe..49ff1c2 100644 --- a/src-tauri/src/cmds/some.rs +++ b/src-tauri/src/cmds/some.rs @@ -1,10 +1,16 @@ use crate::{ - events::{emit::ClashInfoPayload, state::ClashInfoState}, + config::VergeConfig, + events::{ + emit::ClashInfoPayload, + state::{ClashInfoState, VergeConfLock}, + }, utils::{ clash::run_clash_bin, - sysopt::{set_proxy_config, SysProxyConfig}, + config::{read_clash, save_clash, save_verge}, + sysopt::{get_proxy_config, set_proxy_config, SysProxyConfig}, }, }; +use serde_yaml::Mapping; use tauri::{api::process::kill_children, AppHandle, State}; /// restart the sidecar @@ -19,6 +25,7 @@ pub fn restart_sidecar(app_handle: AppHandle, clash_info: State<'_, ClashInfoSta } /// get the clash core info from the state +/// the caller can also get the infomation by clash's api #[tauri::command] pub fn get_clash_info(clash_info: State<'_, ClashInfoState>) -> Result { match clash_info.0.lock() { @@ -27,6 +34,22 @@ pub fn get_clash_info(clash_info: State<'_, ClashInfoState>) -> Result Result<(), String> { + let mut config = read_clash(); + for (key, value) in payload.iter() { + if config.contains_key(key) { + config[key] = value.clone(); + } else { + config.insert(key.clone(), value.clone()); + } + } + save_clash(&config) +} + /// set the system proxy /// Tips: only support windows now #[tauri::command] @@ -68,3 +91,50 @@ pub fn set_sys_proxy(enable: bool, clash_info: State<'_, ClashInfoState>) -> Res Err(_) => Err(format!("can not set proxy")), } } + +/// get the system proxy +/// Tips: only support windows now +#[tauri::command] +pub fn get_sys_proxy() -> Result { + match get_proxy_config() { + Ok(value) => Ok(value), + Err(err) => Err(err.to_string()), + } +} + +/// get the verge config +#[tauri::command] +pub fn get_verge_config(verge_lock: State<'_, VergeConfLock>) -> Result { + match verge_lock.0.lock() { + Ok(arc) => Ok(arc.clone()), + Err(_) => Err(format!("can not get the lock")), + } +} + +/// patch the verge config +#[tauri::command] +pub async fn patch_verge_config( + payload: VergeConfig, + verge_lock: State<'_, VergeConfLock>, +) -> Result<(), String> { + let mut verge = match verge_lock.0.lock() { + Ok(v) => v, + Err(_) => return Err(format!("can not get the lock")), + }; + + if payload.theme_mode.is_some() { + verge.theme_mode = payload.theme_mode; + } + + // todo + if payload.enable_self_startup.is_some() { + verge.enable_self_startup = payload.enable_self_startup; + } + + // todo + if payload.enable_system_proxy.is_some() { + verge.enable_system_proxy = payload.enable_system_proxy; + } + + save_verge(&verge) +} diff --git a/src-tauri/src/config/verge.rs b/src-tauri/src/config/verge.rs index 830924b..5b7e78e 100644 --- a/src-tauri/src/config/verge.rs +++ b/src-tauri/src/config/verge.rs @@ -3,5 +3,12 @@ use serde::{Deserialize, Serialize}; /// ### `verge.yaml` schema #[derive(Default, Debug, Clone, Deserialize, Serialize)] pub struct VergeConfig { - pub something: Option, + /// `light` or `dark` + pub theme_mode: Option, + + /// can the app auto startup + pub enable_self_startup: Option, + + /// set system proxy + pub enable_system_proxy: Option, } diff --git a/src-tauri/src/events/state.rs b/src-tauri/src/events/state.rs index 7640003..67a91fa 100644 --- a/src-tauri/src/events/state.rs +++ b/src-tauri/src/events/state.rs @@ -1,9 +1,13 @@ use std::sync::{Arc, Mutex}; use super::emit::ClashInfoPayload; +use crate::config::VergeConfig; #[derive(Default)] pub struct ClashInfoState(pub Arc>); #[derive(Default)] pub struct ProfileLock(pub Mutex); + +#[derive(Default)] +pub struct VergeConfLock(pub Arc>); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 271769e..96ee2f6 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -10,7 +10,10 @@ mod config; mod events; mod utils; -use crate::{events::state, utils::clash::put_clash_profile}; +use crate::{ + events::state, + utils::{clash::put_clash_profile, config::read_verge}, +}; use std::sync::{Arc, Mutex}; use tauri::{ api, CustomMenuItem, Manager, SystemTray, SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem, @@ -58,8 +61,12 @@ fn main() -> std::io::Result<()> { }) .invoke_handler(tauri::generate_handler![ cmds::some::restart_sidecar, - cmds::some::get_clash_info, cmds::some::set_sys_proxy, + cmds::some::get_sys_proxy, + cmds::some::get_clash_info, + cmds::some::patch_clash_config, + cmds::some::get_verge_config, + cmds::some::patch_verge_config, cmds::profile::import_profile, cmds::profile::update_profile, cmds::profile::get_profiles, @@ -82,6 +89,7 @@ fn main() -> std::io::Result<()> { }; }); + app.manage(state::VergeConfLock(Arc::new(Mutex::new(read_verge())))); app.manage(state::ClashInfoState(Arc::new(Mutex::new(info)))); app.manage(state::ProfileLock::default()); diff --git a/src-tauri/src/utils/config.rs b/src-tauri/src/utils/config.rs index 9569f2c..194344a 100644 --- a/src-tauri/src/utils/config.rs +++ b/src-tauri/src/utils/config.rs @@ -1,5 +1,5 @@ use crate::{ - config::{ClashController, ProfilesConfig}, + config::{ClashController, ProfilesConfig, VergeConfig}, utils::app_home_dir, }; use serde::{de::DeserializeOwned, Serialize}; @@ -7,9 +7,9 @@ use serde_yaml::{Mapping, Value}; use std::{fs, path::PathBuf}; /// read data from yaml as struct T -pub fn read_yaml(path: PathBuf) -> T { - let yaml_str = fs::read_to_string(path).unwrap(); - serde_yaml::from_str::(&yaml_str).unwrap() +pub fn read_yaml(path: PathBuf) -> T { + let yaml_str = fs::read_to_string(path).unwrap_or("".into()); + serde_yaml::from_str::(&yaml_str).unwrap_or(T::default()) } /// - save the data to the file @@ -36,11 +36,20 @@ pub fn save_yaml( } } -/// Get Clash Core Config +/// Get Clash Core Config `config.yaml` pub fn read_clash() -> Mapping { read_yaml::(app_home_dir().join("config.yaml")) } +/// Save the clash core Config `config.yaml` +pub fn save_clash(config: &Mapping) -> Result<(), String> { + save_yaml( + app_home_dir().join("config.yaml"), + config, + Some("# Default Config For Clash Core\n\n"), + ) +} + /// Get infomation of the clash's `external-controller` and `secret` pub fn read_clash_controller() -> ClashController { let config = read_clash(); @@ -107,3 +116,17 @@ pub fn save_profiles(profiles: &ProfilesConfig) -> Result<(), String> { Some("# Profiles Config for Clash Verge\n\n"), ) } + +/// Get the `verge.yaml` +pub fn read_verge() -> VergeConfig { + read_yaml::(app_home_dir().join("verge.yaml")) +} + +/// Save Verge App Config +pub fn save_verge(verge: &VergeConfig) -> Result<(), String> { + save_yaml( + app_home_dir().join("verge.yaml"), + verge, + Some("# The Config for Clash Verge App\n\n"), + ) +}