Skip to content

Commit

Permalink
[release/8.0.1xx-sr10] Revert PR #23052 behavior under new app contex…
Browse files Browse the repository at this point in the history
…t switch (#25677)

* 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
github-actions[bot] and filipnavara authored Nov 5, 2024
1 parent 3658369 commit 50b6e11
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 50b6e11

Please sign in to comment.