summaryrefslogtreecommitdiffstats
path: root/widget
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-12 14:58:06 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2024-03-12 14:58:06 +0100
commit252eb88703196dd1d373fb45cbbb7ee7b85f3726 (patch)
tree8da52353e52376da727c3b2928cb519676a91b7f /widget
parentd0a1da194a7311d599bda0884c973e8eed6c15d8 (diff)
downloadiced-252eb88703196dd1d373fb45cbbb7ee7b85f3726.tar.gz
iced-252eb88703196dd1d373fb45cbbb7ee7b85f3726.tar.bz2
iced-252eb88703196dd1d373fb45cbbb7ee7b85f3726.zip
Use closures for `Checkbox::style`
Diffstat (limited to 'widget')
-rw-r--r--widget/src/checkbox.rs25
-rw-r--r--widget/src/helpers.rs2
2 files changed, 15 insertions, 12 deletions
diff --git a/widget/src/checkbox.rs b/widget/src/checkbox.rs
index f0c7357b..15fb8f58 100644
--- a/widget/src/checkbox.rs
+++ b/widget/src/checkbox.rs
@@ -51,7 +51,7 @@ pub struct Checkbox<
text_shaping: text::Shaping,
font: Option<Renderer::Font>,
icon: Icon<Renderer::Font>,
- style: Style<Theme>,
+ style: Style<'a, Theme>,
}
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
pub fn new(label: impl Into<String>, is_checked: bool) -> Self
where
- Theme: DefaultStyle,
+ Theme: DefaultStyle + 'a,
{
Checkbox {
is_checked,
@@ -91,7 +91,7 @@ where
line_height: text::LineHeight::default(),
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`].
- pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self {
- self.style = style;
+ pub fn style(
+ mut self,
+ style: impl Fn(&Theme, Status) -> Appearance + 'a,
+ ) -> Self {
+ self.style = Box::new(style);
self
}
}
@@ -424,23 +427,23 @@ pub struct Appearance {
}
/// 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`].
pub trait DefaultStyle {
/// Returns the default style of a [`Checkbox`].
- fn default_style() -> Style<Self>;
+ fn default_style(&self, status: Status) -> Appearance;
}
impl DefaultStyle for Theme {
- fn default_style() -> Style<Self> {
- primary
+ fn default_style(&self, status: Status) -> Appearance {
+ primary(self, status)
}
}
impl DefaultStyle for Appearance {
- fn default_style() -> Style<Self> {
- |appearance, _status| *appearance
+ fn default_style(&self, _status: Status) -> Appearance {
+ *self
}
}
diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs
index dde6a207..7912d7b8 100644
--- a/widget/src/helpers.rs
+++ b/widget/src/helpers.rs
@@ -161,7 +161,7 @@ pub fn checkbox<'a, Message, Theme, Renderer>(
is_checked: bool,
) -> Checkbox<'a, Message, Theme, Renderer>
where
- Theme: checkbox::DefaultStyle,
+ Theme: checkbox::DefaultStyle + 'a,
Renderer: core::text::Renderer,
{
Checkbox::new(label, is_checked)