Add helper for setting config values efficiently

This commit is contained in:
Jeremy Soller 2024-01-14 18:55:15 -07:00
parent 8fe14ee46d
commit 6536a66ba2
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
3 changed files with 31 additions and 2 deletions

1
Cargo.lock generated
View file

@ -976,6 +976,7 @@ dependencies = [
"lexical-sort",
"libcosmic",
"log",
"paste",
"rust-embed",
"serde",
"systemicons",

View file

@ -10,6 +10,7 @@ env_logger = "0.10"
lazy_static = "1"
lexical-sort = "0.3.1"
log = "0.4"
paste = "1.0"
serde = { version = "1", features = ["serde_derive"] }
tokio = { version = "1" }
trash = "3.2.0"

View file

@ -416,13 +416,40 @@ impl Application for App {
/// Handle application events here.
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
// Helper for updating config values efficiently
macro_rules! config_set {
($name: ident, $value: expr) => {
match &self.config_handler {
Some(config_handler) => {
match paste::paste! { self.config.[<set_ $name>](config_handler, $value) } {
Ok(_) => {}
Err(err) => {
log::warn!(
"failed to save config {:?}: {}",
stringify!($name),
err
);
}
}
}
None => {
self.config.$name = $value;
log::warn!(
"failed to save config {:?}: no config handler",
stringify!($name)
);
}
}
};
}
match message {
Message::Todo => {
log::warn!("TODO");
}
Message::AppTheme(app_theme) => {
self.config.app_theme = app_theme;
return self.save_config();
config_set!(app_theme, app_theme);
return self.update_config();
}
Message::Config(config) => {
if config != self.config {