verge.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. use crate::utils::{config, dirs};
  2. use anyhow::Result;
  3. use serde::{Deserialize, Serialize};
  4. /// ### `verge.yaml` schema
  5. #[derive(Default, Debug, Clone, Deserialize, Serialize)]
  6. pub struct Verge {
  7. /// app listening port
  8. /// for app singleton
  9. pub app_singleton_port: Option<u16>,
  10. // i18n
  11. pub language: Option<String>,
  12. /// `light` or `dark` or `system`
  13. pub theme_mode: Option<String>,
  14. /// enable blur mode
  15. /// maybe be able to set the alpha
  16. pub theme_blur: Option<bool>,
  17. /// enable traffic graph default is true
  18. pub traffic_graph: Option<bool>,
  19. /// clash tun mode
  20. pub enable_tun_mode: Option<bool>,
  21. /// windows service mode
  22. #[serde(skip_serializing_if = "Option::is_none")]
  23. pub enable_service_mode: Option<bool>,
  24. /// can the app auto startup
  25. pub enable_auto_launch: Option<bool>,
  26. /// not show the window on launch
  27. pub enable_silent_start: Option<bool>,
  28. /// set system proxy
  29. pub enable_system_proxy: Option<bool>,
  30. /// enable proxy guard
  31. pub enable_proxy_guard: Option<bool>,
  32. /// set system proxy bypass
  33. pub system_proxy_bypass: Option<String>,
  34. /// proxy guard duration
  35. pub proxy_guard_duration: Option<u64>,
  36. /// theme setting
  37. pub theme_setting: Option<VergeTheme>,
  38. /// web ui list
  39. pub web_ui_list: Option<Vec<String>>,
  40. /// clash core path
  41. #[serde(skip_serializing_if = "Option::is_none")]
  42. pub clash_core: Option<String>,
  43. /// hotkey map
  44. /// format: {func},{key}
  45. pub hotkeys: Option<Vec<String>>,
  46. /// 切换代理时自动关闭连接
  47. pub auto_close_connection: Option<bool>,
  48. /// 默认的延迟测试连接
  49. pub default_latency_test: Option<String>,
  50. }
  51. #[derive(Default, Debug, Clone, Deserialize, Serialize)]
  52. pub struct VergeTheme {
  53. pub primary_color: Option<String>,
  54. pub secondary_color: Option<String>,
  55. pub primary_text: Option<String>,
  56. pub secondary_text: Option<String>,
  57. pub info_color: Option<String>,
  58. pub error_color: Option<String>,
  59. pub warning_color: Option<String>,
  60. pub success_color: Option<String>,
  61. pub font_family: Option<String>,
  62. pub css_injection: Option<String>,
  63. }
  64. impl Verge {
  65. pub fn new() -> Self {
  66. config::read_yaml::<Verge>(dirs::verge_path())
  67. }
  68. /// Save Verge App Config
  69. pub fn save_file(&self) -> Result<()> {
  70. config::save_yaml(
  71. dirs::verge_path(),
  72. self,
  73. Some("# The Config for Clash Verge App\n\n"),
  74. )
  75. }
  76. /// patch verge config
  77. /// only save to file
  78. pub fn patch_config(&mut self, patch: Verge) -> Result<()> {
  79. macro_rules! patch {
  80. ($key: tt) => {
  81. if patch.$key.is_some() {
  82. self.$key = patch.$key;
  83. }
  84. };
  85. }
  86. patch!(language);
  87. patch!(theme_mode);
  88. patch!(theme_blur);
  89. patch!(traffic_graph);
  90. patch!(enable_tun_mode);
  91. patch!(enable_service_mode);
  92. patch!(enable_auto_launch);
  93. patch!(enable_silent_start);
  94. patch!(enable_system_proxy);
  95. patch!(enable_proxy_guard);
  96. patch!(system_proxy_bypass);
  97. patch!(proxy_guard_duration);
  98. patch!(theme_setting);
  99. patch!(web_ui_list);
  100. patch!(clash_core);
  101. patch!(hotkeys);
  102. patch!(auto_close_connection);
  103. patch!(default_latency_test);
  104. self.save_file()
  105. }
  106. }