Use closures for PickList::style

This commit is contained in:
Héctor Ramón Jiménez 2024-03-12 16:40:56 +01:00
parent 3e190b9ee0
commit b721fd935c
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
3 changed files with 42 additions and 96 deletions

View file

@ -42,7 +42,7 @@ pub struct ComboBox<
on_option_hovered: Option<Box<dyn Fn(T) -> Message>>, on_option_hovered: Option<Box<dyn Fn(T) -> Message>>,
on_close: Option<Message>, on_close: Option<Message>,
on_input: Option<Box<dyn Fn(String) -> Message>>, on_input: Option<Box<dyn Fn(String) -> Message>>,
menu_style: menu::Style<Theme>, menu_style: menu::Style<'a, Theme>,
padding: Padding, padding: Padding,
size: Option<f32>, size: Option<f32>,
} }
@ -125,7 +125,7 @@ where
} }
/// Sets the style of the [`ComboBox`]. /// Sets the style of the [`ComboBox`].
pub fn style(mut self, style: impl Into<Style<Theme>>) -> Self pub fn style(mut self, style: impl Into<Style<'a, Theme>>) -> Self
where where
Theme: 'a, Theme: 'a,
{ {
@ -672,7 +672,7 @@ where
self.state.sync_filtered_options(filtered_options); self.state.sync_filtered_options(filtered_options);
let mut menu = menu::Menu::with_style( let mut menu = menu::Menu::new(
menu, menu,
&filtered_options.options, &filtered_options.options,
hovered_option, hovered_option,
@ -686,7 +686,7 @@ where
(self.on_selected)(x) (self.on_selected)(x)
}, },
self.on_option_hovered.as_deref(), self.on_option_hovered.as_deref(),
self.menu_style, &self.menu_style,
) )
.width(bounds.width) .width(bounds.width)
.padding(self.padding); .padding(self.padding);
@ -765,27 +765,27 @@ where
/// The style of a [`ComboBox`]. /// The style of a [`ComboBox`].
#[allow(missing_debug_implementations)] #[allow(missing_debug_implementations)]
pub struct Style<Theme> { pub struct Style<'a, Theme> {
/// The style of the [`TextInput`] of the [`ComboBox`]. /// 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`]. /// The style of the [`Menu`] of the [`ComboBox`].
/// ///
/// [`Menu`]: menu::Menu /// [`Menu`]: menu::Menu
pub menu: menu::Style<Theme>, pub menu: menu::Style<'a, Theme>,
} }
/// The default style of a [`ComboBox`]. /// The default style of a [`ComboBox`].
pub trait DefaultStyle: Sized { pub trait DefaultStyle: Sized {
/// Returns the default style of a [`ComboBox`]. /// Returns the default style of a [`ComboBox`].
fn default_style() -> Style<Self>; fn default_style() -> Style<'static, Self>;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style() -> Style<'static, Self> {
Style { Style {
text_input: Box::new(text_input::default), text_input: Box::new(text_input::default),
menu: menu::Style::DEFAULT, menu: menu::DefaultStyle::default_style(),
} }
} }
} }

View file

@ -38,7 +38,7 @@ pub struct Menu<
text_line_height: text::LineHeight, text_line_height: text::LineHeight,
text_shaping: text::Shaping, text_shaping: text::Shaping,
font: Option<Renderer::Font>, font: Option<Renderer::Font>,
style: Style<Theme>, style: &'a Style<'a, Theme>,
} }
impl<'a, T, Message, Theme, Renderer> Menu<'a, T, Message, Theme, Renderer> impl<'a, T, Message, Theme, Renderer> Menu<'a, T, Message, Theme, Renderer>
@ -48,37 +48,15 @@ where
Theme: 'a, Theme: 'a,
Renderer: text::Renderer + 'a, Renderer: text::Renderer + 'a,
{ {
/// Creates a new [`Menu`] with the given [`State`], a list of options, and /// Creates a new [`Menu`] with the given [`State`], a list of options,
/// the message to produced when an option is selected. /// the message to produced when an option is selected, and its [`Style`].
pub fn new( pub fn new(
state: &'a mut State, state: &'a mut State,
options: &'a [T], options: &'a [T],
hovered_option: &'a mut Option<usize>, hovered_option: &'a mut Option<usize>,
on_selected: impl FnMut(T) -> Message + 'a, on_selected: impl FnMut(T) -> Message + 'a,
on_option_hovered: Option<&'a dyn Fn(T) -> Message>, on_option_hovered: Option<&'a dyn Fn(T) -> Message>,
) -> Self style: &'a Style<'a, Theme>,
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(
state: &'a mut State,
options: &'a [T],
hovered_option: &'a mut Option<usize>,
on_selected: impl FnMut(T) -> Message + 'a,
on_option_hovered: Option<&'a dyn Fn(T) -> Message>,
style: Style<Theme>,
) -> Self { ) -> Self {
Menu { Menu {
state, state,
@ -135,12 +113,6 @@ where
self self
} }
/// Sets the style of the [`Menu`].
pub fn style(mut self, style: impl Into<Style<Theme>>) -> Self {
self.style = style.into();
self
}
/// Turns the [`Menu`] into an overlay [`Element`] at the given target /// Turns the [`Menu`] into an overlay [`Element`] at the given target
/// position. /// position.
/// ///
@ -190,7 +162,7 @@ where
container: Container<'a, Message, Theme, Renderer>, container: Container<'a, Message, Theme, Renderer>,
width: f32, width: f32,
target_height: f32, target_height: f32,
style: Style<Theme>, style: &'a Style<'a, Theme>,
} }
impl<'a, Message, Theme, Renderer> Overlay<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Overlay<'a, Message, Theme, Renderer>
@ -234,10 +206,10 @@ where
text_line_height, text_line_height,
text_shaping, text_shaping,
padding, padding,
style: style.list, style: &style.list,
}, },
scrollable::Direction::default(), scrollable::Direction::default(),
style.scrollable, &style.scrollable,
), ),
container::transparent, container::transparent,
); );
@ -356,7 +328,7 @@ where
text_line_height: text::LineHeight, text_line_height: text::LineHeight,
text_shaping: text::Shaping, text_shaping: text::Shaping,
font: Option<Renderer::Font>, font: Option<Renderer::Font>,
style: fn(&Theme) -> Appearance, style: &'a dyn Fn(&Theme) -> Appearance,
} }
impl<'a, T, Message, Theme, Renderer> Widget<Message, Theme, Renderer> impl<'a, T, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
@ -599,39 +571,26 @@ pub struct Appearance {
} }
/// The style of the different parts of a [`Menu`]. /// The style of the different parts of a [`Menu`].
#[derive(Debug, PartialEq, Eq)] #[allow(missing_debug_implementations)]
pub struct Style<Theme> { pub struct Style<'a, Theme> {
/// The style of the list of the [`Menu`]. /// The style of the list of the [`Menu`].
pub list: fn(&Theme) -> Appearance, pub list: Box<dyn Fn(&Theme) -> Appearance + 'a>,
/// The style of the [`Scrollable`] of the [`Menu`]. /// The style of the [`Scrollable`] of the [`Menu`].
pub scrollable: fn(&Theme, scrollable::Status) -> scrollable::Appearance, pub scrollable: scrollable::Style<'a, Theme>,
} }
impl Style<Theme> {
/// The default style of a [`Menu`] with the built-in [`Theme`].
pub const DEFAULT: Self = Self {
list: default,
scrollable: scrollable::default,
};
}
impl<Theme> Clone for Style<Theme> {
fn clone(&self) -> Self {
*self
}
}
impl<Theme> Copy for Style<Theme> {}
/// The default style of a [`Menu`]. /// The default style of a [`Menu`].
pub trait DefaultStyle: Sized { pub trait DefaultStyle: Sized {
/// Returns the default style of a [`Menu`]. /// Returns the default style of a [`Menu`].
fn default_style() -> Style<Self>; fn default_style() -> Style<'static, Self>;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style() -> Style<'static, Self> {
Style::<Theme>::DEFAULT Style {
list: Box::new(default),
scrollable: Box::new(scrollable::default),
}
} }
} }

View file

@ -47,7 +47,7 @@ pub struct PickList<
text_shaping: text::Shaping, text_shaping: text::Shaping,
font: Option<Renderer::Font>, font: Option<Renderer::Font>,
handle: Handle<Renderer::Font>, handle: Handle<Renderer::Font>,
style: Style<Theme>, style: Style<'a, Theme>,
} }
impl<'a, T, L, V, Message, Theme, Renderer> impl<'a, T, L, V, Message, Theme, Renderer>
@ -151,7 +151,7 @@ where
} }
/// Sets the style of the [`PickList`]. /// Sets the style of the [`PickList`].
pub fn style(mut self, style: impl Into<Style<Theme>>) -> Self { pub fn style(mut self, style: impl Into<Style<'a, Theme>>) -> Self {
self.style = style.into(); self.style = style.into();
self self
} }
@ -529,7 +529,7 @@ where
let on_select = &self.on_select; let on_select = &self.on_select;
let mut menu = Menu::with_style( let mut menu = Menu::new(
&mut state.menu, &mut state.menu,
self.options.borrow(), self.options.borrow(),
&mut state.hovered_option, &mut state.hovered_option,
@ -539,7 +539,7 @@ where
(on_select)(option) (on_select)(option)
}, },
None, None,
self.style.menu, &self.style.menu,
) )
.width(bounds.width) .width(bounds.width)
.padding(self.padding) .padding(self.padding)
@ -676,40 +676,27 @@ pub struct Appearance {
} }
/// The styles of the different parts of a [`PickList`]. /// The styles of the different parts of a [`PickList`].
#[derive(Debug, PartialEq, Eq)] #[allow(missing_debug_implementations)]
pub struct Style<Theme> { pub struct Style<'a, Theme> {
/// The style of the [`PickList`] itself. /// The style of the [`PickList`] itself.
pub field: fn(&Theme, Status) -> Appearance, pub field: Box<dyn Fn(&Theme, Status) -> Appearance + 'a>,
/// The style of the [`Menu`] of the pick list. /// The style of the [`Menu`] of the pick list.
pub menu: menu::Style<Theme>, pub menu: menu::Style<'a, Theme>,
} }
impl Style<Theme> {
/// The default style of a [`PickList`] with the built-in [`Theme`].
pub const DEFAULT: Self = Self {
field: default,
menu: menu::Style::<Theme>::DEFAULT,
};
}
impl<Theme> Clone for Style<Theme> {
fn clone(&self) -> Self {
*self
}
}
impl<Theme> Copy for Style<Theme> {}
/// The default style of a [`PickList`]. /// The default style of a [`PickList`].
pub trait DefaultStyle: Sized { pub trait DefaultStyle: Sized {
/// Returns the default style of a [`PickList`]. /// Returns the default style of a [`PickList`].
fn default_style() -> Style<Self>; fn default_style() -> Style<'static, Self>;
} }
impl DefaultStyle for Theme { impl DefaultStyle for Theme {
fn default_style() -> Style<Self> { fn default_style() -> Style<'static, Self> {
Style::<Self>::DEFAULT Style {
field: Box::new(default),
menu: menu::DefaultStyle::default_style(),
}
} }
} }