Fine-tune Catalog approach for button, checkbox, and svg

This commit is contained in:
Héctor Ramón Jiménez 2024-03-24 02:08:20 +01:00
parent 999ad2d288
commit e657dc2ecd
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
9 changed files with 167 additions and 149 deletions

View file

@ -50,7 +50,7 @@ highlighter = ["iced_highlighter"]
# Enables experimental multi-window support. # Enables experimental multi-window support.
multi-window = ["iced_winit/multi-window"] multi-window = ["iced_winit/multi-window"]
# Enables the advanced module # Enables the advanced module
advanced = [] advanced = ["iced_widget/advanced"]
# Enables embedding Fira Sans as the default font on Wasm builds # Enables embedding Fira Sans as the default font on Wasm builds
fira-sans = ["iced_renderer/fira-sans"] fira-sans = ["iced_renderer/fira-sans"]
# Enables auto-detecting light/dark mode for the built-in theme # Enables auto-detecting light/dark mode for the built-in theme

View file

@ -1,33 +0,0 @@
//! Box closures with ease.
//!
//! These are just a bunch of types that wrap boxed closures with
//! blanket [`From`] implementations for easy conversions.
//!
//! Mainly, it allows functions to take `Into<T>` where `T` may end
//! up being a boxed closure.
/// A boxed closure that takes `A` by reference and produces `O`.
#[allow(missing_debug_implementations)]
pub struct Unary<'a, A, O>(pub Box<dyn Fn(&A) -> O + 'a>);
impl<'a, A, O, T> From<T> for Unary<'a, A, O>
where
T: Fn(&A) -> O + 'a,
{
fn from(f: T) -> Self {
Self(Box::new(f))
}
}
/// A boxed closure that takes `A` by reference and `B` by value and produces `O`.
#[allow(missing_debug_implementations)]
pub struct Binary<'a, A, B, O>(pub Box<dyn Fn(&A, B) -> O + 'a>);
impl<'a, A, B, O, T> From<T> for Binary<'a, A, B, O>
where
T: Fn(&A, B) -> O + 'a,
{
fn from(f: T) -> Self {
Self(Box::new(f))
}
}

View file

@ -19,7 +19,6 @@
pub mod alignment; pub mod alignment;
pub mod border; pub mod border;
pub mod clipboard; pub mod clipboard;
pub mod closure;
pub mod event; pub mod event;
pub mod font; pub mod font;
pub mod gradient; pub mod gradient;

View file

@ -31,7 +31,7 @@ impl Tiger {
)); ));
let svg = svg(handle).width(Length::Fill).height(Length::Fill).style( let svg = svg(handle).width(Length::Fill).height(Length::Fill).style(
|_theme, _status| svg::Appearance { |_theme, _status| svg::Style {
color: if self.apply_color_filter { color: if self.apply_color_filter {
Some(color!(0x0000ff)) Some(color!(0x0000ff))
} else { } else {

View file

@ -21,6 +21,7 @@ svg = ["iced_renderer/svg"]
canvas = ["iced_renderer/geometry"] canvas = ["iced_renderer/geometry"]
qr_code = ["canvas", "qrcode"] qr_code = ["canvas", "qrcode"]
wgpu = ["iced_renderer/wgpu"] wgpu = ["iced_renderer/wgpu"]
advanced = []
[dependencies] [dependencies]
iced_renderer.workspace = true iced_renderer.workspace = true

View file

@ -1,5 +1,4 @@
//! Allow your users to perform actions by pressing a button. //! Allow your users to perform actions by pressing a button.
use crate::core::closure;
use crate::core::event::{self, Event}; use crate::core::event::{self, Event};
use crate::core::layout; use crate::core::layout;
use crate::core::mouse; use crate::core::mouse;
@ -58,7 +57,7 @@ where
height: Length, height: Length,
padding: Padding, padding: Padding,
clip: bool, clip: bool,
style: Theme::Item<'a>, class: Theme::Class<'a>,
} }
impl<'a, Message, Theme, Renderer> Button<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Button<'a, Message, Theme, Renderer>
@ -80,7 +79,7 @@ where
height: size.height.fluid(), height: size.height.fluid(),
padding: DEFAULT_PADDING, padding: DEFAULT_PADDING,
clip: false, clip: false,
style: Theme::default(), class: Theme::default(),
} }
} }
@ -119,18 +118,30 @@ where
self self
} }
/// Sets the style variant of this [`Button`].
pub fn style(mut self, style: impl Into<Theme::Item<'a>>) -> Self {
self.style = style.into();
self
}
/// Sets whether the contents of the [`Button`] should be clipped on /// Sets whether the contents of the [`Button`] should be clipped on
/// overflow. /// overflow.
pub fn clip(mut self, clip: bool) -> Self { pub fn clip(mut self, clip: bool) -> Self {
self.clip = clip; self.clip = clip;
self self
} }
/// Sets the style of this [`Button`].
#[must_use]
pub fn style(mut self, style: impl Fn(&Theme, Status) -> Style + 'a) -> Self
where
Theme::Class<'a>: From<StyleFn<'a, Theme>>,
{
self.class = (Box::new(style) as StyleFn<'a, Theme>).into();
self
}
/// Sets the style class of this [`Button`].
#[cfg(feature = "advanced")]
#[must_use]
pub fn class(mut self, class: impl Into<Theme::Class<'a>>) -> Self {
self.class = class.into();
self
}
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
@ -302,19 +313,19 @@ where
Status::Active Status::Active
}; };
let styling = theme.style(&self.style, status); let style = theme.style(&self.class, status);
if styling.background.is_some() if style.background.is_some()
|| styling.border.width > 0.0 || style.border.width > 0.0
|| styling.shadow.color.a > 0.0 || style.shadow.color.a > 0.0
{ {
renderer.fill_quad( renderer.fill_quad(
renderer::Quad { renderer::Quad {
bounds, bounds,
border: styling.border, border: style.border,
shadow: styling.shadow, shadow: style.shadow,
}, },
styling style
.background .background
.unwrap_or(Background::Color(Color::TRANSPARENT)), .unwrap_or(Background::Color(Color::TRANSPARENT)),
); );
@ -331,7 +342,7 @@ where
renderer, renderer,
theme, theme,
&renderer::Style { &renderer::Style {
text_color: styling.text_color, text_color: style.text_color,
}, },
content_layout, content_layout,
cursor, cursor,
@ -405,7 +416,7 @@ pub enum Status {
Disabled, Disabled,
} }
/// The appearance of a button. /// The style of a button.
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub struct Style { pub struct Style {
/// The [`Background`] of the button. /// The [`Background`] of the button.
@ -419,7 +430,7 @@ pub struct Style {
} }
impl Style { impl Style {
/// Updates the [`Appearance`] with the given [`Background`]. /// Updates the [`Style`] with the given [`Background`].
pub fn with_background(self, background: impl Into<Background>) -> Self { pub fn with_background(self, background: impl Into<Background>) -> Self {
Self { Self {
background: Some(background.into()), background: Some(background.into()),
@ -428,7 +439,7 @@ impl Style {
} }
} }
impl std::default::Default for Style { impl Default for Style {
fn default() -> Self { fn default() -> Self {
Self { Self {
background: None, background: None,
@ -441,30 +452,30 @@ impl std::default::Default for Style {
/// The theme catalog of a [`Button`]. /// The theme catalog of a [`Button`].
pub trait Catalog: Sized { pub trait Catalog: Sized {
/// The item type of this [`Catalog`]. /// The item class of this [`Catalog`].
type Item<'a>; type Class<'a>;
/// The default item produced by this [`Catalog`]. /// The default class produced by this [`Catalog`].
fn default<'a>() -> Self::Item<'a>; fn default<'a>() -> Self::Class<'a>;
/// The [`Style`] of an item with the given status. /// The [`Style`] of a class with the given status.
fn style(&self, item: &Self::Item<'_>, status: Status) -> Style; fn style(&self, item: &Self::Class<'_>, status: Status) -> Style;
} }
/// The item of a button [`Catalog`] for the built-in [`Theme`]. /// A styling function for a [`Button`].
/// ///
/// This is just a boxed closure: `Fn(&Theme, Status) -> Style`. /// This is just a boxed closure: `Fn(&Theme, Status) -> Style`.
pub type Item<'a, Theme> = closure::Binary<'a, Theme, Status, Style>; pub type StyleFn<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Style + 'a>;
impl Catalog for Theme { impl Catalog for Theme {
type Item<'a> = Item<'a, Self>; type Class<'a> = StyleFn<'a, Self>;
fn default<'a>() -> Self::Item<'a> { fn default<'a>() -> Self::Class<'a> {
closure::Binary::from(primary) Box::new(primary)
} }
fn style(&self, item: &Self::Item<'_>, status: Status) -> Style { fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
(item.0)(self, status) class(self, status)
} }
} }
@ -556,12 +567,12 @@ fn styled(pair: palette::Pair) -> Style {
} }
} }
fn disabled(appearance: Style) -> Style { fn disabled(style: Style) -> Style {
Style { Style {
background: appearance background: style
.background .background
.map(|background| background.scale_alpha(0.5)), .map(|background| background.scale_alpha(0.5)),
text_color: appearance.text_color.scale_alpha(0.5), text_color: style.text_color.scale_alpha(0.5),
..appearance ..style
} }
} }

View file

@ -39,6 +39,7 @@ pub struct Checkbox<
Renderer = crate::Renderer, Renderer = crate::Renderer,
> where > where
Renderer: text::Renderer, Renderer: text::Renderer,
Theme: Catalog,
{ {
is_checked: bool, is_checked: bool,
on_toggle: Option<Box<dyn Fn(bool) -> Message + 'a>>, on_toggle: Option<Box<dyn Fn(bool) -> Message + 'a>>,
@ -51,12 +52,13 @@ pub struct Checkbox<
text_shaping: text::Shaping, text_shaping: text::Shaping,
font: Option<Renderer::Font>, font: Option<Renderer::Font>,
icon: Icon<Renderer::Font>, icon: Icon<Renderer::Font>,
style: Style<'a, Theme>, class: Theme::Class<'a>,
} }
impl<'a, Message, Theme, Renderer> Checkbox<'a, Message, Theme, Renderer> impl<'a, Message, Theme, Renderer> Checkbox<'a, Message, Theme, Renderer>
where where
Renderer: text::Renderer, Renderer: text::Renderer,
Theme: Catalog,
{ {
/// The default size of a [`Checkbox`]. /// The default size of a [`Checkbox`].
const DEFAULT_SIZE: f32 = 16.0; const DEFAULT_SIZE: f32 = 16.0;
@ -69,10 +71,7 @@ where
/// It expects: /// It expects:
/// * the label of the [`Checkbox`] /// * the label of the [`Checkbox`]
/// * a boolean describing whether the [`Checkbox`] is checked or not /// * a boolean describing whether the [`Checkbox`] is checked or not
pub fn new(label: impl Into<String>, is_checked: bool) -> Self pub fn new(label: impl Into<String>, is_checked: bool) -> Self {
where
Theme: DefaultStyle + 'a,
{
Checkbox { Checkbox {
is_checked, is_checked,
on_toggle: None, on_toggle: None,
@ -91,7 +90,7 @@ where
line_height: text::LineHeight::default(), line_height: text::LineHeight::default(),
shaping: text::Shaping::Basic, shaping: text::Shaping::Basic,
}, },
style: Box::new(Theme::default_style), class: Theme::default(),
} }
} }
@ -173,12 +172,21 @@ where
self self
} }
/// Sets the style of the [`Checkbox`]. /// Sets the style of this [`Checkbox`].
pub fn style( #[must_use]
mut self, pub fn style(mut self, style: impl Fn(&Theme, Status) -> Style + 'a) -> Self
style: impl Fn(&Theme, Status) -> Appearance + 'a, where
) -> Self { Theme::Class<'a>: From<StyleFn<'a, Theme>>,
self.style = Box::new(style); {
self.class = (Box::new(style) as StyleFn<'a, Theme>).into();
self
}
/// Sets the style class of this [`Checkbox`].
#[cfg(feature = "advanced")]
#[must_use]
pub fn class(mut self, class: impl Into<Theme::Class<'a>>) -> Self {
self.class = class.into();
self self
} }
} }
@ -187,6 +195,7 @@ impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for Checkbox<'a, Message, Theme, Renderer> for Checkbox<'a, Message, Theme, Renderer>
where where
Renderer: text::Renderer, Renderer: text::Renderer,
Theme: Catalog,
{ {
fn tag(&self) -> tree::Tag { fn tag(&self) -> tree::Tag {
tree::Tag::of::<widget::text::State<Renderer::Paragraph>>() tree::Tag::of::<widget::text::State<Renderer::Paragraph>>()
@ -285,7 +294,7 @@ where
tree: &Tree, tree: &Tree,
renderer: &mut Renderer, renderer: &mut Renderer,
theme: &Theme, theme: &Theme,
style: &renderer::Style, defaults: &renderer::Style,
layout: Layout<'_>, layout: Layout<'_>,
cursor: mouse::Cursor, cursor: mouse::Cursor,
viewport: &Rectangle, viewport: &Rectangle,
@ -304,7 +313,7 @@ where
Status::Active { is_checked } Status::Active { is_checked }
}; };
let appearance = (self.style)(theme, status); let style = theme.style(&self.class, status);
{ {
let layout = children.next().unwrap(); let layout = children.next().unwrap();
@ -313,10 +322,10 @@ where
renderer.fill_quad( renderer.fill_quad(
renderer::Quad { renderer::Quad {
bounds, bounds,
border: appearance.border, border: style.border,
..renderer::Quad::default() ..renderer::Quad::default()
}, },
appearance.background, style.background,
); );
let Icon { let Icon {
@ -341,7 +350,7 @@ where
shaping: *shaping, shaping: *shaping,
}, },
bounds.center(), bounds.center(),
appearance.icon_color, style.icon_color,
*viewport, *viewport,
); );
} }
@ -352,11 +361,11 @@ where
crate::text::draw( crate::text::draw(
renderer, renderer,
style, defaults,
label_layout, label_layout,
tree.state.downcast_ref(), tree.state.downcast_ref(),
crate::text::Appearance { crate::text::Appearance {
color: appearance.text_color, color: style.text_color,
}, },
viewport, viewport,
); );
@ -368,7 +377,7 @@ impl<'a, Message, Theme, Renderer> From<Checkbox<'a, Message, Theme, Renderer>>
for Element<'a, Message, Theme, Renderer> for Element<'a, Message, Theme, Renderer>
where where
Message: 'a, Message: 'a,
Theme: 'a, Theme: 'a + Catalog,
Renderer: 'a + text::Renderer, Renderer: 'a + text::Renderer,
{ {
fn from( fn from(
@ -413,9 +422,9 @@ pub enum Status {
}, },
} }
/// The appearance of a checkbox. /// The style of a checkbox.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
pub struct Appearance { pub struct Style {
/// The [`Background`] of the checkbox. /// The [`Background`] of the checkbox.
pub background: Background, pub background: Background,
/// The icon [`Color`] of the checkbox. /// The icon [`Color`] of the checkbox.
@ -426,29 +435,37 @@ pub struct Appearance {
pub text_color: Option<Color>, pub text_color: Option<Color>,
} }
/// The style of a [`Checkbox`]. /// The theme catalog of a [`Checkbox`].
pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>; pub trait Catalog: Sized {
/// The item class of this [`Catalog`].
type Class<'a>;
/// The default style of a [`Checkbox`]. /// The default class produced by this [`Catalog`].
pub trait DefaultStyle { fn default<'a>() -> Self::Class<'a>;
/// Returns the default style of a [`Checkbox`].
fn default_style(&self, status: Status) -> Appearance; /// The [`Style`] of a class with the given status.
fn style(&self, item: &Self::Class<'_>, status: Status) -> Style;
} }
impl DefaultStyle for Theme { /// A styling function for a [`Checkbox`].
fn default_style(&self, status: Status) -> Appearance { ///
primary(self, status) /// This is just a boxed closure: `Fn(&Theme, Status) -> Style`.
pub type StyleFn<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Style + 'a>;
impl Catalog for Theme {
type Class<'a> = StyleFn<'a, Self>;
fn default<'a>() -> Self::Class<'a> {
Box::new(primary)
} }
}
impl DefaultStyle for Appearance { fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
fn default_style(&self, _status: Status) -> Appearance { class(self, status)
*self
} }
} }
/// A primary checkbox; denoting a main toggle. /// A primary checkbox; denoting a main toggle.
pub fn primary(theme: &Theme, status: Status) -> Appearance { pub fn primary(theme: &Theme, status: Status) -> Style {
let palette = theme.extended_palette(); let palette = theme.extended_palette();
match status { match status {
@ -474,7 +491,7 @@ pub fn primary(theme: &Theme, status: Status) -> Appearance {
} }
/// A secondary checkbox; denoting a complementary toggle. /// A secondary checkbox; denoting a complementary toggle.
pub fn secondary(theme: &Theme, status: Status) -> Appearance { pub fn secondary(theme: &Theme, status: Status) -> Style {
let palette = theme.extended_palette(); let palette = theme.extended_palette();
match status { match status {
@ -500,7 +517,7 @@ pub fn secondary(theme: &Theme, status: Status) -> Appearance {
} }
/// A success checkbox; denoting a positive toggle. /// A success checkbox; denoting a positive toggle.
pub fn success(theme: &Theme, status: Status) -> Appearance { pub fn success(theme: &Theme, status: Status) -> Style {
let palette = theme.extended_palette(); let palette = theme.extended_palette();
match status { match status {
@ -526,7 +543,7 @@ pub fn success(theme: &Theme, status: Status) -> Appearance {
} }
/// A danger checkbox; denoting a negaive toggle. /// A danger checkbox; denoting a negaive toggle.
pub fn danger(theme: &Theme, status: Status) -> Appearance { pub fn danger(theme: &Theme, status: Status) -> Style {
let palette = theme.extended_palette(); let palette = theme.extended_palette();
match status { match status {
@ -556,8 +573,8 @@ fn styled(
base: palette::Pair, base: palette::Pair,
accent: palette::Pair, accent: palette::Pair,
is_checked: bool, is_checked: bool,
) -> Appearance { ) -> Style {
Appearance { Style {
background: Background::Color(if is_checked { background: Background::Color(if is_checked {
accent.color accent.color
} else { } else {

View file

@ -161,7 +161,7 @@ pub fn checkbox<'a, Message, Theme, Renderer>(
is_checked: bool, is_checked: bool,
) -> Checkbox<'a, Message, Theme, Renderer> ) -> Checkbox<'a, Message, Theme, Renderer>
where where
Theme: checkbox::DefaultStyle + 'a, Theme: checkbox::Catalog + 'a,
Renderer: core::text::Renderer, Renderer: core::text::Renderer,
{ {
Checkbox::new(label, is_checked) Checkbox::new(label, is_checked)
@ -367,7 +367,7 @@ pub fn svg<'a, Theme>(
handle: impl Into<core::svg::Handle>, handle: impl Into<core::svg::Handle>,
) -> crate::Svg<'a, Theme> ) -> crate::Svg<'a, Theme>
where where
Theme: crate::svg::DefaultStyle + 'a, Theme: crate::svg::Catalog,
{ {
crate::Svg::new(handle) crate::Svg::new(handle)
} }

View file

@ -20,36 +20,36 @@ 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<'a, Theme = crate::Theme> { pub struct Svg<'a, Theme = crate::Theme>
where
Theme: Catalog,
{
handle: Handle, handle: Handle,
width: Length, width: Length,
height: Length, height: Length,
content_fit: ContentFit, content_fit: ContentFit,
style: Style<'a, Theme>, class: Theme::Class<'a>,
} }
impl<'a, Theme> Svg<'a, Theme> { impl<'a, Theme> Svg<'a, Theme>
where
Theme: Catalog,
{
/// 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
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: Box::new(Theme::default_style), class: Theme::default(),
} }
} }
/// Creates a new [`Svg`] that will display the contents of the file at the /// Creates a new [`Svg`] that will display the contents of the file at the
/// provided path. /// provided path.
#[must_use] #[must_use]
pub fn from_path(path: impl Into<PathBuf>) -> Self pub fn from_path(path: impl Into<PathBuf>) -> Self {
where
Theme: DefaultStyle + 'a,
{
Self::new(Handle::from_path(path)) Self::new(Handle::from_path(path))
} }
@ -78,13 +78,21 @@ impl<'a, Theme> Svg<'a, Theme> {
} }
} }
/// Sets the style variant of this [`Svg`]. /// Sets the style of this [`Svg`].
#[must_use] #[must_use]
pub fn style( pub fn style(mut self, style: impl Fn(&Theme, Status) -> Style + 'a) -> Self
mut self, where
style: impl Fn(&Theme, Status) -> Appearance + 'a, Theme::Class<'a>: From<StyleFn<'a, Theme>>,
) -> Self { {
self.style = Box::new(style); self.class = (Box::new(style) as StyleFn<'a, Theme>).into();
self
}
/// Sets the style class of this [`Svg`].
#[cfg(feature = "advanced")]
#[must_use]
pub fn class(mut self, class: impl Into<Theme::Class<'a>>) -> Self {
self.class = class.into();
self self
} }
} }
@ -93,6 +101,7 @@ impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer>
for Svg<'a, Theme> for Svg<'a, Theme>
where where
Renderer: svg::Renderer, Renderer: svg::Renderer,
Theme: Catalog,
{ {
fn size(&self) -> Size<Length> { fn size(&self) -> Size<Length> {
Size { Size {
@ -167,7 +176,7 @@ where
Status::Idle Status::Idle
}; };
let appearance = (self.style)(theme, status); let appearance = theme.style(&self.class, status);
renderer.draw( renderer.draw(
self.handle.clone(), self.handle.clone(),
@ -189,7 +198,7 @@ where
impl<'a, Message, Theme, Renderer> From<Svg<'a, 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: Catalog + 'a,
Renderer: svg::Renderer + 'a, Renderer: svg::Renderer + 'a,
{ {
fn from(icon: Svg<'a, Theme>) -> Element<'a, Message, Theme, Renderer> { fn from(icon: Svg<'a, Theme>) -> Element<'a, Message, Theme, Renderer> {
@ -208,7 +217,7 @@ pub enum Status {
/// The appearance of an [`Svg`]. /// The appearance of an [`Svg`].
#[derive(Debug, Clone, Copy, PartialEq, Default)] #[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Appearance { pub struct Style {
/// The [`Color`] filter of an [`Svg`]. /// The [`Color`] filter of an [`Svg`].
/// ///
/// Useful for coloring a symbolic icon. /// Useful for coloring a symbolic icon.
@ -217,23 +226,37 @@ pub struct Appearance {
pub color: Option<Color>, pub color: Option<Color>,
} }
/// The style of an [`Svg`]. /// The theme catalog of an [`Svg`].
pub type Style<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Appearance + 'a>; pub trait Catalog {
/// The item class of this [`Catalog`].
type Class<'a>;
/// The default style of an [`Svg`]. /// The default class produced by this [`Catalog`].
pub trait DefaultStyle { fn default<'a>() -> Self::Class<'a>;
/// Returns the default style of an [`Svg`].
fn default_style(&self, status: Status) -> Appearance; /// The [`Style`] of a class with the given status.
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
} }
impl DefaultStyle for Theme { impl Catalog for Theme {
fn default_style(&self, _status: Status) -> Appearance { type Class<'a> = StyleFn<'a, Self>;
Appearance::default()
fn default<'a>() -> Self::Class<'a> {
Box::new(|_theme, _status| Style::default())
}
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
class(self, status)
} }
} }
impl DefaultStyle for Appearance { /// A styling function for an [`Svg`].
fn default_style(&self, _status: Status) -> Appearance { ///
*self /// This is just a boxed closure: `Fn(&Theme, Status) -> Style`.
pub type StyleFn<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Style + 'a>;
impl<'a, Theme> From<Style> for StyleFn<'a, Theme> {
fn from(style: Style) -> Self {
Box::new(move |_theme, _status| style)
} }
} }