From cb130191b20b695ecf430c88b5cdd8079ef20ad5 Mon Sep 17 00:00:00 2001 From: Nate Levin Date: Fri, 6 Jan 2023 19:35:27 +1300 Subject: [PATCH 1/5] 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; } } }); From 010b9b7e65dfd3481c7ad5ef2d7dfa56dcde2b49 Mon Sep 17 00:00:00 2001 From: Nate Levin Date: Fri, 6 Jan 2023 20:50:30 +1300 Subject: [PATCH 2/5] Change text color to light_gray --- src/db/mod.rs | 1 + src/ui/style.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/db/mod.rs b/src/db/mod.rs index 99de0b2e..b7dd326a 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -110,6 +110,7 @@ fn upgrade(db: &Connection, mut version: u16) -> Result<(), Error> { apply_sql!(db, version, 6, "schema6.sql"); apply_sql!(db, version, 7, "schema7.sql"); apply_sql!(db, version, 8, "schema8.sql"); + apply_sql!(db, version, 9, "schema9.sql"); tracing::info!("Database is at version {}", version); Ok(()) } diff --git a/src/ui/style.rs b/src/ui/style.rs index ea51b8bf..c90ec958 100644 --- a/src/ui/style.rs +++ b/src/ui/style.rs @@ -18,7 +18,7 @@ pub(super) fn dark_mode_visuals() -> Visuals { // Foreground colors window_stroke: Stroke::new(1.0, Color32::from_rgb(0x37, 0x96, 0x83)), // DONE - override_text_color: None, + override_text_color: Some(Color32::LIGHT_GRAY), warn_fg_color: Color32::from_rgb(255, 143, 0), // orange error_fg_color: Color32::from_rgb(255, 0, 0), // red hyperlink_color: Color32::from_rgb(0x73, 0x95, 0xae), // DONE From a5fcf6e5b4a9f616f0e0afef0376215d8df6e58e Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Mon, 9 Jan 2023 19:10:26 +1300 Subject: [PATCH 3/5] Schema update not necessary for new settings, save will create them. --- src/db/mod.rs | 1 - src/db/schema9.sql | 1 - 2 files changed, 2 deletions(-) delete mode 100644 src/db/schema9.sql diff --git a/src/db/mod.rs b/src/db/mod.rs index b7dd326a..99de0b2e 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -110,7 +110,6 @@ fn upgrade(db: &Connection, mut version: u16) -> Result<(), Error> { apply_sql!(db, version, 6, "schema6.sql"); apply_sql!(db, version, 7, "schema7.sql"); apply_sql!(db, version, 8, "schema8.sql"); - apply_sql!(db, version, 9, "schema9.sql"); tracing::info!("Database is at version {}", version); Ok(()) } diff --git a/src/db/schema9.sql b/src/db/schema9.sql deleted file mode 100644 index 9cf00858..00000000 --- a/src/db/schema9.sql +++ /dev/null @@ -1 +0,0 @@ -INSERT INTO settings (key, value) values ('light_mode', '1'); \ No newline at end of file From 1af959c9705f20cb99532b156b0cea4f2e9effc0 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Mon, 9 Jan 2023 19:13:06 +1300 Subject: [PATCH 4/5] invert sense of an if statement --- src/ui/settings.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ui/settings.rs b/src/ui/settings.rs index ad567ced..2d405c6d 100644 --- a/src/ui/settings.rs +++ b/src/ui/settings.rs @@ -111,16 +111,7 @@ pub(super) fn update( ui.horizontal(|ui| { ui.label("Switch to"); #[allow(clippy::collapsible_else_if)] - 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 app.settings.light_mode { if ui .add(Button::new("🌙 Dark")) .on_hover_text("Switch to dark mode") @@ -129,6 +120,15 @@ pub(super) fn update( ui.ctx().set_visuals(super::style::dark_mode_visuals()); app.settings.light_mode = false; } + } else { + 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; + } } }); From d18639e1e6eea1ec55f654e72e83154b428aa073 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Mon, 9 Jan 2023 19:16:24 +1300 Subject: [PATCH 5/5] Tweak some dark mode colors --- src/ui/style.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ui/style.rs b/src/ui/style.rs index c90ec958..e7ee0781 100644 --- a/src/ui/style.rs +++ b/src/ui/style.rs @@ -17,15 +17,15 @@ pub(super) fn dark_mode_visuals() -> Visuals { code_bg_color: Color32::from_gray(64), // Foreground colors - window_stroke: Stroke::new(1.0, Color32::from_rgb(0x37, 0x96, 0x83)), // DONE - override_text_color: Some(Color32::LIGHT_GRAY), + window_stroke: Stroke::new(1.0, Color32::from_rgb(0x37, 0x96, 0x83)), + override_text_color: Some(Color32::from_gray(190)), warn_fg_color: Color32::from_rgb(255, 143, 0), // orange error_fg_color: Color32::from_rgb(255, 0, 0), // red - hyperlink_color: Color32::from_rgb(0x73, 0x95, 0xae), // DONE + hyperlink_color: Color32::from_rgb(0x73, 0x95, 0xae), selection: Selection { - bg_fill: Color32::from_rgb(0xb1, 0xa2, 0x96), // DONE - stroke: Stroke::new(1.0, Color32::from_rgb(0x37, 0x96, 0x83)), // DONE + bg_fill: Color32::from_rgb(0x57, 0x4a, 0x40), + stroke: Stroke::new(1.0, Color32::from_rgb(0x37, 0x96, 0x83)), }, window_shadow: Shadow::big_dark(),