summaryrefslogtreecommitdiffstats
path: root/style/src/theme.rs
blob: 91a9e9215ed903cbafd191134c3af08b5ef5e18f (plain) (blame)
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
mod palette;

pub use self::palette::Palette;

use crate::button;

use iced_core::Background;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Theme {
    Light,
    Dark,
}

impl Theme {
    pub fn palette(self) -> Palette {
        match self {
            Self::Light => Palette::LIGHT,
            Self::Dark => Palette::DARK,
        }
    }

    fn extended_palette(&self) -> &palette::Extended {
        match self {
            Self::Light => &palette::EXTENDED_LIGHT,
            Self::Dark => &palette::EXTENDED_DARK,
        }
    }
}

impl Default for Theme {
    fn default() -> Self {
        Self::Light
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Button {
    Primary,
    Secondary,
    Positive,
    Destructive,
    Text,
}

impl Default for Button {
    fn default() -> Self {
        Self::Primary
    }
}

impl button::StyleSheet for Theme {
    type Variant = Button;

    fn active(&self, variant: Self::Variant) -> button::Style {
        let palette = self.extended_palette();

        let style = button::Style {
            border_radius: 2.0,
            ..button::Style::default()
        };

        match variant {
            Button::Primary => button::Style {
                background: Some(palette.primary.strong.into()),
                text_color: palette.primary.text,
                ..style
            },
            Button::Secondary => button::Style {
                background: Some(palette.background.weak.into()),
                text_color: palette.background.text,
                ..style
            },
            Button::Positive => button::Style {
                background: Some(palette.success.base.into()),
                text_color: palette.success.text,
                ..style
            },
            Button::Destructive => button::Style {
                background: Some(palette.danger.base.into()),
                text_color: palette.danger.text,
                ..style
            },
            Button::Text => button::Style {
                text_color: palette.background.text,
                ..style
            },
        }
    }

    fn hovered(&self, variant: Self::Variant) -> button::Style {
        let active = self.active(variant);
        let palette = self.extended_palette();

        let background = match variant {
            Button::Primary => Some(palette.primary.base),
            Button::Secondary => Some(palette.background.strong),
            Button::Positive => Some(palette.success.strong),
            Button::Destructive => Some(palette.danger.strong),
            Button::Text => None,
        };

        button::Style {
            background: background.map(Background::from),
            ..active
        }
    }
}