Use closures for Checkbox::style

This commit is contained in:
Héctor Ramón Jiménez 2024-03-12 14:58:06 +01:00
parent d0a1da194a
commit 252eb88703
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 20 additions and 18 deletions

View file

@ -63,17 +63,16 @@ impl Application for Example {
let default_checkbox = checkbox("Default", self.default) let default_checkbox = checkbox("Default", self.default)
.on_toggle(Message::DefaultToggled); .on_toggle(Message::DefaultToggled);
let styled_checkbox = |label, style| { let styled_checkbox = |label| {
checkbox(label, self.styled) checkbox(label, self.styled)
.on_toggle_maybe(self.default.then_some(Message::StyledToggled)) .on_toggle_maybe(self.default.then_some(Message::StyledToggled))
.style(style)
}; };
let checkboxes = row![ let checkboxes = row![
styled_checkbox("Primary", checkbox::primary), styled_checkbox("Primary").style(checkbox::primary),
styled_checkbox("Secondary", checkbox::secondary), styled_checkbox("Secondary").style(checkbox::secondary),
styled_checkbox("Success", checkbox::success), styled_checkbox("Success").style(checkbox::success),
styled_checkbox("Danger", checkbox::danger), styled_checkbox("Danger").style(checkbox::danger),
] ]
.spacing(20); .spacing(20);

View file

@ -51,7 +51,7 @@ pub struct Checkbox<
text_shaping: text::Shaping, text_shaping: text::Shaping,
font: Option<Renderer::Font>, font: Option<Renderer::Font>,
icon: Icon<Renderer::Font>, icon: Icon<Renderer::Font>,
style: Style<Theme>, style: Style<'a, Theme>,
} }
impl<'a, Message, Theme, Renderer> Checkbox<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Checkbox<'a, Message, Theme, Renderer>
@ -71,7 +71,7 @@ where
/// * a boolean describing whether the [`Checkbox`] is checked or not /// * a boolean describing whether the [`Checkbox`] is checked or not
pub fn new(label: impl Into<String>, is_checked: bool) -> Self pub fn new(label: impl Into<String>, is_checked: bool) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Checkbox { Checkbox {
is_checked, is_checked,
@ -91,7 +91,7 @@ where
line_height: text::LineHeight::default(), line_height: text::LineHeight::default(),
shaping: text::Shaping::Basic, shaping: text::Shaping::Basic,
}, },
style: Theme::default_style(), style: Box::new(Theme::default_style),
} }
} }
@ -174,8 +174,11 @@ where
} }
/// Sets the style of the [`Checkbox`]. /// Sets the style of the [`Checkbox`].
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { pub fn style(
self.style = style; mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self self
} }
} }
@ -424,23 +427,23 @@ pub struct Appearance {
} }
/// The style of a [`Checkbox`]. /// The style of a [`Checkbox`].
pub type Style<Theme> = fn(&Theme, Status) -> Appearance; pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>;
/// The default style of a [`Checkbox`]. /// The default style of a [`Checkbox`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`Checkbox`]. /// Returns the default style of a [`Checkbox`].
fn default_style() -> Style<Self>; fn default_style(&self, status: Status) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self, status: Status) -> Appearance {
primary primary(self, status)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|appearance, _status| *appearance *self
} }
} }

View file

@ -161,7 +161,7 @@ pub fn checkbox<'a, Message, Theme, Renderer>(
is_checked: bool, is_checked: bool,
) -> Checkbox<'a, Message, Theme, Renderer> ) -> Checkbox<'a, Message, Theme, Renderer>
where where
Theme: checkbox::DefaultStyle, Theme: checkbox::DefaultStyle + 'a,
Renderer: core::text::Renderer, Renderer: core::text::Renderer,
{ {
Checkbox::new(label, is_checked) Checkbox::new(label, is_checked)