Use closures for Radio::style

This commit is contained in:
Héctor Ramón Jiménez 2024-03-12 14:45:28 +01:00
parent aeb72d528f
commit 58a0d5b7ff
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 25 additions and 22 deletions

View file

@ -170,15 +170,15 @@ where
/// Creates a new [`Radio`]. /// Creates a new [`Radio`].
/// ///
/// [`Radio`]: crate::Radio /// [`Radio`]: crate::Radio
pub fn radio<Message, Theme, Renderer, V>( pub fn radio<'a, Message, Theme, Renderer, V>(
label: impl Into<String>, label: impl Into<String>,
value: V, value: V,
selected: Option<V>, selected: Option<V>,
on_click: impl FnOnce(V) -> Message, on_click: impl FnOnce(V) -> Message,
) -> Radio<Message, Theme, Renderer> ) -> Radio<'a, Message, Theme, Renderer>
where where
Message: Clone, Message: Clone,
Theme: radio::DefaultStyle, Theme: radio::DefaultStyle + 'a,
Renderer: core::text::Renderer, Renderer: core::text::Renderer,
V: Copy + Eq, V: Copy + Eq,
{ {

View file

@ -17,8 +17,8 @@ use crate::core::{
/// ///
/// # Example /// # Example
/// ```no_run /// ```no_run
/// # type Radio<Message> = /// # type Radio<'a, Message> =
/// # iced_widget::Radio<Message, iced_widget::Theme, iced_widget::renderer::Renderer>; /// # iced_widget::Radio<'a, Message, iced_widget::Theme, iced_widget::renderer::Renderer>;
/// # /// #
/// # use iced_widget::column; /// # use iced_widget::column;
/// #[derive(Debug, Clone, Copy, PartialEq, Eq)] /// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -67,7 +67,7 @@ use crate::core::{
/// let content = column![a, b, c, all]; /// let content = column![a, b, c, all];
/// ``` /// ```
#[allow(missing_debug_implementations)] #[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 where
Renderer: text::Renderer, Renderer: text::Renderer,
{ {
@ -81,10 +81,10 @@ where
text_line_height: text::LineHeight, text_line_height: text::LineHeight,
text_shaping: text::Shaping, text_shaping: text::Shaping,
font: Option<Renderer::Font>, 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 where
Message: Clone, Message: Clone,
Renderer: text::Renderer, Renderer: text::Renderer,
@ -110,7 +110,7 @@ where
f: F, f: F,
) -> Self ) -> Self
where where
Theme: DefaultStyle, Theme: DefaultStyle + 'a,
V: Eq + Copy, V: Eq + Copy,
F: FnOnce(V) -> Message, F: FnOnce(V) -> Message,
{ {
@ -125,7 +125,7 @@ where
text_line_height: text::LineHeight::default(), text_line_height: text::LineHeight::default(),
text_shaping: text::Shaping::Basic, text_shaping: text::Shaping::Basic,
font: None, font: None,
style: Theme::default_style(), style: Box::new(Theme::default_style),
} }
} }
@ -175,14 +175,17 @@ where
} }
/// Sets the style of the [`Radio`] button. /// Sets the style of the [`Radio`] button.
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { pub fn style(
self.style = style; mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self self
} }
} }
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for Radio<Message, Theme, Renderer> for Radio<'a, Message, Theme, Renderer>
where where
Message: Clone, Message: Clone,
Renderer: text::Renderer, 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> for Element<'a, Message, Theme, Renderer>
where where
Message: 'a + Clone, Message: 'a + Clone,
@ -361,7 +364,7 @@ where
Renderer: 'a + text::Renderer, Renderer: 'a + text::Renderer,
{ {
fn from( fn from(
radio: Radio<Message, Theme, Renderer>, radio: Radio<'a, Message, Theme, Renderer>,
) -> Element<'a, Message, Theme, Renderer> { ) -> Element<'a, Message, Theme, Renderer> {
Element::new(radio) Element::new(radio)
} }
@ -398,23 +401,23 @@ pub struct Appearance {
} }
/// The style of a [`Radio`] button. /// 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. /// The default style of a [`Radio`] button.
pub trait DefaultStyle { pub trait DefaultStyle {
/// Returns the default style of a [`Radio`] button. /// Returns the default style of a [`Radio`] button.
fn default_style() -> Style<Self>; fn default_style(&self, status: Status) -> Appearance;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style(&self, status: Status) -> Appearance {
default default(self, status)
} }
} }
impl DefaultStyle for Appearance { impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> { fn default_style(&self, _status: Status) -> Appearance {
|appearance, _status| *appearance *self
} }
} }