Implement theme styling for Checkbox

This commit is contained in:
Héctor Ramón Jiménez 2022-06-04 03:26:53 +02:00
parent 1388d71f03
commit 835877fc63
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
8 changed files with 138 additions and 93 deletions

View file

@ -105,8 +105,7 @@ impl Sandbox for Styling {
self.checkbox_value,
"Check me!",
Message::CheckboxToggled,
)
.style(self.theme);
);
let toggler = Toggler::new(
self.toggler_value,
@ -158,7 +157,7 @@ impl Sandbox for Styling {
}
mod style {
use iced::{checkbox, scrollable, text_input};
use iced::{scrollable, text_input};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Theme {
@ -194,17 +193,8 @@ mod style {
}
}
impl<'a> From<Theme> for Box<dyn checkbox::StyleSheet + 'a> {
fn from(theme: Theme) -> Self {
match theme {
Theme::Light => Default::default(),
Theme::Dark => dark::Checkbox.into(),
}
}
}
mod dark {
use iced::{checkbox, scrollable, text_input, Color};
use iced::{scrollable, text_input, Color};
const SURFACE: Color = Color::from_rgb(
0x40 as f32 / 255.0,
@ -314,32 +304,5 @@ mod style {
}
}
}
pub struct Checkbox;
impl checkbox::StyleSheet for Checkbox {
fn active(&self, is_checked: bool) -> checkbox::Style {
checkbox::Style {
background: if is_checked { ACTIVE } else { SURFACE }
.into(),
checkmark_color: Color::WHITE,
border_radius: 2.0,
border_width: 1.0,
border_color: ACTIVE,
text_color: None,
}
}
fn hovered(&self, is_checked: bool) -> checkbox::Style {
checkbox::Style {
background: Color {
a: 0.8,
..if is_checked { ACTIVE } else { SURFACE }
}
.into(),
..self.active(is_checked)
}
}
}
}
}