From 4a899189e611d077bb1862e60f6086d2aab16a9f Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy Date: Tue, 1 Nov 2022 13:06:00 +0100 Subject: [PATCH] feat: Themable SVGs, applied to window controls --- src/theme/mod.rs | 26 ++++++++++++++++++++++++++ src/widget/header_bar.rs | 3 ++- src/widget/icon.rs | 6 +++++- src/widget/navigation/navbar.rs | 2 ++ 4 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/theme/mod.rs b/src/theme/mod.rs index 5b4e572c..851fcf52 100644 --- a/src/theme/mod.rs +++ b/src/theme/mod.rs @@ -15,6 +15,7 @@ use iced_style::radio; use iced_style::rule; use iced_style::scrollable; use iced_style::slider; +use iced_style::svg; use iced_style::text; use iced_style::text_input; use iced_style::toggler; @@ -665,6 +666,31 @@ impl scrollable::StyleSheet for Theme { } } + +#[derive(Default, Clone, Copy)] +pub enum Svg { + Custom(fn(&Theme) -> svg::Appearance), + #[default] + Default, + Accent, +} + +impl svg::StyleSheet for Theme { + type Style = Svg; + + fn appearance(&self, style: Self::Style) -> svg::Appearance { + let cosmic = self.cosmic(); + + match style { + Svg::Default => Default::default(), + Svg::Custom(appearance) => appearance(self), + Svg::Accent => svg::Appearance { + fill: Some(cosmic.accent.base.into()), + }, + } + } +} + /* * TODO: Text */ diff --git a/src/widget/header_bar.rs b/src/widget/header_bar.rs index 80d089fd..41bd7b4e 100644 --- a/src/widget/header_bar.rs +++ b/src/widget/header_bar.rs @@ -108,8 +108,9 @@ impl Component for HeaderBar { let icon = |name, size, on_press| { super::icon(name, size) + .style(crate::theme::Svg::Accent) .apply(widget::button) - .style(theme::Button::Primary) + .style(theme::Button::Text) .on_press(on_press) }; diff --git a/src/widget/icon.rs b/src/widget/icon.rs index 8faa2436..92399ab6 100644 --- a/src/widget/icon.rs +++ b/src/widget/icon.rs @@ -1,6 +1,10 @@ use iced::{widget::svg, Length}; -pub fn icon(name: &str, size: u16) -> svg::Svg { +pub fn icon(name: &str, size: u16) -> svg::Svg +where + Renderer: iced_native::svg::Renderer, + Renderer::Theme: iced_native::svg::StyleSheet, +{ let handle = match freedesktop_icons::lookup(name) .with_size(size) .with_theme("Pop") diff --git a/src/widget/navigation/navbar.rs b/src/widget/navigation/navbar.rs index 9daf8f11..0fd4cf4b 100644 --- a/src/widget/navigation/navbar.rs +++ b/src/widget/navigation/navbar.rs @@ -98,6 +98,7 @@ where <::Theme as container::StyleSheet>::Style: From, <::Theme as text::StyleSheet>::Style: From, + Renderer::Theme: iced_native::svg::StyleSheet { type State = NavBarState; type Event = NavBarEvent; @@ -231,6 +232,7 @@ where <::Theme as container::StyleSheet>::Style: From, <::Theme as text::StyleSheet>::Style: From, + Renderer::Theme: iced_native::svg::StyleSheet { fn from(nav_bar: NavBar<'a, Message>) -> Self { iced_lazy::component(nav_bar)