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/pane_grid.rs | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) (limited to 'widget/src/pane_grid.rs') diff --git a/widget/src/pane_grid.rs b/widget/src/pane_grid.rs index 62067e66..ae9cd825 100644 --- a/widget/src/pane_grid.rs +++ b/widget/src/pane_grid.rs @@ -112,7 +112,7 @@ pub struct PaneGrid< on_click: Option Message + 'a>>, on_drag: Option Message + 'a>>, on_resize: Option<(f32, Box Message + 'a>)>, - style: fn(&Theme) -> Appearance, + style: Style, } impl<'a, Message, Theme, Renderer> PaneGrid<'a, Message, Theme, Renderer> @@ -128,7 +128,7 @@ where view: impl Fn(Pane, &'a T, bool) -> Content<'a, Message, Theme, Renderer>, ) -> Self where - Theme: Style, + Style: Default, { let contents = if let Some((pane, pane_state)) = state.maximized.and_then(|pane| { @@ -160,7 +160,7 @@ where on_click: None, on_drag: None, on_resize: None, - style: Theme::style(), + style: Style::default(), } } @@ -221,7 +221,7 @@ where /// Sets the style of the [`PaneGrid`]. pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { - self.style = style; + self.style = Style(style); self } @@ -679,7 +679,7 @@ where None }; - let appearance = (self.style)(theme); + let appearance = (self.style.0)(theme); for ((id, (content, tree)), pane_layout) in contents.zip(layout.children()) @@ -1147,15 +1147,27 @@ pub struct Line { pub width: f32, } -/// The definiton of the default style of a [`PaneGrid`]. -pub trait Style { - /// Returns the default style of a [`PaneGrid`]. - fn style() -> fn(&Self) -> Appearance; +/// The style of a [`PaneGrid`]. +#[derive(Debug, PartialEq, Eq)] +pub struct Style(fn(&Theme) -> 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) -> Appearance { - default +impl From Appearance> for Style { + fn from(f: fn(&Theme) -> Appearance) -> Self { + Style(f) } } -- cgit