Skip to content

Commit

Permalink
Merge pull request #2312 from iced-rs/theming-reloaded
Browse files Browse the repository at this point in the history
Theming reloaded
  • Loading branch information
hecrj authored Mar 8, 2024
2 parents 2074757 + 8919f25 commit edf7d7c
Show file tree
Hide file tree
Showing 97 changed files with 5,729 additions and 6,651 deletions.
1 change: 0 additions & 1 deletion .github/workflows/document.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ jobs:
cargo doc --no-deps --all-features \
-p iced_core \
-p iced_highlighter \
-p iced_style \
-p iced_futures \
-p iced_runtime \
-p iced_graphics \
Expand Down
5 changes: 0 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ tokio = ["iced_futures/tokio"]
async-std = ["iced_futures/async-std"]
# Enables `smol` as the `executor::Default` on native platforms
smol = ["iced_futures/smol"]
# Enables advanced color conversion via `palette`
palette = ["iced_core/palette"]
# Enables querying system information
system = ["iced_winit/system"]
# Enables broken "sRGB linear" blending to reproduce color management of the Web
Expand All @@ -57,7 +55,6 @@ advanced = []
fira-sans = ["iced_renderer/fira-sans"]

[dependencies]
iced_core.workspace = true
iced_futures.workspace = true
iced_renderer.workspace = true
iced_widget.workspace = true
Expand Down Expand Up @@ -90,7 +87,6 @@ members = [
"highlighter",
"renderer",
"runtime",
"style",
"tiny_skia",
"wgpu",
"widget",
Expand All @@ -116,7 +112,6 @@ iced_graphics = { version = "0.13.0-dev", path = "graphics" }
iced_highlighter = { version = "0.13.0-dev", path = "highlighter" }
iced_renderer = { version = "0.13.0-dev", path = "renderer" }
iced_runtime = { version = "0.13.0-dev", path = "runtime" }
iced_style = { version = "0.13.0-dev", path = "style" }
iced_tiny_skia = { version = "0.13.0-dev", path = "tiny_skia" }
iced_wgpu = { version = "0.13.0-dev", path = "wgpu" }
iced_widget = { version = "0.13.0-dev", path = "widget" }
Expand Down
5 changes: 2 additions & 3 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions core/src/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 31 additions & 4 deletions core/src/border.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Draw lines around containers.
use crate::Color;
use crate::{Color, Pixels};

/// A border.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
Expand All @@ -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
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions core/src/color.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(feature = "palette")]
use palette::rgb::{Srgb, Srgba};

/// A color in the `sRGB` color space.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -202,39 +209,34 @@ 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 {
Color::new(rgba.red, rgba.green, rgba.blue, rgba.alpha)
}
}

#[cfg(feature = "palette")]
/// Converts from [`Color`] to palette's `Rgba` type.
impl From<Color> for Srgba {
fn from(c: Color) -> Self {
Srgba::new(c.r, c.g, c.b, c.a)
}
}

#[cfg(feature = "palette")]
/// Converts from palette's `Rgb` type to a [`Color`].
impl From<Srgb> for Color {
fn from(rgb: Srgb) -> Self {
Color::new(rgb.red, rgb.green, rgb.blue, 1.0)
}
}

#[cfg(feature = "palette")]
/// Converts from [`Color`] to palette's `Rgb` type.
impl From<Color> for Srgb {
fn from(c: Color) -> Self {
Srgb::new(c.r, c.g, c.b)
}
}

#[cfg(feature = "palette")]
#[cfg(test)]
mod tests {
use super::*;
Expand Down
22 changes: 14 additions & 8 deletions core/src/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
}
2 changes: 2 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit edf7d7c

Please sign in to comment.