Add `Toggler` to styling example

This commit is contained in:
Kaiden42 2020-09-25 15:45:46 +02:00 committed by Héctor Ramón
parent 7a626f3b7b
commit 1ef38cc207
1 changed files with 64 additions and 9 deletions

View File

@ -1,7 +1,7 @@
use iced::{ use iced::{
button, scrollable, slider, text_input, Align, Button, Checkbox, Column, button, scrollable, slider, text_input, Align, Button, Checkbox, Column,
Container, Element, Length, ProgressBar, Radio, Row, Rule, Sandbox, Container, Element, Length, ProgressBar, Radio, Row, Rule, Sandbox,
Scrollable, Settings, Slider, Space, Text, TextInput, Scrollable, Settings, Slider, Space, Text, TextInput, Toggler,
}; };
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
@ -17,7 +17,8 @@ struct Styling {
button: button::State, button: button::State,
slider: slider::State, slider: slider::State,
slider_value: f32, slider_value: f32,
toggle_value: bool, checkbox_value: bool,
toggler_value: bool,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -27,6 +28,7 @@ enum Message {
ButtonPressed, ButtonPressed,
SliderChanged(f32), SliderChanged(f32),
CheckboxToggled(bool), CheckboxToggled(bool),
TogglerToggled(bool),
} }
impl Sandbox for Styling { impl Sandbox for Styling {
@ -46,7 +48,8 @@ impl Sandbox for Styling {
Message::InputChanged(value) => self.input_value = value, Message::InputChanged(value) => self.input_value = value,
Message::ButtonPressed => {} Message::ButtonPressed => {}
Message::SliderChanged(value) => self.slider_value = value, Message::SliderChanged(value) => self.slider_value = value,
Message::CheckboxToggled(value) => self.toggle_value = value, Message::CheckboxToggled(value) => self.checkbox_value = value,
Message::TogglerToggled(value) => self.toggler_value = value,
} }
} }
@ -101,11 +104,19 @@ impl Sandbox for Styling {
.push(Text::new("You did it!")); .push(Text::new("You did it!"));
let checkbox = Checkbox::new( let checkbox = Checkbox::new(
self.toggle_value, self.checkbox_value,
"Toggle me!", "Check me!",
Message::CheckboxToggled, Message::CheckboxToggled,
) )
.width(Length::Fill) .style(self.theme);
let toggler = Toggler::new(
self.toggler_value,
String::from("Toggle me!"),
Message::TogglerToggled,
)
.width(Length::Shrink)
.spacing(10)
.style(self.theme); .style(self.theme);
let content = Column::new() let content = Column::new()
@ -124,7 +135,13 @@ impl Sandbox for Styling {
.align_items(Align::Center) .align_items(Align::Center)
.push(scrollable) .push(scrollable)
.push(Rule::vertical(38).style(self.theme)) .push(Rule::vertical(38).style(self.theme))
.push(checkbox), .push(
Column::new()
.width(Length::Shrink)
.spacing(20)
.push(checkbox)
.push(toggler),
),
); );
Container::new(content) Container::new(content)
@ -140,7 +157,7 @@ impl Sandbox for Styling {
mod style { mod style {
use iced::{ use iced::{
button, checkbox, container, progress_bar, radio, rule, scrollable, button, checkbox, container, progress_bar, radio, rule, scrollable,
slider, text_input, slider, text_input, toggler,
}; };
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -231,6 +248,15 @@ mod style {
} }
} }
impl From<Theme> for Box<dyn toggler::StyleSheet> {
fn from(theme: Theme) -> Self {
match theme {
Theme::Light => Default::default(),
Theme::Dark => dark::Toggler.into(),
}
}
}
impl From<Theme> for Box<dyn rule::StyleSheet> { impl From<Theme> for Box<dyn rule::StyleSheet> {
fn from(theme: Theme) -> Self { fn from(theme: Theme) -> Self {
match theme { match theme {
@ -269,7 +295,7 @@ mod style {
mod dark { mod dark {
use iced::{ use iced::{
button, checkbox, container, progress_bar, radio, rule, scrollable, button, checkbox, container, progress_bar, radio, rule, scrollable,
slider, text_input, Color, slider, text_input, toggler, Color,
}; };
const SURFACE: Color = Color::from_rgb( const SURFACE: Color = Color::from_rgb(
@ -520,6 +546,35 @@ mod style {
} }
} }
pub struct Toggler;
impl toggler::StyleSheet for Toggler {
fn active(&self, is_active: bool) -> toggler::Style {
toggler::Style {
background: if is_active { ACTIVE } else { SURFACE },
background_border: None,
foreground: if is_active { Color::WHITE } else { ACTIVE },
foreground_border: None,
}
}
fn hovered(&self, is_active: bool) -> toggler::Style {
toggler::Style {
background: if is_active { ACTIVE } else { SURFACE },
background_border: None,
foreground: if is_active {
Color {
a: 0.5,
..Color::WHITE
}
} else {
Color { a: 0.5, ..ACTIVE }
},
foreground_border: None,
}
}
}
pub struct Rule; pub struct Rule;
impl rule::StyleSheet for Rule { impl rule::StyleSheet for Rule {