Skip to content

Commit

Permalink
Revert PR #23052 behavior under new app context switch (#25613)
Browse files Browse the repository at this point in the history
* Revert PR #23052 behavior under new Microsoft.Maui.UseLegacyMeasureInvalidatedBehavior app context switch

* Align the switch name with .NET 9 switches

---------

Co-authored-by: Filip Navara <[email protected]>
  • Loading branch information
PureWeen and filipnavara authored Oct 31, 2024
1 parent 8d39193 commit a826952
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
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

0 comments on commit a826952

Please sign in to comment.