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
use gtk::glib::Sender;

use std::cell::RefCell;
use std::collections::VecDeque;
use std::rc::{Rc, Weak};

use super::Widgets;
use crate::factory::{Factory, FactoryListView, FactoryPrototype, FactoryView};
use fragile::Fragile;

/// A dynamic index that updates automatically when items are shifted inside a [`Factory`].
///
/// For example a [`FactoryVecDeque`] has an [`insert`](FactoryVecDeque::insert) method that allows users
/// to insert data at arbitrary positions.
/// If we insert at the front all following widgets will be moved by one which would
/// invalidate their indices.
/// To allow widgets in a [`Factory`] to still send messages with valid indices
/// this type ensures that the indices is always up to date.
///
/// Never send an index as [`usize`] but always as [`DynamicIndex`] or even better as [`WeakDynamicIndex`]
/// to the update function because messages can be queued up and stale by the time they are handled.
///
/// **[`DynamicIndex`] is a smart pointer so cloning will work similar to [`Rc`] and will create
/// a pointer to the same data.**
///
/// In short: only call [`current_index`](DynamicIndex::current_index) from the update function
/// where you actually need the index as [`usize`].
///
/// # Panics
///
/// Sending a [`DynamicIndex`] to a different thread and accessing it will panic.
#[derive(Debug, PartialEq, Eq)]
pub struct DynamicIndex {
    inner: Fragile<Rc<RefCell<usize>>>,
}

impl Clone for DynamicIndex {
    fn clone(&self) -> Self {
        DynamicIndex {
            inner: Fragile::new(self.inner.get().clone()),
        }
    }
}

impl DynamicIndex {
    /// Get the current index number.
    ///
    /// This value is updated by the [`Factory`] and might change after each update function.
    pub fn current_index(&self) -> usize {
        *self.inner.get().borrow()
    }

    /// Creates a [`WeakDynamicIndex`] for sending in messages.
    pub fn downgrade(&self) -> WeakDynamicIndex {
        WeakDynamicIndex {
            inner: Fragile::new(Rc::downgrade(self.inner.get())),
        }
    }

    #[doc(hidden)]
    fn increment(&self) {
        *self.inner.get().borrow_mut() += 1;
    }

    #[doc(hidden)]
    fn decrement(&self) {
        *self.inner.get().borrow_mut() -= 1;
    }

    #[doc(hidden)]
    fn new(index: usize) -> Self {
        DynamicIndex {
            inner: Fragile::new(Rc::new(RefCell::new(index))),
        }
    }
}

/// A weak version of [`DynamicIndex`].
///
/// Use this to send messages to the update function and call [`upgrade`](WeakDynamicIndex::upgrade)
/// to receive the actual [`DynamicIndex`].
///
/// A weak index is preferred for sending in messages because messages can be stale by the time they
/// are handled and the element already deleted. A weak reference doesn't keep the index alive
/// if the element was deleted which allows you to properly handle invalid indices.
///
/// # Panics
///
/// Sending a [`WeakDynamicIndex`] to a different thread and accessing it will panic.
#[derive(Debug)]
pub struct WeakDynamicIndex {
    inner: Fragile<Weak<RefCell<usize>>>,
}

impl Clone for WeakDynamicIndex {
    fn clone(&self) -> Self {
        WeakDynamicIndex {
            inner: Fragile::new(self.inner.get().clone()),
        }
    }
}

impl WeakDynamicIndex {
    /// Attempts to upgrade the [`WeakDynamicIndex`] to a [`DynamicIndex`].
    ///
    /// Returns [`None`] if the index has since been dropped.
    pub fn upgrade(&self) -> Option<DynamicIndex> {
        self.inner.get().upgrade().map(|rc| DynamicIndex {
            inner: Fragile::new(rc),
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum ChangeType {
    Unchanged,
    Add,
    Remove(u16),
    Recreate,
    Update,
}

impl ChangeType {
    fn apply(&mut self, other: ChangeType) {
        match self {
            ChangeType::Unchanged => {
                *self = other;
            }
            ChangeType::Update => {
                if other != ChangeType::Unchanged {
                    *self = other;
                }
            }
            ChangeType::Add => {
                if other == ChangeType::Remove(1) {
                    *self = ChangeType::Unchanged;
                } else {
                    assert_eq!(
                        other,
                        ChangeType::Update,
                        "Logical error in change tracking. Unexpected change: {:?} <- {:?}",
                        self,
                        other
                    );
                };
            }
            ChangeType::Recreate => {
                if other == ChangeType::Remove(1) {
                    *self = ChangeType::Remove(1);
                } else {
                    assert_eq!(
                        other,
                        ChangeType::Update,
                        "Logical error in change tracking. Unexpected change: {:?} <- {:?}",
                        self,
                        other
                    );
                }
            }
            ChangeType::Remove(num) => {
                if other == ChangeType::Add {
                    if *num == 1 {
                        *self = ChangeType::Recreate;
                    } else {
                        *self = ChangeType::Remove(*num - 1);
                    }
                } else if other == ChangeType::Remove(1) {
                    *self = ChangeType::Remove(*num + 1);
                } else {
                    panic!(
                        "Logical error in change tracking. Unexpected change: {:?} <- {:?}",
                        self, other
                    );
                }
            }
        }
    }
}

#[derive(Debug)]
struct Change {
    ty: ChangeType,
    index: usize,
}

impl Change {
    fn new(index: usize, ty: ChangeType) -> Self {
        Change { ty, index }
    }
}

#[derive(Debug)]
struct IndexedData<T> {
    inner: T,
    index: Rc<DynamicIndex>,
}

impl<T> IndexedData<T> {
    fn new(data: T, index: usize) -> Self {
        let index = Rc::new(DynamicIndex::new(index));
        IndexedData { inner: data, index }
    }
}

/// A container similar to [`VecDeque`] that implements [`Factory`].
#[derive(Default, Debug)]
#[allow(clippy::type_complexity)]
pub struct FactoryVecDeque<Data>
where
    Data: FactoryPrototype,
{
    data: VecDeque<IndexedData<Data>>,
    widgets:
        RefCell<VecDeque<Widgets<Data::Widgets, <Data::View as FactoryView<Data::Root>>::Root>>>,
    changes: RefCell<Vec<Change>>,
}

impl<Data> FactoryVecDeque<Data>
where
    Data: FactoryPrototype,
{
    /// Create a new [`FactoryVecDeque`].
    #[must_use]
    pub fn new() -> Self {
        FactoryVecDeque {
            data: VecDeque::new(),
            widgets: RefCell::new(VecDeque::new()),
            changes: RefCell::new(Vec::new()),
        }
    }

    /// Initialize a new [`FactoryVecDeque`] with a normal [`VecDeque`].
    #[must_use]
    pub fn from_vec_deque(data: VecDeque<Data>) -> Self {
        let length = data.len();
        let mut indexed_data = VecDeque::with_capacity(length);
        let mut changes = Vec::with_capacity(length);

        for (num, item) in data.into_iter().enumerate() {
            indexed_data.push_back(IndexedData::new(item, num));
            changes.push(Change {
                ty: ChangeType::Add,
                index: num,
            });
        }
        FactoryVecDeque {
            data: indexed_data,
            widgets: RefCell::new(VecDeque::with_capacity(length)),
            changes: RefCell::new(changes),
        }
    }

    /// Get the internal data of the [`FactoryVecDeque`].
    #[must_use]
    pub fn into_vec_deque(mut self) -> VecDeque<Data> {
        self.data.drain(..).map(|data| data.inner).collect()
    }

    /// Remove all data from the [`FactoryVecDeque`].
    pub fn clear(&mut self) {
        self.add_change(Change {
            ty: ChangeType::Remove(self.data.len() as u16),
            index: 0,
        });
        self.data.clear();
    }

    /// Returns the length as amount of elements stored in this type.
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Returns [`true`] if the length of this type is `0`.
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Insert an element at the end of a [`FactoryVecDeque`].
    pub fn push_back(&mut self, data: Data) {
        let index = self.data.len();
        let data = IndexedData::new(data, index);
        self.add_change(Change::new(index, ChangeType::Add));
        self.data.push_back(data);
    }

    /// Remove an element at the end of a [`FactoryVecDeque`].
    pub fn pop_back(&mut self) -> Option<Data> {
        if let Some(data) = self.data.pop_back() {
            let index = self.data.len();
            self.add_change(Change::new(index, ChangeType::Remove(1)));
            Some(data.inner)
        } else {
            None
        }
    }

    /// Adds an element at the front.
    pub fn push_front(&mut self, data: Data) {
        for elem in &self.data {
            elem.index.increment();
        }
        let index = 0;
        self.add_change(Change::new(index, ChangeType::Add));
        let data = IndexedData::new(data, index);
        self.data.push_front(data);
    }

    /// Removes an element from the front.
    pub fn pop_front(&mut self) -> Option<Data> {
        if let Some(data) = self.data.pop_front() {
            self.add_change(Change::new(0, ChangeType::Remove(1)));
            for elem in &self.data {
                elem.index.decrement();
            }
            Some(data.inner)
        } else {
            None
        }
    }

    /// Adds an element at a given index.
    /// All elements with indices greater than or equal to index will be shifted towards the back.
    pub fn insert(&mut self, index: usize, data: Data) {
        for elem in &self.data {
            if elem.index.current_index() >= index {
                elem.index.increment();
            }
        }
        self.add_change(Change::new(index, ChangeType::Add));
        let data = IndexedData::new(data, index);
        self.data.insert(index, data);
    }

    /// Removes an element at a given index.
    pub fn remove(&mut self, index: usize) -> Option<Data> {
        if let Some(data) = self.data.remove(index) {
            self.add_change(Change::new(index, ChangeType::Remove(1)));
            for elem in &self.data {
                if elem.index.current_index() > index {
                    elem.index.decrement();
                }
            }
            Some(data.inner)
        } else {
            None
        }
    }

    /// Get a reference to data stored at `index`.
    pub fn get(&self, index: usize) -> Option<&Data> {
        self.data.get(index).map(|data| &data.inner)
    }

    /// Get a mutable reference to data stored at `index`.
    ///
    /// Assumes that the data will be modified and the corresponding widget
    /// needs to be updated.
    pub fn get_mut(&mut self, index: usize) -> Option<&mut Data> {
        self.add_change(Change::new(index, ChangeType::Update));

        self.data.get_mut(index).map(|data| &mut data.inner)
    }

    fn add_change(&mut self, change: Change) {
        match change.ty {
            ChangeType::Add => {
                for elem in self.changes.borrow_mut().iter_mut() {
                    if elem.index >= change.index {
                        elem.index += 1;
                    }
                }
            }
            ChangeType::Remove(num) => {
                for elem in self.changes.borrow_mut().iter_mut() {
                    if elem.index > change.index {
                        elem.index -= num as usize;
                    }
                }
            }
            _ => (),
        }
        self.changes.borrow_mut().push(change);
    }

    fn compile_changes(&self) -> Vec<ChangeType> {
        let mut change_map = vec![ChangeType::Unchanged; self.data.len() + 1];

        for change in self.changes.borrow().iter() {
            while change_map.len() < change.index {
                change_map.push(ChangeType::Unchanged);
            }
            change_map[change.index].apply(change.ty);
        }

        change_map
    }
}

impl<Data, View> Factory<Data, View> for FactoryVecDeque<Data>
where
    Data: FactoryPrototype<Factory = Self, View = View>,
    View: FactoryView<Data::Root> + FactoryListView<Data::Root>,
    <Data as FactoryPrototype>::Root: std::clone::Clone,
{
    type Key = DynamicIndex;

    fn generate(&self, view: &View, sender: Sender<Data::Msg>) {
        let change_map = self.compile_changes();
        for (index, change) in change_map.iter().enumerate() {
            let mut widgets = self.widgets.borrow_mut();

            match change {
                ChangeType::Unchanged => (),
                ChangeType::Add => {
                    let data = &self.data[index];
                    let new_widgets = data.inner.init_view(&data.index, sender.clone());
                    let root = Data::root_widget(&new_widgets);
                    let root = if widgets.is_empty() || index == 0 {
                        view.push_front(root)
                    } else {
                        view.insert_after(root, &widgets[index - 1].root)
                    };
                    widgets.insert(
                        index,
                        Widgets {
                            widgets: new_widgets,
                            root,
                        },
                    );
                }
                ChangeType::Update => {
                    let data = &self.data[index];
                    data.inner.view(&data.index, &widgets[index].widgets);
                }
                ChangeType::Remove(num) => {
                    for _ in 0..*num {
                        let remove_widget = widgets.remove(index).unwrap();
                        view.remove(&remove_widget.root);
                    }
                }
                ChangeType::Recreate => {
                    let remove_widget = widgets.pop_back().unwrap();
                    view.remove(&remove_widget.root);
                    let data = &self.data[index];
                    let new_widgets = data.inner.init_view(&data.index, sender.clone());
                    let root = Data::root_widget(&new_widgets);
                    let root = if widgets.is_empty() || index == 0 {
                        view.push_front(root)
                    } else {
                        view.insert_after(root, &widgets[index - 1].root)
                    };
                    widgets.insert(
                        index,
                        Widgets {
                            widgets: new_widgets,
                            root,
                        },
                    );
                }
            }
        }
        self.changes.borrow_mut().clear();
    }
}

impl<Data, View> FactoryVecDeque<Data>
where
    Data: FactoryPrototype<Factory = Self, View = View>,
    View: FactoryView<Data::Root>,
{
    /// Get an immutable iterator for this type
    pub fn iter(&self) -> Iter<'_, Data> {
        Iter {
            inner: self.data.iter(),
        }
    }
}

#[derive(Debug)]
pub struct Iter<'a, Data> {
    inner: std::collections::vec_deque::Iter<'a, IndexedData<Data>>,
}

impl<'a, Data> std::iter::Iterator for Iter<'a, Data> {
    type Item = &'a Data;

    fn next(&mut self) -> Option<Self::Item> {
        self.inner.next().map(|data| &data.inner)
    }
}