aboutsummaryrefslogtreecommitdiff
path: root/src/app/config_io.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/app/config_io.rs')
-rw-r--r--src/app/config_io.rs45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/app/config_io.rs b/src/app/config_io.rs
new file mode 100644
index 00000000..21d4365b
--- /dev/null
+++ b/src/app/config_io.rs
@@ -0,0 +1,45 @@
+use super::App;
+
+impl App {
+ pub(super) fn update_config_file<F>(&mut self, error_context: &str, update: F)
+ where
+ F: FnOnce(&str) -> String,
+ {
+ let path = crate::config::config_path();
+ if let Some(parent) = path.parent() {
+ if let Err(err) = std::fs::create_dir_all(parent) {
+ self.state.config_diagnostic =
+ Some(format!("failed to save {error_context}: {err}"));
+ self.config_diagnostic_deadline =
+ Some(std::time::Instant::now() + std::time::Duration::from_secs(5));
+ return;
+ }
+ }
+
+ let content = std::fs::read_to_string(&path).unwrap_or_default();
+ let new_content = update(&content);
+ if let Err(err) = std::fs::write(&path, new_content) {
+ self.state.config_diagnostic = Some(format!("failed to save {error_context}: {err}"));
+ self.config_diagnostic_deadline =
+ Some(std::time::Instant::now() + std::time::Duration::from_secs(5));
+ }
+ }
+
+ pub(super) fn save_theme(&mut self, name: &str) {
+ self.update_config_file("theme", |content| {
+ crate::config::upsert_section_value(content, "theme", "name", &format!("\"{name}\""))
+ });
+ }
+
+ pub(super) fn save_sound(&mut self, enabled: bool) {
+ self.update_config_file("sound setting", |content| {
+ crate::config::upsert_section_bool(content, "ui.sound", "enabled", enabled)
+ });
+ }
+
+ pub(super) fn save_toast(&mut self, enabled: bool) {
+ self.update_config_file("toast setting", |content| {
+ crate::config::upsert_section_bool(content, "ui.toast", "enabled", enabled)
+ });
+ }
+}