feat: theme management (#46)

This commit is contained in:
Ashley Wulber 2023-06-12 11:49:22 -04:00 committed by GitHub
parent ddc176ea89
commit 9e299db491
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 39 deletions

View file

@ -7,7 +7,8 @@ use cosmic_panel_config::CosmicPanelConfig;
use cosmic_settings_page::{self as page, section};
use cosmic::{
cosmic_config::config_subscription,
cosmic_config::{config_subscription, CosmicConfigEntry},
cosmic_theme::util::CssColor,
iced::{
self,
event::wayland::{self, WindowEvent, WindowState},
@ -28,6 +29,7 @@ use cosmic::{
},
Element, ElementExt,
};
use tracing::error;
use crate::{
config::Config,
@ -45,7 +47,7 @@ use crate::{
widget::{page_title, parent_page_button, search_header, sub_page_button},
};
use std::{borrow::Cow, process};
use std::{borrow::Cow, process, sync::Arc};
#[allow(clippy::struct_excessive_bools)]
#[allow(clippy::module_name_repetitions)]
@ -88,6 +90,7 @@ pub enum Message {
WindowState(WindowState),
PanelConfig(CosmicPanelConfig),
DesktopInfo,
ThemeChanged(Theme),
}
impl Application for SettingsApp {
@ -116,8 +119,8 @@ impl Application for SettingsApp {
search_selections: Vec::default(),
show_maximize: true,
show_minimize: true,
theme: Theme::dark(),
window_width: 0,
theme: theme(),
};
// app.insert_page::<wifi::Page>();
@ -199,6 +202,23 @@ impl Application for SettingsApp {
}
},
),
config_subscription::<u64, cosmic::cosmic_theme::Theme<CssColor>>(
0,
cosmic::cosmic_theme::NAME.into(),
cosmic::cosmic_theme::Theme::<CssColor>::version() as u64,
)
.map(|(_, res)| {
let theme = res.map_or_else(
|(errors, theme)| {
for err in errors {
error!("{:?}", err);
}
theme.into_srgba()
},
cosmic::cosmic_theme::Theme::into_srgba,
);
Message::ThemeChanged(cosmic::theme::Theme::custom(Arc::new(theme)))
}),
])
}
@ -313,6 +333,9 @@ impl Application for SettingsApp {
));
}
}
Message::ThemeChanged(theme) => {
self.theme = theme;
}
}
ret
}
@ -602,3 +625,22 @@ impl SettingsApp {
.map(Message::Page)
}
}
fn theme() -> Theme {
let Ok(helper) = cosmic::cosmic_config::Config::new(
cosmic::cosmic_theme::NAME,
cosmic::cosmic_theme::Theme::<CssColor>::version() as u64,
) else {
return cosmic::theme::Theme::dark();
};
let t = cosmic::cosmic_theme::Theme::get_entry(&helper).map_or_else(
|(errors, theme)| {
for err in errors {
tracing::error!("{:?}", err);
}
theme.into_srgba()
},
cosmic::cosmic_theme::Theme::into_srgba,
);
cosmic::theme::Theme::custom(Arc::new(t))
}