Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the timing of Measure method call when SwipeControl is included #2087

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ protected override void DisconnectHandler(Popup platformView)
{
MauiContext.GetPlatformWindow().SizeChanged -= OnSizeChanged;
}
DetachSwipeControlEvent(platformView);
}

/// <inheritdoc/>
Expand All @@ -141,6 +142,7 @@ protected override void ConnectHandler(Popup platformView)
{
MauiContext.GetPlatformWindow().SizeChanged += OnSizeChanged;
}
AttachSwipeControlEvent(platformView);
base.ConnectHandler(platformView);
}

Expand Down Expand Up @@ -169,4 +171,60 @@ void OnSizeChanged(object? sender, WindowSizeChangedEventArgs e)
PopupExtensions.SetLayout(PlatformView, VirtualView, MauiContext);
}
}

void AttachSwipeControlEvent(Popup platformView)
{
if (platformView.Child is FrameworkElement frameworkElement)
{
if (frameworkElement is SwipeControl)
{
SwipeControl swipeControl = (SwipeControl)frameworkElement;
swipeControl.Loaded += OnLoadedSwipeControl;
}
else
{
var swipeControls = frameworkElement.GetChildren<SwipeControl>();
foreach (SwipeControl? swipeControl in swipeControls)
{
if (swipeControl is not null)
{
swipeControl.Loaded += OnLoadedSwipeControl;
}
}
}
}
}

void DetachSwipeControlEvent(Popup platformView)
{
if (platformView.Child is FrameworkElement frameworkElement)
{
if (frameworkElement is SwipeControl)
{
SwipeControl swipeControl = (SwipeControl)frameworkElement;
swipeControl.Loaded -= OnLoadedSwipeControl;
}
else
{
var swipeControls = frameworkElement.GetChildren<SwipeControl>();
foreach (SwipeControl? swipeControl in swipeControls)
{
if (swipeControl is not null)
{
swipeControl.Loaded -= OnLoadedSwipeControl;
}
}
}
}
}

void OnLoadedSwipeControl(object? sender, RoutedEventArgs e)
{
if (VirtualView is not null)
{
PopupExtensions.SetSize(PlatformView, VirtualView, MauiContext);
PopupExtensions.SetLayout(PlatformView, VirtualView, MauiContext);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Maui.Platform;
using Microsoft.Maui.Primitives;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;

namespace CommunityToolkit.Maui.Core.Views;
Expand Down Expand Up @@ -58,6 +59,33 @@ public static void ConfigureControl(this Popup mauiPopup, IPopup popup, IMauiCon
mauiPopup.SetLayout(popup, mauiContext);
}

static bool CanMeasureContent(Popup mauiPopup)
{
if (mauiPopup.Child is FrameworkElement frameworkElement)
{
if (frameworkElement is SwipeControl)
{
SwipeControl swipeControl = (SwipeControl)frameworkElement;
if (!swipeControl.IsLoaded)
{
return false;
}
}
else
{
var swipeControls = frameworkElement.GetChildren<SwipeControl>();
foreach (SwipeControl? swipeControl in swipeControls)
{
if (swipeControl is not null && !swipeControl.IsLoaded)
{
return false;
}
}
}
}
return true;
}

/// <summary>
/// Method to update the popup size based on the <see cref="Maui.Core.IPopup.Size"/>.
/// </summary>
Expand All @@ -79,7 +107,14 @@ public static void SetSize(this Popup mauiPopup, IPopup popup, IMauiContext? mau
{
if (double.IsNaN(popup.Content.Width) || (double.IsNaN(popup.Content.Height)))
{
currentSize = popup.Content.Measure(double.IsNaN(popup.Content.Width) ? popupParent.Bounds.Width : popup.Content.Width, double.IsNaN(popup.Content.Height) ? popupParent.Bounds.Height : popup.Content.Height);
if (CanMeasureContent(mauiPopup))
{
currentSize = popup.Content.Measure(double.IsNaN(popup.Content.Width) ? popupParent.Bounds.Width : popup.Content.Width, double.IsNaN(popup.Content.Height) ? popupParent.Bounds.Height : popup.Content.Height);
}
else
{
return;
}

if (double.IsNaN(popup.Content.Width))
{
Expand Down Expand Up @@ -129,7 +164,16 @@ public static void SetLayout(this Popup mauiPopup, IPopup popup, IMauiContext? m
ArgumentNullException.ThrowIfNull(popup.Content);

var popupParent = mauiContext.GetPlatformWindow();
popup.Content.Measure(double.PositiveInfinity, double.PositiveInfinity);

if (CanMeasureContent(mauiPopup))
{
popup.Content.Measure(double.PositiveInfinity, double.PositiveInfinity);
}
else
{
return;
}

var contentSize = popup.Content.ToPlatform(mauiContext).DesiredSize;
var popupParentFrame = popupParent.Bounds;

Expand Down