Add helper for setting config values efficiently
This commit is contained in:
parent
8fe14ee46d
commit
6536a66ba2
3 changed files with 31 additions and 2 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
|
@ -976,6 +976,7 @@ dependencies = [
|
||||||
"lexical-sort",
|
"lexical-sort",
|
||||||
"libcosmic",
|
"libcosmic",
|
||||||
"log",
|
"log",
|
||||||
|
"paste",
|
||||||
"rust-embed",
|
"rust-embed",
|
||||||
"serde",
|
"serde",
|
||||||
"systemicons",
|
"systemicons",
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ env_logger = "0.10"
|
||||||
lazy_static = "1"
|
lazy_static = "1"
|
||||||
lexical-sort = "0.3.1"
|
lexical-sort = "0.3.1"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
paste = "1.0"
|
||||||
serde = { version = "1", features = ["serde_derive"] }
|
serde = { version = "1", features = ["serde_derive"] }
|
||||||
tokio = { version = "1" }
|
tokio = { version = "1" }
|
||||||
trash = "3.2.0"
|
trash = "3.2.0"
|
||||||
|
|
|
||||||
31
src/main.rs
31
src/main.rs
|
|
@ -416,13 +416,40 @@ impl Application for App {
|
||||||
|
|
||||||
/// Handle application events here.
|
/// Handle application events here.
|
||||||
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
|
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 {
|
match message {
|
||||||
Message::Todo => {
|
Message::Todo => {
|
||||||
log::warn!("TODO");
|
log::warn!("TODO");
|
||||||
}
|
}
|
||||||
Message::AppTheme(app_theme) => {
|
Message::AppTheme(app_theme) => {
|
||||||
self.config.app_theme = app_theme;
|
config_set!(app_theme, app_theme);
|
||||||
return self.save_config();
|
return self.update_config();
|
||||||
}
|
}
|
||||||
Message::Config(config) => {
|
Message::Config(config) => {
|
||||||
if config != self.config {
|
if config != self.config {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue