summaryrefslogtreecommitdiffstats
path: root/widget
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-12 13:34:51 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-12 13:34:51 +0100
commit66dce4865eb00f7cc7b75e64cd8a96a496916285 (patch)
tree15ecc68f08b3b42dbd789527a5b9f84217b7ce24 /widget
parent60b5822b67e569e99efc9e4176c3e6d3f859d0bb (diff)
downloadiced-66dce4865eb00f7cc7b75e64cd8a96a496916285.tar.gz
iced-66dce4865eb00f7cc7b75e64cd8a96a496916285.tar.bz2
iced-66dce4865eb00f7cc7b75e64cd8a96a496916285.zip
Use closures for `Scrollable::style`
Diffstat (limited to 'widget')
-rw-r--r--widget/src/helpers.rs2
-rw-r--r--widget/src/scrollable.rs35
2 files changed, 18 insertions, 19 deletions
diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs
index db21c8e9..e30b2829 100644
--- a/widget/src/helpers.rs
+++ b/widget/src/helpers.rs
@@ -104,7 +104,7 @@ pub fn scrollable<'a, Message, Theme, Renderer>(
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Scrollable<'a, Message, Theme, Renderer>
where
- Theme: scrollable::DefaultStyle,
+ Theme: scrollable::DefaultStyle + 'a,
Renderer: core::Renderer,
{
Scrollable::new(content)
diff --git a/widget/src/scrollable.rs b/widget/src/scrollable.rs
index 34312752..c03bbb7d 100644
--- a/widget/src/scrollable.rs
+++ b/widget/src/scrollable.rs
@@ -36,7 +36,7 @@ pub struct Scrollable<
direction: Direction,
content: Element<'a, Message, Theme, Renderer>,
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>
@@ -48,7 +48,7 @@ where
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Self
where
- Theme: DefaultStyle,
+ Theme: DefaultStyle + 'a,
{
Self::with_direction(content, Direction::default())
}
@@ -59,20 +59,16 @@ where
direction: Direction,
) -> Self
where
- Theme: DefaultStyle,
+ Theme: DefaultStyle + 'a,
{
- Self::with_direction_and_style(
- content,
- direction,
- Theme::default_style(),
- )
+ Self::with_direction_and_style(content, direction, Theme::default_style)
}
/// Creates a new [`Scrollable`] with the given [`Direction`] and style.
pub fn with_direction_and_style(
content: impl Into<Element<'a, Message, Theme, Renderer>>,
direction: Direction,
- style: fn(&Theme, Status) -> Appearance,
+ style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
let content = content.into();
@@ -95,7 +91,7 @@ where
direction,
content,
on_scroll: None,
- style: style.into(),
+ style: Box::new(style),
}
}
@@ -126,8 +122,11 @@ where
}
/// Sets the style of the [`Scrollable`] .
- 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
}
}
@@ -1603,23 +1602,23 @@ pub struct Scroller {
}
/// 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`].
pub trait DefaultStyle {
/// Returns the default style of a [`Scrollable`].
- 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
}
}