Use closures for Rule::style

This commit is contained in:
Héctor Ramón Jiménez 2024-03-12 13:44:03 +01:00
parent 71b9b3c3b1
commit 66f81c3429
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 23 additions and 22 deletions

View file

@ -315,9 +315,9 @@ pub fn vertical_space() -> Space {
/// Creates a horizontal [`Rule`] with the given height. /// Creates a horizontal [`Rule`] with the given height.
/// ///
/// [`Rule`]: crate::Rule /// [`Rule`]: crate::Rule
pub fn horizontal_rule<Theme>(height: impl Into<Pixels>) -> Rule<Theme> pub fn horizontal_rule<'a, Theme>(height: impl Into<Pixels>) -> Rule<'a, Theme>
where where
Theme: rule::DefaultStyle, Theme: rule::DefaultStyle + 'a,
{ {
Rule::horizontal(height) Rule::horizontal(height)
} }
@ -325,9 +325,9 @@ where
/// Creates a vertical [`Rule`] with the given width. /// Creates a vertical [`Rule`] with the given width.
/// ///
/// [`Rule`]: crate::Rule /// [`Rule`]: crate::Rule
pub fn vertical_rule<Theme>(width: impl Into<Pixels>) -> Rule<Theme> pub fn vertical_rule<'a, Theme>(width: impl Into<Pixels>) -> Rule<'a, Theme>
where where
Theme: rule::DefaultStyle, Theme: rule::DefaultStyle + 'a,
{ {
Rule::vertical(width) Rule::vertical(width)
} }

View file

@ -10,48 +10,49 @@ use crate::core::{
/// Display a horizontal or vertical rule for dividing content. /// Display a horizontal or vertical rule for dividing content.
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Rule<Theme = crate::Theme> { pub struct Rule<'a, Theme = crate::Theme> {
width: Length, width: Length,
height: Length, height: Length,
is_horizontal: bool, 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. /// Creates a horizontal [`Rule`] with the given height.
pub fn horizontal(height: impl Into<Pixels>) -> Self pub fn horizontal(height: impl Into<Pixels>) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Rule { Rule {
width: Length::Fill, width: Length::Fill,
height: Length::Fixed(height.into().0), height: Length::Fixed(height.into().0),
is_horizontal: true, is_horizontal: true,
style: Theme::default_style(), style: Box::new(Theme::default_style),
} }
} }
/// Creates a vertical [`Rule`] with the given width. /// Creates a vertical [`Rule`] with the given width.
pub fn vertical(width: impl Into<Pixels>) -> Self pub fn vertical(width: impl Into<Pixels>) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
{ {
Rule { Rule {
width: Length::Fixed(width.into().0), width: Length::Fixed(width.into().0),
height: Length::Fill, height: Length::Fill,
is_horizontal: false, is_horizontal: false,
style: Theme::default_style(), style: Box::new(Theme::default_style),
} }
} }
/// Sets the style of the [`Rule`]. /// Sets the style of the [`Rule`].
pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { pub fn style(mut self, style: impl Fn(&Theme) -> Appearance + 'a) -> Self {
self.style = style; self.style = Box::new(style);
self 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 where
Renderer: crate::core::Renderer, 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> for Element<'a, Message, Theme, Renderer>
where where
Message: 'a, Message: 'a,
Theme: 'a, Theme: 'a,
Renderer: 'a + crate::core::Renderer, 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) Element::new(rule)
} }
} }
@ -216,23 +217,23 @@ impl FillMode {
} }
/// The style of a [`Rule`]. /// 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`]. /// The default style of a [`Rule`].
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`Rule`]. /// Returns the default style of a [`Rule`].
fn default_style() -> Style<Self>; fn default_style(&self) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self) -> Appearance {
default default(self)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self) -> Appearance {
|appearance| *appearance *self
} }
} }