diff options
| author | pml68 <contact@pml68.dev> | 2025-04-17 00:01:09 +0200 |
|---|---|---|
| committer | pml68 <contact@pml68.dev> | 2025-04-17 00:01:09 +0200 |
| commit | 385187e2db4681b99a5dcef30a0c170f8c60546a (patch) | |
| tree | 78c39329d74545885b0155e84af282cf487e568e /crates/material_theme | |
| parent | refactor(PKGBUILD): use `printf` over `echo` in `pkgver` (diff) | |
| download | iced-builder-385187e2db4681b99a5dcef30a0c170f8c60546a.tar.gz | |
feat(material_theme): implement `checkbox::Catalog`
Diffstat (limited to '')
| -rw-r--r-- | crates/material_theme/Cargo.toml | 2 | ||||
| -rw-r--r-- | crates/material_theme/src/checkbox.rs | 127 | ||||
| -rw-r--r-- | crates/material_theme/src/lib.rs | 1 |
3 files changed, 130 insertions, 0 deletions
diff --git a/crates/material_theme/Cargo.toml b/crates/material_theme/Cargo.toml index eef9605..58c5e0c 100644 --- a/crates/material_theme/Cargo.toml +++ b/crates/material_theme/Cargo.toml @@ -13,7 +13,9 @@ rust-version = "1.85" [features] default = [] +# Provides support for animating with `iced_anim` animate = ["dep:iced_anim"] +# Provides support for `iced_dialog` dialog = ["dep:iced_dialog"] [dependencies] diff --git a/crates/material_theme/src/checkbox.rs b/crates/material_theme/src/checkbox.rs new file mode 100644 index 0000000..ac1f974 --- /dev/null +++ b/crates/material_theme/src/checkbox.rs @@ -0,0 +1,127 @@ +use iced_widget::checkbox::{Catalog, Status, Style, StyleFn}; +use iced_widget::core::{Background, Border, Color, border}; + +use super::Theme; +use crate::utils::{DISABLED_CONTAINER_OPACITY, HOVERED_LAYER_OPACITY, mix}; + +impl Catalog for 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 styled( + background_color: Color, + background_hover: Option<Color>, + icon_color: Color, + border_color: Color, + text_color: Option<Color>, + is_checked: bool, +) -> Style { + Style { + background: Background::Color(if is_checked { + background_color + } else { + background_hover.unwrap_or(Color::TRANSPARENT) + }), + icon_color, + border: if is_checked { + border::rounded(2) + } else { + Border { + color: border_color, + width: 2.0, + radius: border::radius(2), + } + }, + text_color, + } +} + +pub fn default(theme: &Theme, status: Status) -> Style { + let surface = theme.colorscheme.surface; + let primary = theme.colorscheme.primary; + + match status { + Status::Active { is_checked } => styled( + primary.color, + None, + primary.on_primary, + surface.on_surface_variant, + Some(surface.on_surface), + is_checked, + ), + Status::Hovered { is_checked } => styled( + mix(primary.color, surface.on_surface, HOVERED_LAYER_OPACITY), + Some(Color { + a: HOVERED_LAYER_OPACITY, + ..surface.on_surface + }), + primary.on_primary, + surface.on_surface_variant, + Some(surface.on_surface), + is_checked, + ), + Status::Disabled { is_checked } => styled( + Color { + a: DISABLED_CONTAINER_OPACITY, + ..surface.on_surface + }, + None, + surface.color, + Color { + a: DISABLED_CONTAINER_OPACITY, + ..surface.on_surface + }, + Some(surface.on_surface), + is_checked, + ), + } +} + +pub fn error(theme: &Theme, status: Status) -> Style { + let surface = theme.colorscheme.surface; + let error = theme.colorscheme.error; + + match status { + Status::Active { is_checked } => styled( + error.color, + None, + error.on_error, + error.color, + Some(error.color), + is_checked, + ), + Status::Hovered { is_checked } => styled( + mix(error.color, surface.on_surface, HOVERED_LAYER_OPACITY), + Some(Color { + a: HOVERED_LAYER_OPACITY, + ..error.color + }), + error.on_error, + error.color, + Some(error.color), + is_checked, + ), + Status::Disabled { is_checked } => styled( + Color { + a: DISABLED_CONTAINER_OPACITY, + ..surface.on_surface + }, + None, + surface.color, + Color { + a: DISABLED_CONTAINER_OPACITY, + ..surface.on_surface + }, + Some(surface.on_surface), + is_checked, + ), + } +} diff --git a/crates/material_theme/src/lib.rs b/crates/material_theme/src/lib.rs index adffe09..2440538 100644 --- a/crates/material_theme/src/lib.rs +++ b/crates/material_theme/src/lib.rs @@ -5,6 +5,7 @@ use iced_widget::core::theme::{Base, Style}; use serde::Deserialize; pub mod button; +pub mod checkbox; pub mod container; #[cfg(feature = "dialog")] pub mod dialog; |
