From e1062a02d17f5748e4809b76ddcc132f1c912886 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 1 Jan 2020 14:16:10 +0100 Subject: Move styling to a brand new `iced_style` crate --- style/src/button.rs | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 style/src/button.rs (limited to 'style/src/button.rs') diff --git a/style/src/button.rs b/style/src/button.rs new file mode 100644 index 00000000..42286897 --- /dev/null +++ b/style/src/button.rs @@ -0,0 +1,79 @@ +//! Allow your users to perform actions by pressing a button. +use iced_core::{Background, Color}; + +/// The appearance of a button. +#[derive(Debug)] +pub struct Style { + pub shadow_offset: f32, + pub background: Option, + pub border_radius: u16, + pub text_color: Color, +} + +/// A set of rules that dictate the style of a button. +pub trait StyleSheet { + fn active(&self) -> Style; + + fn hovered(&self) -> Style { + let active = self.active(); + + Style { + shadow_offset: active.shadow_offset + 1.0, + ..active + } + } + + fn pressed(&self) -> Style { + Style { + shadow_offset: 0.0, + ..self.active() + } + } + + fn disabled(&self) -> Style { + let active = self.active(); + + Style { + shadow_offset: 0.0, + background: active.background.map(|background| match background { + Background::Color(color) => Background::Color(Color { + a: color.a * 0.5, + ..color + }), + }), + text_color: Color { + a: active.text_color.a * 0.5, + ..active.text_color + }, + ..active + } + } +} + +struct Default; + +impl StyleSheet for Default { + fn active(&self) -> Style { + Style { + shadow_offset: 1.0, + background: Some(Background::Color([0.5, 0.5, 0.5].into())), + border_radius: 5, + text_color: Color::BLACK, + } + } +} + +impl std::default::Default for Box { + fn default() -> Self { + Box::new(Default) + } +} + +impl From for Box +where + T: 'static + StyleSheet, +{ + fn from(style: T) -> Self { + Box::new(style) + } +} -- cgit From 1a0effa961344677daf17b4192243423a154f1bf Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 5 Jan 2020 19:29:12 +0100 Subject: Add border and shadow styling to `Button` --- style/src/button.rs | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) (limited to 'style/src/button.rs') diff --git a/style/src/button.rs b/style/src/button.rs index 42286897..4a9dac45 100644 --- a/style/src/button.rs +++ b/style/src/button.rs @@ -1,15 +1,30 @@ //! Allow your users to perform actions by pressing a button. -use iced_core::{Background, Color}; +use iced_core::{Background, Color, Vector}; /// The appearance of a button. #[derive(Debug)] pub struct Style { - pub shadow_offset: f32, + pub shadow_offset: Vector, pub background: Option, pub border_radius: u16, + pub border_width: u16, + pub border_color: Color, pub text_color: Color, } +impl std::default::Default for Style { + fn default() -> Self { + Style { + shadow_offset: Vector::default(), + background: None, + border_radius: 0, + border_width: 0, + border_color: Color::TRANSPARENT, + text_color: Color::BLACK, + } + } +} + /// A set of rules that dictate the style of a button. pub trait StyleSheet { fn active(&self) -> Style; @@ -18,14 +33,14 @@ pub trait StyleSheet { let active = self.active(); Style { - shadow_offset: active.shadow_offset + 1.0, + shadow_offset: active.shadow_offset + Vector::new(0.0, 1.0), ..active } } fn pressed(&self) -> Style { Style { - shadow_offset: 0.0, + shadow_offset: Vector::default(), ..self.active() } } @@ -34,7 +49,7 @@ pub trait StyleSheet { let active = self.active(); Style { - shadow_offset: 0.0, + shadow_offset: Vector::default(), background: active.background.map(|background| match background { Background::Color(color) => Background::Color(Color { a: color.a * 0.5, @@ -55,9 +70,11 @@ struct Default; impl StyleSheet for Default { fn active(&self) -> Style { Style { - shadow_offset: 1.0, + shadow_offset: Vector::new(0.0, 1.0), background: Some(Background::Color([0.5, 0.5, 0.5].into())), border_radius: 5, + border_width: 0, + border_color: Color::TRANSPARENT, text_color: Color::BLACK, } } -- cgit From 07ef59af78107db103b7a9640a660ecae95b1156 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 5 Jan 2020 19:34:38 +0100 Subject: Implement `Default` for `container::Style` --- style/src/button.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'style/src/button.rs') diff --git a/style/src/button.rs b/style/src/button.rs index 4a9dac45..03c9ab32 100644 --- a/style/src/button.rs +++ b/style/src/button.rs @@ -14,7 +14,7 @@ pub struct Style { impl std::default::Default for Style { fn default() -> Self { - Style { + Self { shadow_offset: Vector::default(), background: None, border_radius: 0, -- cgit From 2bbd395d5dcdf9c92ffb354b8b05444478e4b344 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 6 Jan 2020 18:44:45 +0100 Subject: Draft `styling` example --- style/src/button.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'style/src/button.rs') diff --git a/style/src/button.rs b/style/src/button.rs index 03c9ab32..93c27860 100644 --- a/style/src/button.rs +++ b/style/src/button.rs @@ -75,7 +75,7 @@ impl StyleSheet for Default { border_radius: 5, border_width: 0, border_color: Color::TRANSPARENT, - text_color: Color::BLACK, + text_color: Color::WHITE, } } } -- cgit