Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 56 additions & 7 deletions source/uwp/SharedRenderer/lib/AdaptiveCarouselRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,51 @@ namespace winrt::AdaptiveCards::Rendering::Xaml_Rendering::implementation
if (carouselPageUI != nullptr)
{
carouselUI.Items().Append(carouselPageUI);
}
}

if (fixedHeightInPixel == 0)
{
carouselPageUI.try_as<FrameworkElement>().LayoutUpdated(
[carouselUI](auto&&, auto&&) { SetFlipViewMaxHeight(carouselUI); });
}
}
// 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);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SetFlipViewMaxHeight seems to be an operation that figure out the size for flipview by calling Measure operations on the child contents. So ideally this should get called during the Measure operation of FlipView to avoid such cycles.

*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);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably this may cause flickers as this will lead to mismatch of timing where the size change happens for CarouselUI and FlipView

*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())
Expand Down Expand Up @@ -192,11 +230,22 @@ namespace winrt::AdaptiveCards::Rendering::Xaml_Rendering::implementation
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())
Comment thread
dipeshmsft marked this conversation as resolved.
{
return;
}

auto carouselPageUI = flipView.Items().GetAt(selectIndex).try_as<FrameworkElement>();

if (flipView.IsLoaded())
if (carouselPageUI != nullptr && flipView.IsLoaded())
{
float width = static_cast<float>(flipView.ActualWidth());
if (width <= 0)
Comment thread
dipeshmsft marked this conversation as resolved.
{
return;
}
const winrt::Size noVerticalLimit{width, std::numeric_limits<float>::infinity()};
carouselPageUI.Measure(noVerticalLimit);
auto maxHeight = carouselPageUI.DesiredSize().Height;
Expand Down
Loading