Use closures for Svg::style
This commit is contained in:
parent
66dce4865e
commit
71b9b3c3b1
3 changed files with 31 additions and 25 deletions
|
|
@ -41,12 +41,12 @@ impl Sandbox for Tiger {
|
||||||
));
|
));
|
||||||
|
|
||||||
let svg = svg(handle).width(Length::Fill).height(Length::Fill).style(
|
let svg = svg(handle).width(Length::Fill).height(Length::Fill).style(
|
||||||
if self.apply_color_filter {
|
|_theme, _status| svg::Appearance {
|
||||||
|_theme, _status| svg::Appearance {
|
color: if self.apply_color_filter {
|
||||||
color: Some(color!(0x0000ff)),
|
Some(color!(0x0000ff))
|
||||||
}
|
} else {
|
||||||
} else {
|
None
|
||||||
|_theme, _status| svg::Appearance::default()
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -362,9 +362,11 @@ pub fn image<Handle>(handle: impl Into<Handle>) -> crate::Image<Handle> {
|
||||||
/// [`Svg`]: crate::Svg
|
/// [`Svg`]: crate::Svg
|
||||||
/// [`Handle`]: crate::svg::Handle
|
/// [`Handle`]: crate::svg::Handle
|
||||||
#[cfg(feature = "svg")]
|
#[cfg(feature = "svg")]
|
||||||
pub fn svg<Theme>(handle: impl Into<core::svg::Handle>) -> crate::Svg<Theme>
|
pub fn svg<'a, Theme>(
|
||||||
|
handle: impl Into<core::svg::Handle>,
|
||||||
|
) -> crate::Svg<'a, Theme>
|
||||||
where
|
where
|
||||||
Theme: crate::svg::DefaultStyle,
|
Theme: crate::svg::DefaultStyle + 'a,
|
||||||
{
|
{
|
||||||
crate::Svg::new(handle)
|
crate::Svg::new(handle)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,26 +20,26 @@ pub use crate::core::svg::Handle;
|
||||||
/// [`Svg`] images can have a considerable rendering cost when resized,
|
/// [`Svg`] images can have a considerable rendering cost when resized,
|
||||||
/// specially when they are complex.
|
/// specially when they are complex.
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
pub struct Svg<Theme = crate::Theme> {
|
pub struct Svg<'a, Theme = crate::Theme> {
|
||||||
handle: Handle,
|
handle: Handle,
|
||||||
width: Length,
|
width: Length,
|
||||||
height: Length,
|
height: Length,
|
||||||
content_fit: ContentFit,
|
content_fit: ContentFit,
|
||||||
style: Style<Theme>,
|
style: Style<'a, Theme>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Theme> Svg<Theme> {
|
impl<'a, Theme> Svg<'a, Theme> {
|
||||||
/// Creates a new [`Svg`] from the given [`Handle`].
|
/// Creates a new [`Svg`] from the given [`Handle`].
|
||||||
pub fn new(handle: impl Into<Handle>) -> Self
|
pub fn new(handle: impl Into<Handle>) -> Self
|
||||||
where
|
where
|
||||||
Theme: DefaultStyle,
|
Theme: DefaultStyle + 'a,
|
||||||
{
|
{
|
||||||
Svg {
|
Svg {
|
||||||
handle: handle.into(),
|
handle: handle.into(),
|
||||||
width: Length::Fill,
|
width: Length::Fill,
|
||||||
height: Length::Shrink,
|
height: Length::Shrink,
|
||||||
content_fit: ContentFit::Contain,
|
content_fit: ContentFit::Contain,
|
||||||
style: Theme::default_style(),
|
style: Box::new(Theme::default_style),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -48,7 +48,7 @@ impl<Theme> Svg<Theme> {
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn from_path(path: impl Into<PathBuf>) -> Self
|
pub fn from_path(path: impl Into<PathBuf>) -> Self
|
||||||
where
|
where
|
||||||
Theme: DefaultStyle,
|
Theme: DefaultStyle + 'a,
|
||||||
{
|
{
|
||||||
Self::new(Handle::from_path(path))
|
Self::new(Handle::from_path(path))
|
||||||
}
|
}
|
||||||
|
|
@ -80,13 +80,17 @@ impl<Theme> Svg<Theme> {
|
||||||
|
|
||||||
/// Sets the style variant of this [`Svg`].
|
/// Sets the style variant of this [`Svg`].
|
||||||
#[must_use]
|
#[must_use]
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer> for Svg<Theme>
|
impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
|
||||||
|
for Svg<'a, Theme>
|
||||||
where
|
where
|
||||||
Renderer: svg::Renderer,
|
Renderer: svg::Renderer,
|
||||||
{
|
{
|
||||||
|
|
@ -182,13 +186,13 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Message, Theme, Renderer> From<Svg<Theme>>
|
impl<'a, Message, Theme, Renderer> From<Svg<'a, Theme>>
|
||||||
for Element<'a, Message, Theme, Renderer>
|
for Element<'a, Message, Theme, Renderer>
|
||||||
where
|
where
|
||||||
Theme: 'a,
|
Theme: 'a,
|
||||||
Renderer: svg::Renderer + 'a,
|
Renderer: svg::Renderer + 'a,
|
||||||
{
|
{
|
||||||
fn from(icon: Svg<Theme>) -> Element<'a, Message, Theme, Renderer> {
|
fn from(icon: Svg<'a, Theme>) -> Element<'a, Message, Theme, Renderer> {
|
||||||
Element::new(icon)
|
Element::new(icon)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -214,22 +218,22 @@ pub struct Appearance {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The style of an [`Svg`].
|
/// The style of an [`Svg`].
|
||||||
pub type Style<Theme> = fn(&Theme, Status) -> Appearance;
|
pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>;
|
||||||
|
|
||||||
/// The default style of an [`Svg`].
|
/// The default style of an [`Svg`].
|
||||||
pub trait DefaultStyle {
|
pub trait DefaultStyle {
|
||||||
/// Returns the default style of an [`Svg`].
|
/// Returns the default style of an [`Svg`].
|
||||||
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 {
|
||||||
|_theme, _status| Appearance::default()
|
Appearance::default()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DefaultStyle for Appearance {
|
impl DefaultStyle for Appearance {
|
||||||
fn default_style() -> Style<Self> {
|
fn default_style(&self, _status: Status) -> Appearance {
|
||||||
|appearance, _status| *appearance
|
*self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue