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:
parent
34317bba5d
commit
2088e5d661
5 changed files with 43 additions and 39 deletions
|
|
@ -36,7 +36,7 @@ pub struct Container<
|
||||||
vertical_alignment: alignment::Vertical,
|
vertical_alignment: alignment::Vertical,
|
||||||
clip: bool,
|
clip: bool,
|
||||||
content: Element<'a, Message, Theme, Renderer>,
|
content: Element<'a, Message, Theme, Renderer>,
|
||||||
style: Style<Theme>,
|
style: Style<'a, Theme>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message, Theme, Renderer> Container<'a, Message, Theme, Renderer>
|
impl<'a, Message, Theme, Renderer> Container<'a, Message, Theme, Renderer>
|
||||||
|
|
@ -48,15 +48,15 @@ where
|
||||||
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
||||||
) -> Self
|
) -> Self
|
||||||
where
|
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.
|
/// Creates a [`Container`] with the given content and style.
|
||||||
pub fn with_style(
|
pub fn with_style(
|
||||||
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
||||||
style: fn(&Theme, Status) -> Appearance,
|
style: impl Fn(&Theme, Status) -> Appearance + 'a,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let content = content.into();
|
let content = content.into();
|
||||||
let size = content.as_widget().size_hint();
|
let size = content.as_widget().size_hint();
|
||||||
|
|
@ -71,8 +71,8 @@ where
|
||||||
horizontal_alignment: alignment::Horizontal::Left,
|
horizontal_alignment: alignment::Horizontal::Left,
|
||||||
vertical_alignment: alignment::Vertical::Top,
|
vertical_alignment: alignment::Vertical::Top,
|
||||||
clip: false,
|
clip: false,
|
||||||
|
style: Box::new(style),
|
||||||
content,
|
content,
|
||||||
style,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -137,8 +137,11 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the style of the [`Container`].
|
/// Sets the style of the [`Container`].
|
||||||
pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self {
|
pub fn style(
|
||||||
self.style = style;
|
mut self,
|
||||||
|
style: impl Fn(&Theme, Status) -> Appearance + 'a,
|
||||||
|
) -> Self {
|
||||||
|
self.style = Box::new(style);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -546,41 +549,41 @@ pub enum Status {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The style of a [`Container`].
|
/// 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`].
|
/// The default style of a [`Container`].
|
||||||
pub trait DefaultStyle {
|
pub trait DefaultStyle {
|
||||||
/// Returns the default style of a [`Container`].
|
/// Returns the default style of a [`Container`].
|
||||||
fn default_style() -> Style<Self>;
|
fn default_style(&self, status: Status) -> Appearance;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DefaultStyle for Theme {
|
impl DefaultStyle for Theme {
|
||||||
fn default_style() -> Style<Self> {
|
fn default_style(&self, status: Status) -> Appearance {
|
||||||
transparent
|
transparent(self, status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DefaultStyle for Appearance {
|
impl DefaultStyle for Appearance {
|
||||||
fn default_style() -> Style<Self> {
|
fn default_style(&self, _status: Status) -> Appearance {
|
||||||
|appearance, _status| *appearance
|
*self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DefaultStyle for Color {
|
impl DefaultStyle for Color {
|
||||||
fn default_style() -> Style<Self> {
|
fn default_style(&self, _status: Status) -> Appearance {
|
||||||
|color, _status| Appearance::default().with_background(*color)
|
Appearance::default().with_background(*self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DefaultStyle for Gradient {
|
impl DefaultStyle for Gradient {
|
||||||
fn default_style() -> Style<Self> {
|
fn default_style(&self, _status: Status) -> Appearance {
|
||||||
|gradient, _status| Appearance::default().with_background(*gradient)
|
Appearance::default().with_background(*self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DefaultStyle for gradient::Linear {
|
impl DefaultStyle for gradient::Linear {
|
||||||
fn default_style() -> Style<Self> {
|
fn default_style(&self, _status: Status) -> Appearance {
|
||||||
|gradient, _status| Appearance::default().with_background(*gradient)
|
Appearance::default().with_background(*self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ pub fn container<'a, Message, Theme, Renderer>(
|
||||||
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
||||||
) -> Container<'a, Message, Theme, Renderer>
|
) -> Container<'a, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Theme: container::DefaultStyle,
|
Theme: container::DefaultStyle + 'a,
|
||||||
Renderer: core::Renderer,
|
Renderer: core::Renderer,
|
||||||
{
|
{
|
||||||
Container::new(content)
|
Container::new(content)
|
||||||
|
|
@ -134,7 +134,7 @@ pub fn tooltip<'a, Message, Theme, Renderer>(
|
||||||
position: tooltip::Position,
|
position: tooltip::Position,
|
||||||
) -> crate::Tooltip<'a, Message, Theme, Renderer>
|
) -> crate::Tooltip<'a, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Theme: container::DefaultStyle,
|
Theme: container::DefaultStyle + 'a,
|
||||||
Renderer: core::text::Renderer,
|
Renderer: core::text::Renderer,
|
||||||
{
|
{
|
||||||
Tooltip::new(content, tooltip, position)
|
Tooltip::new(content, tooltip, position)
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@ pub struct Content<
|
||||||
{
|
{
|
||||||
title_bar: Option<TitleBar<'a, Message, Theme, Renderer>>,
|
title_bar: Option<TitleBar<'a, Message, Theme, Renderer>>,
|
||||||
body: Element<'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>
|
impl<'a, Message, Theme, Renderer> Content<'a, Message, Theme, Renderer>
|
||||||
|
|
@ -34,12 +34,12 @@ where
|
||||||
/// Creates a new [`Content`] with the provided body.
|
/// Creates a new [`Content`] with the provided body.
|
||||||
pub fn new(body: impl Into<Element<'a, Message, Theme, Renderer>>) -> Self
|
pub fn new(body: impl Into<Element<'a, Message, Theme, Renderer>>) -> Self
|
||||||
where
|
where
|
||||||
Theme: container::DefaultStyle,
|
Theme: container::DefaultStyle + 'a,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
title_bar: None,
|
title_bar: None,
|
||||||
body: body.into(),
|
body: body.into(),
|
||||||
style: Theme::default_style(),
|
style: Box::new(Theme::default_style),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -55,9 +55,9 @@ where
|
||||||
/// Sets the style of the [`Content`].
|
/// Sets the style of the [`Content`].
|
||||||
pub fn style(
|
pub fn style(
|
||||||
mut self,
|
mut self,
|
||||||
style: fn(&Theme, container::Status) -> container::Appearance,
|
style: impl Fn(&Theme, container::Status) -> container::Appearance + 'a,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.style = style.into();
|
self.style = Box::new(style);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -403,7 +403,7 @@ impl<'a, T, Message, Theme, Renderer> From<T>
|
||||||
for Content<'a, Message, Theme, Renderer>
|
for Content<'a, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
T: Into<Element<'a, Message, Theme, Renderer>>,
|
T: Into<Element<'a, Message, Theme, Renderer>>,
|
||||||
Theme: container::DefaultStyle,
|
Theme: container::DefaultStyle + 'a,
|
||||||
Renderer: crate::core::Renderer,
|
Renderer: crate::core::Renderer,
|
||||||
{
|
{
|
||||||
fn from(element: T) -> Self {
|
fn from(element: T) -> Self {
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ pub struct TitleBar<
|
||||||
controls: Option<Element<'a, Message, Theme, Renderer>>,
|
controls: Option<Element<'a, Message, Theme, Renderer>>,
|
||||||
padding: Padding,
|
padding: Padding,
|
||||||
always_show_controls: bool,
|
always_show_controls: bool,
|
||||||
style: container::Style<Theme>,
|
style: container::Style<'a, Theme>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message, Theme, Renderer> TitleBar<'a, Message, Theme, Renderer>
|
impl<'a, Message, Theme, Renderer> TitleBar<'a, Message, Theme, Renderer>
|
||||||
|
|
@ -37,14 +37,14 @@ where
|
||||||
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
||||||
) -> Self
|
) -> Self
|
||||||
where
|
where
|
||||||
Theme: container::DefaultStyle,
|
Theme: container::DefaultStyle + 'a,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
content: content.into(),
|
content: content.into(),
|
||||||
controls: None,
|
controls: None,
|
||||||
padding: Padding::ZERO,
|
padding: Padding::ZERO,
|
||||||
always_show_controls: false,
|
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`].
|
/// Sets the style of the [`TitleBar`].
|
||||||
pub fn style(
|
pub fn style(
|
||||||
mut self,
|
mut self,
|
||||||
style: fn(&Theme, container::Status) -> container::Appearance,
|
style: impl Fn(&Theme, container::Status) -> container::Appearance + 'a,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.style = style.into();
|
self.style = Box::new(style);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ pub struct Tooltip<
|
||||||
gap: f32,
|
gap: f32,
|
||||||
padding: f32,
|
padding: f32,
|
||||||
snap_within_viewport: bool,
|
snap_within_viewport: bool,
|
||||||
style: container::Style<Theme>,
|
style: container::Style<'a, Theme>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message, Theme, Renderer> Tooltip<'a, Message, Theme, Renderer>
|
impl<'a, Message, Theme, Renderer> Tooltip<'a, Message, Theme, Renderer>
|
||||||
|
|
@ -47,7 +47,7 @@ where
|
||||||
position: Position,
|
position: Position,
|
||||||
) -> Self
|
) -> Self
|
||||||
where
|
where
|
||||||
Theme: container::DefaultStyle,
|
Theme: container::DefaultStyle + 'a,
|
||||||
{
|
{
|
||||||
Tooltip {
|
Tooltip {
|
||||||
content: content.into(),
|
content: content.into(),
|
||||||
|
|
@ -56,7 +56,7 @@ where
|
||||||
gap: 0.0,
|
gap: 0.0,
|
||||||
padding: Self::DEFAULT_PADDING,
|
padding: Self::DEFAULT_PADDING,
|
||||||
snap_within_viewport: true,
|
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`].
|
/// Sets the style of the [`Tooltip`].
|
||||||
pub fn style(
|
pub fn style(
|
||||||
mut self,
|
mut self,
|
||||||
style: fn(&Theme, container::Status) -> container::Appearance,
|
style: impl Fn(&Theme, container::Status) -> container::Appearance + 'a,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
self.style = style.into();
|
self.style = Box::new(style);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -239,7 +239,7 @@ where
|
||||||
positioning: self.position,
|
positioning: self.position,
|
||||||
gap: self.gap,
|
gap: self.gap,
|
||||||
padding: self.padding,
|
padding: self.padding,
|
||||||
style: self.style,
|
style: &self.style,
|
||||||
})))
|
})))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
@ -309,7 +309,8 @@ where
|
||||||
positioning: Position,
|
positioning: Position,
|
||||||
gap: f32,
|
gap: f32,
|
||||||
padding: f32,
|
padding: f32,
|
||||||
style: container::Style<Theme>,
|
style:
|
||||||
|
&'b (dyn Fn(&Theme, container::Status) -> container::Appearance + 'a),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'b, Message, Theme, Renderer>
|
impl<'a, 'b, Message, Theme, Renderer>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue