Try Style newtype instead of trait for Svg widget

This commit is contained in:
Héctor Ramón Jiménez 2024-03-06 17:26:07 +01:00
parent 597a41cea7
commit 8a63774b24
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 26 additions and 14 deletions

View file

@ -366,7 +366,7 @@ pub fn image<Handle>(handle: impl Into<Handle>) -> crate::Image<Handle> {
#[cfg(feature = "svg")] #[cfg(feature = "svg")]
pub fn svg<Theme>(handle: impl Into<core::svg::Handle>) -> crate::Svg<Theme> pub fn svg<Theme>(handle: impl Into<core::svg::Handle>) -> crate::Svg<Theme>
where where
Theme: crate::svg::Style, crate::svg::Style<Theme>: Default,
{ {
crate::Svg::new(handle) crate::Svg::new(handle)
} }

View file

@ -25,21 +25,21 @@ pub struct Svg<Theme = crate::Theme> {
width: Length, width: Length,
height: Length, height: Length,
content_fit: ContentFit, content_fit: ContentFit,
style: fn(&Theme, Status) -> Appearance, style: Style<Theme>,
} }
impl<Theme> Svg<Theme> { impl<Theme> Svg<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: Style, Style<Theme>: Default,
{ {
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::style(), style: Style::default(),
} }
} }
@ -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: Style, Style<Theme>: Default,
{ {
Self::new(Handle::from_path(path)) Self::new(Handle::from_path(path))
} }
@ -163,7 +163,7 @@ where
Status::Idle Status::Idle
}; };
let appearance = (self.style)(theme, status); let appearance = (self.style.0)(theme, status);
renderer.draw( renderer.draw(
self.handle.clone(), self.handle.clone(),
@ -213,14 +213,26 @@ pub struct Appearance {
pub color: Option<Color>, pub color: Option<Color>,
} }
/// The definiton of the default style of an [`Svg`]. /// The style of an [`Svg`].
pub trait Style { #[derive(Debug, PartialEq, Eq)]
/// Returns the default style of an [`Svg`]. pub struct Style<Theme>(fn(&Theme, Status) -> Appearance);
fn style() -> fn(&Self, Status) -> Appearance;
}
impl Style for Theme { impl<Theme> Clone for Style<Theme> {
fn style() -> fn(&Self, Status) -> Appearance { fn clone(&self) -> Self {
|_, _| Appearance::default() *self
}
}
impl<Theme> Copy for Style<Theme> {}
impl Default for Style<Theme> {
fn default() -> Self {
Style(|_, _| Appearance::default())
}
}
impl<Theme> From<fn(&Theme, Status) -> Appearance> for Style<Theme> {
fn from(f: fn(&Theme, Status) -> Appearance) -> Self {
Style(f)
} }
} }