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:
parent
c878e24465
commit
b85c504d72
11 changed files with 65 additions and 63 deletions
|
|
@ -277,7 +277,7 @@ impl Window {
|
||||||
.spacing(16),
|
.spacing(16),
|
||||||
)
|
)
|
||||||
.padding([20, 24])
|
.padding([20, 24])
|
||||||
.style(theme::Container::Custom(list::column::style)),
|
.style(theme::Container::custom(list::column::style)),
|
||||||
)
|
)
|
||||||
.padding(0)
|
.padding(0)
|
||||||
.style(theme::Button::Transparent)
|
.style(theme::Button::Transparent)
|
||||||
|
|
|
||||||
|
|
@ -19,16 +19,18 @@ pub use cosmic_panel_config;
|
||||||
|
|
||||||
const APPLET_PADDING: u32 = 8;
|
const APPLET_PADDING: u32 = 8;
|
||||||
|
|
||||||
pub const APPLET_BUTTON_THEME: Button = Button::Custom {
|
pub fn applet_button_theme() -> Button {
|
||||||
active: |t| iced_style::button::Appearance {
|
Button::Custom {
|
||||||
border_radius: BorderRadius::from(0.0),
|
active: Box::new(|t| iced_style::button::Appearance {
|
||||||
..t.active(&Button::Text)
|
border_radius: BorderRadius::from(0.0),
|
||||||
},
|
..t.active(&Button::Text)
|
||||||
hover: |t| iced_style::button::Appearance {
|
}),
|
||||||
border_radius: BorderRadius::from(0.0),
|
hover: Box::new(|t| iced_style::button::Appearance {
|
||||||
..t.hovered(&Button::Text)
|
border_radius: BorderRadius::from(0.0),
|
||||||
},
|
..t.hovered(&Button::Text)
|
||||||
};
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct CosmicAppletHelper {
|
pub struct CosmicAppletHelper {
|
||||||
|
|
@ -130,7 +132,7 @@ impl CosmicAppletHelper {
|
||||||
};
|
};
|
||||||
|
|
||||||
Container::<Message, Renderer>::new(Container::<Message, Renderer>::new(content).style(
|
Container::<Message, Renderer>::new(Container::<Message, Renderer>::new(content).style(
|
||||||
crate::theme::Container::Custom(|theme| Appearance {
|
crate::theme::Container::custom(|theme| Appearance {
|
||||||
text_color: Some(theme.cosmic().background.on.into()),
|
text_color: Some(theme.cosmic().background.on.into()),
|
||||||
background: Some(Color::from(theme.cosmic().background.base).into()),
|
background: Some(Color::from(theme.cosmic().background.base).into()),
|
||||||
border_radius: 12.0,
|
border_radius: 12.0,
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ impl std::default::Default for Appearance {
|
||||||
|
|
||||||
/// A set of rules that dictate the [`Appearance`] of a container.
|
/// A set of rules that dictate the [`Appearance`] of a container.
|
||||||
pub trait StyleSheet {
|
pub trait StyleSheet {
|
||||||
type Style: Default + Copy;
|
type Style: Default;
|
||||||
|
|
||||||
/// Produces the [`Appearance`] of a container.
|
/// Produces the [`Appearance`] of a container.
|
||||||
fn appearance(&self, style: Self::Style) -> Appearance;
|
fn appearance(&self, style: Self::Style) -> Appearance;
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ mod segmented_button;
|
||||||
|
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::hash::Hasher;
|
use std::hash::Hasher;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
pub use self::segmented_button::SegmentedButton;
|
pub use self::segmented_button::SegmentedButton;
|
||||||
|
|
||||||
|
|
@ -132,15 +133,16 @@ impl LayeredTheme for Theme {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Default)]
|
||||||
pub enum Application {
|
pub enum Application {
|
||||||
|
#[default]
|
||||||
Default,
|
Default,
|
||||||
Custom(fn(&Theme) -> application::Appearance),
|
Custom(Box<dyn Fn(&Theme) -> application::Appearance>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Application {
|
impl Application {
|
||||||
fn default() -> Self {
|
pub fn custom<F: Fn(&Theme) -> application::Appearance + 'static>(f: F) -> Self {
|
||||||
Self::Default
|
Self::Custom(Box::new(f))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -163,7 +165,6 @@ impl application::StyleSheet for Theme {
|
||||||
/*
|
/*
|
||||||
* TODO: Button
|
* TODO: Button
|
||||||
*/
|
*/
|
||||||
#[derive(Clone, Copy)]
|
|
||||||
pub enum Button {
|
pub enum Button {
|
||||||
Deactivated,
|
Deactivated,
|
||||||
Destructive,
|
Destructive,
|
||||||
|
|
@ -175,8 +176,8 @@ pub enum Button {
|
||||||
LinkActive,
|
LinkActive,
|
||||||
Transparent,
|
Transparent,
|
||||||
Custom {
|
Custom {
|
||||||
active: fn(&Theme) -> button::Appearance,
|
active: Box<dyn Fn(&Theme) -> button::Appearance>,
|
||||||
hover: 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 {
|
pub enum Expander {
|
||||||
|
#[default]
|
||||||
Default,
|
Default,
|
||||||
Custom(fn(&Theme) -> expander::Appearance),
|
Custom(Box<dyn Fn(&Theme) -> expander::Appearance>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Expander {
|
impl Expander {
|
||||||
fn default() -> Self {
|
pub fn custom<F: Fn(&Theme) -> expander::Appearance + 'static>(f: F) -> Self {
|
||||||
Self::Default
|
Self::Custom(Box::new(f))
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<fn(&Theme) -> expander::Appearance> for Expander {
|
|
||||||
fn from(f: fn(&Theme) -> expander::Appearance) -> Self {
|
|
||||||
Self::Custom(f)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -471,24 +467,19 @@ impl expander::StyleSheet for Theme {
|
||||||
/*
|
/*
|
||||||
* TODO: Container
|
* TODO: Container
|
||||||
*/
|
*/
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Default)]
|
||||||
pub enum Container {
|
pub enum Container {
|
||||||
Background,
|
Background,
|
||||||
Primary,
|
Primary,
|
||||||
Secondary,
|
Secondary,
|
||||||
|
#[default]
|
||||||
Transparent,
|
Transparent,
|
||||||
Custom(fn(&Theme) -> container::Appearance),
|
Custom(Box<dyn Fn(&Theme) -> container::Appearance>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Container {
|
impl Container {
|
||||||
fn default() -> Self {
|
pub fn custom<F: Fn(&Theme) -> container::Appearance + 'static>(f: F) -> Self {
|
||||||
Self::Transparent
|
Self::Custom(Box::new(f))
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<fn(&Theme) -> container::Appearance> for Container {
|
|
||||||
fn from(_: fn(&Theme) -> container::Appearance) -> Self {
|
|
||||||
Self::default()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -754,17 +745,18 @@ impl pane_grid::StyleSheet for Theme {
|
||||||
/*
|
/*
|
||||||
* TODO: Progress Bar
|
* TODO: Progress Bar
|
||||||
*/
|
*/
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Default)]
|
||||||
pub enum ProgressBar {
|
pub enum ProgressBar {
|
||||||
|
#[default]
|
||||||
Primary,
|
Primary,
|
||||||
Success,
|
Success,
|
||||||
Danger,
|
Danger,
|
||||||
Custom(fn(&Theme) -> progress_bar::Appearance),
|
Custom(Box<dyn Fn(&Theme) -> progress_bar::Appearance>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for ProgressBar {
|
impl ProgressBar {
|
||||||
fn default() -> Self {
|
pub fn custom<F: Fn(&Theme) -> progress_bar::Appearance + 'static>(f: F) -> Self {
|
||||||
Self::Primary
|
Self::Custom(Box::new(f))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -798,17 +790,18 @@ impl progress_bar::StyleSheet for Theme {
|
||||||
/*
|
/*
|
||||||
* TODO: Rule
|
* TODO: Rule
|
||||||
*/
|
*/
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Default)]
|
||||||
pub enum Rule {
|
pub enum Rule {
|
||||||
|
#[default]
|
||||||
Default,
|
Default,
|
||||||
LightDivider,
|
LightDivider,
|
||||||
HeavyDivider,
|
HeavyDivider,
|
||||||
Custom(fn(&Theme) -> rule::Appearance),
|
Custom(Box<dyn Fn(&Theme) -> rule::Appearance>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Rule {
|
impl Rule {
|
||||||
fn default() -> Self {
|
pub fn custom<F: Fn(&Theme) -> rule::Appearance + 'static>(f: F) -> Self {
|
||||||
Self::Default
|
Self::Custom(Box::new(f))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -883,10 +876,10 @@ impl scrollable::StyleSheet for Theme {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Clone, Copy)]
|
#[derive(Clone, Default)]
|
||||||
pub enum Svg {
|
pub enum Svg {
|
||||||
/// Apply a custom appearance filter
|
/// Apply a custom appearance filter
|
||||||
Custom(fn(&Theme) -> svg::Appearance),
|
Custom(Rc<dyn Fn(&Theme) -> svg::Appearance>),
|
||||||
/// No filtering is applied
|
/// No filtering is applied
|
||||||
#[default]
|
#[default]
|
||||||
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 {
|
impl svg::StyleSheet for Theme {
|
||||||
type Style = Svg;
|
type Style = Svg;
|
||||||
|
|
||||||
|
|
@ -948,6 +947,7 @@ pub enum Text {
|
||||||
#[default]
|
#[default]
|
||||||
Default,
|
Default,
|
||||||
Color(Color),
|
Color(Color),
|
||||||
|
// TODO: Can't use dyn Fn since this must be copy
|
||||||
Custom(fn(&Theme) -> text::Appearance),
|
Custom(fn(&Theme) -> text::Appearance),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ use crate::{theme::Theme, widget::segmented_button::ItemStatusAppearance};
|
||||||
use iced_core::{Background, BorderRadius};
|
use iced_core::{Background, BorderRadius};
|
||||||
use palette::{rgb::Rgb, Alpha};
|
use palette::{rgb::Rgb, Alpha};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Default)]
|
#[derive(Default)]
|
||||||
pub enum SegmentedButton {
|
pub enum SegmentedButton {
|
||||||
/// A tabbed widget for switching between views in an interface.
|
/// A tabbed widget for switching between views in an interface.
|
||||||
#[default]
|
#[default]
|
||||||
|
|
@ -14,7 +14,7 @@ pub enum SegmentedButton {
|
||||||
/// A widget for multiple choice selection.
|
/// A widget for multiple choice selection.
|
||||||
Selection,
|
Selection,
|
||||||
/// Or implement any custom theme of your liking.
|
/// Or implement any custom theme of your liking.
|
||||||
Custom(fn(&Theme) -> Appearance),
|
Custom(Box<dyn Fn(&Theme) -> Appearance>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StyleSheet for Theme {
|
impl StyleSheet for Theme {
|
||||||
|
|
|
||||||
|
|
@ -207,7 +207,7 @@ impl<'a> Icon<'a> {
|
||||||
|
|
||||||
fn svg_element<Message: 'static>(&self, handle: svg::Handle) -> Element<'static, Message> {
|
fn svg_element<Message: 'static>(&self, handle: svg::Handle) -> Element<'static, Message> {
|
||||||
svg::Svg::<Renderer>::new(handle)
|
svg::Svg::<Renderer>::new(handle)
|
||||||
.style(self.style)
|
.style(self.style.clone())
|
||||||
.width(self.width.unwrap_or(Length::Units(self.size)))
|
.width(self.width.unwrap_or(Length::Units(self.size)))
|
||||||
.height(self.height.unwrap_or(Length::Units(self.size)))
|
.height(self.height.unwrap_or(Length::Units(self.size)))
|
||||||
.content_fit(self.content_fit)
|
.content_fit(self.content_fit)
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ impl<'a, Message: 'static> ListColumn<'a, Message> {
|
||||||
.spacing(12)
|
.spacing(12)
|
||||||
.apply(iced::widget::container)
|
.apply(iced::widget::container)
|
||||||
.padding([16, 6])
|
.padding([16, 6])
|
||||||
.style(theme::Container::Custom(style))
|
.style(theme::Container::custom(style))
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ where
|
||||||
.apply(container)
|
.apply(container)
|
||||||
.height(Length::Fill)
|
.height(Length::Fill)
|
||||||
.padding(11)
|
.padding(11)
|
||||||
.style(theme::Container::Custom(nav_bar_style))
|
.style(theme::Container::custom(nav_bar_style))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ impl<'a, Message: 'static + Clone> Field<'a, Message> {
|
||||||
.spacing(8)
|
.spacing(8)
|
||||||
.align_items(iced::Alignment::Center)
|
.align_items(iced::Alignment::Center)
|
||||||
.apply(container)
|
.apply(container)
|
||||||
.style(crate::theme::Container::Custom(active_style))
|
.style(crate::theme::Container::custom(active_style))
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ impl<'a, Message: 'static> SpinButton<'a, Message> {
|
||||||
.align_y(Vertical::Center)
|
.align_y(Vertical::Center)
|
||||||
.width(Length::Units(95))
|
.width(Length::Units(95))
|
||||||
.height(Length::Units(32))
|
.height(Length::Units(32))
|
||||||
.style(theme::Container::Custom(container_style))
|
.style(theme::Container::custom(container_style))
|
||||||
.apply(Element::from)
|
.apply(Element::from)
|
||||||
.map(on_change)
|
.map(on_change)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ impl<'a, Message: 'static + Clone> Warning<'a, Message> {
|
||||||
])
|
])
|
||||||
.align_items(Alignment::Center),
|
.align_items(Alignment::Center),
|
||||||
)
|
)
|
||||||
.style(theme::Container::Custom(warning_container))
|
.style(theme::Container::custom(warning_container))
|
||||||
.padding(10)
|
.padding(10)
|
||||||
.align_y(alignment::Vertical::Center)
|
.align_y(alignment::Vertical::Center)
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue