-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathTreeListViewItemsCollection.cs
More file actions
332 lines (294 loc) · 10.3 KB
/
TreeListViewItemsCollection.cs
File metadata and controls
332 lines (294 loc) · 10.3 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
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
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
namespace MaterialDesignThemes.Wpf.Internal;
public class TreeListViewItemsCollection : ObservableCollection<object?>
{
private List<int> ItemLevels { get; } = [];
private List<bool> ItemIsExpanded { get; } = [];
public TreeListViewItemsCollection(object? wrappedSource)
{
if (wrappedSource is IEnumerable items)
{
foreach (object? item in items)
{
Add(item);
}
}
if (wrappedSource is INotifyCollectionChanged newCollectionChanged)
{
CollectionChangedEventManager.AddHandler(newCollectionChanged, ItemsSource_CollectionChanged);
}
}
private int GetPriorNonRootLevelItemsCount(int index, int startingIndex = 0)
{
if (index == 0)
return 0;
int priorRootLevelItems = 0;
int priorNonRootLevelItems = 0;
for (int i = startingIndex; i < ItemLevels.Count; i++)
{
int itemLevel = ItemLevels[i];
if (itemLevel == 0)
{
priorRootLevelItems++;
}
else
{
priorNonRootLevelItems++;
}
if (priorRootLevelItems > index)
{
// We've have passed the provided index, which means we've found a non-prior root parentLevel item; bail out.
break;
}
}
return priorNonRootLevelItems;
}
public int GetLevel(int index)
=> ItemLevels[index];
public bool GetIsExpanded(int index)
=> ItemIsExpanded[index];
public void SetIsExpanded(int index, bool isExpanded)
=> ItemIsExpanded[index] = isExpanded;
public void InsertWithLevel(int index, object? item, int level)
{
if (level < 0) throw new ArgumentOutOfRangeException(nameof(level), level, "Item level must not be negative");
//Always allowed to request previous item parentLevel + 1 as this is inserting a "child"
int previousItemLevel = index > 0 ? ItemLevels[index - 1] : -1;
if (level > previousItemLevel + 1)
{
throw new ArgumentOutOfRangeException(nameof(level), level, $"Item level must not be more than one level greater the previous item ({previousItemLevel})");
}
int nextItemLevel = index < Count ? ItemLevels[index] : 0;
if (level < nextItemLevel)
{
throw new ArgumentOutOfRangeException(nameof(level), level, $"Item level must not be less than the level item after it ({nextItemLevel})");
}
InternalInsertItem(index, item, level);
if (previousItemLevel >= 0 && previousItemLevel == level - 1)
{
ItemIsExpanded[index - 1] = true;
}
}
public object? GetParent(int index)
{
if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index));
int level = ItemLevels[index];
if (level == 0) return null;
for (int i = index - 1; i >= 0; i--)
{
if (ItemLevels[i] == level - 1)
{
return this[i];
}
}
return null;
}
public IEnumerable<int> GetDirectChildrenIndexes(int index)
{
if (index < 0 || index >= Count) throw new ArgumentOutOfRangeException(nameof(index));
return GetDirectChildrenIndexesImplementation(index);
IEnumerable<int> GetDirectChildrenIndexesImplementation(int index)
{
int parentLevel = ItemLevels[index];
for (int i = index + 1; i < ItemLevels.Count; i++)
{
int level = ItemLevels[i];
if (level == parentLevel + 1)
{
yield return i;
}
if (level <= parentLevel)
{
yield break;
}
}
}
}
protected override void RemoveItem(int index)
{
int priorNonRootLevelItems = GetPriorNonRootLevelItemsCount(index);
int adjustedIndex = index + priorNonRootLevelItems;
RemoveOffsetAdjustedItem(adjustedIndex);
}
internal void RemoveOffsetAdjustedItem(int index)
{
int currentLevel = ItemLevels[index];
InternalRemoveItem(index);
while (index < Count && ItemLevels[index] > currentLevel)
{
InternalRemoveItem(index);
}
}
internal void RemoveChildrenOfOffsetAdjustedItem(int index)
{
int currentLevel = ItemLevels[index];
if (index + 1 >= Count || ItemLevels[index + 1] < currentLevel + 1)
return;
ItemIsExpanded[index] = false;
index++;
InternalRemoveItem(index);
while (index < Count && ItemLevels[index] > currentLevel)
{
InternalRemoveItem(index);
}
}
protected override void InsertItem(int index, object? item)
{
int priorNonRootLevelItems = GetPriorNonRootLevelItemsCount(index);
index += priorNonRootLevelItems;
InternalInsertItem(index, item, 0);
}
protected override void MoveItem(int oldIndex, int newIndex)
=> MoveOffsetAdjustedItem(oldIndex, newIndex);
internal void MoveOffsetAdjustedItem(int oldIndex, int newIndex)
{
// Figure out how many items to move (1 + any children/grand-children)
int itemLevel = ItemLevels[oldIndex];
int childIndex = oldIndex + 1;
int childrenCount = 0;
while (childIndex < Count && ItemLevels[childIndex] > itemLevel)
{
childIndex++;
childrenCount++;
}
int insertLevel = ItemLevels[newIndex];
int insertIndex = newIndex;
while (insertIndex + 1 < Count && ItemLevels[insertIndex + 1] > insertLevel)
{
insertIndex++;
}
if (oldIndex < newIndex)
{
// Moving down
// Move children/grand-children first
int oldChildIndex = oldIndex + 1;
for (int j = 0; j < childrenCount; j++)
{
InternalMoveItem(oldChildIndex, insertIndex);
}
// Then move the parent
InternalMoveItem(oldIndex, insertIndex - childrenCount);
}
else
{
// Moving up
// Move the parent first
InternalMoveItem(oldIndex, newIndex);
// Then move children/grand-children
for (int j = 0; j < childrenCount; j++)
{
int oldChildIndex = oldIndex + 1 + j;
int newChildIndex = newIndex + 1 + j;
InternalMoveItem(oldChildIndex, newChildIndex);
}
}
}
private void InternalInsertItem(int index, object? item, int level)
{
ItemIsExpanded.Insert(index, false);
ItemLevels.Insert(index, level);
base.InsertItem(index, item);
}
private void InternalRemoveItem(int index)
{
ItemIsExpanded.RemoveAt(index);
ItemLevels.RemoveAt(index);
base.RemoveItem(index);
}
private void InternalMoveItem(int oldIndex, int newIndex)
{
ItemIsExpanded.MoveItem(oldIndex, newIndex);
ItemLevels.MoveItem(oldIndex, newIndex);
base.MoveItem(oldIndex, newIndex);
}
internal void ReplaceOffsetAdjustedItem(int index, object? item)
{
// NOTE: This is slight change of notification behavior. It now fires at least one Remove (possibly also removing children) and one Add notification on the internal collection; probably not an issue.
int level = GetLevel(index);
RemoveChildrenOfOffsetAdjustedItem(index);
RemoveOffsetAdjustedItem(index);
InsertWithLevel(index, item, level);
}
private void ItemsSource_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
for (int i = 0; i < e.NewItems?.Count; i++)
{
Insert(e.NewStartingIndex + i, e.NewItems[i]!);
}
break;
case NotifyCollectionChangedAction.Remove:
for (int i = 0; i < e.OldItems?.Count; i++)
{
RemoveAt(e.OldStartingIndex + i);
}
break;
case NotifyCollectionChangedAction.Replace:
for (int i = 0; i < e.NewItems?.Count; i++)
{
int newIndex = GetAbsoluteIndex(e.NewStartingIndex + i);
if (newIndex >= 0)
{
ReplaceOffsetAdjustedItem(newIndex, e.NewItems[i]!);
}
}
break;
case NotifyCollectionChangedAction.Move:
for (int i = 0; i < e.NewItems?.Count; i++)
{
int oldIndex = GetAbsoluteIndex(e.OldStartingIndex + i);
int newIndex = GetAbsoluteIndex(e.NewStartingIndex + i);
if (oldIndex >= 0 && newIndex >= 0)
{
Move(oldIndex, newIndex);
}
}
break;
case NotifyCollectionChangedAction.Reset:
Clear();
ItemLevels.Clear();
foreach (object? item in sender as IEnumerable ?? Enumerable.Empty<object?>())
{
Add(item);
}
break;
}
int GetAbsoluteIndex(int relativeIndex)
{
for(int i = 0; i < ItemLevels.Count; i++)
{
if (ItemLevels[i] == 0)
{
relativeIndex--;
}
if (relativeIndex < 0) return i;
}
return -1;
}
}
}
file static class ListExtensions
{
public static void MoveItem(this IList list, int oldIndex, int newIndex)
{
if (oldIndex == newIndex)
return;
object item = list[oldIndex]!;
list.RemoveAt(oldIndex);
list.Insert(newIndex, item);
}
}
public class MoveEventArgs : EventArgs
{
public int OldIndex { get; }
public int NewIndex { get; }
public MoveEventArgs(int oldIndex, int newIndex)
{
OldIndex = oldIndex;
NewIndex = newIndex;
}
}