//! Use the built-in theme and styles. pub mod palette; use self::palette::Extended; pub use self::palette::Palette; use crate::application; use crate::button; use crate::checkbox; use crate::container; use crate::core::widget::text; use crate::menu; use crate::pane_grid; use crate::pick_list; use crate::progress_bar; use crate::radio; use crate::rule; use crate::scrollable; use crate::slider; use crate::svg; use crate::text_input; use crate::toggler; use iced_core::{Background, Color, Vector}; use std::rc::Rc; /// A built-in theme. #[derive(Debug, Clone, PartialEq, Default)] pub enum Theme { /// The built-in light variant. #[default] Light, /// The built-in dark variant. Dark, /// A [`Theme`] that uses a [`Custom`] palette. Custom(Box), } impl Theme { /// Creates a new custom [`Theme`] from the given [`Palette`]. pub fn custom(palette: Palette) -> Self { Self::Custom(Box::new(Custom::new(palette))) } /// Returns the [`Palette`] of the [`Theme`]. pub fn palette(&self) -> Palette { match self { Self::Light => Palette::LIGHT, Self::Dark => Palette::DARK, Self::Custom(custom) => custom.palette, } } /// Returns the [`palette::Extended`] of the [`Theme`]. pub fn extended_palette(&self) -> &palette::Extended { match self { Self::Light => &palette::EXTENDED_LIGHT, Self::Dark => &palette::EXTENDED_DARK, Self::Custom(custom) => &custom.extended, } } } /// A [`Theme`] with a customized [`Palette`]. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Custom { palette: Palette, extended: Extended, } impl Custom { /// Creates a [`Custom`] theme from the given [`Palette`]. pub fn new(palette: Palette) -> Self { Self { palette, extended: Extended::generate(palette), } } } /// The style of an application. #[derive(Default)] pub enum Application { /// The default style. #[default] Default, /// A custom style. Custom(Box>), } impl application::StyleSheet for Theme { type Style = Application; fn appearance(&self, style: &Self::Style) -> application::Appearance { let palette = self.extended_palette(); match style { Application::Default => application::Appearance { background_color: palette.background.base.color, text_color: palette.background.base.text, }, Application::Custom(custom) => custom.appearance(self), } } } impl application::Appearance> application::StyleSheet for T { type Style = Theme; fn appearance(&self, style: &Self::Style) -> application::Appearance { (self)(style) } } impl application::Appearance + 'static> From for Application { fn from(f: T) -> Self { Self::Custom(Box::new(f)) } } /// The style of a button. #[derive(Default)] pub enum Button { /// The primary style. #[default] Primary, /// The secondary style. Secondary, /// The positive style. Positive, /// The destructive style. Destructive, /// The text style. /// /// Useful for links! Text, /// A custom style. Custom(Box>), } impl Button { /// Creates a custom [`Button`] style variant. pub fn custom( style_sheet: impl button::StyleSheet