summaryrefslogtreecommitdiff
path: root/crates/material_theme/src/checkbox.rs
blob: 7a3729c08049481eb0514157ea6d95677ee62626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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<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_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,
        ),
    }
}