From b721fd935c6b689b1365e21dfc864e43bf3a5895 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 12 Mar 2024 16:40:56 +0100 Subject: [PATCH] Use closures for `PickList::style` --- widget/src/combo_box.rs | 20 +++++----- widget/src/overlay/menu.rs | 75 +++++++++----------------------------- widget/src/pick_list.rs | 41 +++++++-------------- 3 files changed, 41 insertions(+), 95 deletions(-) diff --git a/widget/src/combo_box.rs b/widget/src/combo_box.rs index 956678824f..ee24d7429a 100644 --- a/widget/src/combo_box.rs +++ b/widget/src/combo_box.rs @@ -42,7 +42,7 @@ pub struct ComboBox< on_option_hovered: Option Message>>, on_close: Option, on_input: Option Message>>, - menu_style: menu::Style, + menu_style: menu::Style<'a, Theme>, padding: Padding, size: Option, } @@ -125,7 +125,7 @@ where } /// Sets the style of the [`ComboBox`]. - pub fn style(mut self, style: impl Into>) -> Self + pub fn style(mut self, style: impl Into>) -> Self where Theme: 'a, { @@ -672,7 +672,7 @@ where self.state.sync_filtered_options(filtered_options); - let mut menu = menu::Menu::with_style( + let mut menu = menu::Menu::new( menu, &filtered_options.options, hovered_option, @@ -686,7 +686,7 @@ where (self.on_selected)(x) }, self.on_option_hovered.as_deref(), - self.menu_style, + &self.menu_style, ) .width(bounds.width) .padding(self.padding); @@ -765,27 +765,27 @@ where /// The style of a [`ComboBox`]. #[allow(missing_debug_implementations)] -pub struct Style { +pub struct Style<'a, Theme> { /// The style of the [`TextInput`] of the [`ComboBox`]. - pub text_input: text_input::Style<'static, Theme>, + pub text_input: text_input::Style<'a, Theme>, /// The style of the [`Menu`] of the [`ComboBox`]. /// /// [`Menu`]: menu::Menu - pub menu: menu::Style, + pub menu: menu::Style<'a, Theme>, } /// The default style of a [`ComboBox`]. pub trait DefaultStyle: Sized { /// Returns the default style of a [`ComboBox`]. - fn default_style() -> Style; + fn default_style() -> Style<'static, Self>; } impl DefaultStyle for Theme { - fn default_style() -> Style { + fn default_style() -> Style<'static, Self> { Style { text_input: Box::new(text_input::default), - menu: menu::Style::DEFAULT, + menu: menu::DefaultStyle::default_style(), } } } diff --git a/widget/src/overlay/menu.rs b/widget/src/overlay/menu.rs index 746407c645..0364f980d4 100644 --- a/widget/src/overlay/menu.rs +++ b/widget/src/overlay/menu.rs @@ -38,7 +38,7 @@ pub struct Menu< text_line_height: text::LineHeight, text_shaping: text::Shaping, font: Option, - style: Style, + style: &'a Style<'a, Theme>, } impl<'a, T, Message, Theme, Renderer> Menu<'a, T, Message, Theme, Renderer> @@ -48,37 +48,15 @@ where Theme: 'a, Renderer: text::Renderer + 'a, { - /// Creates a new [`Menu`] with the given [`State`], a list of options, and - /// the message to produced when an option is selected. - pub fn new( - state: &'a mut State, - options: &'a [T], - hovered_option: &'a mut Option, - on_selected: impl FnMut(T) -> Message + 'a, - on_option_hovered: Option<&'a dyn Fn(T) -> Message>, - ) -> Self - where - Theme: DefaultStyle, - { - Self::with_style( - state, - options, - hovered_option, - on_selected, - on_option_hovered, - Theme::default_style(), - ) - } - /// Creates a new [`Menu`] with the given [`State`], a list of options, /// the message to produced when an option is selected, and its [`Style`]. - pub fn with_style( + pub fn new( state: &'a mut State, options: &'a [T], hovered_option: &'a mut Option, on_selected: impl FnMut(T) -> Message + 'a, on_option_hovered: Option<&'a dyn Fn(T) -> Message>, - style: Style, + style: &'a Style<'a, Theme>, ) -> Self { Menu { state, @@ -135,12 +113,6 @@ where self } - /// Sets the style of the [`Menu`]. - pub fn style(mut self, style: impl Into>) -> Self { - self.style = style.into(); - self - } - /// Turns the [`Menu`] into an overlay [`Element`] at the given target /// position. /// @@ -190,7 +162,7 @@ where container: Container<'a, Message, Theme, Renderer>, width: f32, target_height: f32, - style: Style, + style: &'a Style<'a, Theme>, } impl<'a, Message, Theme, Renderer> Overlay<'a, Message, Theme, Renderer> @@ -234,10 +206,10 @@ where text_line_height, text_shaping, padding, - style: style.list, + style: &style.list, }, scrollable::Direction::default(), - style.scrollable, + &style.scrollable, ), container::transparent, ); @@ -356,7 +328,7 @@ where text_line_height: text::LineHeight, text_shaping: text::Shaping, font: Option, - style: fn(&Theme) -> Appearance, + style: &'a dyn Fn(&Theme) -> Appearance, } impl<'a, T, Message, Theme, Renderer> Widget @@ -599,39 +571,26 @@ pub struct Appearance { } /// The style of the different parts of a [`Menu`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style { +#[allow(missing_debug_implementations)] +pub struct Style<'a, Theme> { /// The style of the list of the [`Menu`]. - pub list: fn(&Theme) -> Appearance, + pub list: Box Appearance + 'a>, /// The style of the [`Scrollable`] of the [`Menu`]. - pub scrollable: fn(&Theme, scrollable::Status) -> scrollable::Appearance, + pub scrollable: scrollable::Style<'a, Theme>, } -impl Style { - /// The default style of a [`Menu`] with the built-in [`Theme`]. - pub const DEFAULT: Self = Self { - list: default, - scrollable: scrollable::default, - }; -} - -impl Clone for Style { - fn clone(&self) -> Self { - *self - } -} - -impl Copy for Style {} - /// The default style of a [`Menu`]. pub trait DefaultStyle: Sized { /// Returns the default style of a [`Menu`]. - fn default_style() -> Style; + fn default_style() -> Style<'static, Self>; } impl DefaultStyle for Theme { - fn default_style() -> Style { - Style::::DEFAULT + fn default_style() -> Style<'static, Self> { + Style { + list: Box::new(default), + scrollable: Box::new(scrollable::default), + } } } diff --git a/widget/src/pick_list.rs b/widget/src/pick_list.rs index beb4e0c1f2..52d5439705 100644 --- a/widget/src/pick_list.rs +++ b/widget/src/pick_list.rs @@ -47,7 +47,7 @@ pub struct PickList< text_shaping: text::Shaping, font: Option, handle: Handle, - style: Style, + style: Style<'a, Theme>, } impl<'a, T, L, V, Message, Theme, Renderer> @@ -151,7 +151,7 @@ where } /// Sets the style of the [`PickList`]. - pub fn style(mut self, style: impl Into>) -> Self { + pub fn style(mut self, style: impl Into>) -> Self { self.style = style.into(); self } @@ -529,7 +529,7 @@ where let on_select = &self.on_select; - let mut menu = Menu::with_style( + let mut menu = Menu::new( &mut state.menu, self.options.borrow(), &mut state.hovered_option, @@ -539,7 +539,7 @@ where (on_select)(option) }, None, - self.style.menu, + &self.style.menu, ) .width(bounds.width) .padding(self.padding) @@ -676,40 +676,27 @@ pub struct Appearance { } /// The styles of the different parts of a [`PickList`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style { +#[allow(missing_debug_implementations)] +pub struct Style<'a, Theme> { /// The style of the [`PickList`] itself. - pub field: fn(&Theme, Status) -> Appearance, + pub field: Box Appearance + 'a>, /// The style of the [`Menu`] of the pick list. - pub menu: menu::Style, + pub menu: menu::Style<'a, Theme>, } -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 - } -} - -impl Copy for Style {} - /// The default style of a [`PickList`]. pub trait DefaultStyle: Sized { /// Returns the default style of a [`PickList`]. - fn default_style() -> Style; + fn default_style() -> Style<'static, Self>; } impl DefaultStyle for Theme { - fn default_style() -> Style { - Style::::DEFAULT + fn default_style() -> Style<'static, Self> { + Style { + field: Box::new(default), + menu: menu::DefaultStyle::default_style(), + } } }