-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathdata-list.rs
More file actions
188 lines (168 loc) · 5.48 KB
/
Copy pathdata-list.rs
File metadata and controls
188 lines (168 loc) · 5.48 KB
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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
// https://www.apache.org/licenses/LICENSE-2.0
//! Data list example (direct representation)
//!
//! Objective: test performance using a naive list design; stress test with
//! ridiculous numbers of widgets.
//!
//! Compare: `data-list-view.rs` has the same functionality but with a dynamic
//! view, and thus scales *much* better to large numbers of rows.
//!
//! Results (debug build): 1000 entries requires approx 1s for init and may
//! have a small delay on resize but is otherwise very fast.
//!
//! Results (release build): 1k entries is fast, 10k has some noticeable lag
//! (changing the list length and resizing).
//! 50k entries (200k widgets) has slow init and resize but most interaction
//! is still fast.
use kas::prelude::*;
use kas::widgets::edit::{EditBox, EditGuard, Editor};
use kas::widgets::{Button, Label, List, RadioButton, ScrollRegion, Separator, Text};
use kas::widgets::{column, row};
#[derive(Debug)]
struct SelectEntry(usize);
#[derive(Clone, Debug)]
enum Control {
None,
SetLen(usize),
DecrLen,
IncrLen,
Reverse,
Select(usize, String),
UpdateCurrent(String),
}
#[derive(Debug)]
struct Data {
len: usize,
active: usize,
dir: Direction,
active_string: String,
}
impl Data {
fn handle(&mut self, control: Control) {
let len = match control {
Control::None => return,
Control::SetLen(len) => len,
Control::DecrLen => self.len.saturating_sub(1),
Control::IncrLen => self.len.saturating_add(1),
Control::Reverse => {
self.dir = self.dir.reversed();
return;
}
Control::Select(index, text) => {
self.active = index;
self.active_string = text;
return;
}
Control::UpdateCurrent(text) => {
self.active_string = text;
return;
}
};
self.len = len;
if self.active >= len && len > 0 {
self.active = len - 1;
// NOTE: We should update self.active_string here but we cannot
// access the newly active widget's data from here.
}
}
}
#[derive(Debug)]
struct ListEntryGuard(usize);
impl EditGuard for ListEntryGuard {
type Data = Data;
fn activate(&mut self, _: &mut Editor, cx: &mut EventCx, _: &Data) -> IsUsed {
cx.push(SelectEntry(self.0));
Used
}
fn edit(&mut self, edit: &mut Editor, cx: &mut EventCx, data: &Data) {
if data.active == self.0 {
cx.push(Control::UpdateCurrent(edit.as_str().to_string()));
}
}
}
#[impl_self]
mod ListEntry {
// The list entry
#[widget]
#[layout(column! [
row! [self.label, self.radio],
self.edit,
])]
struct ListEntry {
core: widget_core!(),
#[widget(&())]
label: Label<String>,
#[widget(&data.active)]
radio: RadioButton<usize>,
#[widget]
edit: EditBox<ListEntryGuard>,
}
impl Events for Self {
type Data = Data;
fn handle_messages(&mut self, cx: &mut EventCx, data: &Data) {
if let Some(SelectEntry(n)) = cx.try_pop() {
if data.active != n {
cx.push(Control::Select(n, self.edit.as_str().to_string()));
}
}
}
}
impl Self {
fn new(n: usize) -> Self {
ListEntry {
core: Default::default(),
label: Label::new(format!("Entry number {}", n + 1)),
radio: RadioButton::new_msg(
"display this entry",
move |_, active| *active == n,
move || SelectEntry(n),
),
edit: EditBox::new(ListEntryGuard(n)).with_text(format!("Entry #{}", n + 1)),
}
}
}
}
fn main() -> kas::runner::Result<()> {
env_logger::init();
let controls = row![
"Number of rows:",
EditBox::parser(|n| *n, Control::SetLen),
row![
// This button is just a click target; it doesn't do anything!
Button::label_msg("Set", Control::None),
Button::label_msg("−", Control::DecrLen),
Button::label_msg("+", Control::IncrLen),
Button::label_msg("↓↑", Control::Reverse),
]
.map_any(),
];
let data = Data {
len: 5,
active: 0,
dir: Direction::Down,
active_string: ListEntry::new(0).label.as_str().to_string(),
};
let list = List::new(vec![]).on_update(|cx, list, data: &Data| {
list.set_direction(cx, data.dir);
let len = data.len;
if len != list.len() {
list.resize_with(cx, data, len, ListEntry::new);
}
});
let tree = column![
"Demonstration of dynamic widget creation / deletion",
controls.map(|data: &Data| &data.len),
"Contents of selected entry:",
Text::new_str(|data: &Data| &data.active_string),
Separator::new(),
ScrollRegion::new_clip(list).with_fixed_bars(false, true),
];
let ui = tree
.with_state(data)
.on_message(|_, data, control| data.handle(control));
let window = Window::new(ui, "Dynamic widget demo");
kas::runner::Runner::new(())?.with(window).run()
}