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_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::<u8>().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),
))?;

View File

@ -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)
}

View File

@ -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;
}
}
});