From 476b394add6ad72710739b45445fa1c654dc1c9e Mon Sep 17 00:00:00 2001 From: Filip Date: Sat, 22 Apr 2023 21:29:36 +0000 Subject: [PATCH] Persistent dark / light mode --- CHANGELOG.md | 5 ++-- rog-control-center/src/config.rs | 30 +++++++++++++++++++++ rog-control-center/src/widgets/top_bar.rs | 32 ++++++++++++++++++++++- 3 files changed, 64 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 063eafbe..e5ff78de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Adjustments to Anime system events thread - Add "sleep" animetion config options to anime config +- rog-control-center dark/light mode persisency ## [v4.6.0] ### Added @@ -51,7 +52,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [v4.5.7] ### Changed - ROGCC: Don't notify user if changing to same mux mode -- +- ## [v4.5.7] ### Changed @@ -616,4 +617,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix small deadlock with awaits -## [1.0.0] \ No newline at end of file +## [1.0.0] \ No newline at end of file diff --git a/rog-control-center/src/config.rs b/rog-control-center/src/config.rs index fd9d4632..17c00956 100644 --- a/rog-control-center/src/config.rs +++ b/rog-control-center/src/config.rs @@ -17,6 +17,7 @@ pub struct Config { pub ac_command: String, pub bat_command: String, pub enable_notifications: bool, + pub dark_mode: bool, // This field must be last pub enabled_notifications: EnabledNotifications, } @@ -27,6 +28,7 @@ impl Default for Config { run_in_background: true, startup_in_background: false, enable_notifications: true, + dark_mode: true, enabled_notifications: EnabledNotifications::default(), ac_command: String::new(), bat_command: String::new(), @@ -72,6 +74,9 @@ impl Config { } else if let Ok(data) = toml::from_str::(&buf) { info!("Loaded config file {path:?}"); return Ok(data); + } else if let Ok(data) = toml::from_str::(&buf) { + info!("Loaded old v4.6.0 config file {path:?}"); + return Ok(data.into()); } else if let Ok(data) = toml::from_str::(&buf) { info!("Loaded old v4.5.5 config file {path:?}"); return Ok(data.into()); @@ -124,8 +129,33 @@ impl From for Config { startup_in_background: c.startup_in_background, enable_notifications: c.enable_notifications, enabled_notifications: c.enabled_notifications, + dark_mode: true, ac_command: String::new(), bat_command: String::new(), } } } + +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct Config460 { + pub run_in_background: bool, + pub startup_in_background: bool, + pub ac_command: String, + pub bat_command: String, + pub enable_notifications: bool, + pub enabled_notifications: EnabledNotifications, +} + +impl From for Config { + fn from(c: Config460) -> Self { + Self { + run_in_background: c.run_in_background, + startup_in_background: c.startup_in_background, + ac_command: c.ac_command, + bat_command: c.bat_command, + dark_mode: true, + enable_notifications: c.enable_notifications, + enabled_notifications: c.enabled_notifications, + } + } +} diff --git a/rog-control-center/src/widgets/top_bar.rs b/rog-control-center/src/widgets/top_bar.rs index fd727034..6c6cb850 100644 --- a/rog-control-center/src/widgets/top_bar.rs +++ b/rog-control-center/src/widgets/top_bar.rs @@ -8,7 +8,7 @@ impl RogApp { // The top panel is often a good place for a menu bar: egui::menu::bar(ui, |ui| { ui.horizontal(|ui| { - egui::global_dark_light_mode_buttons(ui); + self.dark_light_mode_buttons(ui); egui::warn_if_debug_build(ui); }); @@ -45,4 +45,34 @@ impl RogApp { }); }); } + + fn dark_light_mode_buttons(&mut self, ui: &mut egui::Ui) { + let load_from_cfg = self.config.dark_mode != ui.ctx().style().visuals.dark_mode; + + if ui + .add(egui::SelectableLabel::new( + !self.config.dark_mode, + "☀ Light", + )) + .clicked() + || (load_from_cfg && !self.config.dark_mode) + { + ui.ctx().set_visuals(egui::Visuals::light()); + } + if ui + .add(egui::SelectableLabel::new(self.config.dark_mode, "🌙 Dark")) + .clicked() + || (load_from_cfg && self.config.dark_mode) + { + ui.ctx().set_visuals(egui::Visuals::dark()); + } + + let applied_dark_mode = ui.ctx().style().visuals.dark_mode; + + if self.config.dark_mode != applied_dark_mode { + self.config.dark_mode = applied_dark_mode; + let tmp = self.config.enabled_notifications.clone(); + self.config.save(&tmp).ok(); + } + } }