From cb130191b20b695ecf430c88b5cdd8079ef20ad5 Mon Sep 17 00:00:00 2001 From: Nate Levin Date: Fri, 6 Jan 2023 19:35:27 +1300 Subject: [PATCH] Persist Light Mode/Dark Mode settings, added to DB --- src/db/schema9.sql | 1 + src/settings.rs | 6 ++++++ src/ui/mod.rs | 11 +++++------ src/ui/settings.rs | 6 +++--- 4 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 src/db/schema9.sql diff --git a/src/db/schema9.sql b/src/db/schema9.sql new file mode 100644 index 00000000..9cf00858 --- /dev/null +++ b/src/db/schema9.sql @@ -0,0 +1 @@ +INSERT INTO settings (key, value) values ('light_mode', '1'); \ No newline at end of file diff --git a/src/settings.rs b/src/settings.rs index 89e9490b..4051f1e6 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -13,6 +13,7 @@ pub const DEFAULT_MAX_FPS: u32 = 30; pub const DEFAULT_FEED_RECOMPUTE_INTERVAL_MS: u32 = 2000; pub const DEFAULT_POW: u8 = 0; pub const DEFAULT_OFFLINE: bool = false; +pub const DEFAULT_LIGHT_MODE: bool = true; // true = light false = dark #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Settings { @@ -28,6 +29,7 @@ pub struct Settings { pub feed_recompute_interval_ms: u32, pub pow: u8, pub offline: bool, + pub light_mode: bool, } impl Default for Settings { @@ -45,6 +47,7 @@ impl Default for Settings { feed_recompute_interval_ms: DEFAULT_FEED_RECOMPUTE_INTERVAL_MS, pow: DEFAULT_POW, offline: DEFAULT_OFFLINE, + light_mode: DEFAULT_LIGHT_MODE, } } } @@ -101,6 +104,7 @@ impl Settings { } "pow" => settings.pow = row.1.parse::().unwrap_or(DEFAULT_POW), "offline" => settings.offline = numstr_to_bool(row.1), + "light_mode" => settings.light_mode = numstr_to_bool(row.1), _ => {} } } @@ -131,6 +135,7 @@ impl Settings { ('max_fps', ?),\ ('feed_recompute_interval_ms', ?),\ ('pow', ?),\ + ('light_mode', ?),\ ('offline', ?)", )?; stmt.execute(( @@ -143,6 +148,7 @@ impl Settings { self.max_fps, self.feed_recompute_interval_ms, self.pow, + bool_to_numstr(self.light_mode), bool_to_numstr(self.offline), ))?; diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 467346f2..c79b8d0d 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -99,7 +99,10 @@ impl Drop for GossipUi { impl GossipUi { fn new(cctx: &eframe::CreationContext<'_>) -> Self { - if cctx.egui_ctx.style().visuals.dark_mode { + // grab settings to determine whether to render dark_mode or light_mode + let settings = GLOBALS.settings.blocking_read().clone(); + + if ! settings.light_mode { cctx.egui_ctx.set_visuals(style::dark_mode_visuals()); } else { cctx.egui_ctx.set_visuals(style::light_mode_visuals()); @@ -137,8 +140,6 @@ impl GossipUi { ) }; - let settings = GLOBALS.settings.blocking_read().clone(); - GossipUi { next_frame: Instant::now(), page: Page::FeedGeneral, @@ -181,8 +182,6 @@ impl eframe::App for GossipUi { frame.close(); } - let darkmode: bool = ctx.style().visuals.dark_mode; - egui::TopBottomPanel::top("menu").show(ctx, |ui| { ui.horizontal(|ui| { if ui @@ -273,7 +272,7 @@ impl eframe::App for GossipUi { } Page::You => you::update(self, ctx, frame, ui), Page::Relays => relays::update(self, ctx, frame, ui), - Page::Settings => settings::update(self, ctx, frame, ui, darkmode), + Page::Settings => settings::update(self, ctx, frame, ui), Page::HelpHelp | Page::HelpStats | Page::HelpAbout => { help::update(self, ctx, frame, ui) } diff --git a/src/ui/settings.rs b/src/ui/settings.rs index 6214af53..ad567ced 100644 --- a/src/ui/settings.rs +++ b/src/ui/settings.rs @@ -10,7 +10,6 @@ pub(super) fn update( _ctx: &Context, _frame: &mut eframe::Frame, ui: &mut Ui, - darkmode: bool, ) { ui.heading("Settings"); @@ -111,15 +110,15 @@ pub(super) fn update( ui.horizontal(|ui| { ui.label("Switch to"); - #[allow(clippy::collapsible_else_if)] - if darkmode { + if ! app.settings.light_mode { if ui .add(Button::new("☀ Light")) .on_hover_text("Switch to light mode") .clicked() { ui.ctx().set_visuals(super::style::light_mode_visuals()); + app.settings.light_mode = true; } } else { if ui @@ -128,6 +127,7 @@ pub(super) fn update( .clicked() { ui.ctx().set_visuals(super::style::dark_mode_visuals()); + app.settings.light_mode = false; } } });