From 97555e67af8b4bcc77df69c5e72156e14948150e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 7 Jun 2022 04:11:24 +0200 Subject: Implement theme styling for `Container` --- style/src/container.rs | 39 ++++++--------------------------------- style/src/theme.rs | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 34 deletions(-) (limited to 'style/src') diff --git a/style/src/container.rs b/style/src/container.rs index 2f411611..184310fa 100644 --- a/style/src/container.rs +++ b/style/src/container.rs @@ -3,7 +3,7 @@ use iced_core::{Background, Color}; /// The appearance of a container. #[derive(Debug, Clone, Copy)] -pub struct Style { +pub struct Appearance { pub text_color: Option, pub background: Option, pub border_radius: f32, @@ -11,7 +11,7 @@ pub struct Style { pub border_color: Color, } -impl std::default::Default for Style { +impl std::default::Default for Appearance { fn default() -> Self { Self { text_color: None, @@ -23,37 +23,10 @@ impl std::default::Default for Style { } } -/// A set of rules that dictate the style of a container. +/// A set of rules that dictate the [`Appearance`] of a container. pub trait StyleSheet { - /// Produces the style of a container. - fn style(&self) -> Style; -} - -struct Default; - -impl StyleSheet for Default { - fn style(&self) -> Style { - Style { - text_color: None, - background: None, - border_radius: 0.0, - border_width: 0.0, - border_color: Color::TRANSPARENT, - } - } -} - -impl<'a> std::default::Default for Box { - fn default() -> Self { - Box::new(Default) - } -} + type Style: Default + Copy; -impl<'a, T> From for Box -where - T: StyleSheet + 'a, -{ - fn from(style_sheet: T) -> Self { - Box::new(style_sheet) - } + /// Produces the [`Appearance`] of a container. + fn appearance(&self, style: Self::Style) -> Appearance; } diff --git a/style/src/theme.rs b/style/src/theme.rs index 3eb4ea70..0e9a5964 100644 --- a/style/src/theme.rs +++ b/style/src/theme.rs @@ -5,6 +5,7 @@ pub use self::palette::Palette; use crate::application; use crate::button; use crate::checkbox; +use crate::container; use crate::pane_grid; use crate::progress_bar; use crate::radio; @@ -127,7 +128,6 @@ impl button::StyleSheet for Theme { /* * Checkbox */ - #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Checkbox { Primary, @@ -227,6 +227,50 @@ fn checkbox_appearance( } } +/* + * Container + */ +#[derive(Clone, Copy)] +pub enum Container { + Transparent, + Box, + Custom(fn(&Theme) -> container::Appearance), +} + +impl Default for Container { + fn default() -> Self { + Self::Transparent + } +} + +impl From container::Appearance> for Container { + fn from(f: fn(&Theme) -> container::Appearance) -> Self { + Self::Custom(f) + } +} + +impl container::StyleSheet for Theme { + type Style = Container; + + fn appearance(&self, style: Self::Style) -> container::Appearance { + match style { + Container::Transparent => Default::default(), + Container::Box => { + let palette = self.extended_palette(); + + container::Appearance { + text_color: None, + background: palette.background.weak.color.into(), + border_radius: 2.0, + border_width: 0.0, + border_color: Color::TRANSPARENT, + } + } + Container::Custom(f) => f(self), + } + } +} + /* * Slider */ -- cgit