diff options
author | 2024-03-08 14:00:28 +0100 | |
---|---|---|
committer | 2024-03-08 14:00:28 +0100 | |
commit | edf7d7ca7593f660f4b15f154257471c26df87de (patch) | |
tree | 7cee3cbfbeb2ae5145f1bf6087b61fce4cbed8c9 /core | |
parent | 2074757cdc65ec16eeb1c7a12a5ff3bb5ed00859 (diff) | |
parent | 8919f2593e39f76b273513e959fa6d5ffb78fde2 (diff) | |
download | iced-edf7d7ca7593f660f4b15f154257471c26df87de.tar.gz iced-edf7d7ca7593f660f4b15f154257471c26df87de.tar.bz2 iced-edf7d7ca7593f660f4b15f154257471c26df87de.zip |
Merge pull request #2312 from iced-rs/theming-reloaded
Theming reloaded
Diffstat (limited to '')
-rw-r--r-- | core/Cargo.toml | 5 | ||||
-rw-r--r-- | core/src/background.rs | 13 | ||||
-rw-r--r-- | core/src/border.rs | 35 | ||||
-rw-r--r-- | core/src/color.rs | 14 | ||||
-rw-r--r-- | core/src/gradient.rs | 22 | ||||
-rw-r--r-- | core/src/lib.rs | 2 | ||||
-rw-r--r-- | core/src/theme.rs | 221 | ||||
-rw-r--r-- | core/src/theme/palette.rs (renamed from style/src/theme/palette.rs) | 2 | ||||
-rw-r--r-- | core/src/widget/text.rs | 80 |
9 files changed, 343 insertions, 51 deletions
diff --git a/core/Cargo.toml b/core/Cargo.toml index 2360e822..c273fcb4 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -15,14 +15,13 @@ bitflags.workspace = true glam.workspace = true log.workspace = true num-traits.workspace = true +once_cell.workspace = true +palette.workspace = true smol_str.workspace = true thiserror.workspace = true web-time.workspace = true xxhash-rust.workspace = true -palette.workspace = true -palette.optional = true - [target.'cfg(windows)'.dependencies] raw-window-handle.workspace = true diff --git a/core/src/background.rs b/core/src/background.rs index 347c52c0..eb4b5021 100644 --- a/core/src/background.rs +++ b/core/src/background.rs @@ -11,6 +11,19 @@ pub enum Background { // TODO: Add image variant } +impl Background { + /// Scales the the alpha channel of the [`Background`] by the given + /// factor. + pub fn scale_alpha(self, factor: f32) -> Self { + match self { + Self::Color(color) => Self::Color(color.scale_alpha(factor)), + Self::Gradient(gradient) => { + Self::Gradient(gradient.scale_alpha(factor)) + } + } + } +} + impl From<Color> for Background { fn from(color: Color) -> Self { Background::Color(color) diff --git a/core/src/border.rs b/core/src/border.rs index 64262471..2df24988 100644 --- a/core/src/border.rs +++ b/core/src/border.rs @@ -1,5 +1,5 @@ //! Draw lines around containers. -use crate::Color; +use crate::{Color, Pixels}; /// A border. #[derive(Debug, Clone, Copy, PartialEq, Default)] @@ -15,11 +15,38 @@ pub struct Border { } impl Border { - /// Creates a new default [`Border`] with the given [`Radius`]. - pub fn with_radius(radius: impl Into<Radius>) -> Self { + /// Creates a new default rounded [`Border`] with the given [`Radius`]. + /// + /// ``` + /// # use iced_core::Border; + /// # + /// assert_eq!(Border::rounded(10), Border::default().with_radius(10)); + /// ``` + pub fn rounded(radius: impl Into<Radius>) -> Self { + Self::default().with_radius(radius) + } + + /// Updates the [`Color`] of the [`Border`]. + pub fn with_color(self, color: impl Into<Color>) -> Self { + Self { + color: color.into(), + ..self + } + } + + /// Updates the [`Radius`] of the [`Border`]. + pub fn with_radius(self, radius: impl Into<Radius>) -> Self { Self { radius: radius.into(), - ..Self::default() + ..self + } + } + + /// Updates the width of the [`Border`]. + pub fn with_width(self, width: impl Into<Pixels>) -> Self { + Self { + width: width.into().0, + ..self } } } diff --git a/core/src/color.rs b/core/src/color.rs index b8db322f..4e79defb 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -1,4 +1,3 @@ -#[cfg(feature = "palette")] use palette::rgb::{Srgb, Srgba}; /// A color in the `sRGB` color space. @@ -151,6 +150,14 @@ impl Color { pub fn inverse(self) -> Color { Color::new(1.0f32 - self.r, 1.0f32 - self.g, 1.0f32 - self.b, self.a) } + + /// Scales the alpha channel of the [`Color`] by the given factor. + pub fn scale_alpha(self, factor: f32) -> Color { + Self { + a: self.a * factor, + ..self + } + } } impl From<[f32; 3]> for Color { @@ -202,7 +209,6 @@ macro_rules! color { }}; } -#[cfg(feature = "palette")] /// Converts from palette's `Rgba` type to a [`Color`]. impl From<Srgba> for Color { fn from(rgba: Srgba) -> Self { @@ -210,7 +216,6 @@ impl From<Srgba> for Color { } } -#[cfg(feature = "palette")] /// Converts from [`Color`] to palette's `Rgba` type. impl From<Color> for Srgba { fn from(c: Color) -> Self { @@ -218,7 +223,6 @@ impl From<Color> for Srgba { } } -#[cfg(feature = "palette")] /// Converts from palette's `Rgb` type to a [`Color`]. impl From<Srgb> for Color { fn from(rgb: Srgb) -> Self { @@ -226,7 +230,6 @@ impl From<Srgb> for Color { } } -#[cfg(feature = "palette")] /// Converts from [`Color`] to palette's `Rgb` type. impl From<Color> for Srgb { fn from(c: Color) -> Self { @@ -234,7 +237,6 @@ impl From<Color> for Srgb { } } -#[cfg(feature = "palette")] #[cfg(test)] mod tests { use super::*; diff --git a/core/src/gradient.rs b/core/src/gradient.rs index 4711b044..ccae0bce 100644 --- a/core/src/gradient.rs +++ b/core/src/gradient.rs @@ -12,17 +12,13 @@ pub enum Gradient { } impl Gradient { - /// Adjust the opacity of the gradient by a multiplier applied to each color stop. - pub fn mul_alpha(mut self, alpha_multiplier: f32) -> Self { - match &mut self { + /// Scales the alpha channel of the [`Gradient`] by the given factor. + pub fn scale_alpha(self, factor: f32) -> Self { + match self { Gradient::Linear(linear) => { - for stop in linear.stops.iter_mut().flatten() { - stop.color.a *= alpha_multiplier; - } + Gradient::Linear(linear.scale_alpha(factor)) } } - - self } } @@ -100,4 +96,14 @@ impl Linear { self } + + /// Scales the alpha channel of the [`Linear`] gradient by the given + /// factor. + pub fn scale_alpha(mut self, factor: f32) -> Self { + for stop in self.stops.iter_mut().flatten() { + stop.color.a *= factor; + } + + self + } } diff --git a/core/src/lib.rs b/core/src/lib.rs index 002336ee..d076413e 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -30,6 +30,7 @@ pub mod overlay; pub mod renderer; pub mod svg; pub mod text; +pub mod theme; pub mod time; pub mod touch; pub mod widget; @@ -76,6 +77,7 @@ pub use shadow::Shadow; pub use shell::Shell; pub use size::Size; pub use text::Text; +pub use theme::Theme; pub use transformation::Transformation; pub use vector::Vector; pub use widget::Widget; diff --git a/core/src/theme.rs b/core/src/theme.rs new file mode 100644 index 00000000..21ba2a37 --- /dev/null +++ b/core/src/theme.rs @@ -0,0 +1,221 @@ +//! Use the built-in theme and styles. +pub mod palette; + +pub use palette::Palette; + +use std::fmt; +use std::sync::Arc; + +/// 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, + /// The built-in Dracula variant. + Dracula, + /// The built-in Nord variant. + Nord, + /// The built-in Solarized Light variant. + SolarizedLight, + /// The built-in Solarized Dark variant. + SolarizedDark, + /// The built-in Gruvbox Light variant. + GruvboxLight, + /// The built-in Gruvbox Dark variant. + GruvboxDark, + /// The built-in Catppuccin Latte variant. + CatppuccinLatte, + /// The built-in Catppuccin Frappé variant. + CatppuccinFrappe, + /// The built-in Catppuccin Macchiato variant. + CatppuccinMacchiato, + /// The built-in Catppuccin Mocha variant. + CatppuccinMocha, + /// The built-in Tokyo Night variant. + TokyoNight, + /// The built-in Tokyo Night Storm variant. + TokyoNightStorm, + /// The built-in Tokyo Night Light variant. + TokyoNightLight, + /// The built-in Kanagawa Wave variant. + KanagawaWave, + /// The built-in Kanagawa Dragon variant. + KanagawaDragon, + /// The built-in Kanagawa Lotus variant. + KanagawaLotus, + /// The built-in Moonfly variant. + Moonfly, + /// The built-in Nightfly variant. + Nightfly, + /// The built-in Oxocarbon variant. + Oxocarbon, + /// A [`Theme`] that uses a [`Custom`] palette. + Custom(Arc<Custom>), +} + +impl Theme { + /// A list with all the defined themes. + pub const ALL: &'static [Self] = &[ + Self::Light, + Self::Dark, + Self::Dracula, + Self::Nord, + Self::SolarizedLight, + Self::SolarizedDark, + Self::GruvboxLight, + Self::GruvboxDark, + Self::CatppuccinLatte, + Self::CatppuccinFrappe, + Self::CatppuccinMacchiato, + Self::CatppuccinMocha, + Self::TokyoNight, + Self::TokyoNightStorm, + Self::TokyoNightLight, + Self::KanagawaWave, + Self::KanagawaDragon, + Self::KanagawaLotus, + Self::Moonfly, + Self::Nightfly, + Self::Oxocarbon, + ]; + + /// Creates a new custom [`Theme`] from the given [`Palette`]. + pub fn custom(name: String, palette: Palette) -> Self { + Self::custom_with_fn(name, palette, palette::Extended::generate) + } + + /// Creates a new custom [`Theme`] from the given [`Palette`], with + /// a custom generator of a [`palette::Extended`]. + pub fn custom_with_fn( + name: String, + palette: Palette, + generate: impl FnOnce(Palette) -> palette::Extended, + ) -> Self { + Self::Custom(Arc::new(Custom::with_fn(name, palette, generate))) + } + + /// Returns the [`Palette`] of the [`Theme`]. + pub fn palette(&self) -> Palette { + match self { + Self::Light => Palette::LIGHT, + Self::Dark => Palette::DARK, + Self::Dracula => Palette::DRACULA, + Self::Nord => Palette::NORD, + Self::SolarizedLight => Palette::SOLARIZED_LIGHT, + Self::SolarizedDark => Palette::SOLARIZED_DARK, + Self::GruvboxLight => Palette::GRUVBOX_LIGHT, + Self::GruvboxDark => Palette::GRUVBOX_DARK, + Self::CatppuccinLatte => Palette::CATPPUCCIN_LATTE, + Self::CatppuccinFrappe => Palette::CATPPUCCIN_FRAPPE, + Self::CatppuccinMacchiato => Palette::CATPPUCCIN_MACCHIATO, + Self::CatppuccinMocha => Palette::CATPPUCCIN_MOCHA, + Self::TokyoNight => Palette::TOKYO_NIGHT, + Self::TokyoNightStorm => Palette::TOKYO_NIGHT_STORM, + Self::TokyoNightLight => Palette::TOKYO_NIGHT_LIGHT, + Self::KanagawaWave => Palette::KANAGAWA_WAVE, + Self::KanagawaDragon => Palette::KANAGAWA_DRAGON, + Self::KanagawaLotus => Palette::KANAGAWA_LOTUS, + Self::Moonfly => Palette::MOONFLY, + Self::Nightfly => Palette::NIGHTFLY, + Self::Oxocarbon => Palette::OXOCARBON, + 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::Dracula => &palette::EXTENDED_DRACULA, + Self::Nord => &palette::EXTENDED_NORD, + Self::SolarizedLight => &palette::EXTENDED_SOLARIZED_LIGHT, + Self::SolarizedDark => &palette::EXTENDED_SOLARIZED_DARK, + Self::GruvboxLight => &palette::EXTENDED_GRUVBOX_LIGHT, + Self::GruvboxDark => &palette::EXTENDED_GRUVBOX_DARK, + Self::CatppuccinLatte => &palette::EXTENDED_CATPPUCCIN_LATTE, + Self::CatppuccinFrappe => &palette::EXTENDED_CATPPUCCIN_FRAPPE, + Self::CatppuccinMacchiato => { + &palette::EXTENDED_CATPPUCCIN_MACCHIATO + } + Self::CatppuccinMocha => &palette::EXTENDED_CATPPUCCIN_MOCHA, + Self::TokyoNight => &palette::EXTENDED_TOKYO_NIGHT, + Self::TokyoNightStorm => &palette::EXTENDED_TOKYO_NIGHT_STORM, + Self::TokyoNightLight => &palette::EXTENDED_TOKYO_NIGHT_LIGHT, + Self::KanagawaWave => &palette::EXTENDED_KANAGAWA_WAVE, + Self::KanagawaDragon => &palette::EXTENDED_KANAGAWA_DRAGON, + Self::KanagawaLotus => &palette::EXTENDED_KANAGAWA_LOTUS, + Self::Moonfly => &palette::EXTENDED_MOONFLY, + Self::Nightfly => &palette::EXTENDED_NIGHTFLY, + Self::Oxocarbon => &palette::EXTENDED_OXOCARBON, + Self::Custom(custom) => &custom.extended, + } + } +} + +impl fmt::Display for Theme { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Light => write!(f, "Light"), + Self::Dark => write!(f, "Dark"), + Self::Dracula => write!(f, "Dracula"), + Self::Nord => write!(f, "Nord"), + Self::SolarizedLight => write!(f, "Solarized Light"), + Self::SolarizedDark => write!(f, "Solarized Dark"), + Self::GruvboxLight => write!(f, "Gruvbox Light"), + Self::GruvboxDark => write!(f, "Gruvbox Dark"), + Self::CatppuccinLatte => write!(f, "Catppuccin Latte"), + Self::CatppuccinFrappe => write!(f, "Catppuccin Frappé"), + Self::CatppuccinMacchiato => write!(f, "Catppuccin Macchiato"), + Self::CatppuccinMocha => write!(f, "Catppuccin Mocha"), + Self::TokyoNight => write!(f, "Tokyo Night"), + Self::TokyoNightStorm => write!(f, "Tokyo Night Storm"), + Self::TokyoNightLight => write!(f, "Tokyo Night Light"), + Self::KanagawaWave => write!(f, "Kanagawa Wave"), + Self::KanagawaDragon => write!(f, "Kanagawa Dragon"), + Self::KanagawaLotus => write!(f, "Kanagawa Lotus"), + Self::Moonfly => write!(f, "Moonfly"), + Self::Nightfly => write!(f, "Nightfly"), + Self::Oxocarbon => write!(f, "Oxocarbon"), + Self::Custom(custom) => custom.fmt(f), + } + } +} + +/// A [`Theme`] with a customized [`Palette`]. +#[derive(Debug, Clone, PartialEq)] +pub struct Custom { + name: String, + palette: Palette, + extended: palette::Extended, +} + +impl Custom { + /// Creates a [`Custom`] theme from the given [`Palette`]. + pub fn new(name: String, palette: Palette) -> Self { + Self::with_fn(name, palette, palette::Extended::generate) + } + + /// Creates a [`Custom`] theme from the given [`Palette`] with + /// a custom generator of a [`palette::Extended`]. + pub fn with_fn( + name: String, + palette: Palette, + generate: impl FnOnce(Palette) -> palette::Extended, + ) -> Self { + Self { + name, + palette, + extended: generate(palette), + } + } +} + +impl fmt::Display for Custom { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.name) + } +} diff --git a/style/src/theme/palette.rs b/core/src/theme/palette.rs index 15a964cd..985a54a8 100644 --- a/style/src/theme/palette.rs +++ b/core/src/theme/palette.rs @@ -1,5 +1,5 @@ //! Define the colors of a theme. -use crate::core::{color, Color}; +use crate::{color, Color}; use once_cell::sync::Lazy; use palette::color_difference::Wcag21RelativeContrast; diff --git a/core/src/widget/text.rs b/core/src/widget/text.rs index 0796c4e4..a220127c 100644 --- a/core/src/widget/text.rs +++ b/core/src/widget/text.rs @@ -17,7 +17,6 @@ pub use text::{LineHeight, Shaping}; #[allow(missing_debug_implementations)] pub struct Text<'a, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { content: Cow<'a, str>, @@ -29,12 +28,11 @@ where vertical_alignment: alignment::Vertical, font: Option<Renderer::Font>, shaping: Shaping, - style: Theme::Style, + style: Style<Theme>, } impl<'a, Theme, Renderer> Text<'a, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { /// Create a new fragment of [`Text`] with the given contents. @@ -49,7 +47,7 @@ where horizontal_alignment: alignment::Horizontal::Left, vertical_alignment: alignment::Vertical::Top, shaping: Shaping::Basic, - style: Default::default(), + style: Style::default(), } } @@ -74,8 +72,20 @@ where } /// Sets the style of the [`Text`]. - pub fn style(mut self, style: impl Into<Theme::Style>) -> Self { - self.style = style.into(); + pub fn style(mut self, style: fn(&Theme) -> Appearance) -> Self { + self.style = Style::Themed(style); + self + } + + /// Sets the [`Color`] of the [`Text`]. + pub fn color(mut self, color: impl Into<Color>) -> Self { + self.style = Style::Colored(Some(color.into())); + self + } + + /// Sets the [`Color`] of the [`Text`], if `Some`. + pub fn color_maybe(mut self, color: Option<impl Into<Color>>) -> Self { + self.style = Style::Colored(color.map(Into::into)); self } @@ -123,7 +133,6 @@ pub struct State<P: Paragraph>(P); impl<'a, Message, Theme, Renderer> Widget<Message, Theme, Renderer> for Text<'a, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { fn tag(&self) -> tree::Tag { @@ -175,14 +184,12 @@ where ) { let state = tree.state.downcast_ref::<State<Renderer::Paragraph>>(); - draw( - renderer, - style, - layout, - state, - theme.appearance(self.style.clone()), - viewport, - ); + let appearance = match self.style { + Style::Themed(f) => f(theme), + Style::Colored(color) => Appearance { color }, + }; + + draw(renderer, style, layout, state, appearance, viewport); } } @@ -273,7 +280,7 @@ pub fn draw<Renderer>( impl<'a, Message, Theme, Renderer> From<Text<'a, Theme, Renderer>> for Element<'a, Message, Theme, Renderer> where - Theme: StyleSheet + 'a, + Theme: 'a, Renderer: text::Renderer + 'a, { fn from( @@ -285,7 +292,6 @@ where impl<'a, Theme, Renderer> Clone for Text<'a, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { fn clone(&self) -> Self { @@ -298,7 +304,7 @@ where horizontal_alignment: self.horizontal_alignment, vertical_alignment: self.vertical_alignment, font: self.font, - style: self.style.clone(), + style: self.style, shaping: self.shaping, } } @@ -306,7 +312,6 @@ where impl<'a, Theme, Renderer> From<&'a str> for Text<'a, Theme, Renderer> where - Theme: StyleSheet, Renderer: text::Renderer, { fn from(content: &'a str) -> Self { @@ -317,7 +322,7 @@ where impl<'a, Message, Theme, Renderer> From<&'a str> for Element<'a, Message, Theme, Renderer> where - Theme: StyleSheet + 'a, + Theme: 'a, Renderer: text::Renderer + 'a, { fn from(content: &'a str) -> Self { @@ -325,15 +330,6 @@ where } } -/// The style sheet of some text. -pub trait StyleSheet { - /// The supported style of the [`StyleSheet`]. - type Style: Default + Clone; - - /// Produces the [`Appearance`] of some text. - fn appearance(&self, style: Self::Style) -> Appearance; -} - /// The apperance of some text. #[derive(Debug, Clone, Copy, Default)] pub struct Appearance { @@ -342,3 +338,29 @@ pub struct Appearance { /// The default, `None`, means using the inherited color. pub color: Option<Color>, } + +#[derive(Debug)] +enum Style<Theme> { + Themed(fn(&Theme) -> Appearance), + Colored(Option<Color>), +} + +impl<Theme> Clone for Style<Theme> { + fn clone(&self) -> Self { + *self + } +} + +impl<Theme> Copy for Style<Theme> {} + +impl<Theme> Default for Style<Theme> { + fn default() -> Self { + Style::Colored(None) + } +} + +impl<Theme> From<fn(&Theme) -> Appearance> for Style<Theme> { + fn from(f: fn(&Theme) -> Appearance) -> Self { + Style::Themed(f) + } +} |