Try using closures for Container::style

`Box` should not allocate for zero-sized types; so
we should not be incurring much overhead. Just a
bit of indirection.
This commit is contained in:
Héctor Ramón Jiménez 2024-03-12 13:25:06 +01:00
parent 34317bba5d
commit 2088e5d661
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
5 changed files with 43 additions and 39 deletions

View file

@ -36,7 +36,7 @@ pub struct Container<
vertical_alignment: alignment::Vertical,
clip: bool,
content: Element<'a, Message, Theme, Renderer>,
style: Style<Theme>,
style: Style<'a, Theme>,
}
impl<'a, Message, Theme, Renderer> Container<'a, Message, Theme, Renderer>
@ -48,15 +48,15 @@ where
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Self
where
Theme: DefaultStyle,
Theme: DefaultStyle + 'a,
{
Self::with_style(content, Theme::default_style())
Self::with_style(content, Theme::default_style)
}
/// Creates a [`Container`] with the given content and style.
pub fn with_style(
content: impl Into<Element<'a, Message, Theme, Renderer>>,
style: fn(&Theme, Status) -> Appearance,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
let content = content.into();
let size = content.as_widget().size_hint();
@ -71,8 +71,8 @@ where
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
clip: false,
style: Box::new(style),
content,
style,
}
}
@ -137,8 +137,11 @@ where
}
/// Sets the style of the [`Container`].
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self {
self.style = style;
pub fn style(
mut self,
style: impl Fn(&Theme, Status) -> Appearance + 'a,
) -> Self {
self.style = Box::new(style);
self
}
@ -546,41 +549,41 @@ pub enum Status {
}
/// The style of a [`Container`].
pub type Style<Theme> = fn(&Theme, Status) -> Appearance;
pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>;
/// The default style of a [`Container`].
pub trait DefaultStyle {
/// Returns the default style of a [`Container`].
fn default_style() -> Style<Self>;
fn default_style(&self, status: Status) -> Appearance;
}
impl DefaultStyle for Theme {
fn default_style() -> Style<Self> {
transparent
fn default_style(&self, status: Status) -> Appearance {
transparent(self, status)
}
}
impl DefaultStyle for Appearance {
fn default_style() -> Style<Self> {
|appearance, _status| *appearance
fn default_style(&self, _status: Status) -> Appearance {
*self
}
}
impl DefaultStyle for Color {
fn default_style() -> Style<Self> {
|color, _status| Appearance::default().with_background(*color)
fn default_style(&self, _status: Status) -> Appearance {
Appearance::default().with_background(*self)
}
}
impl DefaultStyle for Gradient {
fn default_style() -> Style<Self> {
|gradient, _status| Appearance::default().with_background(*gradient)
fn default_style(&self, _status: Status) -> Appearance {
Appearance::default().with_background(*self)
}
}
impl DefaultStyle for gradient::Linear {
fn default_style() -> Style<Self> {
|gradient, _status| Appearance::default().with_background(*gradient)
fn default_style(&self, _status: Status) -> Appearance {
Appearance::default().with_background(*self)
}
}

View file

@ -58,7 +58,7 @@ pub fn container<'a, Message, Theme, Renderer>(
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Container<'a, Message, Theme, Renderer>
where
Theme: container::DefaultStyle,
Theme: container::DefaultStyle + 'a,
Renderer: core::Renderer,
{
Container::new(content)
@ -134,7 +134,7 @@ pub fn tooltip<'a, Message, Theme, Renderer>(
position: tooltip::Position,
) -> crate::Tooltip<'a, Message, Theme, Renderer>
where
Theme: container::DefaultStyle,
Theme: container::DefaultStyle + 'a,
Renderer: core::text::Renderer,
{
Tooltip::new(content, tooltip, position)

View file

@ -24,7 +24,7 @@ pub struct Content<
{
title_bar: Option<TitleBar<'a, Message, Theme, Renderer>>,
body: Element<'a, Message, Theme, Renderer>,
style: container::Style<Theme>,
style: container::Style<'a, Theme>,
}
impl<'a, Message, Theme, Renderer> Content<'a, Message, Theme, Renderer>
@ -34,12 +34,12 @@ where
/// Creates a new [`Content`] with the provided body.
pub fn new(body: impl Into<Element<'a, Message, Theme, Renderer>>) -> Self
where
Theme: container::DefaultStyle,
Theme: container::DefaultStyle + 'a,
{
Self {
title_bar: None,
body: body.into(),
style: Theme::default_style(),
style: Box::new(Theme::default_style),
}
}
@ -55,9 +55,9 @@ where
/// Sets the style of the [`Content`].
pub fn style(
mut self,
style: fn(&Theme, container::Status) -> container::Appearance,
style: impl Fn(&Theme, container::Status) -> container::Appearance + 'a,
) -> Self {
self.style = style.into();
self.style = Box::new(style);
self
}
}
@ -403,7 +403,7 @@ impl<'a, T, Message, Theme, Renderer> From<T>
for Content<'a, Message, Theme, Renderer>
where
T: Into<Element<'a, Message, Theme, Renderer>>,
Theme: container::DefaultStyle,
Theme: container::DefaultStyle + 'a,
Renderer: crate::core::Renderer,
{
fn from(element: T) -> Self {

View file

@ -25,7 +25,7 @@ pub struct TitleBar<
controls: Option<Element<'a, Message, Theme, Renderer>>,
padding: Padding,
always_show_controls: bool,
style: container::Style<Theme>,
style: container::Style<'a, Theme>,
}
impl<'a, Message, Theme, Renderer> TitleBar<'a, Message, Theme, Renderer>
@ -37,14 +37,14 @@ where
content: impl Into<Element<'a, Message, Theme, Renderer>>,
) -> Self
where
Theme: container::DefaultStyle,
Theme: container::DefaultStyle + 'a,
{
Self {
content: content.into(),
controls: None,
padding: Padding::ZERO,
always_show_controls: false,
style: Theme::default_style(),
style: Box::new(Theme::default_style),
}
}
@ -66,9 +66,9 @@ where
/// Sets the style of the [`TitleBar`].
pub fn style(
mut self,
style: fn(&Theme, container::Status) -> container::Appearance,
style: impl Fn(&Theme, container::Status) -> container::Appearance + 'a,
) -> Self {
self.style = style.into();
self.style = Box::new(style);
self
}

View file

@ -28,7 +28,7 @@ pub struct Tooltip<
gap: f32,
padding: f32,
snap_within_viewport: bool,
style: container::Style<Theme>,
style: container::Style<'a, Theme>,
}
impl<'a, Message, Theme, Renderer> Tooltip<'a, Message, Theme, Renderer>
@ -47,7 +47,7 @@ where
position: Position,
) -> Self
where
Theme: container::DefaultStyle,
Theme: container::DefaultStyle + 'a,
{
Tooltip {
content: content.into(),
@ -56,7 +56,7 @@ where
gap: 0.0,
padding: Self::DEFAULT_PADDING,
snap_within_viewport: true,
style: Theme::default_style(),
style: Box::new(Theme::default_style),
}
}
@ -81,9 +81,9 @@ where
/// Sets the style of the [`Tooltip`].
pub fn style(
mut self,
style: fn(&Theme, container::Status) -> container::Appearance,
style: impl Fn(&Theme, container::Status) -> container::Appearance + 'a,
) -> Self {
self.style = style.into();
self.style = Box::new(style);
self
}
}
@ -239,7 +239,7 @@ where
positioning: self.position,
gap: self.gap,
padding: self.padding,
style: self.style,
style: &self.style,
})))
} else {
None
@ -309,7 +309,8 @@ where
positioning: Position,
gap: f32,
padding: f32,
style: container::Style<Theme>,
style:
&'b (dyn Fn(&Theme, container::Status) -> container::Appearance + 'a),
}
impl<'a, 'b, Message, Theme, Renderer>