diff options
| author | 2024-03-12 14:45:28 +0100 | |
|---|---|---|
| committer | 2024-03-12 14:52:26 +0100 | |
| commit | 58a0d5b7fff3000c46f5f2c468fce9442c78ef20 (patch) | |
| tree | b45d041cc6fbcb9ccd393f62606c14f52498a3ff /widget/src | |
| parent | aeb72d528fce58ac94df3c015c10d6fe05f81a01 (diff) | |
| download | iced-58a0d5b7fff3000c46f5f2c468fce9442c78ef20.tar.gz iced-58a0d5b7fff3000c46f5f2c468fce9442c78ef20.tar.bz2 iced-58a0d5b7fff3000c46f5f2c468fce9442c78ef20.zip | |
Use closures for `Radio::style`
Diffstat (limited to '')
| -rw-r--r-- | widget/src/helpers.rs | 6 | ||||
| -rw-r--r-- | 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<Message, Theme, Renderer, V>( +pub fn radio<'a, Message, Theme, Renderer, V>(      label: impl Into<String>,      value: V,      selected: Option<V>,      on_click: impl FnOnce(V) -> Message, -) -> Radio<Message, Theme, Renderer> +) -> 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<Message> = -/// #     iced_widget::Radio<Message, iced_widget::Theme, iced_widget::renderer::Renderer>; +/// # 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<Message, Theme = crate::Theme, Renderer = crate::Renderer> +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<Renderer::Font>, -    style: Style<Theme>, +    style: Style<'a, Theme>,  } -impl<Message, Theme, Renderer> Radio<Message, Theme, Renderer> +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<Message, Theme, Renderer> Widget<Message, Theme, Renderer> -    for Radio<Message, Theme, Renderer> +impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer> +    for Radio<'a, Message, Theme, Renderer>  where      Message: Clone,      Renderer: text::Renderer, @@ -353,7 +356,7 @@ where      }  } -impl<'a, Message, Theme, Renderer> From<Radio<Message, Theme, Renderer>> +impl<'a, Message, Theme, Renderer> From<Radio<'a, Message, Theme, Renderer>>      for Element<'a, Message, Theme, Renderer>  where      Message: 'a + Clone, @@ -361,7 +364,7 @@ where      Renderer: 'a + text::Renderer,  {      fn from( -        radio: Radio<Message, Theme, Renderer>, +        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<Theme> = fn(&Theme, Status) -> Appearance; +pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>;  /// The default style of a [`Radio`] button.  pub trait DefaultStyle {      /// Returns the default style of a [`Radio`] button. -    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      }  } | 
