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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! Adapters enable an arbitrarily large scrolling list of widgets to be
//! created using finite memory.
//!
//! See `examples/adapter.rs` for an example.
//!
//! An Adapter view maintains a collection of widgets which are re-used as
//! the view is scrolled to display new content.
//!
//! The Adaptable trait is the primary way for a Widget to update it's visuals
//! in response to a change in an external data source.

use std::collections::HashMap;

use crate::dims::Rect;
use crate::platform::{DefaultRenderPlatform, RenderPlatform};
use crate::pointer::PointerId;
use crate::watch::WatchedMeta;
use crate::widget::{
    Widget, WidgetChildReceiver, WidgetContent, WidgetGraphicReceiver,
    WidgetInit, WidgetRect,
};

/// Trait representing some view which may "adapt" to a specific change in
/// external data.
///
/// See the [module-level documentation](./index.html) for more details.
pub trait Adaptable<T: ?Sized> {
    /// Update `self` in acordance with the provided `data`
    fn adapt(&mut self, data: &T);

    /// Create a new instance from provided `data`
    fn from(data: &T) -> Self;
}

/// An implementation of this trait is passed to AdapterLayout implementations.
///
/// It provides the utilities an adapter layout uses to control the view.
pub trait AdapterLayoutInterface<Layout: AdapterLayout + ?Sized> {
    /// A rectangle which represents the extants of the view. Returned by
    /// the `bounds` method.
    type Bounds: Rect;

    /// A rectangle which represents an individual element to be positioned
    /// by the layout.
    type Element: Rect;

    /// Get the position last set with `update_reference`, offset by any
    /// scrolling that has happened since.
    fn reference_position(&self) -> (f32, f32);

    /// Get the rectangle which represents the extants of the view.
    fn bounds(&self) -> &Self::Bounds;

    /// Update the reference and rest positions.
    ///
    /// The rest position can be used in the case of overscroll to return the
    /// list to a preferred position.
    fn update_positions(
        &mut self,
        reference_position: (f32, f32),
        rest_position: (f32, f32),
    );

    /// Get the number of times get_element has been called during this
    /// layout operation.
    fn num_active_elements(&self) -> usize;

    /// Get or construct a widget from some data.
    fn get_element(
        &mut self,
        key: Layout::ElementKey,
        data: &Layout::ElementData,
    ) -> &mut Self::Element;
}

/// An adapter layout defines how elements in the adapter view are organized.
pub trait AdapterLayout {
    /// The key for looking up existing widgets.
    type ElementKey: std::hash::Hash + Eq;

    /// The type of the collection the layout understands.
    type Collection: ?Sized;

    /// The type of the data widgets will need to adapt to.
    type ElementData;

    /// Get a reference to the collection.
    fn data(&self) -> &Self::Collection;

    /// Get a mutable reference to the collection.
    fn data_mut(&mut self) -> &mut Self::Collection;

    /// Execute the layout, using the provided interface to position elements.
    fn layout(&mut self, interface: impl AdapterLayoutInterface<Self>);

    /// Get an approximate location of an element that is not currently in
    /// view.
    fn element_location(
        &mut self,
        item: &Self::ElementKey,
        reference_position: (f32, f32),
    ) -> Option<(f32, f32)>;
}

struct AdapterData<Layout, Content, Platform>
where
    Layout: AdapterLayout,
    Content: WidgetContent<Platform> + Adaptable<Layout::ElementData>,
    Platform: RenderPlatform,
{
    active: HashMap<Layout::ElementKey, Widget<Content, Platform>>,
    inactive: Vec<Widget<Content, Platform>>,
    child_flag: WatchedMeta,
    position: (f32, f32),
    rest_position: (f32, f32),
}

struct Interface<'a, Layout, Content, Platform>
where
    Layout: AdapterLayout,
    Content: WidgetContent<Platform> + Adaptable<Layout::ElementData>,
    Platform: RenderPlatform,
{
    rect: &'a WidgetRect,
    data: &'a mut AdapterData<Layout, Content, Platform>,
    prev: HashMap<Layout::ElementKey, Widget<Content, Platform>>,
}

impl<'a, Layout, Content, Platform> AdapterLayoutInterface<Layout>
    for Interface<'a, Layout, Content, Platform>
where
    Layout: AdapterLayout,
    Content: WidgetContent<Platform> + Adaptable<Layout::ElementData>,
    Platform: RenderPlatform,
{
    type Bounds = WidgetRect;
    type Element = Widget<Content, Platform>;

    fn reference_position(&self) -> (f32, f32) {
        self.data.position
    }

    fn bounds(&self) -> &WidgetRect {
        &self.rect
    }

    fn update_positions(
        &mut self,
        position: (f32, f32),
        rest_position: (f32, f32),
    ) {
        self.data.position = position;
        self.data.rest_position = rest_position;
    }

    fn num_active_elements(&self) -> usize {
        self.data.active.len()
    }

    fn get_element(
        &mut self,
        key: Layout::ElementKey,
        data: &Layout::ElementData,
    ) -> &mut Self::Element {
        use std::collections::hash_map::Entry;
        match self.data.active.entry(key) {
            Entry::Occupied(bucket) => bucket.into_mut(),
            Entry::Vacant(bucket) => {
                let inactive = &mut self.data.inactive;
                let child_flag = &mut self.data.child_flag;
                // try to get from previous
                let element = self
                    .prev
                    .remove(bucket.key())
                    // otherwise try to get anything existing and adapt it
                    .or_else(|| {
                        inactive.pop().map(|mut el| {
                            el.adapt(data);
                            el
                        })
                    })
                    // otherwise, create a new widget
                    .unwrap_or_else(|| {
                        child_flag.trigger();
                        Adaptable::from(data)
                    });
                bucket.insert(element)
            }
        }
    }
}

impl<'a, Layout, Content, Platform> Drop
    for Interface<'a, Layout, Content, Platform>
where
    Layout: AdapterLayout,
    Content: WidgetContent<Platform> + Adaptable<Layout::ElementData>,
    Platform: RenderPlatform,
{
    fn drop(&mut self) {
        let remaining = std::mem::take(&mut self.prev);
        self.data
            .inactive
            .extend(remaining.into_iter().map(|(_k, v)| v));
    }
}

/// Base adapter view.
pub struct AdapterView<Layout, Content, Platform>
where
    Layout: AdapterLayout,
    Content: WidgetContent<Platform> + Adaptable<Layout::ElementData>,
    Platform: RenderPlatform,
{
    inner: AdapterData<Layout, Content, Platform>,
    data_flag: WatchedMeta,
    position_flag: WatchedMeta,
    layout: Layout,
    primary_pointer: Option<PointerId>,
}

impl<Layout, Content, Platform> AdapterView<Layout, Content, Platform>
where
    Layout: 'static + AdapterLayout,
    Content: WidgetContent<Platform> + Adaptable<Layout::ElementData>,
    Platform: RenderPlatform,
{
    /// Get the data collection stored by the layout.
    pub fn data(&self) -> &Layout::Collection {
        self.data_flag.watched();
        self.layout.data()
    }

    /// Get a mutable reference to the collection stored by the layout.
    pub fn data_mut(&mut self) -> &mut Layout::Collection {
        // if data is modified, flush the active children
        let old = std::mem::take(&mut self.inner.active);
        self.inner.inactive.extend(old.into_iter().map(|(_k, v)| v));
        self.data_flag.trigger();
        self.data_flag.watched();
        self.layout.data_mut()
    }

    /// This provides a Watched iterator of every Widget the AdapterView
    /// has instantiated.  This allows the parent widget of the AdapterView
    /// to listen to events from the content Widgets.
    pub fn watch_each_child(
        &self,
    ) -> impl Iterator<Item = &Widget<Content, Platform>> {
        self.inner.child_flag.watched();
        self.inner.active.values().chain(&self.inner.inactive)
    }
}

impl<Layout, Content, Platform> Default
    for AdapterView<Layout, Content, Platform>
where
    Layout: 'static + AdapterLayout + Default,
    Content: WidgetContent<Platform> + Adaptable<Layout::ElementData>,
    Platform: RenderPlatform,
{
    fn default() -> Self {
        let layout = Layout::default();
        AdapterView {
            inner: AdapterData {
                active: HashMap::default(),
                inactive: Vec::default(),
                child_flag: WatchedMeta::new(),
                position: (0.0, 0.0),
                rest_position: (0.0, 0.0),
            },
            data_flag: WatchedMeta::default(),
            position_flag: WatchedMeta::default(),
            layout,
            primary_pointer: None,
        }
    }
}

impl<Layout, Content, Platform> WidgetContent<Platform>
    for AdapterView<Layout, Content, Platform>
where
    Layout: 'static + AdapterLayout,
    Content: WidgetContent<Platform> + Adaptable<Layout::ElementData>,
    Platform: RenderPlatform,
{
    fn init(mut init: impl WidgetInit<Self, Platform>) {
        init.watch(|this, rect| {
            this.position_flag.watched();
            this.data_flag.watched();
            let prev = std::mem::take(&mut this.inner.active);
            let interface = Interface {
                rect: &rect,
                data: &mut this.inner,
                prev,
            };
            this.layout.layout(interface);
        });
    }

    fn children(&mut self, mut receiver: impl WidgetChildReceiver<Platform>) {
        for (_k, child) in self.inner.active.iter_mut() {
            receiver.child(child);
        }
    }

    fn graphics(&mut self, _receiver: impl WidgetGraphicReceiver<Platform>) {
        // no graphics
    }

    fn pointer_event(
        &mut self,
        extra: &mut crate::widget::WidgetExtra<'_>,
        event: &mut crate::pointer::PointerEvent,
    ) -> bool {
        use crate::pointer::PointerAction;
        match event.action() {
            PointerAction::Down => {
                let grabbed =
                    self.hittest(extra, event.pos()) && event.try_grab(extra);
                if grabbed {
                    self.primary_pointer.get_or_insert(event.id());
                }
                grabbed
            }
            PointerAction::Move(x, y) => {
                if Some(event.id()) == self.primary_pointer {
                    self.inner.position.0 += x;
                    self.inner.position.1 += y;
                    self.position_flag.trigger();
                    true
                } else {
                    false
                }
            }
            PointerAction::Wheel(x, y) => {
                if self.primary_pointer.is_none()
                    && self.hittest(extra, event.pos())
                {
                    self.inner.position.0 += x;
                    self.inner.position.1 += y;
                    self.position_flag.trigger();
                    true
                } else {
                    false
                }
            }
            PointerAction::GrabStolen => {
                if Some(event.id()) == self.primary_pointer {
                    self.primary_pointer = None;
                }
                true
            }
            PointerAction::Up => {
                let ungrabbed = event.try_ungrab(extra.id());
                if ungrabbed && Some(event.id()) == self.primary_pointer {
                    self.primary_pointer = None;
                }
                ungrabbed
            }
            _ => false,
        }
    }
}

/// An adapter view which displays the contents of a Vec growing downwards.
pub type DownwardVecAdapter<T, W, P = DefaultRenderPlatform> =
    Widget<AdapterView<DownwardVecLayout<T>, W, P>, P>;

/// An adapter layout which lays out elements from a Vec growing downwards.
#[derive(Default)]
pub struct DownwardVecLayout<T> {
    data: Vec<T>,
    reference_index: usize,
    avg_size: f32,
}

impl<T> AdapterLayout for DownwardVecLayout<T> {
    type ElementKey = usize;
    type Collection = Vec<T>;
    type ElementData = T;

    fn data(&self) -> &Vec<T> {
        &self.data
    }

    fn data_mut(&mut self) -> &mut Vec<T> {
        &mut self.data
    }

    fn layout(&mut self, mut interface: impl AdapterLayoutInterface<Self>) {
        let (top, bottom) = {
            let bounds = interface.bounds();
            (bounds.top(), bounds.bottom())
        };
        let middle = (top + bottom) / 2.0;
        let mut cursor_back = top + interface.reference_position().1;
        let ref_index = self.reference_index;
        let prev_index = ref_index.saturating_sub(1);
        let mut cursor_fwd;
        let drawn_index;
        if self.data.is_empty() {
            // if the list is empty, don't render or change anything
            return;
        } else if let Some(value) = self.data.get(ref_index) {
            // position reference element
            let el = interface.get_element(ref_index, value);
            el.set_top(cursor_back);
            cursor_fwd = el.bottom();
            drawn_index = ref_index;
        } else if let Some(value) = self.data.get(prev_index) {
            // if we couldn't get the ref element, the list has shrunk
            // but if ref-1 exists, we can still position backwards
            cursor_fwd = cursor_back;
            let el = interface.get_element(prev_index, value);
            el.set_bottom(cursor_fwd);
            cursor_back = el.top();
            drawn_index = prev_index;
        } else {
            // we have nothing to go off of, so just update the rest position
            self.avg_size = 100.0;
            self.reference_index = self.data.len();
            let reference_update = (0.0, 0.0);
            let rest_update = (0.0, bottom - top);
            interface.update_positions(reference_update, rest_update);
            return;
        }
        struct Nearest {
            index: usize,
            pos: f32,
            dist: f32,
        }
        let mut nearest = Nearest {
            index: drawn_index,
            pos: cursor_back,
            dist: (cursor_back - middle).abs(),
        };
        // draw elements before the initially drawn one
        let mut index_back = drawn_index;
        while cursor_back < top && index_back > 0 {
            index_back -= 1;
            let el = interface.get_element(index_back, &self.data[index_back]);
            el.set_bottom(cursor_back);
            cursor_back = el.top();
            let dist = (cursor_back - middle).abs();
            if dist < nearest.dist {
                nearest = Nearest {
                    index: index_back,
                    pos: cursor_back,
                    dist,
                };
            }
        }
        let mut index_fwd = drawn_index;
        // draw elements after the initially drawn one
        while cursor_fwd > bottom && index_fwd < (self.data.len() - 1) {
            index_fwd += 1;
            let el = interface.get_element(index_fwd, &self.data[index_fwd]);
            el.set_top(cursor_fwd);
            let dist = (cursor_fwd - middle).abs();
            if dist < nearest.dist {
                nearest = Nearest {
                    index: index_fwd,
                    pos: cursor_fwd,
                    dist,
                };
            }
            cursor_fwd = el.bottom();
        }
        let count = interface.num_active_elements() as f32;
        self.avg_size = (cursor_back - cursor_fwd).abs() / count;
        let rest_pos = if cursor_back < top {
            nearest.pos + (top - cursor_back)
        } else if cursor_fwd > bottom {
            let limit = if index_back == 0 {
                // prevent wiggling if the whole list is smaller than the view
                cursor_back - top
            } else {
                f32::INFINITY
            };
            nearest.pos - (cursor_fwd - bottom).min(limit)
        } else {
            nearest.pos
        };
        self.reference_index = nearest.index;
        let reference_update = (0.0, nearest.pos - top);
        let rest_update = (0.0, rest_pos - top);
        interface.update_positions(reference_update, rest_update);
    }

    fn element_location(
        &mut self,
        item: &Self::ElementKey,
        reference_position: (f32, f32),
    ) -> Option<(f32, f32)> {
        let item = *item;
        let dist = if item >= self.data.len() {
            return None;
        } else if item > self.reference_index {
            (item - self.reference_index) as f32 * -1.0
        } else {
            (self.reference_index - item) as f32
        };
        let pos = reference_position.1 + (dist * self.avg_size);
        Some((reference_position.0, pos))
    }
}