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

Revert PR #23052 behavior under new app context switch #25613

Merged
merged 2 commits into from
Oct 31, 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
10 changes: 10 additions & 0 deletions src/Controls/src/Core/LegacyLayouts/Layout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,11 @@ void OnInternalAdded(View view)
{
InvalidateLayout();
}

if (UseLegacyMeasureInvalidatedBehaviorEnabled)
{
view.MeasureInvalidated += OnChildMeasureInvalidated;
}
}

void OnInternalRemoved(View view, int oldIndex)
Expand All @@ -618,6 +623,11 @@ void OnInternalRemoved(View view, int oldIndex)
{
InvalidateLayout();
}

if (UseLegacyMeasureInvalidatedBehaviorEnabled)
{
view.MeasureInvalidated -= OnChildMeasureInvalidated;
}
}

bool ShouldLayoutChildren()
Expand Down
20 changes: 20 additions & 0 deletions src/Controls/src/Core/Page/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public partial class Page : VisualElement, ILayout, IPageController, IElementCon

readonly Lazy<PlatformConfigurationRegistry<Page>> _platformConfigurationRegistry;

bool _allocatedFlag;
Rect _containerArea;

bool _containerAreaSet;
Expand Down Expand Up @@ -542,6 +543,7 @@ protected override void OnParentSet()
/// <param name="height">The height allocated to the page.</param>
protected override void OnSizeAllocated(double width, double height)
{
_allocatedFlag = true;
base.OnSizeAllocated(width, height);
UpdateChildrenLayout();
}
Expand Down Expand Up @@ -603,7 +605,12 @@ internal virtual void OnChildMeasureInvalidated(VisualElement child, Invalidatio
}
}

_allocatedFlag = false;
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
if (!_allocatedFlag && Width >= 0 && Height >= 0 && UseLegacyMeasureInvalidatedBehaviorEnabled)
{
SizeAllocated(Width, Height);
}
}

internal void OnAppearing(Action action)
Expand Down Expand Up @@ -705,6 +712,13 @@ void InternalChildrenOnCollectionChanged(object sender, NotifyCollectionChangedE
for (var i = 0; i < e.OldItems.Count; i++)
{
var item = (Element)e.OldItems[i];

if (UseLegacyMeasureInvalidatedBehaviorEnabled &&
item is VisualElement visual)
{
visual.MeasureInvalidated -= OnChildMeasureInvalidated;
}

RemoveLogicalChild(item);
}
}
Expand All @@ -721,6 +735,12 @@ void InternalChildrenOnCollectionChanged(object sender, NotifyCollectionChangedE
insertIndex = InternalChildren.IndexOf(item);
}

if (UseLegacyMeasureInvalidatedBehaviorEnabled &&
item is VisualElement visual)
{
visual.MeasureInvalidated += OnChildMeasureInvalidated;
}

InsertLogicalChild(insertIndex, item);

if (item is VisualElement)
Expand Down
12 changes: 11 additions & 1 deletion src/Controls/src/Core/VisualElement/VisualElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,13 @@ internal void InvokeFocusChangeRequested(FocusRequestArgs args) =>
FocusChangeRequested?.Invoke(this, args);
internal bool HasFocusChangeRequestedEvent => FocusChangeRequested is not null;

// Reverts the behavior changed in https://github.com/dotnet/maui/pull/23052/. This unblock customers that
// rely on the previous behavior or that are affected by the event propagation being slow on deep control
// hierarchies. This can be removed once a better fix is in place, such as https://github.com/dotnet/maui/pull/24848
// or https://github.com/dotnet/maui/pull/25291.
internal static bool UseLegacyMeasureInvalidatedBehaviorEnabled { get; } =
AppContext.TryGetSwitch("Microsoft.Maui.RuntimeFeature.UseLegacyMeasureInvalidatedBehavior", out var enabled) && enabled;

/// <summary>
/// Invalidates the measure of an element.
/// </summary>
Expand Down Expand Up @@ -1375,7 +1382,10 @@ internal virtual void InvalidateMeasureInternal(InvalidationTrigger trigger)
}

MeasureInvalidated?.Invoke(this, new InvalidationEventArgs(trigger));
(Parent as VisualElement)?.OnChildMeasureInvalidatedInternal(this, trigger);
if (!UseLegacyMeasureInvalidatedBehaviorEnabled)
{
(Parent as VisualElement)?.OnChildMeasureInvalidatedInternal(this, trigger);
}
}

internal virtual void OnChildMeasureInvalidatedInternal(VisualElement child, InvalidationTrigger trigger)
Expand Down