Reintroduce Box for style_sheet in Radio

This commit is contained in:
Héctor Ramón Jiménez 2021-10-31 17:14:10 +07:00
parent d758006ee9
commit bd7b086ec1
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
6 changed files with 29 additions and 14 deletions

View file

@ -76,7 +76,7 @@ impl Sandbox for ScrollableDemo {
Some(*theme),
Message::ThemeChanged,
)
.style(theme.clone().into()),
.style(*theme),
)
},
);

View file

@ -25,11 +25,11 @@ impl<'a> From<Theme> for Box<dyn container::StyleSheet + 'a> {
}
}
impl From<Theme> for &'static dyn radio::StyleSheet {
impl<'a> From<Theme> for Box<dyn radio::StyleSheet + 'a> {
fn from(theme: Theme) -> Self {
match theme {
Theme::Light => Default::default(),
Theme::Dark => &dark::Radio,
Theme::Dark => dark::Radio.into(),
}
}
}

View file

@ -64,7 +64,7 @@ impl Sandbox for Styling {
Some(self.theme),
Message::ThemeChanged,
)
.style(self.theme.into()),
.style(self.theme),
)
},
);
@ -185,11 +185,11 @@ mod style {
}
}
impl From<Theme> for &'static dyn radio::StyleSheet {
impl<'a> From<Theme> for Box<dyn radio::StyleSheet + 'a> {
fn from(theme: Theme) -> Self {
match theme {
Theme::Light => Default::default(),
Theme::Dark => &dark::Radio,
Theme::Dark => dark::Radio.into(),
}
}
}

View file

@ -53,7 +53,7 @@ pub struct Radio<'a, Message, Renderer: text::Renderer> {
text_size: Option<u16>,
text_color: Option<Color>,
font: Renderer::Font,
style_sheet: &'a dyn StyleSheet,
style_sheet: Box<dyn StyleSheet + 'a>,
}
impl<'a, Message, Renderer: text::Renderer> Radio<'a, Message, Renderer>
@ -135,8 +135,11 @@ where
}
/// Sets the style of the [`Radio`] button.
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<Box<dyn StyleSheet + 'a>>,
) -> Self {
self.style_sheet = style_sheet.into();
self
}
}

View file

@ -37,8 +37,17 @@ impl StyleSheet for Default {
}
}
impl std::default::Default for &'static dyn StyleSheet {
impl std::default::Default for Box<dyn StyleSheet> {
fn default() -> Self {
&Default
Box::new(Default)
}
}
impl<'a, T> From<T> for Box<dyn StyleSheet + 'a>
where
T: StyleSheet + 'a,
{
fn from(style_sheet: T) -> Self {
Box::new(style_sheet)
}
}

View file

@ -38,7 +38,7 @@ pub struct Radio<'a, Message> {
id: Option<String>,
name: Option<String>,
#[allow(dead_code)]
style_sheet: &'a dyn StyleSheet,
style_sheet: Box<dyn StyleSheet + 'a>,
}
impl<'a, Message> Radio<'a, Message> {
@ -71,8 +71,11 @@ impl<'a, Message> Radio<'a, Message> {
}
/// Sets the style of the [`Radio`] button.
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<Box<dyn StyleSheet + 'a>>,
) -> Self {
self.style_sheet = style_sheet.into();
self
}