aboutsummaryrefslogtreecommitdiff
path: root/src/toggler.rs
blob: ad6bbb1fd32e318e23c1cb39c0e337844aa8f86b (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
use iced_widget::core::{Background, Color};
use iced_widget::toggler::{Catalog, Status, Style, StyleFn};

use super::Theme;
use crate::utils::{
    HOVERED_LAYER_OPACITY, disabled_container, 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: Background,
    foreground: Background,
    text_color: Color,
    border: Option<Color>,
) -> Style {
    Style {
        background,
        background_border_width: if border.is_some() { 1.5 } else { 0.0 },
        background_border_color: border.unwrap_or(Color::TRANSPARENT),
        foreground,
        foreground_border_width: 0.0,
        foreground_border_color: Color::TRANSPARENT,
        text_color: Some(text_color),
        border_radius: None,
        padding_ratio: 0.2,
    }
}

pub fn default(theme: &Theme, status: Status) -> Style {
    let surface = theme.colors().surface;
    let primary = theme.colors().primary;

    match status {
        Status::Active { is_toggled } => {
            if is_toggled {
                styled(
                    primary.color.into(),
                    primary.text.into(),
                    surface.text,
                    None,
                )
            } else {
                styled(
                    surface.container.highest.into(),
                    theme.colors().outline.color.into(),
                    surface.text,
                    Some(theme.colors().outline.color),
                )
            }
        }
        Status::Hovered { is_toggled } => {
            if is_toggled {
                styled(
                    primary.color.into(),
                    primary.container.into(),
                    surface.text,
                    None,
                )
            } else {
                styled(
                    mix(
                        surface.container.highest,
                        surface.text,
                        HOVERED_LAYER_OPACITY,
                    )
                    .into(),
                    surface.text_variant.into(),
                    surface.text,
                    Some(theme.colors().outline.color),
                )
            }
        }
        Status::Disabled { is_toggled } => {
            if is_toggled {
                styled(
                    disabled_container(surface.text).into(),
                    disabled_text(surface.color).into(),
                    surface.text,
                    None,
                )
            } else {
                styled(
                    disabled_container(surface.container.highest).into(),
                    disabled_text(surface.text).into(),
                    surface.text,
                    Some(disabled_text(surface.text)),
                )
            }
        }
    }
}