forked from microsoft/microsoft-ui-xaml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopNavigationViewDataProvider.cpp
378 lines (326 loc) · 10.1 KB
/
TopNavigationViewDataProvider.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
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
// 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 "Vector.h"
#include "SplitDataSourceBase.h"
#include "TopNavigationViewDataProvider.h"
#include "NavigationViewItem.h"
TopNavigationViewDataProvider::TopNavigationViewDataProvider(const ITrackerHandleManager* m_owner)
:SplitDataSourceT()
, m_rawDataSource(m_owner)
, m_dataSource(m_owner)
{
auto lambda = [this](const winrt::IInspectable& value)
{
return IndexOf(value);
};
auto primaryVector = std::make_shared<SplitVectorT>(m_owner, NavigationViewSplitVectorID::PrimaryList, lambda);
auto overflowVector = std::make_shared<SplitVectorT>(m_owner, NavigationViewSplitVectorID::OverflowList, lambda);
InitializeSplitVectors({ primaryVector, overflowVector });
}
winrt::IVector<winrt::IInspectable> TopNavigationViewDataProvider::GetPrimaryItems()
{
return GetVector(NavigationViewSplitVectorID::PrimaryList)->GetVector();
}
winrt::IVector<winrt::IInspectable> TopNavigationViewDataProvider::GetOverflowItems()
{
return GetVector(NavigationViewSplitVectorID::OverflowList)->GetVector();
}
// The raw data is from MenuItems or MenuItemsSource
void TopNavigationViewDataProvider::SetDataSource(const winrt::IInspectable& rawData)
{
if (ShouldChangeDataSource(rawData)) // avoid to create multiple of datasource for the same raw data
{
winrt::ItemsSourceView dataSource = nullptr;
if (rawData)
{
dataSource = winrt::ItemsSourceView(rawData);
}
ChangeDataSource(dataSource);
m_rawDataSource.set(rawData);
if (dataSource)
{
MoveAllItemsToPrimaryList();
}
}
}
bool TopNavigationViewDataProvider::ShouldChangeDataSource(winrt::IInspectable const& rawData)
{
return rawData != m_rawDataSource.get();
}
void TopNavigationViewDataProvider::OnRawDataChanged(std::function<void(winrt::NotifyCollectionChangedEventArgs const& args)> const& dataChangeCallback)
{
m_dataChangeCallback = dataChangeCallback;
}
int TopNavigationViewDataProvider::IndexOf(const winrt::IInspectable& value)
{
if (auto dataSource = m_dataSource.get())
{
return dataSource.IndexOf(value);
}
return -1;
}
winrt::IInspectable TopNavigationViewDataProvider::GetAt(int index)
{
if (auto dataSource = m_dataSource.get())
{
return dataSource.GetAt(index);
}
return nullptr;
}
int TopNavigationViewDataProvider::Size()
{
if (auto dataSource = m_dataSource.get())
{
return static_cast<int>(dataSource.Count());
}
return 0;
}
NavigationViewSplitVectorID TopNavigationViewDataProvider::DefaultVectorIDOnInsert()
{
return NavigationViewSplitVectorID::NotInitialized;
}
float TopNavigationViewDataProvider::DefaultAttachedData()
{
return std::numeric_limits<float>::min();
}
void TopNavigationViewDataProvider::MoveAllItemsToPrimaryList()
{
for (int i = 0; i < Size(); i++)
{
MoveItemToVector(i, NavigationViewSplitVectorID::PrimaryList);
}
}
std::vector<int> TopNavigationViewDataProvider::ConvertPrimaryIndexToIndex(std::vector<int> const& indexesInPrimary)
{
std::vector<int> indexes;
if (!indexesInPrimary.empty())
{
auto vector = GetVector(NavigationViewSplitVectorID::PrimaryList);
if (vector)
{
// transform PrimaryList index to OrignalVector index
std::transform(indexesInPrimary.begin(), indexesInPrimary.end(), std::back_inserter(indexes),
[&vector](int index) -> int
{
return vector->IndexToIndexInOriginalVector(index);
});
}
}
return indexes;
}
int TopNavigationViewDataProvider::ConvertOriginalIndexToIndex(int originalIndex)
{
auto const vector = GetVector(IsItemInPrimaryList(originalIndex) ? NavigationViewSplitVectorID::PrimaryList : NavigationViewSplitVectorID::OverflowList);
return vector->IndexFromIndexInOriginalVector(originalIndex);
}
void TopNavigationViewDataProvider::MoveItemsOutOfPrimaryList(std::vector<int> const& indexes)
{
MoveItemsToList(indexes, NavigationViewSplitVectorID::OverflowList);
}
void TopNavigationViewDataProvider::MoveItemsToPrimaryList(std::vector<int> const& indexes)
{
MoveItemsToList(indexes, NavigationViewSplitVectorID::PrimaryList);
}
void TopNavigationViewDataProvider::MoveItemsToList(std::vector<int> const& indexes, NavigationViewSplitVectorID vectorID)
{
for (auto &index : indexes)
{
MoveItemToVector(index, vectorID);
};
}
int TopNavigationViewDataProvider::GetPrimaryListSize()
{
return GetPrimaryItems().Size();
}
int TopNavigationViewDataProvider::GetNavigationViewItemCountInPrimaryList()
{
int count = 0;
for (int i = 0; i < Size(); i++)
{
if (IsItemInPrimaryList(i) && IsContainerNavigationViewItem(i))
{
count++;
}
}
return count;
}
int TopNavigationViewDataProvider::GetNavigationViewItemCountInTopNav()
{
int count = 0;
for (int i = 0; i < Size(); i++)
{
if (IsContainerNavigationViewItem(i))
{
count++;
}
}
return count;
}
void TopNavigationViewDataProvider::UpdateWidthForPrimaryItem(int indexInPrimary, float width)
{
auto vector = GetVector(NavigationViewSplitVectorID::PrimaryList);
if (vector)
{
const auto index = vector->IndexToIndexInOriginalVector(indexInPrimary);
SetWidthForItem(index, width);
}
}
float TopNavigationViewDataProvider::WidthRequiredToRecoveryAllItemsToPrimary()
{
auto width = 0.f;
for (int i=0; i<Size(); i++)
{
if (!IsItemInPrimaryList(i))
{
width += GetWidthForItem(i);
}
}
width -= m_overflowButtonCachedWidth;
return std::max(0.f, width);
}
bool TopNavigationViewDataProvider::HasInvalidWidth(std::vector<int> & items)
{
bool hasInvalidWidth = false;
for (auto &index : items)
{
if (!IsValidWidthForItem(index))
{
hasInvalidWidth = true;
break;
}
}
return hasInvalidWidth;
}
float TopNavigationViewDataProvider::GetWidthForItem(int index)
{
auto width = AttachedData(index);
if (!IsValidWidth(width))
{
width = 0;
}
return width;
}
float TopNavigationViewDataProvider::CalculateWidthForItems(std::vector<int> &items)
{
float width = 0.f;
for (auto &index : items)
{
width += GetWidthForItem(index);
}
return width;
}
void TopNavigationViewDataProvider::InvalidWidthCache()
{
ResetAttachedData(-1.0f);
}
float TopNavigationViewDataProvider::OverflowButtonWidth()
{
return m_overflowButtonCachedWidth;
}
void TopNavigationViewDataProvider::OverflowButtonWidth(float width)
{
m_overflowButtonCachedWidth = width;
}
bool TopNavigationViewDataProvider::IsItemSelectableInPrimaryList(const winrt::IInspectable& value)
{
const int index = IndexOf(value);
return (index != -1);
}
int TopNavigationViewDataProvider::IndexOf(const winrt::IInspectable& value, NavigationViewSplitVectorID vectorID)
{
return IndexOfImpl(value, vectorID);
}
void TopNavigationViewDataProvider::OnDataSourceChanged(const winrt::IInspectable& sender, const winrt::NotifyCollectionChangedEventArgs& args)
{
switch (args.Action())
{
case winrt::NotifyCollectionChangedAction::Add:
{
OnInsertAt(args.NewStartingIndex(), args.NewItems().Size());
break;
}
case winrt::NotifyCollectionChangedAction::Remove:
{
OnRemoveAt(args.OldStartingIndex(), args.OldItems().Size());
break;
}
case winrt::NotifyCollectionChangedAction::Reset:
{
OnClear();
break;
}
case winrt::NotifyCollectionChangedAction::Replace:
{
OnRemoveAt(args.OldStartingIndex(), args.OldItems().Size());
OnInsertAt(args.NewStartingIndex(), args.NewItems().Size());
break;
}
}
if (m_dataChangeCallback)
{
m_dataChangeCallback(args);
}
}
bool TopNavigationViewDataProvider::IsValidWidth(float width)
{
return (width >= 0) && (width < std::numeric_limits<float>::max());
}
bool TopNavigationViewDataProvider::IsValidWidthForItem(int index)
{
auto width = AttachedData(index);
return IsValidWidth(width);
}
void TopNavigationViewDataProvider::SetWidthForItem(int index, float width)
{
if (IsValidWidth(width))
{
AttachedData(index, width);
}
}
void TopNavigationViewDataProvider::ChangeDataSource(winrt::ItemsSourceView newValue)
{
auto oldValue = m_dataSource.get();
if (oldValue != newValue)
{
// update to the new datasource.
if (oldValue)
{
oldValue.CollectionChanged(m_dataSourceChanged);
}
Clear();
m_dataSource.set(newValue);
SyncAndInitVectorFlagsWithID(NavigationViewSplitVectorID::NotInitialized, DefaultAttachedData());
if (newValue)
{
m_dataSourceChanged = newValue.CollectionChanged({ this, &TopNavigationViewDataProvider::OnDataSourceChanged });
}
}
// Move all to primary list
MoveItemsToVector(NavigationViewSplitVectorID::NotInitialized);
}
bool TopNavigationViewDataProvider::IsItemInPrimaryList(int index)
{
return GetVectorIDForItem(index) == NavigationViewSplitVectorID::PrimaryList;
}
bool TopNavigationViewDataProvider::IsContainerNavigationViewItem(int index)
{
bool isContainerNavigationViewItem = true;
auto item = GetAt(index);
if (item && (item.try_as<winrt::NavigationViewItemHeader>() || item.try_as<winrt::NavigationViewItemSeparator>()))
{
isContainerNavigationViewItem = false;
}
return isContainerNavigationViewItem;
}
bool TopNavigationViewDataProvider::IsContainerNavigationViewHeader(int index)
{
bool isContainerNavigationViewHeader = false;
auto item = GetAt(index);
if (item && item.try_as<winrt::NavigationViewItemHeader>())
{
isContainerNavigationViewHeader = true;
}
return isContainerNavigationViewHeader;
}