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
355
use crate::scrollview::*;
use zaplib::*;

#[derive(Default)]
pub struct List {
    pub multi_select: bool,
    pub list_items: Vec<ListItem>,
    pub scroll_item_in_view: Option<usize>,
    pub set_scroll_pos: Option<Vec2>,
    //pub tail_list: bool,
    pub start_item: usize,
    pub end_item: usize,
    pub end_fill: usize,
    pub selection: Vec<usize>,
    //pub last_range: Option<(usize, usize)>,
}

#[derive(Default)]
pub struct ListItem {
    component_id: ComponentId,
    area: Area,
    pub animator: Animator,
    pub is_selected: bool,
}

impl ListItem {
    pub fn area(&self) -> Area {
        self.area
    }

    pub fn set_area(&mut self, area: Area) {
        self.area = area;
    }
}

#[derive(Debug)]
pub enum ListLogicEvent {
    Animate,
    Select,
    Deselect,
    Cleanup,
    Over,
    Out,
}

pub enum ListEvent {
    SelectSingle(usize),
    SelectDouble(usize),
    SelectMultiple,
    None,
}

#[derive(PartialEq)]
pub enum ListSelect {
    None,
    Single(usize),
    Range(usize),
    Toggle(usize),
    All,
}

impl ListSelect {
    pub fn item_index(&self) -> Option<usize> {
        match self {
            ListSelect::Single(index) => Some(*index),
            _ => None,
        }
    }
}

impl List {
    #[must_use]
    pub fn with_multi_select(self, multi_select: bool) -> Self {
        Self { multi_select, ..self }
    }

    pub fn set_list_len(&mut self, len: usize) {
        if self.list_items.len() != len {
            self.selection.truncate(0);
        }
        if self.list_items.len() < len {
            for _ in self.list_items.len()..len {
                self.list_items.push(ListItem::default())
            }
        } else {
            self.list_items.truncate(len);
        }
    }

    pub fn handle_list_scroll_bars(&mut self, cx: &mut Cx, event: &mut Event, view: &mut ScrollView) -> bool {
        if view.handle(cx, event) {
            cx.request_draw();
            match &event {
                Event::PointerScroll { .. } => {
                    return true;
                }
                Event::PointerMove { .. } => {
                    return true;
                }
                _ => (),
            }
        }
        false
    }

    pub fn begin_list(&mut self, cx: &mut Cx, view: &mut ScrollView, tail_list: bool, row_height: f32) {
        view.begin_view(cx, LayoutSize::FILL);
        self.set_visible_range_and_scroll(cx, view, tail_list, row_height);
    }

    pub fn walk_box_to_end(&mut self, cx: &mut Cx, row_height: f32) {
        let left = (self.list_items.len() - self.end_item) as f32 * row_height;
        cx.add_box(LayoutSize::new(Width::Fill, Height::Fix(left)));
    }

    pub fn end_list(&mut self, cx: &mut Cx, view: &mut ScrollView) {
        view.end_view(cx);
        if let Some(set_scroll_pos) = self.set_scroll_pos {
            view.set_scroll_pos(cx, set_scroll_pos);
        }
    }

    pub fn set_visible_range_and_scroll(&mut self, cx: &mut Cx, view: &mut ScrollView, tail_list: bool, row_height: f32) {
        let view_rect = cx.get_box_rect();

        // the maximum scroll position given the amount of log items
        let max_scroll_y = ((self.list_items.len() + 1) as f32 * row_height - view_rect.size.y).max(0.);

        // tail the log
        let (scroll_pos, set_scroll_pos) = if tail_list {
            (Vec2 { x: 0., y: max_scroll_y }, true)
        } else {
            let sp = view.get_scroll_pos(cx);

            // scroll item into view
            if let Some(scroll_item_in_view) = self.scroll_item_in_view {
                self.scroll_item_in_view = None;
                let item_y = scroll_item_in_view as f32 * row_height;
                let dy = (item_y + row_height) - (sp.y + view_rect.size.y);
                if item_y < sp.y {
                    (Vec2 { x: 0., y: item_y }, true)
                } else if dy > 0. {
                    (Vec2 { x: 0., y: sp.y + dy }, true)
                } else {
                    (sp, false)
                }
            } else {
                // clamp the scrollbar to our max list size
                if sp.y > max_scroll_y {
                    (Vec2 { x: 0., y: max_scroll_y }, false)
                } else {
                    (sp, false)
                }
            }
        };

        let start_item = (scroll_pos.y / row_height).floor() as usize;
        let end_item = ((scroll_pos.y + view_rect.size.y + row_height) / row_height).ceil() as usize;

        self.start_item = start_item.min(self.list_items.len());
        self.end_fill = end_item;
        self.end_item = end_item.min(self.list_items.len());

        let start_scroll = (self.start_item as f32) * row_height;
        if set_scroll_pos {
            self.set_scroll_pos = Some(scroll_pos);
        } else {
            self.set_scroll_pos = None;
        }
        // lets move the draw position forward by scrollpos.y
        cx.move_draw_pos(0., start_scroll);
    }

    pub fn get_next_single_selection(&self) -> ListSelect {
        if let Some(last) = self.selection.last() {
            let next = last + 1;
            if next >= self.list_items.len() {
                // wrap around
                ListSelect::Single(*last)
            } else {
                ListSelect::Single(next)
            }
        } else {
            println!("GOT SELECT 0");
            ListSelect::Single(0)
        }
    }

    pub fn get_prev_single_selection(&self) -> ListSelect {
        if let Some(first) = self.selection.last() {
            if *first == 0 {
                // wrap around
                ListSelect::Single(*first) //self.list_items.len().max(1) - 1)
            } else {
                ListSelect::Single(first - 1)
            }
        } else {
            ListSelect::Single(0)
        }
    }

    pub fn handle_list_logic<F>(
        &mut self,
        cx: &mut Cx,
        event: &mut Event,
        select: ListSelect,
        mut dblclick: bool,
        mut cb: F,
    ) -> ListEvent
    where
        F: FnMut(&mut Cx, ListLogicEvent, &mut ListItem, usize),
    {
        let mut select = select;
        for counter in self.start_item..self.end_item {
            if counter >= self.list_items.len() {
                break;
            }
            let item = &mut self.list_items[counter];
            if item.animator.handle(cx, event) {
                cb(cx, ListLogicEvent::Animate, item, counter)
            }
            match event.hits_pointer(cx, item.component_id, item.area.get_rect_for_first_instance(cx)) {
                Event::PointerDown(pe) => {
                    cx.set_down_mouse_cursor(MouseCursor::Hand);
                    if self.multi_select && (pe.modifiers.logo || pe.modifiers.control) {
                        select = ListSelect::Toggle(counter)
                    } else if self.multi_select && pe.modifiers.shift {
                        select = ListSelect::Range(counter)
                    } else {
                        select = ListSelect::Single(counter);
                        if pe.tap_count > 1 {
                            dblclick = true;
                        }
                    }
                }
                Event::PointerUp(_pe) => {}
                Event::PointerMove(_pe) => {}
                Event::PointerHover(pe) => {
                    cx.set_hover_mouse_cursor(MouseCursor::Hand);
                    match pe.hover_state {
                        HoverState::In => {
                            cb(cx, ListLogicEvent::Over, item, counter);
                        }
                        HoverState::Out => {
                            cb(cx, ListLogicEvent::Out, item, counter);
                        }
                        _ => (),
                    }
                }
                _ => (),
            }
        }

        // clean up outside of window
        /*
        if let Some(last_range) = self.last_range {
            for counter in last_range.0..last_range.1 {
                if counter >= self.list_items.len() {
                    break;
                }
                if counter < self.start_item || counter >= self.end_item {
                    let dm = &mut self.list_items[counter];
                    cb(cx, ListLogicEvent::Deselect, dm, counter);
                }
            }
        }
        self.last_range = Some((self.start_item, self.end_item));
        */
        match select {
            ListSelect::Range(select_index) => {
                if let Some(first) = self.selection.first() {
                    if let Some(last) = self.selection.last() {
                        let (start, end) = if select_index < *first {
                            (select_index, *last)
                        } else if select_index > *last {
                            (*first, select_index)
                        } else {
                            (select_index, select_index)
                        };

                        for counter in &self.selection {
                            if *counter >= self.list_items.len() || *counter >= start && *counter <= end {
                                continue;
                            }
                            let dm = &mut self.list_items[*counter];
                            if *counter != select_index {
                                dm.is_selected = false;
                                cb(cx, ListLogicEvent::Deselect, dm, select_index);
                            }
                        }
                        self.selection.truncate(0);
                        for i in start..=end {
                            let dm = &mut self.list_items[i];
                            dm.is_selected = true;
                            cb(cx, ListLogicEvent::Select, dm, i);
                            self.selection.push(i);
                        }
                    }
                }
            }
            ListSelect::Toggle(select_index) => {
                let dm = &mut self.list_items[select_index];
                if dm.is_selected {
                    dm.is_selected = false;
                    cb(cx, ListLogicEvent::Deselect, dm, select_index);
                    if let Some(pos) = self.selection.iter().position(|v| *v == select_index) {
                        self.selection.remove(pos);
                    }
                } else {
                    self.selection.push(select_index);
                    dm.is_selected = true;
                    cb(cx, ListLogicEvent::Over, dm, select_index);
                }
            }
            ListSelect::All => {
                self.selection.truncate(0);
                for i in 0..self.list_items.len() {
                    self.selection.push(i);
                    let dm = &mut self.list_items[i];
                    dm.is_selected = true;
                    cb(cx, ListLogicEvent::Over, dm, i);
                }
            }
            ListSelect::Single(select_index) => {
                for counter in &self.selection {
                    if *counter >= self.list_items.len() {
                        continue;
                    }
                    let dm = &mut self.list_items[*counter];
                    if *counter != select_index {
                        dm.is_selected = false;
                        cb(cx, ListLogicEvent::Cleanup, dm, *counter);
                    }
                }
                self.selection.truncate(0);
                if select_index < self.list_items.len() {
                    self.selection.push(select_index);
                    let dm = &mut self.list_items[select_index];
                    dm.is_selected = true;
                    cb(cx, ListLogicEvent::Over, dm, select_index);
                    if dblclick {
                        return ListEvent::SelectDouble(select_index);
                    } else {
                        return ListEvent::SelectSingle(select_index);
                    }
                }
            }
            _ => (),
        }
        match select {
            ListSelect::Range(_) | ListSelect::Toggle(_) | ListSelect::All => ListEvent::SelectMultiple,
            _ => ListEvent::None,
        }
    }
}