1use serde::{Deserialize, Serialize};
2use std::sync::{OnceLock, RwLock};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
5#[serde(rename_all = "camelCase")]
6pub enum UpdateUiMode {
7 #[default]
9 Builtin,
10 Custom,
13}
14
15#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
16#[serde(rename_all = "camelCase")]
17pub struct UpdateConfig {
18 #[serde(default = "default_enabled")]
24 pub auto_check_app: bool,
25 #[serde(default)]
26 pub ui_mode: UpdateUiMode,
27 #[serde(default = "default_enabled")]
28 pub force_update_gate: bool,
29}
30
31impl Default for UpdateConfig {
32 fn default() -> Self {
33 Self {
34 auto_check_app: true,
35 ui_mode: UpdateUiMode::Builtin,
36 force_update_gate: true,
37 }
38 }
39}
40
41fn default_enabled() -> bool {
42 true
43}
44
45fn config_store() -> &'static RwLock<UpdateConfig> {
46 static UPDATE_CONFIG: OnceLock<RwLock<UpdateConfig>> = OnceLock::new();
47 UPDATE_CONFIG.get_or_init(|| RwLock::new(UpdateConfig::default()))
48}
49
50pub fn update_config() -> UpdateConfig {
51 config_store()
52 .read()
53 .unwrap_or_else(|err| err.into_inner())
54 .clone()
55}
56
57pub fn configure_update(config: UpdateConfig) {
58 *config_store()
59 .write()
60 .unwrap_or_else(|err| err.into_inner()) = config;
61}