From 34e224df3613779ffb67ab8989a8ef2c697d6573 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Thu, 12 May 2022 04:07:09 -0600 Subject: [PATCH] Avoid having every access of Bounds or Frame allocate a new Rect (#7049) * Avoid having every access of Bounds or Frame allocate a new Rect * Make [sigh] MockBounds work * Fix IsMocked * Fix iOS tests * Fix Blazor tests? * No, for real this time --- .../tests/MauiDeviceTests/HandlerTestBase.cs | 3 +- .../VisualElement/VisualElement.Impl.cs | 18 +++-------- src/Controls/src/Core/VisualElement.cs | 32 +++++++++++-------- .../tests/DeviceTests/HandlerTestBase.cs | 4 +++ 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs b/src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs index c24759505a4b..58b6a872b951 100644 --- a/src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs +++ b/src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs @@ -59,7 +59,8 @@ protected THandler CreateHandler(IView view) handler.SetVirtualView(view); view.Handler = handler; - view.Arrange(new Rect(0, 0, view.Width, view.Height)); + var size = view.Measure(double.PositiveInfinity, double.PositiveInfinity); + view.Arrange(new Rect(0, 0, size.Width, size.Height)); handler.PlatformArrange(view.Frame); return handler; diff --git a/src/Controls/src/Core/HandlerImpl/VisualElement/VisualElement.Impl.cs b/src/Controls/src/Core/HandlerImpl/VisualElement/VisualElement.Impl.cs index cbbff446de7d..1db38959c549 100644 --- a/src/Controls/src/Core/HandlerImpl/VisualElement/VisualElement.Impl.cs +++ b/src/Controls/src/Core/HandlerImpl/VisualElement/VisualElement.Impl.cs @@ -15,26 +15,18 @@ public partial class VisualElement : IView EventHandler? _unloaded; bool _watchingPlatformLoaded; + Rect _frame = new Rect(0, 0, -1, -1); + /// public Rect Frame { - get => Bounds; + get => _frame; set { - if (value.X == X && value.Y == Y && value.Height == Height && value.Width == Width) + if (_frame == value) return; - BatchBegin(); - - X = value.X; - Y = value.Y; - Width = value.Width; - Height = value.Height; - - SizeAllocated(Width, Height); - SizeChanged?.Invoke(this, EventArgs.Empty); - - BatchCommit(); + UpdateBoundsComponents(value); } } diff --git a/src/Controls/src/Core/VisualElement.cs b/src/Controls/src/Core/VisualElement.cs index 9cb03e49e8ed..5d4fffc03ed8 100644 --- a/src/Controls/src/Core/VisualElement.cs +++ b/src/Controls/src/Core/VisualElement.cs @@ -441,16 +441,10 @@ public IList Behaviors /// public Rect Bounds { - get { return new Rect(X, Y, Width, Height); } + get { return IsMocked() ? new Rect(_mockX, _mockY, _mockWidth, _mockHeight) : _frame; } private set { - if (value.X == X && value.Y == Y && value.Height == Height && value.Width == Width) - return; - BatchBegin(); - X = value.X; - Y = value.Y; - SetSize(value.Width, value.Height); - BatchCommit(); + Frame = value; } } @@ -1020,6 +1014,11 @@ internal void MockBounds(Rect bounds) #endif } + bool IsMocked() + { + return _mockX != -1 || _mockY != -1 || _mockWidth != -1 || _mockHeight != -1; + } + internal virtual void OnConstraintChanged(LayoutConstraint oldConstraint, LayoutConstraint newConstraint) => ComputeConstrainsForChildren(); internal virtual void OnIsPlatformEnabledChanged() @@ -1198,16 +1197,21 @@ void IPropertyPropagationController.PropagatePropertyChanged(string propertyName PropertyPropagationExtensions.PropagatePropertyChanged(propertyName, this, ((IVisualTreeElement)this).GetVisualChildren()); } - void SetSize(double width, double height) + void UpdateBoundsComponents(Rect bounds) { - if (Width == width && Height == height) - return; + _frame = bounds; + + BatchBegin(); - Width = width; - Height = height; + X = bounds.X; + Y = bounds.Y; + Width = bounds.Width; + Height = bounds.Height; - SizeAllocated(width, height); + SizeAllocated(Width, Height); SizeChanged?.Invoke(this, EventArgs.Empty); + + BatchCommit(); } public class FocusRequestArgs : EventArgs diff --git a/src/Controls/tests/DeviceTests/HandlerTestBase.cs b/src/Controls/tests/DeviceTests/HandlerTestBase.cs index 6bd5f84c9c6b..47ee5d0f86c2 100644 --- a/src/Controls/tests/DeviceTests/HandlerTestBase.cs +++ b/src/Controls/tests/DeviceTests/HandlerTestBase.cs @@ -134,6 +134,10 @@ protected THandler CreateHandler(IElement element, IMauiContext mauiCo var size = view.Measure(view.Width, view.Height); var w = size.Width; var h = size.Height; +#elif IOS + var size = view.Measure(double.PositiveInfinity, double.PositiveInfinity); + var w = size.Width; + var h = size.Height; #else // Windows cannot measure without the view being loaded // iOS needs more love when I get an IDE again