From 833538ee7f3a60a839304762dfc29b0881d19094 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 7 Mar 2024 20:11:32 +0100 Subject: Leverage `DefaultStyle` traits instead of `Default` --- widget/src/pick_list.rs | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'widget/src/pick_list.rs') diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index 649daafe..cfeabbb7 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -69,7 +69,7 @@ where on_select: impl Fn(T) -> Message + 'a, ) -> Self where - Style: Default, + Theme: DefaultStyle, { Self { on_select: Box::new(on_select), @@ -85,7 +85,7 @@ where text_shaping: text::Shaping::Basic, font: None, handle: Handle::default(), - style: Style::default(), + style: Theme::default_style(), } } @@ -266,7 +266,7 @@ where Status::Active }; - let appearance = (self.style.pick_list)(theme, status); + let appearance = (self.style.field)(theme, status); renderer.fill_quad( renderer::Quad { @@ -737,16 +737,24 @@ pub struct Appearance { pub border: Border, } -/// The different styles of a [`PickList`]. +/// The styles of the different parts of a [`PickList`]. #[derive(Debug, PartialEq, Eq)] pub struct Style { /// The style of the [`PickList`] itself. - pub pick_list: fn(&Theme, Status) -> Appearance, + pub field: fn(&Theme, Status) -> Appearance, /// The style of the [`Menu`] of the pick list. pub menu: menu::Style, } +impl Style { + /// The default style of a [`PickList`] with the built-in [`Theme`]. + pub const DEFAULT: Self = Self { + field: default, + menu: menu::Style::::DEFAULT, + }; +} + impl Clone for Style { fn clone(&self) -> Self { *self @@ -755,16 +763,19 @@ impl Clone for Style { impl Copy for Style {} -impl Default for Style { - fn default() -> Self { - Self { - pick_list: default, - menu: menu::Style::default(), - } +/// The default style of a [`PickList`]. +pub trait DefaultStyle: Sized { + /// Returns the default style of a [`PickList`]. + fn default_style() -> Style; +} + +impl DefaultStyle for Theme { + fn default_style() -> Style { + Style::::DEFAULT } } -/// The default style of a [`PickList`]. +/// The default style of the field of a [`PickList`]. pub fn default(theme: &Theme, status: Status) -> Appearance { let palette = theme.extended_palette(); -- cgit