-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
main.rs
283 lines (273 loc) · 10.1 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use instant::Instant;
use person::PersonType;
use web_sys::{HtmlElement, HtmlInputElement};
use yew::html::Scope;
use yew::prelude::*;
mod person;
mod random;
pub enum Msg {
CreatePersons(usize),
CreatePersonsPrepend(usize),
ChangeRatio(f64),
DeletePersonById(usize),
DeleteEverybody,
SwapRandom,
ReverseList,
SortById,
SortByName,
SortByAge,
SortByAddress,
ToggleKeyed,
Rendered(Instant),
}
pub struct App {
persons: Vec<PersonType>,
last_id: usize,
keyed: bool,
build_component_ratio: f64,
delta_ref: NodeRef,
}
impl Component for App {
type Message = Msg;
type Properties = ();
fn create(_ctx: &Context<Self>) -> Self {
Self {
persons: Vec::with_capacity(200),
last_id: 0,
keyed: true,
build_component_ratio: 0.5,
delta_ref: NodeRef::default(),
}
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::CreatePersons(n) => {
for _ in 0..n {
self.last_id += 1;
self.persons.push(PersonType::new_random(
self.last_id,
self.build_component_ratio,
));
}
true
}
Msg::CreatePersonsPrepend(n) => {
for _ in 0..n {
self.last_id += 1;
self.persons.insert(
0,
PersonType::new_random(self.last_id, self.build_component_ratio),
);
}
true
}
Msg::ChangeRatio(ratio) => {
#[allow(clippy::float_cmp)] // it's fine here?
if self.build_component_ratio != ratio {
self.build_component_ratio = ratio;
log::info!("Ratio changed: {}", ratio);
true
} else {
false
}
}
Msg::DeletePersonById(id) => {
if let Some(idx) = self.persons.iter().position(|p| p.info().id == id) {
self.persons.remove(idx);
true
} else {
false
}
}
Msg::DeleteEverybody => {
self.persons.clear();
true
}
Msg::SwapRandom => {
let (a, b) = random::choose_two_distinct_mut(&mut self.persons).unwrap();
log::info!("Swapping {} and {}.", a.info().id, b.info().id);
std::mem::swap(a, b);
true
}
Msg::ReverseList => {
self.persons.reverse();
true
}
Msg::SortById => {
self.persons
.sort_unstable_by(|a, b| a.info().id.cmp(&b.info().id));
true
}
Msg::SortByName => {
self.persons
.sort_unstable_by(|a, b| a.info().name.cmp(&b.info().name));
true
}
Msg::SortByAge => {
self.persons.sort_by_key(|p| p.info().age);
true
}
Msg::SortByAddress => {
self.persons
.sort_unstable_by(|a, b| a.info().address.cmp(&b.info().address));
true
}
Msg::ToggleKeyed => {
self.keyed = !self.keyed;
true
}
Msg::Rendered(time_before) => {
let time_after = Instant::now();
let elapsed_max = time_after - time_before;
log::info!("Rendering started {} ms ago.", elapsed_max.as_millis());
if let Some(input) = self.delta_ref.cast::<HtmlElement>() {
let delta_text =
format!("The last rendering took {} ms", elapsed_max.as_millis());
input.set_inner_text(&delta_text);
}
false
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
ctx.link().send_message(Msg::Rendered(Instant::now()));
html! {
<div class="container">
<div class="row">
<p class="h2" ref={self.delta_ref.clone()}/>
<hr />
</div>
{ self.action_view(ctx.link()) }
{ self.info_view() }
</div>
}
}
}
impl App {
fn action_view(&self, link: &Scope<Self>) -> Html {
html! {
<>
{ self.button_view(link) }
<div class="row">
<div class="col">
<p class="h5">
{ "Person type ratio (0=only tags <= ratio <= 1=only components): " }
{ self.build_component_ratio }
</p>
<input name="ratio" type="range" class="form-control-range" min="0.0" max="1.0" step="any"
oninput={link.callback(|e: InputEvent| {
let input: HtmlInputElement = e.target_unchecked_into();
Msg::ChangeRatio(input.value_as_number())
})}
/>
</div>
</div>
</>
}
}
fn button_view(&self, link: &Scope<Self>) -> Html {
html! {
<>
<div class="row">
<div class="col">
<button class="btn_size alert alert-danger" onclick={link.callback(|_| Msg::DeleteEverybody)}>
{ "Delete everybody" }
</button>
</div>
<div class="col">
<button class="btn_size alert alert-success" onclick={link.callback(|_| Msg::CreatePersons(1))}>
{ "Create 1" }
</button>
</div>
<div class="col">
<button class="btn_size alert alert-success" onclick={link.callback(|_| Msg::CreatePersons(5))}>
{ "Create 5" }
</button>
</div>
<div class="col">
<button class="btn_size alert alert-success" onclick={link.callback(|_| Msg::CreatePersons(100))}>
{ "Create 100" }
</button>
</div>
<div class="col">
<button class="btn_size alert alert-success" onclick={link.callback(|_| Msg::CreatePersons(500))}>
{ "Create 500" }
</button>
</div>
<div class="col">
<button class="btn_size alert alert-success" onclick={link.callback(|_| Msg::CreatePersonsPrepend(1))}>
{ "Prepend 1" }
</button>
</div>
<div class="col">
<button class="btn_size alert alert-success" onclick={link.callback(|_| Msg::CreatePersonsPrepend(5))}>
{ "Prepend 5" }
</button>
</div>
</div>
<div class="row">
<div class="col">
<button class="btn_size alert alert-warning" onclick={link.callback(|_| Msg::ToggleKeyed)}>
{ if self.keyed { "Disable keys" } else { "Enable keys" } }
</button>
</div>
<div class="col">
<button class="btn_size alert alert-info" onclick={link.callback(|_| Msg::SwapRandom)}>
{ "Swap random" }
</button>
</div>
<div class="col">
<button class="btn_size alert alert-info" onclick={link.callback(|_| Msg::ReverseList)}>
{ "Reverse list" }
</button>
</div>
<div class="col">
<button class="btn_size alert alert-info" onclick={link.callback(|_| Msg::SortById)}>
{ "Sort by id" }
</button>
</div>
<div class="col">
<button class="btn_size alert alert-info" onclick={link.callback(|_| Msg::SortByName)}>
{ "Sort by name" }
</button>
</div>
<div class="col">
<button class="btn_size alert alert-info" onclick={link.callback(|_| Msg::SortByAge)}>
{ "Sort by age" }
</button>
</div>
<div class="col">
<button class="btn_size alert alert-info" onclick={link.callback(|_| Msg::SortByAddress)}>
{ "Sort by address" }
</button>
</div>
</div>
</>
}
}
fn info_view(&self) -> Html {
let ids = if self.persons.len() < 20 {
self.persons
.iter()
.map(|p| p.info().id.to_string())
.collect::<Vec<_>>()
.join(" ")
} else {
String::from("<too many>")
};
html! {
<div>
<p class="h5">{ "Number of persons: " }{ self.persons.len() }</p>
<p class="h5">{ "Ids: " }{ ids }</p>
<hr />
<div class="persons">
{ for self.persons.iter().map(|p| p.render(self.keyed)) }
</div>
</div>
}
}
}
fn main() {
wasm_logger::init(wasm_logger::Config::new(log::Level::Trace));
yew::Renderer::<App>::new().render();
}