From eed19dcf81334d0849744f1918ba880d5a7acc1c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sun, 31 Oct 2021 17:39:24 +0700 Subject: Reintroduce `Box` for `style_sheet` in `Scrollable` --- examples/scrollable/src/main.rs | 2 +- examples/scrollable/src/style.rs | 4 ++-- examples/styling/src/main.rs | 6 +++--- native/src/widget/scrollable.rs | 9 ++++++--- style/src/scrollable.rs | 13 +++++++++++-- web/src/widget/scrollable.rs | 12 ++++++------ 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/examples/scrollable/src/main.rs b/examples/scrollable/src/main.rs index 272f0ce2..3416b83d 100644 --- a/examples/scrollable/src/main.rs +++ b/examples/scrollable/src/main.rs @@ -95,7 +95,7 @@ impl Sandbox for ScrollableDemo { .on_scroll(move |offset| { Message::Scrolled(i, offset) }) - .style(theme.clone().into()) + .style(*theme) .push(Text::new(variant.title)) .push( Button::new( diff --git a/examples/scrollable/src/style.rs b/examples/scrollable/src/style.rs index 3c8e5234..ec1f13db 100644 --- a/examples/scrollable/src/style.rs +++ b/examples/scrollable/src/style.rs @@ -34,11 +34,11 @@ impl<'a> From for Box { } } -impl From for &'static dyn scrollable::StyleSheet { +impl<'a> From for Box { fn from(theme: Theme) -> Self { match theme { Theme::Light => Default::default(), - Theme::Dark => &dark::Scrollable, + Theme::Dark => dark::Scrollable.into(), } } } diff --git a/examples/styling/src/main.rs b/examples/styling/src/main.rs index 73f965d9..a0ba2607 100644 --- a/examples/styling/src/main.rs +++ b/examples/styling/src/main.rs @@ -98,7 +98,7 @@ impl Sandbox for Styling { let scrollable = Scrollable::new(&mut self.scroll) .width(Length::Fill) .height(Length::Units(100)) - .style(self.theme.into()) + .style(self.theme) .push(Text::new("Scroll me!")) .push(Space::with_height(Length::Units(800))) .push(Text::new("You did it!")); @@ -212,11 +212,11 @@ mod style { } } - impl From for &'static dyn scrollable::StyleSheet { + impl<'a> From for Box { fn from(theme: Theme) -> Self { match theme { Theme::Light => Default::default(), - Theme::Dark => &dark::Scrollable, + Theme::Dark => dark::Scrollable.into(), } } } diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index 781fe73e..05e9b347 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -27,7 +27,7 @@ pub struct Scrollable<'a, Message, Renderer> { scroller_width: u16, content: Column<'a, Message, Renderer>, on_scroll: Option Message>>, - style_sheet: &'a dyn StyleSheet, + style_sheet: Box, } impl<'a, Message, Renderer: crate::Renderer> Scrollable<'a, Message, Renderer> { @@ -123,8 +123,11 @@ impl<'a, Message, Renderer: crate::Renderer> Scrollable<'a, Message, Renderer> { } /// Sets the style of the [`Scrollable`] . - pub fn style(mut self, style_sheet: &'a dyn StyleSheet) -> Self { - self.style_sheet = style_sheet; + pub fn style( + mut self, + style_sheet: impl Into>, + ) -> Self { + self.style_sheet = style_sheet.into(); self } diff --git a/style/src/scrollable.rs b/style/src/scrollable.rs index 741d9d39..9de72add 100644 --- a/style/src/scrollable.rs +++ b/style/src/scrollable.rs @@ -60,8 +60,17 @@ impl StyleSheet for Default { } } -impl std::default::Default for &'static dyn StyleSheet { +impl std::default::Default for Box { fn default() -> Self { - &Default + Box::new(Default) + } +} + +impl<'a, T> From for Box +where + T: StyleSheet + 'a, +{ + fn from(style_sheet: T) -> Self { + Box::new(style_sheet) } } diff --git a/web/src/widget/scrollable.rs b/web/src/widget/scrollable.rs index 595eb26e..22cb61be 100644 --- a/web/src/widget/scrollable.rs +++ b/web/src/widget/scrollable.rs @@ -14,7 +14,7 @@ pub struct Scrollable<'a, Message> { max_height: u32, content: Column<'a, Message>, #[allow(dead_code)] - style_sheet: &'a dyn StyleSheet, + style_sheet: Box, } impl<'a, Message> Scrollable<'a, Message> { @@ -78,11 +78,11 @@ impl<'a, Message> Scrollable<'a, Message> { } /// Sets the style of the [`Scrollable`] . - pub fn style<'b>(mut self, style_sheet: &'b dyn StyleSheet) -> Self - where - 'b: 'a, - { - self.style_sheet = style_sheet; + pub fn style( + mut self, + style_sheet: impl Into>, + ) -> Self { + self.style_sheet = style_sheet.into(); self } -- cgit