-
Notifications
You must be signed in to change notification settings - Fork 596
Expand file tree
/
Copy pathAdaptiveCarouselRenderer.cpp
More file actions
260 lines (218 loc) · 10.9 KB
/
AdaptiveCarouselRenderer.cpp
File metadata and controls
260 lines (218 loc) · 10.9 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "pch.h"
#include "ActionHelpers.h"
#include "AdaptiveCarouselRenderer.h"
#include "AdaptiveCarouselRenderer.g.cpp"
#include "AdaptiveRenderArgs.h"
#include "WholeItemsPanel.h"
#include "XamlBuilder.h"
#include <windows.ui.xaml.data.h>
namespace winrt::AdaptiveCards::Rendering::Xaml_Rendering::implementation
{
winrt::UIElement AdaptiveCarouselRenderer::Render(
winrt::AdaptiveCards::ObjectModel::Xaml_Rendering::IAdaptiveCardElement const& element,
winrt::AdaptiveCards::Rendering::Xaml_Rendering::AdaptiveRenderContext const& context,
winrt::AdaptiveCards::Rendering::Xaml_Rendering::AdaptiveRenderArgs const& renderArgs)
{
auto carousel = element.as<winrt::IAdaptiveCarousel>();
auto styledCollection = element.as<winrt::IAdaptiveContainerBase>();
StackPanel stackPanel{};
FlipView carouselUI{};
bool loopEnabled{false};
if (carousel.AutoLoop())
{
loopEnabled = carousel.AutoLoop().GetBoolean();
}
PipsPager pipsPager{};
pipsPager.HorizontalAlignment(winrt::HorizontalAlignment::Center);
pipsPager.NumberOfPages(carousel.Pages().Size());
auto hostConfig = context.HostConfig();
// FlipView has its own background color property, so we need to clear the background color
carouselUI.Background(winrt::SolidColorBrush{GetColorFromString("#00FFFFFF")});
//auto adaptiveCarouselContainer = element.as<winrt::IAdaptiveContainerBase>();
// Get any RTL setting set on either the current context or on this container. Any value set on the
// container should be set on the context to apply to all children
auto previousContextRtl = context.Rtl();
auto currentRtl = previousContextRtl;
auto containerRtl = carousel.Rtl();
bool updatedRtl = false;
if (containerRtl)
{
currentRtl = containerRtl;
context.Rtl(currentRtl);
updatedRtl = true;
}
if (currentRtl)
{
carouselUI.FlowDirection(currentRtl.GetBoolean() ? winrt::FlowDirection::RightToLeft : winrt::FlowDirection::LeftToRight);
pipsPager.FlowDirection(currentRtl.GetBoolean() ? winrt::FlowDirection::RightToLeft : winrt::FlowDirection::LeftToRight);
}
carouselUI.SelectionChanged([carouselUI, pipsPager, loopEnabled](auto &&, auto &&) {
auto val = carouselUI.SelectedIndex();
if (loopEnabled &&
static_cast<unsigned int>(val) == carouselUI.Items().Size())
{
pipsPager.SelectedPageIndex(0);
carouselUI.SelectedIndex(0);
}
else
{
pipsPager.SelectedPageIndex(carouselUI.SelectedIndex());
}
});
pipsPager.SelectedIndexChanged([carouselUI](PipsPager pager, IPipsPagerSelectedIndexChangedEventArgs) {
carouselUI.SelectedIndex(pager.SelectedPageIndex());
});
stackPanel.Children().Append(carouselUI);
stackPanel.Children().Append(pipsPager);
winrt::AdaptiveFeatureRegistration featureRegistration = context.FeatureRegistration();
boolean ancestorHasFallback = renderArgs.AncestorHasFallback();
try
{
winrt::Border carouselBorder{};
auto gridContainer = winrt::make<winrt::implementation::WholeItemsPanel>();
// Assign vertical alignment to strech so column will stretch and respect vertical content alignment
auto containerHeightType = element.Height();
if (containerHeightType == winrt::HeightType::Auto)
{
gridContainer.VerticalAlignment(winrt::VerticalAlignment::Stretch);
}
uint32_t carouselMinHeight = styledCollection.MinHeight();
if (carouselMinHeight > 0)
{
gridContainer.MinHeight(carouselMinHeight);
}
auto fixedHeightInPixel = carousel.HeightInPixels();
if (fixedHeightInPixel)
{
carouselUI.MaxHeight(static_cast<double>(fixedHeightInPixel));
}
auto containerStyle =
XamlHelpers::HandleStylingAndPadding(styledCollection, carouselBorder, context, renderArgs);
auto newRenderArgs =
winrt::make<winrt::implementation::AdaptiveRenderArgs>(containerStyle, renderArgs.ParentElement(), renderArgs);
for (auto page : carousel.Pages())
{
auto [carouselPageUI, carouselPage] =
::AdaptiveCards::Rendering::Xaml_Rendering::XamlBuilder::RenderAsUIElement(
page, context, newRenderArgs, featureRegistration, ancestorHasFallback);
if (carouselPageUI != nullptr)
{
carouselUI.Items().Append(carouselPageUI);
}
// If no fixed height was specified, compute the max height from page content
// after the FlipView has been loaded (not during layout) to avoid layout cycles.
if (fixedHeightInPixel == 0)
{
auto isUpdatingHeight = std::make_shared<bool>(false);
// Use Loaded event to set initial height — fires once after the element
// is in the visual tree and has been measured, but not during a layout pass.
auto loadedToken = carouselUI.Loaded([carouselUI, isUpdatingHeight](auto&&, auto&&) {
if (*isUpdatingHeight)
return;
*isUpdatingHeight = true;
SetFlipViewMaxHeight(carouselUI);
*isUpdatingHeight = false;
});
// Use SizeChanged to adjust if the FlipView width changes (e.g. window resize).
// SizeChanged fires after layout completes, so it won't cause a layout cycle
// as long as we guard against re-entrancy.
auto sizeChangedToken =
carouselUI.SizeChanged([carouselUI, isUpdatingHeight](auto&&, winrt::SizeChangedEventArgs const&) {
if (*isUpdatingHeight)
return;
*isUpdatingHeight = true;
SetFlipViewMaxHeight(carouselUI);
*isUpdatingHeight = false;
});
// Update height when the selected page changes, since pages may differ in size.
auto selectionChangedToken = carouselUI.SelectionChanged([carouselUI, isUpdatingHeight](auto&&, auto&&) {
if (*isUpdatingHeight)
return;
*isUpdatingHeight = true;
SetFlipViewMaxHeight(carouselUI);
*isUpdatingHeight = false;
});
carouselUI.Unloaded([ carouselUI, loadedToken, sizeChangedToken, selectionChangedToken ](auto&&, auto&&) {
carouselUI.Loaded(loadedToken);
carouselUI.SizeChanged(sizeChangedToken);
carouselUI.SelectionChanged(selectionChangedToken);
});
}
}
if (carousel.InitialPage())
{
carouselUI.SelectedIndex(carousel.InitialPage().GetUInt32());
}
auto verticalContentAlignmentReference = carousel.VerticalContentAlignment();
winrt::VerticalContentAlignment verticalContentAlignment =
GetValueFromRef(verticalContentAlignmentReference, winrt::VerticalContentAlignment::Top);
XamlHelpers::SetVerticalContentAlignmentToChildren(gridContainer, verticalContentAlignment);
// Check if backgroundImage defined
auto backgroundImage = carousel.BackgroundImage();
if (IsBackgroundImageValid(backgroundImage))
{
winrt::Grid rootElement{};
XamlHelpers::ApplyBackgroundToRoot(rootElement, backgroundImage, context);
// Add rootElement to containerBorder
carouselBorder.Child(rootElement);
// Add containerPanel to rootElement
XamlHelpers::AppendXamlElementToPanel(gridContainer, rootElement, containerHeightType);
}
else
{
// instead, directly add containerPanel to containerBorder
carouselBorder.Child(gridContainer);
}
// If we changed the context's rtl setting, set it back after rendering the children
if (updatedRtl)
{
context.Rtl(previousContextRtl);
}
XamlHelpers::SetStyleFromResourceDictionary(context, L"Adaptive.Carousel", carouselUI);
auto adaptiveBaseElement = element.try_as<winrt::IAdaptiveCardElement>();
auto heightType = adaptiveBaseElement.Height();
XamlHelpers::AppendXamlElementToPanel(stackPanel, gridContainer, heightType);
auto selectAction = styledCollection.SelectAction();
return ::AdaptiveCards::Rendering::Xaml_Rendering::ActionHelpers::HandleSelectAction(
element, selectAction, context, renderArgs, carouselBorder, XamlHelpers::SupportsInteractivity(hostConfig), true);
}
catch (winrt::hresult_error const& ex)
{
// In case we need to perform fallback, propagate it up to the parent
if (ex.code() == E_PERFORM_FALLBACK)
{
throw ex;
}
XamlHelpers::ErrForRenderFailedForElement(context, element.ElementTypeString(), ex.message());
}
return nullptr;
}
void SetFlipViewMaxHeight(FlipView const& flipView)
{
auto selectIndex = flipView.SelectedIndex();
// FIX: Validate selectedIndex is within bounds
if (selectIndex < 0 || static_cast<uint32_t>(selectIndex) >= flipView.Items().Size())
{
return;
}
auto carouselPageUI = flipView.Items().GetAt(selectIndex).try_as<FrameworkElement>();
if (carouselPageUI != nullptr && flipView.IsLoaded())
{
float width = static_cast<float>(flipView.ActualWidth());
if (width <= 0)
{
return;
}
const winrt::Size noVerticalLimit{width, std::numeric_limits<float>::infinity()};
carouselPageUI.Measure(noVerticalLimit);
auto maxHeight = carouselPageUI.DesiredSize().Height;
auto previousMaxHeight = flipView.MaxHeight();
if ((std::numeric_limits<float>::infinity() == previousMaxHeight) || previousMaxHeight < maxHeight)
{
flipView.MaxHeight(maxHeight);
}
}
}
}