forked from microsoft/microsoft-ui-xaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInspectingDataSource.cpp
267 lines (241 loc) · 7.73 KB
/
InspectingDataSource.cpp
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#include <pch.h>
#include <common.h>
#include <BindableVector.h>
#include "ItemsRepeater.common.h"
#include "InspectingDataSource.h"
InspectingDataSource::InspectingDataSource(const winrt::IInspectable& source)
{
if (!source)
{
throw winrt::hresult_invalid_argument(L"Argument 'source' is null.");
}
if (const auto vector = source.try_as<winrt::IVector<winrt::IInspectable>>())
{
m_vector.set(vector);
ListenToCollectionChanges();
}
else if (const auto bindableVector = source.try_as<winrt::IBindableVector>())
{
m_vector.set(reinterpret_cast<const winrt::IVector<winrt::IInspectable>&>(bindableVector));
ListenToCollectionChanges();
}
else if (const auto vectorView = source.try_as<winrt::IVectorView<winrt::IInspectable>>())
{
m_vectorView.set(vectorView);
ListenToCollectionChanges();
}
else if (const auto iterable = source.try_as<winrt::IIterable<winrt::IInspectable>>())
{
m_vector.set(WrapIterable(iterable));
}
else if (const auto bindableIterable = source.try_as<winrt::IBindableIterable>())
{
m_vector.set(WrapIterable(reinterpret_cast<const winrt::IIterable<winrt::IInspectable>&>(bindableIterable)));
}
else
{
throw winrt::hresult_invalid_argument(L"Argument 'source' is not a supported vector.");
}
m_uniqueIdMaping = source.try_as<winrt::IKeyIndexMapping>();
}
InspectingDataSource::~InspectingDataSource()
{
UnListenToCollectionChanges();
}
#pragma region IDataSourceOverrides
int32_t InspectingDataSource::GetSizeCore()
{
if (m_vectorView)
{
return static_cast<int>(m_vectorView.get().Size());
}
else
{
return static_cast<int>(m_vector.get().Size());
}
}
winrt::IInspectable InspectingDataSource::GetAtCore(int index)
{
if (m_vectorView)
{
return m_vectorView.get().GetAt(static_cast<unsigned>(index));
}
else
{
return m_vector.get().GetAt(static_cast<unsigned>(index));
}
}
bool InspectingDataSource::HasKeyIndexMappingCore()
{
return m_uniqueIdMaping != nullptr;
}
winrt::hstring InspectingDataSource::KeyFromIndexCore(int index)
{
if (m_uniqueIdMaping)
{
return m_uniqueIdMaping.KeyFromIndex(index);
}
else
{
throw winrt::hresult_not_implemented();
}
}
int InspectingDataSource::IndexFromKeyCore(winrt::hstring const& id)
{
if (m_uniqueIdMaping)
{
return m_uniqueIdMaping.IndexFromKey(id);
}
else
{
throw winrt::hresult_not_implemented();
}
}
int InspectingDataSource::IndexOfCore(winrt::IInspectable const& value)
{
int index = -1;
if (m_vectorView)
{
auto v = static_cast<uint32_t>(-1);
if (m_vectorView.get().IndexOf(value, v))
{
index = static_cast<int>(v);
}
}
else if (m_vector)
{
auto v = static_cast<uint32_t>(-1);
if (m_vector.get().IndexOf(value, v))
{
index = static_cast<int>(v);
}
}
return index;
}
#pragma endregion
winrt::IVector<winrt::IInspectable>
InspectingDataSource::WrapIterable(const winrt::IIterable<winrt::IInspectable>& iterable)
{
auto vector = winrt::make<Vector<winrt::IInspectable, MakeVectorParam<VectorFlag::DependencyObjectBase>()>>();
auto iterator = iterable.First();
while (iterator.HasCurrent())
{
vector.Append(iterator.Current());
iterator.MoveNext();
}
return vector;
}
void InspectingDataSource::UnListenToCollectionChanges()
{
if (auto notifyCollection = m_notifyCollectionChanged.safe_get())
{
notifyCollection.CollectionChanged(m_eventToken);
}
else if (auto bindableObservableCollection = m_bindableObservableVector.safe_get())
{
bindableObservableCollection.VectorChanged(m_eventToken);
}
else if (auto observableCollection = m_observableVector.safe_get())
{
observableCollection.VectorChanged(m_eventToken);
}
}
void InspectingDataSource::ListenToCollectionChanges()
{
if (!m_vector)
{
MUX_ASSERT(m_vectorView);
}
else
{
MUX_ASSERT(m_vector);
}
const auto incc = [this]() {
if (m_vectorView)
{
return m_vectorView.try_as<winrt::INotifyCollectionChanged>();
}
else
{
return m_vector.try_as<winrt::INotifyCollectionChanged>();
}
}();
if(incc)
{
m_eventToken = incc.CollectionChanged({ this, &InspectingDataSource::OnCollectionChanged });
m_notifyCollectionChanged.set(incc);
}
else if (const auto bindableObservableVector = m_vector.try_as<winrt::IBindableObservableVector>())
{
m_eventToken = bindableObservableVector.VectorChanged({ this, &InspectingDataSource::OnBindableVectorChanged });
m_bindableObservableVector.set(bindableObservableVector);
}
else if(const auto observableVector = m_vector.try_as<winrt::IObservableVector<winrt::IInspectable>>()){
m_eventToken = observableVector.VectorChanged({ this, &InspectingDataSource::OnVectorChanged });
m_observableVector.set(observableVector);
}
}
void InspectingDataSource::OnCollectionChanged(
const winrt::IInspectable& /*sender*/,
const winrt::NotifyCollectionChangedEventArgs& e)
{
OnItemsSourceChanged(e);
}
void InspectingDataSource::OnBindableVectorChanged(
const winrt::IBindableObservableVector& sender,
const winrt::IInspectable& e)
{
OnVectorChanged(
reinterpret_cast<const winrt::IObservableVector<winrt::IInspectable>&>(sender),
reinterpret_cast<const winrt::Collections::IVectorChangedEventArgs&>(e));
}
void InspectingDataSource::OnVectorChanged(
const winrt::Collections::IObservableVector<winrt::IInspectable>& /*sender*/,
const winrt::Collections::IVectorChangedEventArgs& e)
{
// We need to build up NotifyCollectionChangedEventArgs here to raise the event.
// There is opportunity to make this faster by caching the args if it does
// show up as a perf issue.
// Also note that we do not access the data - we just add nullptr. We just
// need the count.
winrt::NotifyCollectionChangedAction action{};
int oldStartingIndex = -1;
int newStartingIndex = -1;
auto oldItems = winrt::make<Vector<winrt::IInspectable, MakeVectorParam<VectorFlag::Bindable>()>>();
auto newItems = winrt::make<Vector<winrt::IInspectable, MakeVectorParam<VectorFlag::Bindable>()>>();
switch (e.CollectionChange())
{
case winrt::Collections::CollectionChange::ItemInserted:
action = winrt::NotifyCollectionChangedAction::Add;
newStartingIndex = static_cast<int>(e.Index());
newItems.Append(nullptr);
break;
case winrt::Collections::CollectionChange::ItemRemoved:
action = winrt::NotifyCollectionChangedAction::Remove;
oldStartingIndex = static_cast<int>(e.Index());
oldItems.Append(nullptr);
break;
case winrt::Collections::CollectionChange::ItemChanged:
action = winrt::NotifyCollectionChangedAction::Replace;
oldStartingIndex = static_cast<int>(e.Index());
newStartingIndex = oldStartingIndex;
newItems.Append(nullptr);
oldItems.Append(nullptr);
break;
case winrt::Collections::CollectionChange::Reset:
action = winrt::NotifyCollectionChangedAction::Reset;
break;
default:
MUX_ASSERT(false);
break;
}
OnItemsSourceChanged(
winrt::NotifyCollectionChangedEventArgs(
action,
newItems,
oldItems,
newStartingIndex,
oldStartingIndex));
}