Use closures for Toggler::style

This commit is contained in:
Héctor Ramón Jiménez 2024-03-12 14:54:28 +01:00
parent 6741630218
commit d0a1da194a
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 15 additions and 12 deletions

View file

@ -194,7 +194,7 @@ pub fn toggler<'a, Message, Theme, Renderer>(
f: impl Fn(bool) -> Message + 'a,
) -> Toggler<'a, Message, Theme, Renderer>
where
Theme: toggler::DefaultStyle,
Theme: toggler::DefaultStyle + 'a,
Renderer: core::text::Renderer,
{
Toggler::new(label, is_checked, f)

View file

@ -48,7 +48,7 @@ pub struct Toggler<
text_shaping: text::Shaping,
spacing: f32,
font: Option<Renderer::Font>,
style: Style<Theme>,
style: Style<'a, Theme>,
}
impl<'a, Message, Theme, Renderer> Toggler<'a, Message, Theme, Renderer>
@ -72,7 +72,7 @@ where
f: F,
) -> Self
where
Theme: DefaultStyle,
Theme: 'a + DefaultStyle,
F: 'a + Fn(bool) -> Message,
{
Toggler {
@ -87,7 +87,7 @@ where
text_shaping: text::Shaping::Basic,
spacing: Self::DEFAULT_SIZE / 2.0,
font: None,
style: Theme::default_style(),
style: Box::new(Theme::default_style),
}
}
@ -145,8 +145,11 @@ where
}
/// Sets the style of the [`Toggler`].
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self {
self.style = style.into();
pub fn style(
mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self
}
}
@ -398,23 +401,23 @@ pub struct Appearance {
}
/// The style of a [`Toggler`].
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 [`Toggler`].
pub trait DefaultStyle {
/// Returns the default style of a [`Toggler`].
fn default_style() -> Style<Self>;
fn default_style(&self, status: Status) -> Appearance;
}
impl DefaultStyle for Theme {
fn default_style() -> Style<Self> {
default
fn default_style(&self, status: Status) -> Appearance {
default(self, status)
}
}
impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> {
|appearance, _status| *appearance
fn default_style(&self, _status: Status) -> Appearance {
*self
}
}