summaryrefslogtreecommitdiffstats
path: root/widget/src/rule.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector@hecrj.dev>2024-03-12 18:17:19 +0100
committerLibravatar GitHub <noreply@github.com>2024-03-12 18:17:19 +0100
commit3d915d3cb30e5d08829aa2928676a53c505a601e (patch)
tree4acb46e00ef3037aad6a8273ab67d4278d560784 /widget/src/rule.rs
parent34317bba5db0a0f9e3ffdbbac0d7136a32bd0f95 (diff)
parent98621aa344a7a0e1b23f320d21a4687af559998e (diff)
downloadiced-3d915d3cb30e5d08829aa2928676a53c505a601e.tar.gz
iced-3d915d3cb30e5d08829aa2928676a53c505a601e.tar.bz2
iced-3d915d3cb30e5d08829aa2928676a53c505a601e.zip
Merge pull request #2326 from iced-rs/closure-styles
Use closures for widget styling
Diffstat (limited to '')
-rw-r--r--widget/src/rule.rs37
1 files changed, 19 insertions, 18 deletions
diff --git a/widget/src/rule.rs b/widget/src/rule.rs
index 8580d4c7..9fa5f74f 100644
--- a/widget/src/rule.rs
+++ b/widget/src/rule.rs
@@ -10,48 +10,49 @@ use crate::core::{
/// Display a horizontal or vertical rule for dividing content.
#[allow(missing_debug_implementations)]
-pub struct Rule<Theme = crate::Theme> {
+pub struct Rule<'a, Theme = crate::Theme> {
width: Length,
height: Length,
is_horizontal: bool,
- style: Style<Theme>,
+ style: Style<'a, Theme>,
}
-impl<Theme> Rule<Theme> {
+impl<'a, Theme> Rule<'a, Theme> {
/// Creates a horizontal [`Rule`] with the given height.
pub fn horizontal(height: impl Into<Pixels>) -> Self
where
- Theme: DefaultStyle,
+ Theme: DefaultStyle + 'a,
{
Rule {
width: Length::Fill,
height: Length::Fixed(height.into().0),
is_horizontal: true,
- style: Theme::default_style(),
+ style: Box::new(Theme::default_style),
}
}
/// Creates a vertical [`Rule`] with the given width.
pub fn vertical(width: impl Into<Pixels>) -> Self
where
- Theme: DefaultStyle,
+ Theme: DefaultStyle + 'a,
{
Rule {
width: Length::Fixed(width.into().0),
height: Length::Fill,
is_horizontal: false,
- style: Theme::default_style(),
+ style: Box::new(Theme::default_style),
}
}
/// Sets the style of the [`Rule`].
- pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self {
- self.style = style;
+ pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self {
+ self.style = Box::new(style);
self
}
}
-impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> for Rule<Theme>
+impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
+ for Rule<'a, Theme>
where
Renderer: crate::core::Renderer,
{
@@ -126,14 +127,14 @@ where
}
}
-impl<'a, Message, Theme, Renderer> From<Rule<Theme>>
+impl<'a, Message, Theme, Renderer> From<Rule<'a, Theme>>
for Element<'a, Message, Theme, Renderer>
where
Message: 'a,
Theme: 'a,
Renderer: 'a + crate::core::Renderer,
{
- fn from(rule: Rule<Theme>) -> Element<'a, Message, Theme, Renderer> {
+ fn from(rule: Rule<'a, Theme>) -> Element<'a, Message, Theme, Renderer> {
Element::new(rule)
}
}
@@ -216,23 +217,23 @@ impl FillMode {
}
/// The style of a [`Rule`].
-pub type Style<Theme> = fn(&Theme) -> Appearance;
+pub type Style<'a, Theme> = Box<dyn Fn(&Theme) -> Appearance + 'a>;
/// The default style of a [`Rule`].
pub trait DefaultStyle {
/// Returns the default style of a [`Rule`].
- fn default_style() -> Style<Self>;
+ fn default_style(&self) -> Appearance;
}
impl DefaultStyle for Theme {
- fn default_style() -> Style<Self> {
- default
+ fn default_style(&self) -> Appearance {
+ default(self)
}
}
impl DefaultStyle for Appearance {
- fn default_style() -> Style<Self> {
- |appearance| *appearance
+ fn default_style(&self) -> Appearance {
+ *self
}
}