Use closures for Button::style
This commit is contained in:
parent
2088e5d661
commit
60b5822b67
2 changed files with 17 additions and 14 deletions
|
|
@ -56,7 +56,7 @@ where
|
||||||
height: Length,
|
height: Length,
|
||||||
padding: Padding,
|
padding: Padding,
|
||||||
clip: bool,
|
clip: bool,
|
||||||
style: Style<Theme>,
|
style: Style<'a, Theme>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message, Theme, Renderer> Button<'a, Message, Theme, Renderer>
|
impl<'a, Message, Theme, Renderer> Button<'a, Message, Theme, Renderer>
|
||||||
|
|
@ -68,7 +68,7 @@ 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,
|
||||||
{
|
{
|
||||||
let content = content.into();
|
let content = content.into();
|
||||||
let size = content.as_widget().size_hint();
|
let size = content.as_widget().size_hint();
|
||||||
|
|
@ -80,7 +80,7 @@ where
|
||||||
height: size.height.fluid(),
|
height: size.height.fluid(),
|
||||||
padding: DEFAULT_PADDING,
|
padding: DEFAULT_PADDING,
|
||||||
clip: false,
|
clip: false,
|
||||||
style: Theme::default_style(),
|
style: Box::new(Theme::default_style),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -120,8 +120,11 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the style variant of this [`Button`].
|
/// Sets the style variant of this [`Button`].
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -439,29 +442,29 @@ impl std::default::Default for Appearance {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The style of a [`Button`].
|
/// The style of a [`Button`].
|
||||||
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 [`Button`].
|
/// The default style of a [`Button`].
|
||||||
pub trait DefaultStyle {
|
pub trait DefaultStyle {
|
||||||
/// Returns the default style of a [`Button`].
|
/// Returns the default style of a [`Button`].
|
||||||
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 {
|
||||||
primary
|
primary(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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -117,7 +117,7 @@ pub fn button<'a, Message, Theme, Renderer>(
|
||||||
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
content: impl Into<Element<'a, Message, Theme, Renderer>>,
|
||||||
) -> Button<'a, Message, Theme, Renderer>
|
) -> Button<'a, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Theme: button::DefaultStyle,
|
Theme: button::DefaultStyle + 'a,
|
||||||
Renderer: core::Renderer,
|
Renderer: core::Renderer,
|
||||||
{
|
{
|
||||||
Button::new(content)
|
Button::new(content)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue