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

Don't invalidate SuperView if not connected to Window #24637

Merged
merged 2 commits into from
Sep 9, 2024
Merged
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 @@ -204,10 +204,31 @@ protected override void Dispose(bool disposing)
}
}

bool _pendingSuperViewSetNeedsLayout;

public override void SetNeedsLayout()
{
base.SetNeedsLayout();
this.Superview?.SetNeedsLayout();

if (Window is not null)
{
_pendingSuperViewSetNeedsLayout = false;
this.Superview?.SetNeedsLayout();
}
else{
_pendingSuperViewSetNeedsLayout = true;
}
}

public override void MovedToWindow()
{
base.MovedToWindow();
if (_pendingSuperViewSetNeedsLayout)
{
this.Superview?.SetNeedsLayout();
}

_pendingSuperViewSetNeedsLayout = false;
}

[Microsoft.Maui.Controls.Internals.Preserve(Conditional = true)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
override Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.MovedToWindow() -> void
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
override Microsoft.Maui.Controls.Handlers.Compatibility.FrameRenderer.MovedToWindow() -> void
16 changes: 8 additions & 8 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue24434.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ public TestPage()
AutomationId = "ClickMe",
Command = new Command(async () =>
{
TaskCompletionSource taskCompletionSource = new TaskCompletionSource();
vsl.Unloaded += (_, _) =>
{
vsl.Add(new Label { Text = "Hello, World!", AutomationId = "Success" });
taskCompletionSource.TrySetResult();
};
var secondPage = new ContentPage() { Content = new Label() { Text = "I should just disappear" } };

await Navigation.PushAsync(new ContentPage() { Content = new Label() { Text = "I should just disappear" } });
await Navigation.PushAsync(secondPage);
await Navigation.PushModalAsync(new ContentPage() { Content = new Label() { Text = "I should just disappear" } });

await taskCompletionSource.Task.WaitAsync(TimeSpan.FromSeconds(5));
// Ensure that the VSL is unloaded otherwise the test isn't really valid
mattleibow marked this conversation as resolved.
Show resolved Hide resolved
if (!vsl.IsLoaded)
{
vsl.Add(new Label { Text = "Hello, World!", AutomationId = "Success" });
}

await Navigation.PopModalAsync();
await Navigation.PopAsync();
})
Expand Down
4 changes: 2 additions & 2 deletions src/Core/src/Platform/iOS/LayoutView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public override void SubviewAdded(UIView uiview)
{
InvalidateConstraintsCache();
base.SubviewAdded(uiview);
this.Superview?.SetNeedsLayout();
TryToInvalidateSuperView(false);
}

public override void WillRemoveSubview(UIView uiview)
{
InvalidateConstraintsCache();
base.WillRemoveSubview(uiview);
this.Superview?.SetNeedsLayout();
TryToInvalidateSuperView(false);
}

public override UIView? HitTest(CGPoint point, UIEvent? uievent)
Expand Down
24 changes: 23 additions & 1 deletion src/Core/src/Platform/iOS/MauiView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.Maui.Platform
{
public abstract class MauiView : UIView, ICrossPlatformLayoutBacking, IVisualTreeElementProvidable, IUIViewLifeCycleEvents
{
bool _fireSetNeedsLayoutOnParentWhenWindowAttached;
static bool? _respondsToSafeArea;

double _lastMeasureHeight = double.NaN;
Expand Down Expand Up @@ -140,7 +141,27 @@ public override void SetNeedsLayout()
{
InvalidateConstraintsCache();
base.SetNeedsLayout();
this.Superview?.SetNeedsLayout();
TryToInvalidateSuperView(false);
}

private protected void TryToInvalidateSuperView(bool shouldOnlyInvalidateIfPending)
{
if (shouldOnlyInvalidateIfPending && !_fireSetNeedsLayoutOnParentWhenWindowAttached)
{
return;
}

// We check for Window to avoid scenarios where an invalidate might propagate up the tree
// To a SuperView that's been disposed which will cause a crash when trying to access it
if (Window is not null)
{
this.Superview?.SetNeedsLayout();
_fireSetNeedsLayoutOnParentWhenWindowAttached = false;
}
else
{
_fireSetNeedsLayoutOnParentWhenWindowAttached = true;
}
}

IVisualTreeElement? IVisualTreeElementProvidable.GetElement()
Expand Down Expand Up @@ -173,6 +194,7 @@ public override void MovedToWindow()
{
base.MovedToWindow();
_movedToWindow?.Invoke(this, EventArgs.Empty);
TryToInvalidateSuperView(true);
}
}
}
7 changes: 6 additions & 1 deletion src/Core/src/Platform/iOS/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,12 @@ public static void UpdateBackgroundLayerFrame(this UIView view)
public static void InvalidateMeasure(this UIView platformView, IView view)
{
platformView.SetNeedsLayout();
platformView.Superview?.SetNeedsLayout();

// MauiView/WrapperView already propagates the SetNeedsLayout to the parent
if (platformView is not MauiView && platformView is not WrapperView)
{
platformView.Superview?.SetNeedsLayout();
}
}

public static void UpdateWidth(this UIView platformView, IView view)
Expand Down
23 changes: 22 additions & 1 deletion src/Core/src/Platform/iOS/WrapperView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Microsoft.Maui.Platform
{
public partial class WrapperView : UIView, IDisposable, IUIViewLifeCycleEvents
{
bool _fireSetNeedsLayoutOnParentWhenWindowAttached;
WeakReference<ICrossPlatformLayout>? _crossPlatformLayoutReference;

internal ICrossPlatformLayout? CrossPlatformLayout
Expand Down Expand Up @@ -239,8 +240,27 @@ internal CGSize SizeThatFitsWrapper(CGSize originalSpec, double virtualViewWidth
public override void SetNeedsLayout()
{
base.SetNeedsLayout();
TryToInvalidateSuperView(false);
}

private protected void TryToInvalidateSuperView(bool onlyIfPending)
{
if (onlyIfPending && !_fireSetNeedsLayoutOnParentWhenWindowAttached)
{
return;
}

this.Superview?.SetNeedsLayout();
// We check for Window to avoid scenarios where an invalidate might propagate up the tree
// To a SuperView that's been disposed which will cause a crash when trying to access it
if (Window is not null)
{
_fireSetNeedsLayoutOnParentWhenWindowAttached = false;
this.Superview?.SetNeedsLayout();
}
else
{
_fireSetNeedsLayoutOnParentWhenWindowAttached = true;
}
}

partial void ClipChanged()
Expand Down Expand Up @@ -360,6 +380,7 @@ public override void MovedToWindow()
{
base.MovedToWindow();
_movedToWindow?.Invoke(this, EventArgs.Empty);
TryToInvalidateSuperView(true);
}
}
}
Loading