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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
use std::collections::VecDeque;
use std::fmt::Debug;
use std::ops::Range;

use cushy::context::LayoutContext;
use cushy::ConstraintLimit;
use figures::IntoUnsigned;

use super::scroll::OwnedWidget;
use crate::context::{AsEventContext, EventContext, Trackable};
use crate::figures::units::{Px, UPx};
use crate::figures::{IntoSigned, Point, Rect, Round, Size, Zero};
use crate::kludgine::app::winit::event::{MouseScrollDelta, TouchPhase};
use crate::kludgine::app::winit::window::CursorIcon;
use crate::value::{
    Destination, Dynamic, DynamicReader, IntoDynamic, IntoValue, MapEachCloned, Source, Watcher,
};
use crate::widget::{
    Callback, EventHandling, MakeWidget, MountedWidget, Widget, WidgetInstance, HANDLED, IGNORED,
};
use crate::widgets::scroll::ScrollBar;
use crate::window::DeviceId;

#[derive(Debug)]
struct RowMaker(Callback<usize, WidgetInstance>);

impl RowMaker {
    fn make_row(
        &mut self,
        index: usize,
        context: &mut LayoutContext<'_, '_, '_, '_>,
    ) -> VirtualListItem {
        VirtualListItem {
            index,
            mounted: context.push_child(self.0.invoke(index)),
        }
    }
}

#[derive(Debug)]
struct VirtualListItem {
    index: usize,
    mounted: MountedWidget,
}

#[derive(Debug)]
/// A virtuallized list view
///
/// This widget allows scrolling a list of rows by lazily loading only the rows
/// that are currently being displayed to the screen.
pub struct VirtualList {
    make_row: RowMaker,
    vertical_scroll: OwnedWidget<ScrollBar>,
    horizontal_scroll: OwnedWidget<ScrollBar>,
    items: VecDeque<VirtualListItem>,
    content_size: Dynamic<Size<UPx>>,
    contents: Watcher,
    contents_generation: usize,
    /// Maximum scroll value - `max_scroll.y` + `control_size.height` should be
    /// the height of the content.
    pub max_scroll: DynamicReader<Point<UPx>>,
    /// Current scroll value. Changes to this dynamic will scroll the list
    /// programmatically.
    pub scroll: Dynamic<Point<UPx>>,
    control_size: Dynamic<Size<UPx>>,

    item_count: DynamicReader<usize>,
    item_size: Dynamic<Size<UPx>>,

    visible_range: Dynamic<Range<usize>>,
}

impl VirtualList {
    /// Creates a new [`VirtualList`] that displays `item_count` rows, loading
    /// each row as needed by invoking `make_row`.
    ///
    /// `make_row` will be called each time a new row becomes visible. As rows
    /// are no longer visible, they will be freed, ensuring a minimum number of
    /// widgets is kept in memory at any given time.
    ///
    /// Each row will be sized to match the first visible row. To ensure all
    /// rows have a consistent size, use the [`Resize`](../Resize) widget.
    pub fn new<MakeRow, Row>(item_count: impl IntoValue<usize>, mut make_row: MakeRow) -> Self
    where
        MakeRow: FnMut(usize) -> Row + Send + 'static,
        Row: MakeWidget,
    {
        let make_row = RowMaker(Callback::new(move |row| make_row(row).make_widget()));
        let scroll = Dynamic::<Point<UPx>>::default();
        let item_size = Dynamic::new(Size::ZERO);
        let item_count = item_count.into_value().into_dynamic().into_reader();
        let content_size = Dynamic::new(Size::default());

        let x = scroll.map_each_cloned(|scroll| scroll.x);
        x.for_each_cloned({
            let scroll = scroll.clone();
            move |x| {
                if let Ok(mut scroll) = scroll.try_lock() {
                    if scroll.x != x {
                        scroll.x = x;
                    }
                }
            }
        })
        .persist();
        let y = scroll.map_each_cloned(|scroll| scroll.y);
        y.for_each_cloned({
            let scroll = scroll.clone();
            move |y| {
                if let Ok(mut scroll) = scroll.try_lock() {
                    if scroll.y != y {
                        scroll.y = y;
                    }
                }
            }
        })
        .persist();
        let horizontal = ScrollBar::new(content_size.map_each_cloned(|size| size.width), x, false);
        let mut vertical =
            ScrollBar::new(content_size.map_each_cloned(|size| size.height), y, true);
        vertical.synchronize_visibility_with(&horizontal);
        let max_scroll = (&horizontal.max_scroll(), &vertical.max_scroll())
            .map_each_cloned(|(x, y)| Point::new(x, y))
            .into_reader();

        let contents = Watcher::default();
        let contents_generation = contents.get();

        Self {
            make_row,
            contents,
            contents_generation,
            vertical_scroll: OwnedWidget::new(vertical),
            horizontal_scroll: OwnedWidget::new(horizontal),
            items: VecDeque::new(),
            control_size: Dynamic::new(Size::default()),
            content_size,
            max_scroll,
            scroll,

            item_size,
            item_count,
            visible_range: Dynamic::default(),
        }
    }

    /// Returns a [`Watcher`] that when notified will force this list to reload
    /// its contents, including the currently visible rows.
    pub const fn content_watcher(&self) -> &Watcher {
        &self.contents
    }

    /// Returns a reader for the maximum scroll value.
    ///
    /// This represents the maximum amount that the scroll can be moved by.
    #[must_use]
    pub const fn max_scroll(&self) -> &DynamicReader<Point<UPx>> {
        &self.max_scroll
    }

    /// Returns a reader for the size of the scrollable area.
    #[must_use]
    pub fn content_size(&self) -> DynamicReader<Size<UPx>> {
        self.content_size.create_reader()
    }

    /// Returns a reader for the size of this Scroll widget.
    #[must_use]
    pub fn control_size(&self) -> DynamicReader<Size<UPx>> {
        self.control_size.create_reader()
    }

    /// Returns a reader for number of visible items. 0 indexed.
    #[must_use]
    pub fn visible_range(&self) -> DynamicReader<Range<usize>> {
        self.visible_range.create_reader()
    }

    fn show_scrollbars(&mut self, context: &mut EventContext<'_>) {
        let mut vertical = self.vertical_scroll.expect_made_mut().widget().lock();
        vertical
            .downcast_mut::<ScrollBar>()
            .expect("a ScrollBar")
            .show(context);
    }

    fn hide_scrollbars(&mut self, context: &mut EventContext<'_>) {
        let mut vertical = self.vertical_scroll.expect_made_mut().widget().lock();
        vertical
            .downcast_mut::<ScrollBar>()
            .expect("a ScrollBar")
            .hide(context);
    }

    fn clear(&mut self, context: &mut LayoutContext<'_, '_, '_, '_>) {
        for item in self.items.drain(..) {
            context.remove_child(&item.mounted);
        }
    }

    fn layout_scrollbars(
        &mut self,
        available_space: Size<ConstraintLimit>,
        new_control_size: Size<UPx>,
        context: &mut LayoutContext<'_, '_, '_, '_>,
    ) {
        let horizontal = self
            .horizontal_scroll
            .make_if_needed()
            .mounted(&mut context.as_event_context());
        let scrollbar_layout = context.for_other(&horizontal).layout(available_space);
        context.set_child_layout(
            &horizontal,
            Rect::new(
                Point::new(
                    Px::ZERO,
                    available_space
                        .height
                        .fit_measured(new_control_size.height)
                        .saturating_sub(scrollbar_layout.height)
                        .into_signed(),
                ),
                scrollbar_layout.into_signed(),
            ),
        );
        let vertical = self
            .vertical_scroll
            .make_if_needed()
            .mounted(&mut context.as_event_context());
        let scrollbar_layout = context.for_other(&vertical).layout(available_space);
        context.set_child_layout(
            &vertical,
            Rect::new(
                Point::new(
                    available_space
                        .width
                        .fit_measured(new_control_size.width)
                        .saturating_sub(scrollbar_layout.width)
                        .into_signed(),
                    Px::ZERO,
                ),
                scrollbar_layout.into_signed(),
            ),
        );
    }

    fn layout_rows(
        &mut self,
        item_count: usize,
        available_space: Size<ConstraintLimit>,
        context: &mut LayoutContext<'_, '_, '_, '_>,
    ) -> Size<UPx> {
        let generation = self.contents.get_tracking_redraw(context);
        if generation != self.contents_generation {
            self.contents_generation = generation;
            self.clear(context);
        }
        let mut item_size = self.calculate_item_size(available_space, context).ceil();

        let content_height = item_size.height * u32::try_from(item_count).unwrap_or(u32::MAX);
        let content_height = content_height.into_unsigned();

        let new_control_size = Size::new(
            available_space.width.fill_or_fit(item_size.width),
            available_space.height.fill_or_fit(content_height),
        )
        .ceil();
        if item_size.width < new_control_size.width {
            item_size.width = new_control_size.width;
        }

        self.layout_scrollbars(available_space, new_control_size, context);
        let scroll = self.scroll.get_tracking_invalidate(context);

        let max_scroll_x = item_size.width.saturating_sub(new_control_size.width);
        let max_scroll_y = content_height.saturating_sub(new_control_size.height);
        let scroll = scroll.min(Point::new(max_scroll_x, max_scroll_y));

        let start_item = (scroll.y.floor() / item_size.height).floor().get() as usize;
        let end_item = ((scroll.y.ceil() + new_control_size.height) / item_size.height)
            .ceil()
            .get() as usize;
        let end_item = end_item.min(item_count - 1);

        self.visible_range.set(start_item..end_item);

        let first = self.items.front().map(|t| t.index);
        let last = self.items.back().map(|t| t.index);

        if self.items.is_empty() || first.unwrap() > end_item || last.unwrap() < start_item {
            self.clear(context);
            self.items.extend(
                (start_item..=end_item).map(|index| self.make_row.make_row(index, context)),
            );
        } else {
            let first = first.expect("List is not empty");
            let last = last.expect("List is not empty");
            while self
                .items
                .front()
                .map_or(false, |item| item.index < start_item)
            {
                context.remove_child(&self.items.pop_front().expect("at least one item").mounted);
            }
            while self
                .items
                .back()
                .map_or(false, |item| item.index > end_item)
            {
                context.remove_child(&self.items.pop_back().expect("at least one item").mounted);
            }
            // no extend front :(
            for item in (start_item..first).rev() {
                self.items.push_front(self.make_row.make_row(item, context));
            }
            self.items.extend(
                ((last + 1)..=end_item).map(|index| self.make_row.make_row(index, context)),
            );
        }

        let x = -scroll.x.into_signed();
        let mut y = -(scroll.y % item_size.height).into_signed();
        let constraint = item_size.map(ConstraintLimit::Fill);
        for item in &self.items {
            let child_size = context.for_other(&item.mounted).layout(constraint);

            context.set_child_layout(
                &item.mounted,
                Rect::new(Point::new(x, y), item_size.min(child_size).into_signed()),
            );
            y += item_size.height.into_signed();
        }

        self.control_size.set(new_control_size);
        self.content_size
            .set(Size::new(item_size.width, content_height));
        self.item_size.set(item_size);

        new_control_size
    }

    fn calculate_item_size(
        &mut self,
        available_space: Size<ConstraintLimit>,
        context: &mut LayoutContext<'_, '_, '_, '_>,
    ) -> Size<UPx> {
        if self.items.is_empty() {
            self.items.push_front(self.make_row.make_row(0, context));
        }

        context
            .for_other(
                &self
                    .items
                    .front()
                    .expect("at least one mounted item")
                    .mounted,
            )
            .layout(available_space.map(|space| ConstraintLimit::SizeToFit(space.max())))
    }
}

impl Widget for VirtualList {
    fn hit_test(&mut self, _location: Point<Px>, _context: &mut EventContext<'_>) -> bool {
        true
    }

    fn hover(
        &mut self,
        _location: Point<Px>,
        context: &mut EventContext<'_>,
    ) -> Option<CursorIcon> {
        self.show_scrollbars(context);

        None
    }

    fn unhover(&mut self, context: &mut EventContext<'_>) {
        self.hide_scrollbars(context);
    }

    fn mounted(&mut self, context: &mut EventContext<'_>) {
        for child in &mut self.items {
            child.mounted.remount_if_needed(context);
        }
    }

    fn redraw(&mut self, context: &mut cushy::context::GraphicsContext<'_, '_, '_, '_>) {
        self.item_count.invalidate_when_changed(context);
        self.contents.invalidate_when_changed(context);
        for child in &mut self.items {
            context.for_other(&child.mounted).redraw();
        }
        let vertical = self
            .vertical_scroll
            .expect_made_mut()
            .mounted(&mut context.as_event_context());
        context.for_other(&vertical).redraw();
        let horizontal = self
            .horizontal_scroll
            .expect_made_mut()
            .mounted(&mut context.as_event_context());
        context.for_other(&horizontal).redraw();
    }

    fn layout(
        &mut self,
        available_space: Size<ConstraintLimit>,
        context: &mut LayoutContext<'_, '_, '_, '_>,
    ) -> Size<UPx> {
        let item_count = self.item_count.get_tracking_invalidate(context);
        if item_count == 0 {
            return available_space.map(ConstraintLimit::min);
        }

        self.layout_rows(item_count, available_space, context)
    }

    fn mouse_wheel(
        &mut self,
        _device_id: DeviceId,
        delta: MouseScrollDelta,
        _phase: TouchPhase,
        context: &mut EventContext<'_>,
    ) -> EventHandling {
        let mut handled = false;
        {
            let mut vertical = self.vertical_scroll.expect_made().widget().lock();
            handled |= vertical
                .downcast_mut::<ScrollBar>()
                .expect("a ScrollBar")
                .mouse_wheel(delta, context)
                .is_break();
            let mut horizontal = self.horizontal_scroll.expect_made().widget().lock();
            handled |= horizontal
                .downcast_mut::<ScrollBar>()
                .expect("a ScrollBar")
                .mouse_wheel(delta, context)
                .is_break();
        }
        if handled {
            self.show_scrollbars(context);
            context.set_needs_redraw();

            HANDLED
        } else {
            IGNORED
        }
    }
}