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
158
159
160
161
162
163
164
165
166
167
|
mod palette;
pub use self::palette::Palette;
use crate::application;
use crate::button;
use crate::slider;
use iced_core::{Background, Color};
#[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
}
}
impl application::StyleSheet for Theme {
fn background_color(&self) -> Color {
let palette = self.extended_palette();
palette.background.base.color
}
fn text_color(&self) -> Color {
let palette = self.extended_palette();
palette.background.base.text
}
}
#[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 Style = Button;
fn active(&self, style: Self::Style) -> button::Appearance {
let palette = self.extended_palette();
let appearance = button::Appearance {
border_radius: 2.0,
..button::Appearance::default()
};
let from_pair = |pair: palette::Pair| button::Appearance {
background: Some(pair.color.into()),
text_color: pair.text,
..appearance
};
match style {
Button::Primary => from_pair(palette.primary.strong),
Button::Secondary => from_pair(palette.secondary.base),
Button::Positive => from_pair(palette.success.base),
Button::Destructive => from_pair(palette.danger.base),
Button::Text => button::Appearance {
text_color: palette.background.base.text,
..appearance
},
}
}
fn hovered(&self, style: Self::Style) -> button::Appearance {
let active = self.active(style);
let palette = self.extended_palette();
let background = match style {
Button::Primary => Some(palette.primary.base.color),
Button::Secondary => Some(palette.background.strong.color),
Button::Positive => Some(palette.success.strong.color),
Button::Destructive => Some(palette.danger.strong.color),
Button::Text => None,
};
button::Appearance {
background: background.map(Background::from),
..active
}
}
}
impl slider::StyleSheet for Theme {
type Style = ();
fn active(&self, _style: Self::Style) -> slider::Appearance {
let palette = self.extended_palette();
let handle = slider::Handle {
shape: slider::HandleShape::Rectangle {
width: 8,
border_radius: 4.0,
},
color: Color::WHITE,
border_color: Color::WHITE,
border_width: 1.0,
};
slider::Appearance {
rail_colors: (palette.primary.base.color, Color::TRANSPARENT),
handle: slider::Handle {
color: palette.background.base.color,
border_color: palette.primary.base.color,
..handle
},
}
}
fn hovered(&self, style: Self::Style) -> slider::Appearance {
let active = self.active(style);
let palette = self.extended_palette();
slider::Appearance {
handle: slider::Handle {
color: palette.primary.weak.color,
..active.handle
},
..active
}
}
fn dragging(&self, style: Self::Style) -> slider::Appearance {
let active = self.active(style);
let palette = self.extended_palette();
slider::Appearance {
handle: slider::Handle {
color: palette.primary.base.color,
..active.handle
},
..active
}
}
}
|