Allow Checkbox style to change based on its state

This commit is contained in:
Héctor Ramón Jiménez 2020-01-08 03:30:15 +01:00
parent 08faaaf623
commit cae4463e83
3 changed files with 20 additions and 13 deletions

View File

@ -493,20 +493,27 @@ mod style {
pub struct Checkbox; pub struct Checkbox;
impl checkbox::StyleSheet for Checkbox { impl checkbox::StyleSheet for Checkbox {
fn active(&self) -> checkbox::Style { fn active(&self, is_checked: bool) -> checkbox::Style {
checkbox::Style { checkbox::Style {
background: Background::Color(SURFACE), background: Background::Color(if is_checked {
checkmark_color: ACTIVE, ACTIVE
} else {
SURFACE
}),
checkmark_color: Color::WHITE,
border_radius: 2, border_radius: 2,
border_width: 1, border_width: 1,
border_color: ACTIVE, border_color: ACTIVE,
} }
} }
fn hovered(&self) -> checkbox::Style { fn hovered(&self, is_checked: bool) -> checkbox::Style {
checkbox::Style { checkbox::Style {
background: Background::Color(Color { a: 0.5, ..SURFACE }), background: Background::Color(Color {
..self.active() a: 0.8,
..if is_checked { ACTIVE } else { SURFACE }
}),
..self.active(is_checked)
} }
} }
} }

View File

@ -13,15 +13,15 @@ pub struct Style {
/// A set of rules that dictate the style of a checkbox. /// A set of rules that dictate the style of a checkbox.
pub trait StyleSheet { pub trait StyleSheet {
fn active(&self) -> Style; fn active(&self, is_checked: bool) -> Style;
fn hovered(&self) -> Style; fn hovered(&self, is_checked: bool) -> Style;
} }
struct Default; struct Default;
impl StyleSheet for Default { impl StyleSheet for Default {
fn active(&self) -> Style { fn active(&self, _is_checked: bool) -> Style {
Style { Style {
background: Background::Color(Color::from_rgb(0.95, 0.95, 0.95)), background: Background::Color(Color::from_rgb(0.95, 0.95, 0.95)),
checkmark_color: Color::from_rgb(0.3, 0.3, 0.3), checkmark_color: Color::from_rgb(0.3, 0.3, 0.3),
@ -31,10 +31,10 @@ impl StyleSheet for Default {
} }
} }
fn hovered(&self) -> Style { fn hovered(&self, is_checked: bool) -> Style {
Style { Style {
background: Background::Color(Color::from_rgb(0.90, 0.90, 0.90)), background: Background::Color(Color::from_rgb(0.90, 0.90, 0.90)),
..self.active() ..self.active(is_checked)
} }
} }
} }

View File

@ -21,9 +21,9 @@ impl checkbox::Renderer for Renderer {
style_sheet: &Self::Style, style_sheet: &Self::Style,
) -> Self::Output { ) -> Self::Output {
let style = if is_mouse_over { let style = if is_mouse_over {
style_sheet.hovered() style_sheet.hovered(is_checked)
} else { } else {
style_sheet.active() style_sheet.active(is_checked)
}; };
let checkbox = Primitive::Quad { let checkbox = Primitive::Quad {