summaryrefslogtreecommitdiff
path: root/crates/material_theme/src/button.rs
blob: 2800ac44570bff3fd34a4f94bc9b492010b9aabb (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use iced_widget::button::{Catalog, Status, Style, StyleFn};
use iced_widget::core::{Background, Border, Color, border};

use crate::Theme;
use crate::utils::{
    HOVERED_LAYER_OPACITY, PRESSED_LAYER_OPACITY, disabled_container,
    disabled_text, elevation, mix, shadow_from_elevation,
};

impl Catalog for Theme {
    type Class<'a> = StyleFn<'a, Self>;

    fn default<'a>() -> Self::Class<'a> {
        Box::new(filled)
    }

    fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
        class(self, status)
    }
}

fn button(
    background: Color,
    foreground: Color,
    disabled: Color,
    shadow_color: Color,
    elevation_level: u8,
    status: Status,
) -> Style {
    let active = Style {
        background: Some(Background::Color(background)),
        text_color: foreground,
        border: border::rounded(400),
        shadow: shadow_from_elevation(elevation(elevation_level), shadow_color),
        snap: cfg!(feature = "crisp"),
    };

    match status {
        Status::Active => active,
        Status::Pressed => Style {
            background: Some(Background::Color(mix(
                background,
                foreground,
                HOVERED_LAYER_OPACITY,
            ))),
            ..active
        },
        Status::Hovered => Style {
            background: Some(Background::Color(mix(
                background,
                foreground,
                PRESSED_LAYER_OPACITY,
            ))),
            shadow: shadow_from_elevation(
                elevation(elevation_level + 1),
                shadow_color,
            ),
            ..active
        },
        Status::Disabled => Style {
            background: Some(Background::Color(disabled_container(disabled))),
            text_color: disabled_text(disabled),
            border: border::rounded(400),
            ..Default::default()
        },
    }
}

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

    let foreground = theme.colors().primary.color;
    let background = surface.surface_container.low;
    let disabled = surface.on_surface;

    let shadow_color = theme.colors().shadow;

    button(background, foreground, disabled, shadow_color, 1, status)
}

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

    let foreground = primary.on_primary;
    let background = primary.color;
    let disabled = theme.colors().surface.on_surface;

    let shadow_color = theme.colors().shadow;

    button(background, foreground, disabled, shadow_color, 0, status)
}

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

    let foreground = secondary.on_secondary_container;
    let background = secondary.secondary_container;
    let disabled = theme.colors().surface.on_surface;
    let shadow_color = theme.colors().shadow;

    button(background, foreground, disabled, shadow_color, 0, status)
}

pub fn outlined(theme: &Theme, status: Status) -> Style {
    let foreground = theme.colors().primary.color;
    let background = Color::TRANSPARENT;
    let disabled = theme.colors().surface.on_surface;

    let outline = theme.colors().outline.color;

    let border = match status {
        Status::Active | Status::Pressed | Status::Hovered => Border {
            color: outline,
            width: 1.0,
            radius: 400.into(),
        },
        Status::Disabled => Border {
            color: disabled_container(disabled),
            width: 1.0,
            radius: 400.into(),
        },
    };

    let style = button(
        background,
        foreground,
        disabled,
        Color::TRANSPARENT,
        0,
        status,
    );

    Style { border, ..style }
}

pub fn text(theme: &Theme, status: Status) -> Style {
    let foreground = theme.colors().primary.color;
    let background = Color::TRANSPARENT;
    let disabled = theme.colors().surface.on_surface;

    let style = button(
        background,
        foreground,
        disabled,
        Color::TRANSPARENT,
        0,
        status,
    );

    match status {
        Status::Hovered | Status::Pressed => style,
        Status::Active | Status::Disabled => Style {
            background: None,
            ..style
        },
    }
}