diff options
author | 2024-03-12 15:35:48 +0100 | |
---|---|---|
committer | 2024-03-12 15:35:48 +0100 | |
commit | d1e40495410049aedb6756be1febd83bae5eee1e (patch) | |
tree | 21d8790f03a6114156dae79a5152bd4d9b9ee8a8 /widget | |
parent | 252eb88703196dd1d373fb45cbbb7ee7b85f3726 (diff) | |
download | iced-d1e40495410049aedb6756be1febd83bae5eee1e.tar.gz iced-d1e40495410049aedb6756be1febd83bae5eee1e.tar.bz2 iced-d1e40495410049aedb6756be1febd83bae5eee1e.zip |
Use closures for `TextInput::style`
Diffstat (limited to 'widget')
-rw-r--r-- | widget/src/combo_box.rs | 32 | ||||
-rw-r--r-- | widget/src/helpers.rs | 4 | ||||
-rw-r--r-- | widget/src/text_input.rs | 29 |
3 files changed, 29 insertions, 36 deletions
diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index bddf2789..95667882 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -62,7 +62,7 @@ where on_selected: impl Fn(T) -> Message + 'static, ) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { let style = Theme::default_style(); @@ -125,7 +125,10 @@ where } /// Sets the style of the [`ComboBox`]. - pub fn style(mut self, style: impl Into<Style<Theme>>) -> Self { + pub fn style(mut self, style: impl Into<Style<Theme>>) -> Self + where + Theme: 'a, + { let style = style.into(); self.text_input = self.text_input.style(style.text_input); @@ -761,10 +764,10 @@ where } /// The style of a [`ComboBox`]. -#[derive(Debug, PartialEq, Eq)] +#[allow(missing_debug_implementations)] pub struct Style<Theme> { /// The style of the [`TextInput`] of the [`ComboBox`]. - pub text_input: fn(&Theme, text_input::Status) -> text_input::Appearance, + pub text_input: text_input::Style<'static, Theme>, /// The style of the [`Menu`] of the [`ComboBox`]. /// @@ -772,22 +775,6 @@ pub struct Style<Theme> { pub menu: menu::Style<Theme>, } -impl Style<Theme> { - /// The default style of a [`ComboBox`]. - pub const DEFAULT: Self = Self { - text_input: text_input::default, - menu: menu::Style::<Theme>::DEFAULT, - }; -} - -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } -} - -impl<Theme> Copy for Style<Theme> {} - /// The default style of a [`ComboBox`]. pub trait DefaultStyle: Sized { /// Returns the default style of a [`ComboBox`]. @@ -796,6 +783,9 @@ pub trait DefaultStyle: Sized { impl DefaultStyle for Theme { fn default_style() -> Style<Self> { - Style::<Self>::DEFAULT + Style { + text_input: Box::new(text_input::default), + menu: menu::Style::DEFAULT, + } } } diff --git a/widget/src/helpers.rs b/widget/src/helpers.rs index 7912d7b8..2606826d 100644 --- a/widget/src/helpers.rs +++ b/widget/src/helpers.rs @@ -209,7 +209,7 @@ pub fn text_input<'a, Message, Theme, Renderer>( ) -> TextInput<'a, Message, Theme, Renderer> where Message: Clone, - Theme: text_input::DefaultStyle, + Theme: text_input::DefaultStyle + 'a, Renderer: core::text::Renderer, { TextInput::new(placeholder, value) @@ -291,7 +291,7 @@ pub fn combo_box<'a, T, Message, Theme, Renderer>( ) -> ComboBox<'a, T, Message, Theme, Renderer> where T: std::fmt::Display + Clone, - Theme: combo_box::DefaultStyle, + Theme: combo_box::DefaultStyle + 'a, Renderer: core::text::Renderer, { ComboBox::new(state, placeholder, selection, on_selected) diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 9ed8480a..b161ec74 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -75,7 +75,7 @@ pub struct TextInput< on_paste: Option<Box<dyn Fn(String) -> Message + 'a>>, on_submit: Option<Message>, icon: Option<Icon<Renderer::Font>>, - style: Style<Theme>, + style: Style<'a, Theme>, } /// The default [`Padding`] of a [`TextInput`]. @@ -90,9 +90,9 @@ where /// its current value. pub fn new(placeholder: &str, value: &str) -> Self where - Theme: DefaultStyle, + Theme: DefaultStyle + 'a, { - Self::with_style(placeholder, value, Theme::default_style()) + Self::with_style(placeholder, value, Theme::default_style) } /// Creates a new [`TextInput`] with the given placeholder, @@ -100,7 +100,7 @@ where pub fn with_style( placeholder: &str, value: &str, - style: fn(&Theme, Status) -> Appearance, + style: impl Fn(&Theme, Status) -> Appearance + 'a, ) -> Self { TextInput { id: None, @@ -116,7 +116,7 @@ where on_paste: None, on_submit: None, icon: None, - style: style.into(), + style: Box::new(style), } } @@ -203,8 +203,11 @@ where } /// Sets the style of the [`TextInput`]. - pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style.into(); + pub fn style( + mut self, + style: impl Fn(&Theme, Status) -> Appearance + 'a, + ) -> Self { + self.style = Box::new(style); self } @@ -1413,23 +1416,23 @@ pub struct Appearance { } /// The style of a [`TextInput`]. -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 [`TextInput`]. pub trait DefaultStyle { /// Returns the default style of a [`TextInput`]. - 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 } } |