Persist Light Mode/Dark Mode settings, added to DB

This commit is contained in:
Nate Levin 2023-01-06 19:35:27 +13:00
parent d9cf30dc7a
commit cb130191b2
4 changed files with 15 additions and 9 deletions

1
src/db/schema9.sql Normal file
View File

@ -0,0 +1 @@
INSERT INTO settings (key, value) values ('light_mode', '1');

View File

@ -13,6 +13,7 @@ pub const DEFAULT_MAX_FPS: u32 = 30;
pub const DEFAULT_FEED_RECOMPUTE_INTERVAL_MS: u32 = 2000; pub const DEFAULT_FEED_RECOMPUTE_INTERVAL_MS: u32 = 2000;
pub const DEFAULT_POW: u8 = 0; pub const DEFAULT_POW: u8 = 0;
pub const DEFAULT_OFFLINE: bool = false; pub const DEFAULT_OFFLINE: bool = false;
pub const DEFAULT_LIGHT_MODE: bool = true; // true = light false = dark
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Settings { pub struct Settings {
@ -28,6 +29,7 @@ pub struct Settings {
pub feed_recompute_interval_ms: u32, pub feed_recompute_interval_ms: u32,
pub pow: u8, pub pow: u8,
pub offline: bool, pub offline: bool,
pub light_mode: bool,
} }
impl Default for Settings { impl Default for Settings {
@ -45,6 +47,7 @@ impl Default for Settings {
feed_recompute_interval_ms: DEFAULT_FEED_RECOMPUTE_INTERVAL_MS, feed_recompute_interval_ms: DEFAULT_FEED_RECOMPUTE_INTERVAL_MS,
pow: DEFAULT_POW, pow: DEFAULT_POW,
offline: DEFAULT_OFFLINE, offline: DEFAULT_OFFLINE,
light_mode: DEFAULT_LIGHT_MODE,
} }
} }
} }
@ -101,6 +104,7 @@ impl Settings {
} }
"pow" => settings.pow = row.1.parse::<u8>().unwrap_or(DEFAULT_POW), "pow" => settings.pow = row.1.parse::<u8>().unwrap_or(DEFAULT_POW),
"offline" => settings.offline = numstr_to_bool(row.1), "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', ?),\ ('max_fps', ?),\
('feed_recompute_interval_ms', ?),\ ('feed_recompute_interval_ms', ?),\
('pow', ?),\ ('pow', ?),\
('light_mode', ?),\
('offline', ?)", ('offline', ?)",
)?; )?;
stmt.execute(( stmt.execute((
@ -143,6 +148,7 @@ impl Settings {
self.max_fps, self.max_fps,
self.feed_recompute_interval_ms, self.feed_recompute_interval_ms,
self.pow, self.pow,
bool_to_numstr(self.light_mode),
bool_to_numstr(self.offline), bool_to_numstr(self.offline),
))?; ))?;

View File

@ -99,7 +99,10 @@ impl Drop for GossipUi {
impl GossipUi { impl GossipUi {
fn new(cctx: &eframe::CreationContext<'_>) -> Self { 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()); cctx.egui_ctx.set_visuals(style::dark_mode_visuals());
} else { } else {
cctx.egui_ctx.set_visuals(style::light_mode_visuals()); cctx.egui_ctx.set_visuals(style::light_mode_visuals());
@ -137,8 +140,6 @@ impl GossipUi {
) )
}; };
let settings = GLOBALS.settings.blocking_read().clone();
GossipUi { GossipUi {
next_frame: Instant::now(), next_frame: Instant::now(),
page: Page::FeedGeneral, page: Page::FeedGeneral,
@ -181,8 +182,6 @@ impl eframe::App for GossipUi {
frame.close(); frame.close();
} }
let darkmode: bool = ctx.style().visuals.dark_mode;
egui::TopBottomPanel::top("menu").show(ctx, |ui| { egui::TopBottomPanel::top("menu").show(ctx, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
if ui if ui
@ -273,7 +272,7 @@ impl eframe::App for GossipUi {
} }
Page::You => you::update(self, ctx, frame, ui), Page::You => you::update(self, ctx, frame, ui),
Page::Relays => relays::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 => { Page::HelpHelp | Page::HelpStats | Page::HelpAbout => {
help::update(self, ctx, frame, ui) help::update(self, ctx, frame, ui)
} }

View File

@ -10,7 +10,6 @@ pub(super) fn update(
_ctx: &Context, _ctx: &Context,
_frame: &mut eframe::Frame, _frame: &mut eframe::Frame,
ui: &mut Ui, ui: &mut Ui,
darkmode: bool,
) { ) {
ui.heading("Settings"); ui.heading("Settings");
@ -111,15 +110,15 @@ pub(super) fn update(
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("Switch to"); ui.label("Switch to");
#[allow(clippy::collapsible_else_if)] #[allow(clippy::collapsible_else_if)]
if darkmode { if ! app.settings.light_mode {
if ui if ui
.add(Button::new("☀ Light")) .add(Button::new("☀ Light"))
.on_hover_text("Switch to light mode") .on_hover_text("Switch to light mode")
.clicked() .clicked()
{ {
ui.ctx().set_visuals(super::style::light_mode_visuals()); ui.ctx().set_visuals(super::style::light_mode_visuals());
app.settings.light_mode = true;
} }
} else { } else {
if ui if ui
@ -128,6 +127,7 @@ pub(super) fn update(
.clicked() .clicked()
{ {
ui.ctx().set_visuals(super::style::dark_mode_visuals()); ui.ctx().set_visuals(super::style::dark_mode_visuals());
app.settings.light_mode = false;
} }
} }
}); });