forked from iced-rs/iced
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
160 lines (141 loc) · 4.5 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
use iced::theme::{self, Theme};
use iced::widget::{
button, checkbox, column, container, horizontal_rule, progress_bar, radio,
row, scrollable, slider, text, text_input, toggler, vertical_rule,
vertical_space,
};
use iced::{Alignment, Color, Element, Length, Sandbox, Settings};
pub fn main() -> iced::Result {
Styling::run(Settings::default())
}
#[derive(Default)]
struct Styling {
theme: Theme,
input_value: String,
slider_value: f32,
checkbox_value: bool,
toggler_value: bool,
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum ThemeType {
Light,
Dark,
Custom,
}
#[derive(Debug, Clone)]
enum Message {
ThemeChanged(ThemeType),
InputChanged(String),
ButtonPressed,
SliderChanged(f32),
CheckboxToggled(bool),
TogglerToggled(bool),
}
impl Sandbox for Styling {
type Message = Message;
fn new() -> Self {
Styling::default()
}
fn title(&self) -> String {
String::from("Styling - Iced")
}
fn update(&mut self, message: Message) {
match message {
Message::ThemeChanged(theme) => {
self.theme = match theme {
ThemeType::Light => Theme::Light,
ThemeType::Dark => Theme::Dark,
ThemeType::Custom => Theme::custom(theme::Palette {
background: Color::from_rgb(1.0, 0.9, 1.0),
text: Color::BLACK,
primary: Color::from_rgb(0.5, 0.5, 0.0),
success: Color::from_rgb(0.0, 1.0, 0.0),
danger: Color::from_rgb(1.0, 0.0, 0.0),
}),
}
}
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,
}
}
fn view(&self) -> Element<Message> {
let choose_theme =
[ThemeType::Light, ThemeType::Dark, ThemeType::Custom]
.iter()
.fold(
column![text("Choose a theme:")].spacing(10),
|column, theme| {
column.push(radio(
format!("{theme:?}"),
*theme,
Some(match self.theme {
Theme::Light => ThemeType::Light,
Theme::Dark => ThemeType::Dark,
Theme::Custom { .. } => ThemeType::Custom,
}),
Message::ThemeChanged,
))
},
);
let text_input = text_input(
"Type something...",
&self.input_value,
Message::InputChanged,
)
.padding(10)
.size(20);
let button = button("Submit")
.padding(10)
.on_press(Message::ButtonPressed);
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(800), "You did it!"]
.width(Length::Fill),
)
.height(100);
let checkbox = checkbox(
"Check me!",
self.checkbox_value,
Message::CheckboxToggled,
);
let toggler = toggler(
String::from("Toggle me!"),
self.toggler_value,
Message::TogglerToggled,
)
.width(Length::Shrink)
.spacing(10);
let content = column![
choose_theme,
horizontal_rule(38),
row![text_input, button].spacing(10),
slider,
progress_bar,
row![
scrollable,
vertical_rule(38),
column![checkbox, toggler].spacing(20)
]
.spacing(10)
.height(100)
.align_items(Alignment::Center),
]
.spacing(20)
.padding(20)
.max_width(600);
container(content)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.into()
}
fn theme(&self) -> Theme {
self.theme.clone()
}
}