-
Notifications
You must be signed in to change notification settings - Fork 0
/
big_list_widget.rs
354 lines (306 loc) · 10.9 KB
/
big_list_widget.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use log::{debug, error, warn};
use crate::config::theme::Theme;
use crate::experiments::screenspace::Screenspace;
use crate::experiments::subwidget_pointer::SubwidgetPointer;
use crate::io::input_event::InputEvent;
use crate::io::output::Output;
use crate::layout::layout::Layout;
use crate::layout::leaf_layout::LeafLayout;
use crate::layout::split_layout::{SplitDirection, SplitLayout, SplitRule};
use crate::primitives::arrow::Arrow;
use crate::primitives::rect::Rect;
use crate::primitives::scroll_enum::ScrollEnum;
use crate::primitives::xy::XY;
use crate::subwidget;
use crate::widget::any_msg::{AnyMsg, AsAny};
use crate::widget::complex_widget::{ComplexWidget, DisplayState};
use crate::widget::fill_policy::SizePolicy;
use crate::widget::widget::{get_new_widget_id, Widget, WID};
use crate::widgets::big_list::msg::BigListWidgetMsg;
use crate::widgets::text_widget::TextWidget;
/*
This is list of bigger items, to be paired with scroll.
*/
//TODO implement pg-up pg-down
pub struct BigList<T: Widget> {
//TODO I did not add the direction
wid: WID,
items: Vec<(SplitRule, T)>,
item_idx: usize,
last_size: Option<XY>,
no_items_text: TextWidget,
display_state: Option<DisplayState<Self>>,
kite: XY,
size_policy: SizePolicy,
}
impl<T: Widget> BigList<T> {
pub const TYPENAME: &'static str = "big_list";
pub fn new(items: Vec<(SplitRule, T)>) -> Self {
BigList {
wid: get_new_widget_id(),
items,
item_idx: 0,
last_size: None,
no_items_text: TextWidget::new(Box::new("empty")),
display_state: None,
kite: XY::ZERO,
size_policy: SizePolicy::MATCH_LAYOUTS_WIDTH,
}
}
fn will_accept(&self, se: &ScrollEnum) -> bool {
let can_go_up = self.item_idx > 0;
let can_go_down = self.item_idx + 1 < self.items.len();
match se {
ScrollEnum::Arrow(arrow) => match arrow {
Arrow::Up => can_go_up,
Arrow::Down => can_go_down,
Arrow::Left => false,
Arrow::Right => false,
},
ScrollEnum::Home => can_go_up,
ScrollEnum::End => can_go_down,
ScrollEnum::PageUp => can_go_up,
ScrollEnum::PageDown => can_go_down,
}
}
pub fn with_size_policy(self, size_policy: SizePolicy) -> Self {
Self { size_policy, ..self }
}
fn last_page_height(&self) -> Option<u16> {
self.last_size.map(|xy| xy.y)
}
fn get_item_widget_ptr(&self, idx: usize) -> SubwidgetPointer<Self> {
let idx2 = idx;
SubwidgetPointer::new(
Box::new(move |s: &Self| &s.items[idx].1),
Box::new(move |s: &mut Self| &mut s.items[idx2].1),
)
}
pub fn add_item(&mut self, split_rule: SplitRule, item: T) {
if self.items.is_empty() {
self.update_focus_path()
}
self.items.push((split_rule, item));
}
fn set_kite(&mut self, going_up: bool) {
if let Some(ds) = &self.display_state {
let mut rect_op: Option<Rect> = None;
let selected_id = self.items[self.item_idx].1.id();
for wwr in &ds.wwrs {
if wwr.widget().get(self).id() == selected_id {
rect_op = Some(wwr.rect().clone());
break;
}
}
if let Some(rect) = rect_op {
if going_up {
self.kite = rect.upper_left();
} else {
self.kite = XY::new(rect.upper_left().x, rect.lower_right().y);
}
} else {
error!("failed to set kite - id {} not found", selected_id);
}
}
}
pub fn items(&self) -> impl Iterator<Item = &T> {
self.items.iter().map(|(_split_rule, widget)| widget)
}
pub fn is_empty(&self) -> bool {
self.items().next().is_none()
}
fn update_focus_path(&mut self) {
let widget_ptr = self.get_item_widget_ptr(self.item_idx);
if let Some(ds) = self.display_state.as_mut() {
ds.focused = widget_ptr;
} else {
warn!("no display_state");
}
}
pub fn get_selected_id(&self) -> usize {
self.item_idx
}
pub fn get_selected_item(&self) -> &T {
//TODO unwrap
self.items.get(self.item_idx).map(|(_, item)| item).unwrap()
}
pub fn get_selected_item_mut(&mut self) -> &mut T {
//TODO unwrap
self.items.get_mut(self.item_idx).map(|(_, item)| item).unwrap()
}
}
impl<T: Widget> Widget for BigList<T> {
fn id(&self) -> WID {
self.wid
}
fn typename(&self) -> &'static str {
Self::TYPENAME
}
fn static_typename() -> &'static str
where
Self: Sized,
{
Self::TYPENAME
}
fn prelayout(&mut self) {
debug!("prelayout {}", self.typename());
self.complex_prelayout();
}
fn full_size(&self) -> XY {
warn!("using completely arbitrary value - expected to be filling the space");
XY::new(10, 4) // TODO completely arbitrary
}
fn layout(&mut self, screenspace: Screenspace) {
self.complex_layout(screenspace)
}
fn size_policy(&self) -> SizePolicy {
self.size_policy
}
fn on_input(&self, input_event: InputEvent) -> Option<Box<dyn AnyMsg>> {
match input_event {
InputEvent::KeyInput(key) => {
if let Some(se) = ScrollEnum::from_key(key) {
if self.will_accept(&se) {
BigListWidgetMsg::Scroll(se).someboxed()
} else {
None
}
} else {
None
}
}
_ => None,
}
}
fn update(&mut self, msg: Box<dyn AnyMsg>) -> Option<Box<dyn AnyMsg>> {
let our_msg = msg.as_msg::<BigListWidgetMsg>();
if our_msg.is_none() {
warn!("expecetd ListWidgetMsg, got {:?}", msg);
return None;
}
return match our_msg.unwrap() {
BigListWidgetMsg::Scroll(se) => {
match se {
ScrollEnum::Arrow(arrow) => match arrow {
Arrow::Up => {
if self.item_idx > 0 {
self.item_idx -= 1;
self.update_focus_path();
self.set_kite(true);
} else {
warn!("arrow up widget can't handle");
}
None
}
Arrow::Down => {
if self.item_idx + 1 < self.items.len() {
self.item_idx += 1;
self.update_focus_path();
self.set_kite(false);
} else {
warn!("arrow down widget can't handle");
}
None
}
_ => None,
},
ScrollEnum::Home => {
if self.item_idx > 0 {
self.item_idx = 0;
self.update_focus_path();
self.set_kite(true);
} else {
warn!("home widget can't handle");
}
None
}
ScrollEnum::End => {
if self.item_idx + 1 < self.items.len() {
self.item_idx = self.items.len() - 1;
self.update_focus_path();
self.set_kite(false);
} else {
warn!("end widget can't handle");
}
None
}
ScrollEnum::PageUp => {
if let Some(_height) = self.last_page_height() {
if self.item_idx > 0 {
// if self.pos < height {
// self.pos = 0
// } else {
//
// }
} else {
warn!("page_up widget can't handle")
}
} else {
warn!("page_up prior layout")
}
None
}
ScrollEnum::PageDown => {
if let Some(_height) = self.last_page_height() {
} else {
warn!("page_down prior layout")
}
None
}
}
}
};
}
fn get_focused(&self) -> Option<&dyn Widget> {
self.complex_get_focused()
}
fn get_focused_mut(&mut self) -> Option<&mut dyn Widget> {
self.complex_get_focused_mut()
}
fn render(&self, theme: &Theme, focused: bool, output: &mut dyn Output) {
#[cfg(test)]
{
let total_size = self.display_state.as_ref().unwrap().total_size;
output.emit_metadata(crate::io::output::Metadata {
id: self.wid,
typename: self.typename().to_string(),
rect: Rect::from_zero(total_size),
focused,
});
}
self.complex_render(theme, focused, output)
}
fn kite(&self) -> XY {
self.kite
}
}
impl<T: Widget> ComplexWidget for BigList<T> {
fn get_layout(&self) -> Box<dyn Layout<Self>> {
if self.items.is_empty() {
LeafLayout::new(subwidget!(Self.no_items_text)).boxed()
} else {
let mut spl = SplitLayout::new(SplitDirection::Vertical);
for idx in 0..self.items.len() {
let rule = self.items[idx].0;
spl = spl.with(rule, LeafLayout::new(self.get_item_widget_ptr(idx)).boxed());
}
spl.boxed()
}
}
fn get_default_focused(&self) -> SubwidgetPointer<Self> {
if self.items.is_empty() {
subwidget!(Self.no_items_text)
} else {
self.get_item_widget_ptr(0)
}
}
fn set_display_state(&mut self, display_state: DisplayState<Self>) {
self.display_state = Some(display_state);
}
fn get_display_state_op(&self) -> Option<&DisplayState<Self>> {
self.display_state.as_ref()
}
fn get_display_state_mut_op(&mut self) -> Option<&mut DisplayState<Self>> {
self.display_state.as_mut()
}
}