theme: Use boxed functions instead fn pointers (#99)

This is more general, and necessary if the custom theming is dynamically
generated.

Iced's builtin theme also uses `Box`ed `Fn`, or `Rc` where clone is
required, so this seems reasonable.

Only `Text` is left using `fn`, since it needs to be `Copy`. Hopefully
that can be changed in Iced at some point.

`::custom` methods are added to make these variants a little more
convenient to construct. This replaces a couple `From` implementations,
which are potentially problematic with a generic.
This commit is contained in:
Ian Douglas Scott 2023-04-28 16:04:57 -07:00 committed by GitHub
parent c878e24465
commit b85c504d72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 65 additions and 63 deletions

View file

@ -52,7 +52,7 @@ impl std::default::Default for Appearance {
/// A set of rules that dictate the [`Appearance`] of a container.
pub trait StyleSheet {
type Style: Default + Copy;
type Style: Default;
/// Produces the [`Appearance`] of a container.
fn appearance(&self, style: Self::Style) -> Appearance;

View file

@ -6,6 +6,7 @@ mod segmented_button;
use std::hash::Hash;
use std::hash::Hasher;
use std::rc::Rc;
pub use self::segmented_button::SegmentedButton;
@ -132,15 +133,16 @@ impl LayeredTheme for Theme {
}
}
#[derive(Clone, Copy)]
#[derive(Default)]
pub enum Application {
#[default]
Default,
Custom(fn(&Theme) -> application::Appearance),
Custom(Box<dyn Fn(&Theme) -> application::Appearance>),
}
impl Default for Application {
fn default() -> Self {
Self::Default
impl Application {
pub fn custom<F: Fn(&Theme) -> application::Appearance + 'static>(f: F) -> Self {
Self::Custom(Box::new(f))
}
}
@ -163,7 +165,6 @@ impl application::StyleSheet for Theme {
/*
* TODO: Button
*/
#[derive(Clone, Copy)]
pub enum Button {
Deactivated,
Destructive,
@ -175,8 +176,8 @@ pub enum Button {
LinkActive,
Transparent,
Custom {
active: fn(&Theme) -> button::Appearance,
hover: fn(&Theme) -> button::Appearance,
active: Box<dyn Fn(&Theme) -> button::Appearance>,
hover: Box<dyn Fn(&Theme) -> button::Appearance>,
},
}
@ -439,21 +440,16 @@ impl checkbox::StyleSheet for Theme {
}
}
#[derive(Clone, Copy)]
#[derive(Default)]
pub enum Expander {
#[default]
Default,
Custom(fn(&Theme) -> expander::Appearance),
Custom(Box<dyn Fn(&Theme) -> expander::Appearance>),
}
impl Default for Expander {
fn default() -> Self {
Self::Default
}
}
impl From<fn(&Theme) -> expander::Appearance> for Expander {
fn from(f: fn(&Theme) -> expander::Appearance) -> Self {
Self::Custom(f)
impl Expander {
pub fn custom<F: Fn(&Theme) -> expander::Appearance + 'static>(f: F) -> Self {
Self::Custom(Box::new(f))
}
}
@ -471,24 +467,19 @@ impl expander::StyleSheet for Theme {
/*
* TODO: Container
*/
#[derive(Clone, Copy)]
#[derive(Default)]
pub enum Container {
Background,
Primary,
Secondary,
#[default]
Transparent,
Custom(fn(&Theme) -> container::Appearance),
Custom(Box<dyn Fn(&Theme) -> container::Appearance>),
}
impl Default for Container {
fn default() -> Self {
Self::Transparent
}
}
impl From<fn(&Theme) -> container::Appearance> for Container {
fn from(_: fn(&Theme) -> container::Appearance) -> Self {
Self::default()
impl Container {
pub fn custom<F: Fn(&Theme) -> container::Appearance + 'static>(f: F) -> Self {
Self::Custom(Box::new(f))
}
}
@ -754,17 +745,18 @@ impl pane_grid::StyleSheet for Theme {
/*
* TODO: Progress Bar
*/
#[derive(Clone, Copy)]
#[derive(Default)]
pub enum ProgressBar {
#[default]
Primary,
Success,
Danger,
Custom(fn(&Theme) -> progress_bar::Appearance),
Custom(Box<dyn Fn(&Theme) -> progress_bar::Appearance>),
}
impl Default for ProgressBar {
fn default() -> Self {
Self::Primary
impl ProgressBar {
pub fn custom<F: Fn(&Theme) -> progress_bar::Appearance + 'static>(f: F) -> Self {
Self::Custom(Box::new(f))
}
}
@ -798,17 +790,18 @@ impl progress_bar::StyleSheet for Theme {
/*
* TODO: Rule
*/
#[derive(Clone, Copy)]
#[derive(Default)]
pub enum Rule {
#[default]
Default,
LightDivider,
HeavyDivider,
Custom(fn(&Theme) -> rule::Appearance),
Custom(Box<dyn Fn(&Theme) -> rule::Appearance>),
}
impl Default for Rule {
fn default() -> Self {
Self::Default
impl Rule {
pub fn custom<F: Fn(&Theme) -> rule::Appearance + 'static>(f: F) -> Self {
Self::Custom(Box::new(f))
}
}
@ -883,10 +876,10 @@ impl scrollable::StyleSheet for Theme {
}
}
#[derive(Default, Clone, Copy)]
#[derive(Clone, Default)]
pub enum Svg {
/// Apply a custom appearance filter
Custom(fn(&Theme) -> svg::Appearance),
Custom(Rc<dyn Fn(&Theme) -> svg::Appearance>),
/// No filtering is applied
#[default]
Default,
@ -915,6 +908,12 @@ impl Hash for Svg {
}
}
impl Svg {
pub fn custom<F: Fn(&Theme) -> svg::Appearance + 'static>(f: F) -> Self {
Self::Custom(Rc::new(f))
}
}
impl svg::StyleSheet for Theme {
type Style = Svg;
@ -948,6 +947,7 @@ pub enum Text {
#[default]
Default,
Color(Color),
// TODO: Can't use dyn Fn since this must be copy
Custom(fn(&Theme) -> text::Appearance),
}

View file

@ -6,7 +6,7 @@ use crate::{theme::Theme, widget::segmented_button::ItemStatusAppearance};
use iced_core::{Background, BorderRadius};
use palette::{rgb::Rgb, Alpha};
#[derive(Clone, Copy, Default)]
#[derive(Default)]
pub enum SegmentedButton {
/// A tabbed widget for switching between views in an interface.
#[default]
@ -14,7 +14,7 @@ pub enum SegmentedButton {
/// A widget for multiple choice selection.
Selection,
/// Or implement any custom theme of your liking.
Custom(fn(&Theme) -> Appearance),
Custom(Box<dyn Fn(&Theme) -> Appearance>),
}
impl StyleSheet for Theme {