improv: add transparency and blur support

- Adds both transparency and blur support.
- Provides convenience `set_transparent` and `enable_blur` methods.
This commit is contained in:
Eduardo Flores 2024-10-30 19:33:17 +01:00
parent 4b562867ec
commit 7e3430d10a
2 changed files with 26 additions and 1 deletions

View file

@ -92,3 +92,14 @@ impl crate::app::Core {
pub fn set_theme<M: Send + 'static>(theme: crate::Theme) -> iced::Task<Message<M>> { pub fn set_theme<M: Send + 'static>(theme: crate::Theme) -> iced::Task<Message<M>> {
message::cosmic(super::cosmic::Message::AppThemeChange(theme)) message::cosmic(super::cosmic::Message::AppThemeChange(theme))
} }
pub fn set_transparent<M: Send + 'static>(
theme: cosmic_theme::Theme,
alpha: f32,
) -> iced::Task<Message<M>> {
message::cosmic(super::cosmic::Message::EnableTransparency(theme, alpha))
}
pub fn enable_blur<M: Send + 'static>() -> iced::Task<Message<M>> {
message::cosmic(super::cosmic::Message::EnableBlur)
}

View file

@ -25,6 +25,10 @@ use palette::color_difference::EuclideanDistance;
pub enum Message { pub enum Message {
/// Application requests theme change. /// Application requests theme change.
AppThemeChange(Theme), AppThemeChange(Theme),
/// Enables blur support.
EnableBlur,
/// Enables transparency.
EnableTransparency(cosmic_theme::Theme, f32),
/// Requests to close the window. /// Requests to close the window.
Close, Close,
/// Closes or shows the context drawer. /// Closes or shows the context drawer.
@ -429,7 +433,17 @@ impl<T: Application> Cosmic<T> {
THEME.lock().unwrap().set_theme(theme.theme_type); THEME.lock().unwrap().set_theme(theme.theme_type);
} }
Message::EnableBlur => {
if let Some(id) = self.app.core().main_window_id() {
return Task::batch(vec![iced::runtime::window::enable_blur(id)]);
}
}
Message::EnableTransparency(mut theme, alpha) => {
theme.background.base.alpha *= alpha;
return self.cosmic_update(Message::AppThemeChange(crate::Theme::custom(
std::sync::Arc::new(theme),
)));
}
Message::SystemThemeChange(keys, theme) => { Message::SystemThemeChange(keys, theme) => {
let cur_is_dark = THEME.lock().unwrap().theme_type.is_dark(); let cur_is_dark = THEME.lock().unwrap().theme_type.is_dark();
// Ignore updates if the current theme mode does not match. // Ignore updates if the current theme mode does not match.