Skip to content

Commit

Permalink
🐛 Change settings to be loaded only once from disk
Browse files Browse the repository at this point in the history
Add the `get_or_init` function to `[Settings]` that will cache the
result in memory once it reads the settings file from disk.
  • Loading branch information
a-kenji committed Jul 18, 2024
1 parent c573899 commit de4b8ce
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 9 deletions.
2 changes: 1 addition & 1 deletion client/src/component/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl iced::widget::container::StyleSheet for Style {
type Style = iced::Theme;

fn appearance(&self, _style: &Self::Style) -> iced::widget::container::Appearance {
let color_settings = crate::settings::Settings::new();
let color_settings = crate::settings::Settings::get_or_init();

iced::widget::container::Appearance {
background: None,
Expand Down
8 changes: 4 additions & 4 deletions client/src/component/query_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ impl iced::widget::text_input::StyleSheet for Style {
}

fn placeholder_color(&self, _style: &Self::Style) -> iced::Color {
let color_settings = crate::settings::Settings::new();
let color_settings = crate::settings::Settings::get_or_init();
crate::settings::hexcolor(&color_settings.color.surface)
}

fn value_color(&self, _style: &Self::Style) -> iced::Color {
let color_settings = crate::settings::Settings::new();
let color_settings = crate::settings::Settings::get_or_init();
crate::settings::hexcolor(&color_settings.color.text)
}

fn disabled_color(&self, _style: &Self::Style) -> iced::Color {
let color_settings = crate::settings::Settings::new();
let color_settings = crate::settings::Settings::get_or_init();
crate::settings::hexcolor(&color_settings.color.surface)
}

fn selection_color(&self, _style: &Self::Style) -> iced::Color {
let color_settings = crate::settings::Settings::new();
let color_settings = crate::settings::Settings::get_or_init();
crate::settings::hexcolor(&color_settings.color.surface)
}
}
8 changes: 4 additions & 4 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ impl iced::application::StyleSheet for SandboxStyle {
type Style = iced::Theme;

fn appearance(&self, _style: &Self::Style) -> iced::application::Appearance {
let color_settings = crate::settings::Settings::new();
let color_settings = crate::settings::Settings::get_or_init();

iced::application::Appearance {
background_color: iced::Color::TRANSPARENT,
Expand All @@ -543,7 +543,7 @@ impl iced::widget::container::StyleSheet for ApplicationWrapperStyle {
type Style = iced::Theme;

fn appearance(&self, _style: &Self::Style) -> iced::widget::container::Appearance {
let color_settings = crate::settings::Settings::new();
let color_settings = crate::settings::Settings::get_or_init();
iced::widget::container::Appearance {
background: Some(iced::Background::Color(settings::hexcolor(
&color_settings.color.background,
Expand All @@ -561,7 +561,7 @@ impl iced::widget::scrollable::StyleSheet for ScrollableStyle {
type Style = iced::Theme;

fn active(&self, _style: &Self::Style) -> iced::widget::scrollable::Scrollbar {
let color_settings = crate::settings::Settings::new();
let color_settings = crate::settings::Settings::get_or_init();
iced::widget::scrollable::Scrollbar {
background: None,
border_radius: iced::BorderRadius::from(0.),
Expand All @@ -581,7 +581,7 @@ impl iced::widget::scrollable::StyleSheet for ScrollableStyle {
_style: &Self::Style,
_is_mouse_over_scrollbar: bool,
) -> iced::widget::scrollable::Scrollbar {
let color_settings = crate::settings::Settings::new();
let color_settings = crate::settings::Settings::get_or_init();
iced::widget::scrollable::Scrollbar {
background: None,
border_radius: iced::BorderRadius::from(0.),
Expand Down
6 changes: 6 additions & 0 deletions client/src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use hex_color::HexColor;
use serde::Deserialize;
use std::sync::OnceLock;

pub fn hexcolor(color: &str) -> iced::Color {
let hex_col = HexColor::parse(color).unwrap_or_else(|_| {
Expand Down Expand Up @@ -327,6 +328,11 @@ impl Settings {
}
config_result.unwrap()
}

pub fn get_or_init() -> &'static Self {
static SETTINGS: OnceLock<Settings> = OnceLock::new();
SETTINGS.get_or_init(|| Self::new())
}
}

impl std::convert::TryFrom<crate::cli::CliArgs> for Settings {
Expand Down

0 comments on commit de4b8ce

Please sign in to comment.