diff options
author | 2024-03-07 20:11:32 +0100 | |
---|---|---|
committer | 2024-03-07 20:11:32 +0100 | |
commit | 833538ee7f3a60a839304762dfc29b0881d19094 (patch) | |
tree | 7afbc69659c95f9cbec58c938f1939cca3290b04 /widget/src/container.rs | |
parent | 44f002f64a9d53040f09affe69bd92675e302e16 (diff) | |
download | iced-833538ee7f3a60a839304762dfc29b0881d19094.tar.gz iced-833538ee7f3a60a839304762dfc29b0881d19094.tar.bz2 iced-833538ee7f3a60a839304762dfc29b0881d19094.zip |
Leverage `DefaultStyle` traits instead of `Default`
Diffstat (limited to 'widget/src/container.rs')
-rw-r--r-- | widget/src/container.rs | 49 |
1 files changed, 16 insertions, 33 deletions
diff --git a/widget/src/container.rs b/widget/src/container.rs index 99d877fe..5e16312c 100644 --- a/widget/src/container.rs +++ b/widget/src/container.rs @@ -47,9 +47,9 @@ where content: impl Into<Element<'a, Message, Theme, Renderer>>, ) -> Self where - Style<Theme>: Default, + Theme: DefaultStyle, { - Self::with_style(content, Style::default().0) + Self::with_style(content, Theme::default_style()) } /// Creates a [`Container`] with the given content and style. @@ -71,7 +71,7 @@ where vertical_alignment: alignment::Vertical::Top, clip: false, content, - style: Style(style), + style, } } @@ -137,7 +137,7 @@ where /// Sets the style of the [`Container`]. pub fn style(mut self, style: fn(&Theme, Status) -> Appearance) -> Self { - self.style = Style(style); + self.style = style; self } @@ -275,7 +275,7 @@ where Status::Idle }; - let style = (self.style.0)(theme, status); + let style = (self.style)(theme, status); if let Some(clipped_viewport) = bounds.intersection(viewport) { draw_background(renderer, &style, bounds); @@ -546,40 +546,23 @@ pub enum Status { } /// The style of a [`Container`]. -#[derive(Debug, PartialEq, Eq)] -pub struct Style<Theme>(fn(&Theme, Status) -> Appearance); +pub type Style<Theme> = fn(&Theme, Status) -> Appearance; -impl<Theme> Style<Theme> { - /// Resolves the [`Style`] with the given `Theme` and [`Status`] to - /// produce an [`Appearance`]. - pub fn resolve(self, theme: &Theme, status: Status) -> Appearance { - (self.0)(theme, status) - } -} - -impl<Theme> Clone for Style<Theme> { - fn clone(&self) -> Self { - *self - } -} - -impl<Theme> Copy for Style<Theme> {} - -impl Default for Style<Theme> { - fn default() -> Self { - Style(transparent) - } +/// The default style of a [`Container`]. +pub trait DefaultStyle { + /// Returns the default style of a [`Container`]. + fn default_style() -> Style<Self>; } -impl Default for Style<Appearance> { - fn default() -> Self { - Style(|appearance, _status| *appearance) +impl DefaultStyle for Theme { + fn default_style() -> Style<Self> { + transparent } } -impl<Theme> From<fn(&Theme, Status) -> Appearance> for Style<Theme> { - fn from(f: fn(&Theme, Status) -> Appearance) -> Self { - Style(f) +impl DefaultStyle for Appearance { + fn default_style() -> Style<Self> { + |appearance, _status| *appearance } } |