Skip to content

Commit 50b6e11

Browse files
[release/8.0.1xx-sr10] Revert PR #23052 behavior under new app context 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 <filip.navara@gmail.com>
1 parent 3658369 commit 50b6e11

3 files changed

Lines changed: 41 additions & 1 deletion

File tree

src/Controls/src/Core/LegacyLayouts/Layout.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,11 @@ void OnInternalAdded(View view)
609609
{
610610
InvalidateLayout();
611611
}
612+
613+
if (UseLegacyMeasureInvalidatedBehaviorEnabled)
614+
{
615+
view.MeasureInvalidated += OnChildMeasureInvalidated;
616+
}
612617
}
613618

614619
void OnInternalRemoved(View view, int oldIndex)
@@ -618,6 +623,11 @@ void OnInternalRemoved(View view, int oldIndex)
618623
{
619624
InvalidateLayout();
620625
}
626+
627+
if (UseLegacyMeasureInvalidatedBehaviorEnabled)
628+
{
629+
view.MeasureInvalidated -= OnChildMeasureInvalidated;
630+
}
621631
}
622632

623633
bool ShouldLayoutChildren()

src/Controls/src/Core/Page/Page.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public partial class Page : VisualElement, ILayout, IPageController, IElementCon
6666

6767
readonly Lazy<PlatformConfigurationRegistry<Page>> _platformConfigurationRegistry;
6868

69+
bool _allocatedFlag;
6970
Rect _containerArea;
7071

7172
bool _containerAreaSet;
@@ -542,6 +543,7 @@ protected override void OnParentSet()
542543
/// <param name="height">The height allocated to the page.</param>
543544
protected override void OnSizeAllocated(double width, double height)
544545
{
546+
_allocatedFlag = true;
545547
base.OnSizeAllocated(width, height);
546548
UpdateChildrenLayout();
547549
}
@@ -603,7 +605,12 @@ internal virtual void OnChildMeasureInvalidated(VisualElement child, Invalidatio
603605
}
604606
}
605607

608+
_allocatedFlag = false;
606609
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
610+
if (!_allocatedFlag && Width >= 0 && Height >= 0 && UseLegacyMeasureInvalidatedBehaviorEnabled)
611+
{
612+
SizeAllocated(Width, Height);
613+
}
607614
}
608615

609616
internal void OnAppearing(Action action)
@@ -705,6 +712,13 @@ void InternalChildrenOnCollectionChanged(object sender, NotifyCollectionChangedE
705712
for (var i = 0; i < e.OldItems.Count; i++)
706713
{
707714
var item = (Element)e.OldItems[i];
715+
716+
if (UseLegacyMeasureInvalidatedBehaviorEnabled &&
717+
item is VisualElement visual)
718+
{
719+
visual.MeasureInvalidated -= OnChildMeasureInvalidated;
720+
}
721+
708722
RemoveLogicalChild(item);
709723
}
710724
}
@@ -721,6 +735,12 @@ void InternalChildrenOnCollectionChanged(object sender, NotifyCollectionChangedE
721735
insertIndex = InternalChildren.IndexOf(item);
722736
}
723737

738+
if (UseLegacyMeasureInvalidatedBehaviorEnabled &&
739+
item is VisualElement visual)
740+
{
741+
visual.MeasureInvalidated += OnChildMeasureInvalidated;
742+
}
743+
724744
InsertLogicalChild(insertIndex, item);
725745

726746
if (item is VisualElement)

src/Controls/src/Core/VisualElement/VisualElement.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,13 @@ internal void InvokeFocusChangeRequested(FocusRequestArgs args) =>
13451345
FocusChangeRequested?.Invoke(this, args);
13461346
internal bool HasFocusChangeRequestedEvent => FocusChangeRequested is not null;
13471347

1348+
// Reverts the behavior changed in https://github.com/dotnet/maui/pull/23052/. This unblock customers that
1349+
// rely on the previous behavior or that are affected by the event propagation being slow on deep control
1350+
// hierarchies. This can be removed once a better fix is in place, such as https://github.com/dotnet/maui/pull/24848
1351+
// or https://github.com/dotnet/maui/pull/25291.
1352+
internal static bool UseLegacyMeasureInvalidatedBehaviorEnabled { get; } =
1353+
AppContext.TryGetSwitch("Microsoft.Maui.RuntimeFeature.UseLegacyMeasureInvalidatedBehavior", out var enabled) && enabled;
1354+
13481355
/// <summary>
13491356
/// Invalidates the measure of an element.
13501357
/// </summary>
@@ -1375,7 +1382,10 @@ internal virtual void InvalidateMeasureInternal(InvalidationTrigger trigger)
13751382
}
13761383

13771384
MeasureInvalidated?.Invoke(this, new InvalidationEventArgs(trigger));
1378-
(Parent as VisualElement)?.OnChildMeasureInvalidatedInternal(this, trigger);
1385+
if (!UseLegacyMeasureInvalidatedBehaviorEnabled)
1386+
{
1387+
(Parent as VisualElement)?.OnChildMeasureInvalidatedInternal(this, trigger);
1388+
}
13791389
}
13801390

13811391
internal virtual void OnChildMeasureInvalidatedInternal(VisualElement child, InvalidationTrigger trigger)

0 commit comments

Comments
 (0)