-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathmain.rs
208 lines (183 loc) · 6.07 KB
/
main.rs
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use iced::keyboard;
use iced::widget::{
button, center, checkbox, column, container, horizontal_rule, pick_list,
progress_bar, row, scrollable, slider, text, text_input, toggler,
vertical_rule, vertical_space,
};
use iced::{Center, Element, Fill, Subscription, Theme};
pub fn main() -> iced::Result {
iced::application("Styling - Iced", Styling::update, Styling::view)
.subscription(Styling::subscription)
.theme(Styling::theme)
.run()
}
#[derive(Default)]
struct Styling {
theme: Theme,
input_value: String,
slider_value: f32,
checkbox_value: bool,
toggler_value: bool,
}
#[derive(Debug, Clone)]
enum Message {
ThemeChanged(Theme),
InputChanged(String),
ButtonPressed,
SliderChanged(f32),
CheckboxToggled(bool),
TogglerToggled(bool),
PreviousTheme,
NextTheme,
}
impl Styling {
fn update(&mut self, message: Message) {
match message {
Message::ThemeChanged(theme) => {
self.theme = theme;
}
Message::InputChanged(value) => self.input_value = value,
Message::ButtonPressed => {}
Message::SliderChanged(value) => self.slider_value = value,
Message::CheckboxToggled(value) => self.checkbox_value = value,
Message::TogglerToggled(value) => self.toggler_value = value,
Message::PreviousTheme | Message::NextTheme => {
if let Some(current) = Theme::ALL
.iter()
.position(|candidate| &self.theme == candidate)
{
self.theme = if matches!(message, Message::NextTheme) {
Theme::ALL[(current + 1) % Theme::ALL.len()].clone()
} else if current == 0 {
Theme::ALL
.last()
.expect("Theme::ALL must not be empty")
.clone()
} else {
Theme::ALL[current - 1].clone()
};
}
}
}
}
fn view(&self) -> Element<Message> {
let choose_theme = column![
text("Theme:"),
pick_list(Theme::ALL, Some(&self.theme), Message::ThemeChanged)
.width(Fill),
]
.spacing(10);
let text_input = text_input("Type something...", &self.input_value)
.on_input(Message::InputChanged)
.padding(10)
.size(20);
let styled_button = |label| {
button(text(label).width(Fill).center())
.padding(10)
.on_press(Message::ButtonPressed)
};
let primary = styled_button("Primary");
let success = styled_button("Success").style(button::success);
let warning = styled_button("Warning").style(button::warning);
let danger = styled_button("Danger").style(button::danger);
let slider =
|| slider(0.0..=100.0, self.slider_value, Message::SliderChanged);
let progress_bar = || progress_bar(0.0..=100.0, self.slider_value);
let scrollable = scrollable(column![
"Scroll me!",
vertical_space().height(800),
"You did it!"
])
.width(Fill)
.height(100);
let checkbox = checkbox("Check me!", self.checkbox_value)
.on_toggle(Message::CheckboxToggled);
let toggler = toggler(self.toggler_value)
.label("Toggle me!")
.on_toggle(Message::TogglerToggled)
.spacing(10);
let card = {
container(
column![
text("Card Example").size(24),
slider(),
progress_bar(),
]
.spacing(20),
)
.width(Fill)
.padding(20)
.style(container::bordered_box)
};
let content = column![
choose_theme,
horizontal_rule(38),
text_input,
row![primary, success, warning, danger]
.spacing(10)
.align_y(Center),
slider(),
progress_bar(),
row![
scrollable,
vertical_rule(38),
column![checkbox, toggler].spacing(20)
]
.spacing(10)
.height(100)
.align_y(Center),
card
]
.spacing(20)
.padding(20)
.max_width(600);
center(content).into()
}
fn subscription(&self) -> Subscription<Message> {
keyboard::on_key_press(|key, _modifiers| match key {
keyboard::Key::Named(
keyboard::key::Named::ArrowUp | keyboard::key::Named::ArrowLeft,
) => Some(Message::PreviousTheme),
keyboard::Key::Named(
keyboard::key::Named::ArrowDown
| keyboard::key::Named::ArrowRight,
) => Some(Message::NextTheme),
_ => None,
})
}
fn theme(&self) -> Theme {
self.theme.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
use rayon::prelude::*;
use iced_test::{Error, simulator};
#[test]
#[ignore]
fn it_showcases_every_theme() -> Result<(), Error> {
Theme::ALL
.par_iter()
.cloned()
.map(|theme| {
let mut styling = Styling::default();
styling.update(Message::ThemeChanged(theme));
let theme = styling.theme();
let mut ui = simulator(styling.view());
let snapshot = ui.snapshot(&theme)?;
assert!(
snapshot.matches_hash(format!(
"snapshots/{theme}",
theme = theme
.to_string()
.to_ascii_lowercase()
.replace(" ", "_")
))?,
"snapshots for {theme} should match!"
);
Ok(())
})
.collect()
}
}