From 664251f3f5c7b76f69a97683af1468094bba887f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 14 May 2022 01:47:55 +0200 Subject: Draft first-class `Theme` support RFC: https://github.com/iced-rs/rfcs/pull/6 --- style/src/button.rs | 46 +++++++++------------------------------------- 1 file changed, 9 insertions(+), 37 deletions(-) (limited to 'style/src/button.rs') diff --git a/style/src/button.rs b/style/src/button.rs index de2de4f4..9f00185c 100644 --- a/style/src/button.rs +++ b/style/src/button.rs @@ -27,10 +27,12 @@ impl std::default::Default for Style { /// A set of rules that dictate the style of a button. pub trait StyleSheet { - fn active(&self) -> Style; + type Variant; - fn hovered(&self) -> Style { - let active = self.active(); + fn active(&self, variant: Self::Variant) -> Style; + + fn hovered(&self, variant: Self::Variant) -> Style { + let active = self.active(variant); Style { shadow_offset: active.shadow_offset + Vector::new(0.0, 1.0), @@ -38,15 +40,15 @@ pub trait StyleSheet { } } - fn pressed(&self) -> Style { + fn pressed(&self, variant: Self::Variant) -> Style { Style { shadow_offset: Vector::default(), - ..self.active() + ..self.active(variant) } } - fn disabled(&self) -> Style { - let active = self.active(); + fn disabled(&self, variant: Self::Variant) -> Style { + let active = self.active(variant); Style { shadow_offset: Vector::default(), @@ -64,33 +66,3 @@ pub trait StyleSheet { } } } - -struct Default; - -impl StyleSheet for Default { - fn active(&self) -> Style { - Style { - shadow_offset: Vector::new(0.0, 0.0), - background: Some(Background::Color([0.87, 0.87, 0.87].into())), - border_radius: 2.0, - border_width: 1.0, - border_color: [0.7, 0.7, 0.7].into(), - text_color: Color::BLACK, - } - } -} - -impl<'a> std::default::Default for Box { - fn default() -> Self { - Box::new(Default) - } -} - -impl<'a, T> From for Box -where - T: StyleSheet + 'a, -{ - fn from(style_sheet: T) -> Self { - Box::new(style_sheet) - } -} -- cgit From cf0230072c01ea9523f4d98a3656f5c975b3f347 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 26 May 2022 23:07:34 +0200 Subject: Rename `Variant` to `Style` and `Style` to `Appearance` --- style/src/button.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'style/src/button.rs') diff --git a/style/src/button.rs b/style/src/button.rs index 9f00185c..c63a6b71 100644 --- a/style/src/button.rs +++ b/style/src/button.rs @@ -3,7 +3,7 @@ use iced_core::{Background, Color, Vector}; /// The appearance of a button. #[derive(Debug, Clone, Copy)] -pub struct Style { +pub struct Appearance { pub shadow_offset: Vector, pub background: Option, pub border_radius: f32, @@ -12,7 +12,7 @@ pub struct Style { pub text_color: Color, } -impl std::default::Default for Style { +impl std::default::Default for Appearance { fn default() -> Self { Self { shadow_offset: Vector::default(), @@ -27,30 +27,30 @@ impl std::default::Default for Style { /// A set of rules that dictate the style of a button. pub trait StyleSheet { - type Variant; + type Style: Default + Copy; - fn active(&self, variant: Self::Variant) -> Style; + fn active(&self, style: Self::Style) -> Appearance; - fn hovered(&self, variant: Self::Variant) -> Style { - let active = self.active(variant); + fn hovered(&self, style: Self::Style) -> Appearance { + let active = self.active(style); - Style { + Appearance { shadow_offset: active.shadow_offset + Vector::new(0.0, 1.0), ..active } } - fn pressed(&self, variant: Self::Variant) -> Style { - Style { + fn pressed(&self, style: Self::Style) -> Appearance { + Appearance { shadow_offset: Vector::default(), - ..self.active(variant) + ..self.active(style) } } - fn disabled(&self, variant: Self::Variant) -> Style { - let active = self.active(variant); + fn disabled(&self, style: Self::Style) -> Appearance { + let active = self.active(style); - Style { + Appearance { shadow_offset: Vector::default(), background: active.background.map(|background| match background { Background::Color(color) => Background::Color(Color { -- cgit