-
Notifications
You must be signed in to change notification settings - Fork 705
/
Copy pathItemsRepeater.cpp
767 lines (649 loc) · 24.3 KB
/
ItemsRepeater.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
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
// 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 "ItemsRepeater.common.h"
#include "ItemsRepeater.h"
#include "RepeaterLayoutContext.h"
#include "ChildrenInTabFocusOrderIterable.h"
#include "SharedHelpers.h"
#include "RepeaterAutomationPeer.h"
#include "ViewportManagerWithPlatformFeatures.h"
#include "ViewportManagerDownlevel.h"
#include "RuntimeProfiler.h"
#include "ItemTemplateWrapper.h"
// Change to 'true' to turn on debugging outputs in Output window
bool RepeaterTrace::s_IsDebugOutputEnabled{ false };
winrt::Point ItemsRepeater::ClearedElementsArrangePosition = winrt::Point(-10000.0f, -10000.0f);
winrt::Rect ItemsRepeater::InvalidRect = { -1.f, -1.f, -1.f, -1.f };
ItemsRepeater::ItemsRepeater()
{
__RP_Marker_ClassById(RuntimeProfiler::ProfId_ItemsRepeater);
if (SharedHelpers::IsRS5OrHigher())
{
m_viewportManager = std::make_shared<ViewportManagerWithPlatformFeatures>(this);
}
else
{
m_viewportManager = std::make_shared<ViewportManagerDownLevel>(this);
}
winrt::AutomationProperties::SetAccessibilityView(*this, winrt::AccessibilityView::Raw);
if (SharedHelpers::IsRS3OrHigher())
{
TabFocusNavigation(winrt::KeyboardNavigationMode::Once);
XYFocusKeyboardNavigation(winrt::XYFocusKeyboardNavigationMode::Enabled);
}
Loaded({ this, &ItemsRepeater::OnLoaded });
Unloaded({ this, &ItemsRepeater::OnUnloaded });
// Initialize the cached layout to the default value
auto layout = Layout().as<winrt::VirtualizingLayout>();
OnLayoutChanged(nullptr, layout);
}
ItemsRepeater::~ItemsRepeater()
{
m_itemTemplate = nullptr;
m_animator = nullptr;
m_layout = nullptr;
}
#pragma region IUIElementOverrides
winrt::AutomationPeer ItemsRepeater::OnCreateAutomationPeer()
{
return winrt::make<RepeaterAutomationPeer>(*this);
}
#pragma endregion
#pragma region IUIElementOverrides7
winrt::IIterable<winrt::DependencyObject> ItemsRepeater::GetChildrenInTabFocusOrder()
{
return CreateChildrenInTabFocusOrderIterable();
}
#pragma endregion
#pragma region IUIElementOverrides8
void ItemsRepeater::OnBringIntoViewRequested(winrt::BringIntoViewRequestedEventArgs const& e)
{
m_viewportManager->OnBringIntoViewRequested(e);
}
#pragma endregion
#pragma region IFrameworkElementOverrides
winrt::Size ItemsRepeater::MeasureOverride(winrt::Size const& availableSize)
{
if (m_isLayoutInProgress)
{
throw winrt::hresult_error(E_FAIL, L"Reentrancy detected during layout.");
}
if (IsProcessingCollectionChange())
{
throw winrt::hresult_error(E_FAIL, L"Cannot run layout in the middle of a collection change.");
}
m_viewportManager->OnOwnerMeasuring();
m_isLayoutInProgress = true;
auto layoutInProgress = gsl::finally([this]()
{
m_isLayoutInProgress = false;
});
m_viewManager.PrunePinnedElements();
winrt::Rect extent{};
winrt::Size desiredSize{};
if (auto layout = Layout())
{
auto layoutContext = GetLayoutContext();
// Expensive operation, do it only in debug builds.
#ifdef _DEBUG
auto virtualContext = winrt::get_self<VirtualizingLayoutContext>(layoutContext);
virtualContext->Indent(Indent());
#endif
// Checking if we have an data template and it is empty
if (m_isItemTemplateEmpty) {
// Has no content, so we will draw nothing and request zero space
extent = winrt::Rect{ m_layoutOrigin.X, m_layoutOrigin.Y, 0, 0 };
}
else {
desiredSize = layout.Measure(layoutContext, availableSize);
extent = winrt::Rect{ m_layoutOrigin.X, m_layoutOrigin.Y, desiredSize.Width, desiredSize.Height };
}
// Clear auto recycle candidate elements that have not been kept alive by layout - i.e layout did not
// call GetElementAt(index).
auto children = Children();
for (unsigned i = 0u; i < children.Size(); ++i)
{
auto element = children.GetAt(i);
auto virtInfo = GetVirtualizationInfo(element);
if (virtInfo->Owner() == ElementOwner::Layout &&
virtInfo->AutoRecycleCandidate() &&
!virtInfo->KeepAlive())
{
REPEATER_TRACE_INFO(L"AutoClear - %d \n", virtInfo->Index());
ClearElementImpl(element);
}
}
}
m_viewportManager->SetLayoutExtent(extent);
m_lastAvailableSize = availableSize;
return desiredSize;
}
winrt::Size ItemsRepeater::ArrangeOverride(winrt::Size const& finalSize)
{
if (m_isLayoutInProgress)
{
throw winrt::hresult_error(E_FAIL, L"Reentrancy detected during layout.");
}
if (IsProcessingCollectionChange())
{
throw winrt::hresult_error(E_FAIL, L"Cannot run layout in the middle of a collection change.");
}
m_isLayoutInProgress = true;
auto layoutInProgress = gsl::finally([this]()
{
m_isLayoutInProgress = false;
});
winrt::Size arrangeSize{};
if (auto layout = Layout())
{
arrangeSize = layout.Arrange(GetLayoutContext(), finalSize);
}
// The view manager might clear elements during this call.
// That's why we call it before arranging cleared elements
// off screen.
m_viewManager.OnOwnerArranged();
auto children = Children();
for (unsigned i = 0u; i < children.Size(); ++i)
{
auto element = children.GetAt(i);
auto virtInfo = GetVirtualizationInfo(element);
virtInfo->KeepAlive(false);
if (virtInfo->Owner() == ElementOwner::ElementFactory ||
virtInfo->Owner() == ElementOwner::PinnedPool)
{
// Toss it away. And arrange it with size 0 so that XYFocus won't use it.
element.Arrange(winrt::Rect{
ClearedElementsArrangePosition.X - static_cast<float>(element.DesiredSize().Width),
ClearedElementsArrangePosition.Y - static_cast<float>(element.DesiredSize().Height),
0.0f,
0.0f });
}
else
{
const auto newBounds = CachedVisualTreeHelpers::GetLayoutSlot(element.as<winrt::FrameworkElement>());
if (virtInfo->ArrangeBounds() != ItemsRepeater::InvalidRect &&
newBounds != virtInfo->ArrangeBounds())
{
m_animationManager.OnElementBoundsChanged(element, virtInfo->ArrangeBounds(), newBounds);
}
virtInfo->ArrangeBounds(newBounds);
}
}
m_viewportManager->OnOwnerArranged();
m_animationManager.OnOwnerArranged();
return arrangeSize;
}
#pragma endregion
#pragma region IRepeater interface.
winrt::ItemsSourceView ItemsRepeater::ItemsSourceView()
{
return m_itemsSourceView.get();
}
int32_t ItemsRepeater::GetElementIndex(winrt::UIElement const& element)
{
return GetElementIndexImpl(element);
}
winrt::UIElement ItemsRepeater::TryGetElement(int index)
{
return GetElementFromIndexImpl(index);
}
void ItemsRepeater::PinElement(winrt::UIElement const& element)
{
m_viewManager.UpdatePin(element, true /* addPin */);
}
void ItemsRepeater::UnpinElement(winrt::UIElement const& element)
{
m_viewManager.UpdatePin(element, false /* addPin */);
}
winrt::UIElement ItemsRepeater::GetOrCreateElement(int index)
{
return GetOrCreateElementImpl(index);
}
#pragma endregion
winrt::UIElement ItemsRepeater::GetElementImpl(int index, bool forceCreate, bool suppressAutoRecycle)
{
auto element = m_viewManager.GetElement(index, forceCreate, suppressAutoRecycle);
return element;
}
void ItemsRepeater::ClearElementImpl(const winrt::UIElement& element)
{
// Clearing an element due to a collection change
// is more strict in that pinned elements will be forcibly
// unpinned and sent back to the view generator.
const bool isClearedDueToCollectionChange =
IsProcessingCollectionChange() &&
(m_processingItemsSourceChange.get().Action() == winrt::NotifyCollectionChangedAction::Remove ||
m_processingItemsSourceChange.get().Action() == winrt::NotifyCollectionChangedAction::Replace ||
m_processingItemsSourceChange.get().Action() == winrt::NotifyCollectionChangedAction::Reset);
m_viewManager.ClearElement(element, isClearedDueToCollectionChange);
m_viewportManager->OnElementCleared(element);
}
int ItemsRepeater::GetElementIndexImpl(const winrt::UIElement& element)
{
// Verify that element is actually a child of this ItemsRepeater
auto const parent = winrt::VisualTreeHelper::GetParent(element);
if (parent == *this)
{
auto virtInfo = TryGetVirtualizationInfo(element);
return m_viewManager.GetElementIndex(virtInfo);
}
return -1;
}
winrt::UIElement ItemsRepeater::GetElementFromIndexImpl(int index)
{
winrt::UIElement result = nullptr;
auto children = Children();
for (unsigned i = 0u; i < children.Size() && !result; ++i)
{
auto element = children.GetAt(i);
auto virtInfo = TryGetVirtualizationInfo(element);
if (virtInfo && virtInfo->IsRealized() && virtInfo->Index() == index)
{
result = element;
}
}
return result;
}
winrt::UIElement ItemsRepeater::GetOrCreateElementImpl(int index)
{
if (index >= 0 && index >= ItemsSourceView().Count())
{
throw winrt::hresult_invalid_argument(L"Argument index is invalid.");
}
if (m_isLayoutInProgress)
{
throw winrt::hresult_error(E_FAIL, L"GetOrCreateElement invocation is not allowed during layout.");
}
auto element = GetElementFromIndexImpl(index);
const bool isAnchorOutsideRealizedRange = !element;
if (isAnchorOutsideRealizedRange)
{
if (!Layout())
{
throw winrt::hresult_error(E_FAIL, L"Cannot make an Anchor when there is no attached layout.");
}
element = GetLayoutContext().GetOrCreateElementAt(index);
element.Measure({ std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity() });
}
m_viewportManager->OnMakeAnchor(element, isAnchorOutsideRealizedRange);
InvalidateMeasure();
return element;
}
/*static*/
winrt::com_ptr<VirtualizationInfo> ItemsRepeater::TryGetVirtualizationInfo(const winrt::UIElement& element)
{
auto value = element.GetValue(GetVirtualizationInfoProperty());
return winrt::get_self<VirtualizationInfo>(value)->get_strong();
}
/*static*/
winrt::com_ptr<VirtualizationInfo> ItemsRepeater::GetVirtualizationInfo(const winrt::UIElement& element)
{
auto result = TryGetVirtualizationInfo(element);
if (!result)
{
result = CreateAndInitializeVirtualizationInfo(element);
}
return result;
}
/* static */
winrt::com_ptr<VirtualizationInfo> ItemsRepeater::CreateAndInitializeVirtualizationInfo(const winrt::UIElement& element)
{
MUX_ASSERT(!TryGetVirtualizationInfo(element));
auto result = winrt::make_self<VirtualizationInfo>();
element.SetValue(GetVirtualizationInfoProperty(), result.as<winrt::IInspectable>());
return result;
}
void ItemsRepeater::OnPropertyChanged(const winrt::DependencyPropertyChangedEventArgs& args)
{
winrt::IDependencyProperty property = args.Property();
if (property == s_ItemsSourceProperty)
{
if (args.NewValue() != args.OldValue())
{
auto newValue = args.NewValue();
auto newDataSource = newValue.try_as<winrt::ItemsSourceView>();
if (newValue && !newDataSource)
{
newDataSource = winrt::ItemsSourceView(newValue);
}
OnDataSourcePropertyChanged(m_itemsSourceView.get(), newDataSource);
}
}
else if (property == s_ItemTemplateProperty)
{
OnItemTemplateChanged(args.OldValue().as<winrt::IElementFactory>(), args.NewValue().as<winrt::IElementFactory>());
}
else if (property == s_LayoutProperty)
{
OnLayoutChanged(args.OldValue().as<winrt::Layout>(), args.NewValue().as<winrt::Layout>());
}
else if (property == s_AnimatorProperty)
{
OnAnimatorChanged(args.OldValue().as<winrt::ElementAnimator>(), args.NewValue().as<winrt::ElementAnimator>());
}
else if (property == s_HorizontalCacheLengthProperty)
{
m_viewportManager->HorizontalCacheLength(unbox_value<double>(args.NewValue()));
}
else if (property == s_VerticalCacheLengthProperty)
{
m_viewportManager->VerticalCacheLength(unbox_value<double>(args.NewValue()));
}
}
void ItemsRepeater::OnElementPrepared(const winrt::UIElement& element, int index)
{
m_viewportManager->OnElementPrepared(element);
if (m_elementPreparedEventSource)
{
if (!m_elementPreparedArgs)
{
m_elementPreparedArgs = tracker_ref<winrt::ItemsRepeaterElementPreparedEventArgs>(this, winrt::make<ItemsRepeaterElementPreparedEventArgs>(element, index));
}
else
{
winrt::get_self<ItemsRepeaterElementPreparedEventArgs>(m_elementPreparedArgs.get())->Update(element, index);
}
m_elementPreparedEventSource(*this, m_elementPreparedArgs.get());
}
}
void ItemsRepeater::OnElementClearing(const winrt::UIElement& element)
{
if (m_elementClearingEventSource)
{
if (!m_elementClearingArgs)
{
m_elementClearingArgs = tracker_ref<winrt::ItemsRepeaterElementClearingEventArgs>(this, winrt::make<ItemsRepeaterElementClearingEventArgs>(element));
}
else
{
winrt::get_self<ItemsRepeaterElementClearingEventArgs>(m_elementClearingArgs.get())->Update(element);
}
m_elementClearingEventSource(*this, m_elementClearingArgs.get());
}
}
void ItemsRepeater::OnElementIndexChanged(const winrt::UIElement& element, int oldIndex, int newIndex)
{
if (m_elementIndexChangedEventSource)
{
if (!m_elementIndexChangedArgs)
{
m_elementIndexChangedArgs = tracker_ref<winrt::ItemsRepeaterElementIndexChangedEventArgs>(this, winrt::make<ItemsRepeaterElementIndexChangedEventArgs>(element, oldIndex, newIndex));
}
else
{
winrt::get_self<ItemsRepeaterElementIndexChangedEventArgs>(m_elementIndexChangedArgs.get())->Update(element, oldIndex, newIndex);
}
m_elementIndexChangedEventSource(*this, m_elementIndexChangedArgs.get());
}
}
// Provides an indentation based on repeater elements in the UI Tree that
// can be used to make logging a little easier to read.
int ItemsRepeater::Indent()
{
int indent = 1;
// Expensive, so we do it only in debug builds.
#ifdef _DEBUG
auto parent = this->Parent().as<winrt::FrameworkElement>();
while (parent && !parent.try_as<winrt::ItemsRepeater>())
{
parent = parent.Parent().as<winrt::FrameworkElement>();
}
if (parent)
{
auto parentRepeater = winrt::get_self<ItemsRepeater>(parent.as<winrt::ItemsRepeater>());
indent = parentRepeater->Indent();
}
#endif
return indent * 4;
}
void ItemsRepeater::OnLoaded(const winrt::IInspectable& /*sender*/, const winrt::RoutedEventArgs& /*args*/)
{
// If we skipped an unload event, reset the scrollers now and invalidate measure so that we get a new
// layout pass during which we will hookup new scrollers.
if (_loadedCounter > _unloadedCounter)
{
InvalidateMeasure();
m_viewportManager->ResetScrollers();
}
++_loadedCounter;
}
void ItemsRepeater::OnUnloaded(const winrt::IInspectable& /*sender*/, const winrt::RoutedEventArgs& /*args*/)
{
++_unloadedCounter;
// Only reset the scrollers if this unload event is in-sync.
if (_unloadedCounter == _loadedCounter)
{
m_viewportManager->ResetScrollers();
}
}
void ItemsRepeater::OnDataSourcePropertyChanged(const winrt::ItemsSourceView& oldValue, const winrt::ItemsSourceView& newValue)
{
if (m_isLayoutInProgress)
{
throw winrt::hresult_error(E_FAIL, L"Cannot set ItemsSourceView during layout.");
}
m_itemsSourceView.set(newValue);
if (oldValue)
{
m_itemsSourceViewChanged.revoke();
}
if (newValue)
{
m_itemsSourceViewChanged = newValue.CollectionChanged(winrt::auto_revoke, { this, &ItemsRepeater::OnItemsSourceViewChanged });
}
if (auto const layout = Layout())
{
auto const args = winrt::NotifyCollectionChangedEventArgs(
winrt::NotifyCollectionChangedAction::Reset,
nullptr /* newItems */,
nullptr /* oldItems */,
-1 /* newIndex */,
-1 /* oldIndex */);
args.Action();
auto const processingChange = gsl::finally([this]()
{
m_processingItemsSourceChange.set(nullptr);
});
m_processingItemsSourceChange.set(args);
if (auto const virtualLayout = layout.try_as<winrt::VirtualizingLayout>())
{
virtualLayout.OnItemsChangedCore(GetLayoutContext(), newValue, args);
}
else if (auto const nonVirtualLayout = layout.try_as<winrt::NonVirtualizingLayout>())
{
// Walk through all the elements and make sure they are cleared for
// non-virtualizing layouts.
for (const auto& element: Children())
{
if (GetVirtualizationInfo(element)->IsRealized())
{
ClearElementImpl(element);
}
}
Children().Clear();
}
InvalidateMeasure();
}
}
void ItemsRepeater::OnItemTemplateChanged(const winrt::IElementFactory& oldValue, const winrt::IElementFactory& newValue)
{
if (m_isLayoutInProgress && oldValue)
{
throw winrt::hresult_error(E_FAIL, L"ItemTemplate cannot be changed during layout.");
}
// Since the ItemTemplate has changed, we need to re-evaluate all the items that
// have already been created and are now in the tree. The easiest way to do that
// would be to do a reset.. Note that this has to be done before we change the template
// so that the cleared elements go back into the old template.
if (auto const layout = Layout())
{
auto const args = winrt::NotifyCollectionChangedEventArgs(
winrt::NotifyCollectionChangedAction::Reset,
nullptr /* newItems */,
nullptr /* oldItems */,
-1 /* newIndex */,
-1 /* oldIndex */);
args.Action();
auto const processingChange = gsl::finally([this]()
{
m_processingItemsSourceChange.set(nullptr);
});
m_processingItemsSourceChange.set(args);
if (auto const virtualLayout = layout.try_as<winrt::VirtualizingLayout>())
{
virtualLayout.OnItemsChangedCore(GetLayoutContext(), newValue, args);
}
else if (auto const nonVirtualLayout = layout.try_as<winrt::NonVirtualizingLayout>())
{
// Walk through all the elements and make sure they are cleared for
// non-virtualizing layouts.
for (auto const& child : Children())
{
if (GetVirtualizationInfo(child)->IsRealized())
{
ClearElementImpl(child);
}
}
}
}
if (!SharedHelpers::IsRS5OrHigher())
{
// Bug in framework's reference tracking causes crash during
// UIAffinityQueue cleanup. To avoid that bug, take a strong ref
m_itemTemplate = newValue;
}
// Clear flag for bug #776
m_isItemTemplateEmpty = false;
m_itemTemplateWrapper = newValue.try_as<winrt::IElementFactoryShim>();
if (!m_itemTemplateWrapper)
{
// ItemTemplate set does not implement IElementFactoryShim. We also
// want to support DataTemplate and DataTemplateSelectors automagically.
if (auto dataTemplate = newValue.try_as<winrt::DataTemplate>())
{
m_itemTemplateWrapper = winrt::make<ItemTemplateWrapper>(dataTemplate);
if (!dataTemplate.LoadContent().as<winrt::FrameworkElement>()) {
// We have a DataTemplate which is empty, so we need to set it to true
m_isItemTemplateEmpty = true;
}
}
else if (auto selector = newValue.try_as<winrt::DataTemplateSelector>())
{
m_itemTemplateWrapper = winrt::make<ItemTemplateWrapper>(selector);
}
else
{
throw winrt::hresult_invalid_argument(L"ItemTemplate");
}
}
InvalidateMeasure();
}
void ItemsRepeater::OnLayoutChanged(const winrt::Layout& oldValue, const winrt::Layout& newValue)
{
if (m_isLayoutInProgress)
{
throw winrt::hresult_error(E_FAIL, L"Layout cannot be changed during layout.");
}
m_viewManager.OnLayoutChanging();
m_animationManager.OnLayoutChanging();
if (oldValue)
{
oldValue.UninitializeForContext(GetLayoutContext());
m_measureInvalidated.revoke();
m_arrangeInvalidated.revoke();
// Walk through all the elements and make sure they are cleared
auto children = Children();
for (unsigned i = 0u; i < children.Size(); ++i)
{
auto element = children.GetAt(i);
if (GetVirtualizationInfo(element)->IsRealized())
{
ClearElementImpl(element);
}
}
m_layoutState.set(nullptr);
}
if (!SharedHelpers::IsRS5OrHigher())
{
// Bug in framework's reference tracking causes crash during
// UIAffinityQueue cleanup. To avoid that bug, take a strong ref
m_layout = newValue;
}
if (newValue)
{
newValue.InitializeForContext(GetLayoutContext());
m_measureInvalidated = newValue.MeasureInvalidated(winrt::auto_revoke, { this, &ItemsRepeater::InvalidateMeasureForLayout });
m_arrangeInvalidated = newValue.ArrangeInvalidated(winrt::auto_revoke, { this, &ItemsRepeater::InvalidateArrangeForLayout });
}
bool isVirtualizingLayout = newValue != nullptr && newValue.try_as<winrt::VirtualizingLayout>() != nullptr;
m_viewportManager->OnLayoutChanged(isVirtualizingLayout);
InvalidateMeasure();
}
void ItemsRepeater::OnAnimatorChanged(const winrt::ElementAnimator& /* oldValue */, const winrt::ElementAnimator& newValue)
{
m_animationManager.OnAnimatorChanged(newValue);
if (!SharedHelpers::IsRS5OrHigher())
{
// Bug in framework's reference tracking causes crash during
// UIAffinityQueue cleanup. To avoid that bug, take a strong ref
m_animator = newValue;
}
}
void ItemsRepeater::OnItemsSourceViewChanged(const winrt::IInspectable& sender, const winrt::NotifyCollectionChangedEventArgs& args)
{
if (m_isLayoutInProgress)
{
// Bad things will follow if the data changes while we are in the middle of a layout pass.
throw winrt::hresult_error(E_FAIL, L"Changes in data source are not allowed during layout.");
}
if (IsProcessingCollectionChange())
{
throw winrt::hresult_error(E_FAIL, L"Changes in the data source are not allowed during another change in the data source.");
}
m_processingItemsSourceChange.set(args);
auto processingChange = gsl::finally([this]()
{
m_processingItemsSourceChange.set(nullptr);
});
m_animationManager.OnItemsSourceChanged(sender, args);
m_viewManager.OnItemsSourceChanged(sender, args);
if (auto layout = Layout())
{
if (auto virtualLayout = layout.try_as<winrt::VirtualizingLayout>())
{
virtualLayout.OnItemsChangedCore(GetLayoutContext(), sender, args);
}
else
{
// NonVirtualizingLayout
InvalidateMeasure();
}
}
}
void ItemsRepeater::InvalidateMeasureForLayout(winrt::Layout const&, winrt::IInspectable const&)
{
InvalidateMeasure();
}
void ItemsRepeater::InvalidateArrangeForLayout(winrt::Layout const&, winrt::IInspectable const&)
{
InvalidateArrange();
}
winrt::VirtualizingLayoutContext ItemsRepeater::GetLayoutContext()
{
if (!m_layoutContext)
{
m_layoutContext.set(winrt::make<RepeaterLayoutContext>(*this));
}
return m_layoutContext.get();
}
winrt::IIterable<winrt::DependencyObject> ItemsRepeater::CreateChildrenInTabFocusOrderIterable()
{
auto children = Children();
if (children.Size() > 0u)
{
return winrt::make<ChildrenInTabFocusOrderIterable>(*this);
}
return nullptr;
}