From 34e7c6593a9e0f56cee5db18b7258717cf6bc11b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 6 Mar 2024 20:30:58 +0100 Subject: Use `Style` struct pattern instead of trait for all widgets --- widget/src/text_input.rs | 53 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 17 deletions(-) (limited to 'widget/src/text_input.rs') diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index 11b0a5d5..bae84db7 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -77,7 +77,7 @@ pub struct TextInput< on_paste: Option Message + 'a>>, on_submit: Option, icon: Option>, - style: fn(&Theme, Status) -> Appearance, + style: Style, } /// The default [`Padding`] of a [`TextInput`]. @@ -88,15 +88,22 @@ where Message: Clone, Renderer: text::Renderer, { - /// Creates a new [`TextInput`]. - /// - /// It expects: - /// - a placeholder, - /// - the current value + /// Creates a new [`TextInput`] with the given placeholder and + /// its current value. pub fn new(placeholder: &str, value: &str) -> Self where - Theme: Style, + Style: Default, { + Self::with_style(placeholder, value, Style::default().0) + } + + /// Creates a new [`TextInput`] with the given placeholder, + /// its current value, and its style. + pub fn with_style( + placeholder: &str, + value: &str, + style: fn(&Theme, Status) -> Appearance, + ) -> Self { TextInput { id: None, placeholder: String::from(placeholder), @@ -111,7 +118,7 @@ where on_paste: None, on_submit: None, icon: None, - style: Theme::style(), + style: style.into(), } } @@ -199,7 +206,7 @@ where /// Sets the style of the [`TextInput`]. pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style; + self.style = style.into(); self } @@ -337,7 +344,7 @@ where Status::Active }; - let appearance = (self.style)(theme, status); + let appearance = (self.style.0)(theme, status); renderer.fill_quad( renderer::Quad { @@ -1406,15 +1413,27 @@ pub struct Appearance { pub selection: Color, } -/// The definiton of the default style of a [`TextInput`]. -pub trait Style { - /// Returns the default style of a [`TextInput`]. - fn style() -> fn(&Self, Status) -> Appearance; +/// The style of a [`TextInput`]. +#[derive(Debug, PartialEq, Eq)] +pub struct Style(fn(&Theme, Status) -> Appearance); + +impl Clone for Style { + fn clone(&self) -> Self { + *self + } +} + +impl Copy for Style {} + +impl Default for Style { + fn default() -> Self { + Style(default) + } } -impl Style for Theme { - fn style() -> fn(&Self, Status) -> Appearance { - default +impl From Appearance> for Style { + fn from(f: fn(&Theme, Status) -> Appearance) -> Self { + Style(f) } } -- cgit