Use closures for Scrollable::style

This commit is contained in:
Héctor Ramón Jiménez 2024-03-12 13:34:51 +01:00
parent 60b5822b67
commit 66dce4865e
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 18 additions and 19 deletions

View file

@ -104,7 +104,7 @@ pub fn scrollable<'a, Message, Theme, Renderer>(
content: impl Into<Element<'a, Message, Theme, Renderer>>, content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Scrollable<'a, Message, Theme, Renderer> ) -> Scrollable<'a, Message, Theme, Renderer>
where where
Theme: scrollable::DefaultStyle, Theme: scrollable::DefaultStyle + 'a,
Renderer: core::Renderer, Renderer: core::Renderer,
{ {
Scrollable::new(content) Scrollable::new(content)

View file

@ -36,7 +36,7 @@ pub struct Scrollable<
direction: Direction, direction: Direction,
content: Element<'a, Message, Theme, Renderer>, content: Element<'a, Message, Theme, Renderer>,
on_scroll: Option<Box<dyn Fn(Viewport) -> Message + 'a>>, on_scroll: Option<Box<dyn Fn(Viewport) -> Message + 'a>>,
style: Style<Theme>, style: Style<'a, Theme>,
} }
impl<'a, Message, Theme, Renderer> Scrollable<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Scrollable<'a, Message, Theme, Renderer>
@ -48,7 +48,7 @@ where
content: impl Into<Element<'a, Message, Theme, Renderer>>, content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Self ) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Self::with_direction(content, Direction::default()) Self::with_direction(content, Direction::default())
} }
@ -59,20 +59,16 @@ where
direction: Direction, direction: Direction,
) -> Self ) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Self::with_direction_and_style( Self::with_direction_and_style(content, direction, Theme::default_style)
content,
direction,
Theme::default_style(),
)
} }
/// Creates a new [`Scrollable`] with the given [`Direction`] and style. /// Creates a new [`Scrollable`] with the given [`Direction`] and style.
pub fn with_direction_and_style( pub fn with_direction_and_style(
content: impl Into<Element<'a, Message, Theme, Renderer>>, content: impl Into<Element<'a, Message, Theme, Renderer>>,
direction: Direction, direction: Direction,
style: fn(&Theme, Status) -> Appearance, style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self { ) -> Self {
let content = content.into(); let content = content.into();
@ -95,7 +91,7 @@ where
direction, direction,
content, content,
on_scroll: None, on_scroll: None,
style: style.into(), style: Box::new(style),
} }
} }
@ -126,8 +122,11 @@ where
} }
/// Sets the style of the [`Scrollable`] . /// Sets the style of the [`Scrollable`] .
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { pub fn style(
self.style = style.into(); mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self self
} }
} }
@ -1603,23 +1602,23 @@ pub struct Scroller {
} }
/// The style of a [`Scrollable`]. /// The style of a [`Scrollable`].
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 [`Scrollable`]. /// The default style of a [`Scrollable`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`Scrollable`]. /// Returns the default style of a [`Scrollable`].
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 {
default default(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
} }
} }