From 58a0d5b7fff3000c46f5f2c468fce9442c78ef20 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 12 Mar 2024 14:45:28 +0100 Subject: Use closures for `Radio::style` --- widget/src/helpers.rs | 6 +++--- widget/src/radio.rs | 41 ++++++++++++++++++++++------------------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index cc9bfeef..896e2c13 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -170,15 +170,15 @@ where /// Creates a new [`Radio`]. /// /// [`Radio`]: crate::Radio -pub fn radio( +pub fn radio<'a, Message, Theme, Renderer, V>( label: impl Into, value: V, selected: Option, on_click: impl FnOnce(V) -> Message, -) -> Radio +) -> Radio<'a, Message, Theme, Renderer> where Message: Clone, - Theme: radio::DefaultStyle, + Theme: radio::DefaultStyle + 'a, Renderer: core::text::Renderer, V: Copy + Eq, { diff --git a/widget/src/radio.rs b/widget/src/radio.rs index 5e4a3c1f..a7b7dd03 100644 --- a/widget/src/radio.rs +++ b/widget/src/radio.rs @@ -17,8 +17,8 @@ use crate::core::{ /// /// # Example /// ```no_run -/// # type Radio = -/// # iced_widget::Radio; +/// # type Radio<'a, Message> = +/// # iced_widget::Radio<'a, Message, iced_widget::Theme, iced_widget::renderer::Renderer>; /// # /// # use iced_widget::column; /// #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -67,7 +67,7 @@ use crate::core::{ /// let content = column![a, b, c, all]; /// ``` #[allow(missing_debug_implementations)] -pub struct Radio +pub struct Radio<'a, Message, Theme = crate::Theme, Renderer = crate::Renderer> where Renderer: text::Renderer, { @@ -81,10 +81,10 @@ where text_line_height: text::LineHeight, text_shaping: text::Shaping, font: Option, - style: Style, + style: Style<'a, Theme>, } -impl Radio +impl<'a, Message, Theme, Renderer> Radio<'a, Message, Theme, Renderer> where Message: Clone, Renderer: text::Renderer, @@ -110,7 +110,7 @@ where f: F, ) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, V: Eq + Copy, F: FnOnce(V) -> Message, { @@ -125,7 +125,7 @@ where text_line_height: text::LineHeight::default(), text_shaping: text::Shaping::Basic, font: None, - style: Theme::default_style(), + style: Box::new(Theme::default_style), } } @@ -175,14 +175,17 @@ where } /// Sets the style of the [`Radio`] button. - 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 } } -impl Widget - for Radio +impl<'a, Message, Theme, Renderer> Widget + for Radio<'a, Message, Theme, Renderer> where Message: Clone, Renderer: text::Renderer, @@ -353,7 +356,7 @@ where } } -impl<'a, Message, Theme, Renderer> From> +impl<'a, Message, Theme, Renderer> From> for Element<'a, Message, Theme, Renderer> where Message: 'a + Clone, @@ -361,7 +364,7 @@ where Renderer: 'a + text::Renderer, { fn from( - radio: Radio, + radio: Radio<'a, Message, Theme, Renderer>, ) -> Element<'a, Message, Theme, Renderer> { Element::new(radio) } @@ -398,23 +401,23 @@ pub struct Appearance { } /// The style of a [`Radio`] button. -pub type Style = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box Appearance + 'a>; /// The default style of a [`Radio`] button. pub trait DefaultStyle { /// Returns the default style of a [`Radio`] button. - fn default_style() -> Style; + fn default_style(&self, status: Status) -> Appearance; } impl DefaultStyle for Theme { - fn default_style() -> Style { - default + fn default_style(&self, status: Status) -> Appearance { + default(self, status) } } impl DefaultStyle for Appearance { - fn default_style() -> Style { - |appearance, _status| *appearance + fn default_style(&self, _status: Status) -> Appearance { + *self } } -- cgit