From 602565f98f3a22fa1c39e054c6a88b3c31a36599 Mon Sep 17 00:00:00 2001 From: pml68 Date: Thu, 26 Jun 2025 11:41:52 +0200 Subject: feat: initial commit --- src/checkbox.rs | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/checkbox.rs (limited to 'src/checkbox.rs') diff --git a/src/checkbox.rs b/src/checkbox.rs new file mode 100644 index 0000000..7a3729c --- /dev/null +++ b/src/checkbox.rs @@ -0,0 +1,115 @@ +use iced_widget::checkbox::{Catalog, Status, Style, StyleFn}; +use iced_widget::core::{Background, Border, Color, border}; + +use super::Theme; +use crate::utils::{HOVERED_LAYER_OPACITY, disabled_text, 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_unchecked: Option, + icon_color: Color, + border_color: Color, + text_color: Option, + is_checked: bool, +) -> Style { + Style { + background: Background::Color(if is_checked { + background_color + } else { + background_unchecked.unwrap_or(Color::TRANSPARENT) + }), + icon_color, + border: if is_checked { + border::rounded(2) + } else { + Border { + color: border_color, + width: 2.0, + radius: 2.into(), + } + }, + text_color, + } +} + +pub fn default(theme: &Theme, status: Status) -> Style { + let surface = theme.colors().surface; + let primary = theme.colors().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( + disabled_text(surface.on_surface), + None, + surface.color, + disabled_text(surface.on_surface), + Some(surface.on_surface), + is_checked, + ), + } +} + +pub fn error(theme: &Theme, status: Status) -> Style { + let surface = theme.colors().surface; + let error = theme.colors().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( + disabled_text(surface.on_surface), + None, + surface.color, + disabled_text(surface.on_surface), + Some(surface.on_surface), + is_checked, + ), + } +} -- cgit v1.2.3