-
Notifications
You must be signed in to change notification settings - Fork 705
/
Copy pathUniformGridLayoutState.cpp
167 lines (147 loc) · 6.22 KB
/
UniformGridLayoutState.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
// 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 "IFlowLayoutAlgorithmDelegates.h"
#include "FlowLayoutAlgorithm.h"
#include "UniformGridLayoutState.h"
CppWinRTActivatableClassWithBasicFactory(UniformGridLayoutState);
void UniformGridLayoutState::InitializeForContext(
const winrt::VirtualizingLayoutContext& context,
IFlowLayoutAlgorithmDelegates* callbacks)
{
m_flowAlgorithm.InitializeForContext(context, callbacks);
context.LayoutStateCore(*this);
}
void UniformGridLayoutState::UninitializeForContext(const winrt::VirtualizingLayoutContext& context)
{
m_flowAlgorithm.UninitializeForContext(context);
if (m_cachedFirstElement)
{
context.RecycleElement(m_cachedFirstElement);
}
}
void UniformGridLayoutState::EnsureElementSize(
const winrt::Size availableSize,
const winrt::VirtualizingLayoutContext& context,
const double layoutItemWidth,
const double LayoutItemHeight,
const winrt::UniformGridLayoutItemsStretch& stretch,
const winrt::Orientation& orientation,
double minRowSpacing,
double minColumnSpacing)
{
if (context.ItemCount() > 0)
{
// If the first element is realized we don't need to cache it or to get it from the context
if (auto realizedElement = m_flowAlgorithm.GetElementIfRealized(0))
{
realizedElement.Measure(availableSize);
SetSize(realizedElement, layoutItemWidth, LayoutItemHeight, availableSize, stretch, orientation, minRowSpacing, minColumnSpacing);
m_cachedFirstElement = nullptr;
}
else
{
if (!m_cachedFirstElement)
{
// we only cache if we aren't realizing it
m_cachedFirstElement = context.GetOrCreateElementAt(0, winrt::ElementRealizationOptions::ForceCreate | winrt::ElementRealizationOptions::SuppressAutoRecycle); // expensive
}
m_cachedFirstElement.Measure(availableSize);
SetSize(m_cachedFirstElement, layoutItemWidth, LayoutItemHeight, availableSize, stretch, orientation, minRowSpacing, minColumnSpacing);
// See if we can move ownership to the flow algorithm. If we can, we do not need a local cache.
bool added = m_flowAlgorithm.TryAddElement0(m_cachedFirstElement);
if (added)
{
m_cachedFirstElement = nullptr;
}
}
}
}
void UniformGridLayoutState::SetSize(
winrt::UIElement UIElement,
const double layoutItemWidth,
const double LayoutItemHeight,
const winrt::Size availableSize,
const winrt::UniformGridLayoutItemsStretch& stretch,
const winrt::Orientation& orientation,
double minRowSpacing,
double minColumnSpacing)
{
m_effectiveItemWidth = (std::isnan(layoutItemWidth) ? UIElement.DesiredSize().Width : layoutItemWidth);
m_effectiveItemHeight = (std::isnan(LayoutItemHeight) ? UIElement.DesiredSize().Height : LayoutItemHeight);
auto availableSizeMinor = orientation == winrt::Orientation::Horizontal ? availableSize.Width : availableSize.Height;
auto minorItemSpacing = orientation == winrt::Orientation::Vertical ? minRowSpacing : minColumnSpacing;
auto itemSizeMinor = orientation == winrt::Orientation::Horizontal ? m_effectiveItemWidth : m_effectiveItemHeight;
itemSizeMinor += minorItemSpacing;
auto numItemsPerColumn = static_cast<int>(std::max(1.0, availableSizeMinor / itemSizeMinor));
auto remainingSpace = ((int)availableSizeMinor) % ((int)itemSizeMinor);
auto extraMinorPixelsForEachItem = remainingSpace / numItemsPerColumn;
if (stretch == winrt::UniformGridLayoutItemsStretch::Fill)
{
if (orientation == winrt::Orientation::Horizontal)
{
m_effectiveItemWidth += extraMinorPixelsForEachItem;
}
else
{
m_effectiveItemHeight += extraMinorPixelsForEachItem;
}
}
else if (stretch == winrt::UniformGridLayoutItemsStretch::Uniform)
{
auto itemSizeMajor = orientation == winrt::Orientation::Horizontal ? m_effectiveItemHeight : m_effectiveItemWidth;
auto extraMajorPixelsForEachItem = itemSizeMajor * (extraMinorPixelsForEachItem / itemSizeMinor);
if (orientation == winrt::Orientation::Horizontal)
{
m_effectiveItemWidth += extraMinorPixelsForEachItem;
m_effectiveItemHeight += extraMajorPixelsForEachItem;
}
else
{
m_effectiveItemHeight += extraMinorPixelsForEachItem;
m_effectiveItemWidth += extraMajorPixelsForEachItem;
}
}
}
void UniformGridLayoutState::EnsureFirstElementOwnership(winrt::VirtualizingLayoutContext const& context)
{
if (m_cachedFirstElement != nullptr && m_flowAlgorithm.GetElementIfRealized(0))
{
// We created the element, but then flowlayout algorithm took ownership, so we can clear it and
// let flowlayout algorithm do its thing.
context.RecycleElement(m_cachedFirstElement);
m_cachedFirstElement = nullptr;
}
}
void UniformGridLayoutState::ClearElementOnDataSourceChange(winrt::VirtualizingLayoutContext const& context, winrt::NotifyCollectionChangedEventArgs const& args)
{
if (m_cachedFirstElement)
{
bool shouldClear = false;
switch (args.Action())
{
case winrt::NotifyCollectionChangedAction::Add:
shouldClear = args.NewStartingIndex() == 0;
break;
case winrt::NotifyCollectionChangedAction::Replace:
shouldClear = args.NewStartingIndex() == 0 || args.OldStartingIndex() == 0;
break;
case winrt::NotifyCollectionChangedAction::Remove:
shouldClear = args.OldStartingIndex() == 0;
break;
case winrt::NotifyCollectionChangedAction::Reset:
shouldClear = true;
break;
case winrt::NotifyCollectionChangedAction::Move:
throw winrt::hresult_not_implemented();
break;
}
if (shouldClear)
{
context.RecycleElement(m_cachedFirstElement);
m_cachedFirstElement = nullptr;
}
}
}