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_editor.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'widget/src/text_editor.rs') diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 91670228..73b006fa 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -42,7 +42,7 @@ pub struct TextEditor< width: Length, height: Length, padding: Padding, - style: fn(&Theme, Status) -> Appearance, + style: Style, on_edit: Option Message + 'a>>, highlighter_settings: Highlighter::Settings, highlighter_format: fn( @@ -59,7 +59,7 @@ where /// Creates new [`TextEditor`] with the given [`Content`]. pub fn new(content: &'a Content) -> Self where - Theme: Style, + Style: Default, { Self { content, @@ -69,7 +69,7 @@ where width: Length::Fill, height: Length::Shrink, padding: Padding::new(5.0), - style: Theme::style(), + style: Style::default(), on_edit: None, highlighter_settings: (), highlighter_format: |_highlight, _theme| { @@ -144,7 +144,7 @@ where /// Sets the style of the [`TextEditor`]. pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = style; + self.style = style.into(); self } } @@ -506,7 +506,7 @@ where Status::Active }; - let appearance = (self.style)(theme, status); + let appearance = (self.style.0)(theme, status); renderer.fill_quad( renderer::Quad { @@ -809,15 +809,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 [`TextEditor`]. +#[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