use crate::virtual_slider::Status;
use iced_core::{Background, Border, Color, theme::palette};
#[derive(Debug, Clone)]
pub struct Style {
pub background: Option<Background>,
pub border: Border,
pub line_width: f32,
pub line_color: Color,
pub line_up_color: Option<Color>,
pub line_down_color: Option<Color>,
}
impl Style {
pub fn with_background(self, background: impl Into<Background>) -> Self {
Self {
background: Some(background.into()),
..self
}
}
}
impl Default for Style {
fn default() -> Self {
Style {
background: None,
border: Border::default(),
line_width: 2.0,
line_color: Color::BLACK,
line_up_color: None,
line_down_color: None,
}
}
}
pub trait Catalog: Sized {
type Class<'a>;
fn default<'a>() -> Self::Class<'a>;
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
}
pub type StyleFn<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Style + 'a>;
impl Catalog for iced_core::Theme {
type Class<'a> = StyleFn<'a, Self>;
fn default<'a>() -> Self::Class<'a> {
Box::new(default)
}
fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
class(self, status)
}
}
pub fn default(theme: &iced_core::Theme, status: Status) -> Style {
let palette = theme.extended_palette();
match status {
Status::Idle => styled(palette.background.neutral),
Status::Hovered | Status::Gesturing => styled(palette.background.stronger),
Status::Disabled => disabled(styled(palette.background.neutral)),
}
}
fn styled(pair: palette::Pair) -> Style {
Style {
background: Some(Background::Color(pair.color)),
line_color: pair.text,
..Style::default()
}
}
fn disabled(style: Style) -> Style {
Style {
background: style.background.map(|b| b.scale_alpha(0.5)),
border: Border {
color: style.border.color.scale_alpha(0.5),
..style.border
},
line_color: style.line_color.scale_alpha(0.5),
line_up_color: style.line_up_color.map(|c| c.scale_alpha(0.5)),
line_down_color: style.line_down_color.map(|c| c.scale_alpha(0.5)),
..style
}
}