Repository navigation
Expand file tree
/
Copy pathSelectionList.cs
More file actions
142 lines (116 loc) · 3.34 KB
/
Copy pathSelectionList.cs
File metadata and controls
142 lines (116 loc) · 3.34 KB
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
#nullable disable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
namespace Microsoft.Maui.Controls
{
// Used by the SelectableItemsView to keep track of (and respond to changes in) the SelectedItems property
internal class SelectionList : IList<object>
{
static readonly IList<object> s_empty = new List<object>(0);
readonly SelectableItemsView _selectableItemsView;
readonly IList<object> _internal;
IList<object> _shadow;
bool _externalChange;
public SelectionList(SelectableItemsView selectableItemsView, IList<object> items = null)
{
_selectableItemsView = selectableItemsView ?? throw new ArgumentNullException(nameof(selectableItemsView));
_internal = items ?? new List<object>();
_shadow = Copy();
if (items is INotifyCollectionChanged incc)
{
incc.CollectionChanged += OnCollectionChanged;
}
}
public object this[int index] { get => _internal[index]; set => _internal[index] = value; }
public int Count => _internal.Count;
public bool IsReadOnly => false;
public void Add(object item)
{
_externalChange = true;
_internal.Add(item);
_externalChange = false;
_selectableItemsView.SelectedItemsPropertyChanged(_shadow, _internal);
_shadow.Add(item);
}
public void Clear()
{
_externalChange = true;
_internal.Clear();
_externalChange = false;
_selectableItemsView.SelectedItemsPropertyChanged(_shadow, s_empty);
_shadow.Clear();
}
public bool Contains(object item)
{
return _internal.Contains(item);
}
public void CopyTo(object[] array, int arrayIndex)
{
_internal.CopyTo(array, arrayIndex);
}
public IEnumerator<object> GetEnumerator()
{
return _internal.GetEnumerator();
}
public int IndexOf(object item)
{
return _internal.IndexOf(item);
}
public void Insert(int index, object item)
{
_externalChange = true;
_internal.Insert(index, item);
_externalChange = false;
_selectableItemsView.SelectedItemsPropertyChanged(_shadow, _internal);
_shadow.Insert(index, item);
}
public bool Remove(object item)
{
_externalChange = true;
var removed = _internal.Remove(item);
_externalChange = false;
if (removed)
{
_selectableItemsView.SelectedItemsPropertyChanged(_shadow, _internal);
_shadow.Remove(item);
}
return removed;
}
public void RemoveAt(int index)
{
_externalChange = true;
_internal.RemoveAt(index);
_externalChange = false;
_selectableItemsView.SelectedItemsPropertyChanged(_shadow, _internal);
_shadow.RemoveAt(index);
}
IEnumerator IEnumerable.GetEnumerator()
{
return _internal.GetEnumerator();
}
List<object> Copy()
{
var items = new List<object>();
for (int n = 0; n < _internal.Count; n++)
{
items.Add(_internal[n]);
}
return items;
}
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
{
if (_externalChange)
{
// If this change was initiated by a renderer or direct manipulation of ColllectionView.SelectedItems,
// we don't need to send a selection change notification
return;
}
// This change is coming from a bound viewmodel property
// Emit a selection change notification, then bring the shadow copy up-to-date
_selectableItemsView.SelectedItemsPropertyChanged(_shadow, _internal);
_shadow = Copy();
}
}
}