From 468adaafc3616183fecebb76302746e8056e2c6b Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Wed, 11 May 2022 14:00:33 -0600 Subject: [PATCH 1/6] Avoid having every access of Bounds or Frame allocate a new Rect --- .../VisualElement/VisualElement.Impl.cs | 18 +++++-------- src/Controls/src/Core/VisualElement.cs | 25 ++++++++----------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/src/Controls/src/Core/HandlerImpl/VisualElement/VisualElement.Impl.cs b/src/Controls/src/Core/HandlerImpl/VisualElement/VisualElement.Impl.cs index cbbff446de7d..2cba1fcd36f3 100644 --- a/src/Controls/src/Core/HandlerImpl/VisualElement/VisualElement.Impl.cs +++ b/src/Controls/src/Core/HandlerImpl/VisualElement/VisualElement.Impl.cs @@ -15,26 +15,20 @@ public partial class VisualElement : IView EventHandler? _unloaded; bool _watchingPlatformLoaded; + Rect _frame = Rect.Zero; + /// 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); + _frame = value; - BatchCommit(); + UpdateBoundsComponents(value); } } diff --git a/src/Controls/src/Core/VisualElement.cs b/src/Controls/src/Core/VisualElement.cs index be7cf7003fdf..83350adb8e19 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 _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; } } @@ -1199,16 +1193,19 @@ 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; + 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 From dd86f3c86658b2c9910a0ff75d26a4abc8f1569c Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Wed, 11 May 2022 16:33:50 -0600 Subject: [PATCH 2/6] Make [sigh] MockBounds work --- .../Core/HandlerImpl/VisualElement/VisualElement.Impl.cs | 4 +--- src/Controls/src/Core/VisualElement.cs | 9 ++++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Controls/src/Core/HandlerImpl/VisualElement/VisualElement.Impl.cs b/src/Controls/src/Core/HandlerImpl/VisualElement/VisualElement.Impl.cs index 2cba1fcd36f3..1db38959c549 100644 --- a/src/Controls/src/Core/HandlerImpl/VisualElement/VisualElement.Impl.cs +++ b/src/Controls/src/Core/HandlerImpl/VisualElement/VisualElement.Impl.cs @@ -15,7 +15,7 @@ public partial class VisualElement : IView EventHandler? _unloaded; bool _watchingPlatformLoaded; - Rect _frame = Rect.Zero; + Rect _frame = new Rect(0, 0, -1, -1); /// public Rect Frame @@ -26,8 +26,6 @@ public Rect Frame if (_frame == value) return; - _frame = value; - UpdateBoundsComponents(value); } } diff --git a/src/Controls/src/Core/VisualElement.cs b/src/Controls/src/Core/VisualElement.cs index 83350adb8e19..5c80ac0ee367 100644 --- a/src/Controls/src/Core/VisualElement.cs +++ b/src/Controls/src/Core/VisualElement.cs @@ -441,7 +441,7 @@ public IList Behaviors /// public Rect Bounds { - get { return _frame; } + get { return IsMocked() ? new Rect(_mockX, _mockY, _mockWidth, _mockHeight) : _frame; } private set { Frame = value; @@ -1015,6 +1015,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() @@ -1195,6 +1200,8 @@ void IPropertyPropagationController.PropagatePropertyChanged(string propertyName void UpdateBoundsComponents(Rect bounds) { + _frame = bounds; + BatchBegin(); X = bounds.X; From bd71e14ed229da3faea5197493af5d0cd96d01ab Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Wed, 11 May 2022 16:35:32 -0600 Subject: [PATCH 3/6] Fix IsMocked --- src/Controls/src/Core/VisualElement.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controls/src/Core/VisualElement.cs b/src/Controls/src/Core/VisualElement.cs index 5c80ac0ee367..058384e7f18f 100644 --- a/src/Controls/src/Core/VisualElement.cs +++ b/src/Controls/src/Core/VisualElement.cs @@ -1017,7 +1017,7 @@ internal void MockBounds(Rect bounds) bool IsMocked() { - return _mockX != -1 && _mockY != -1 && _mockWidth != -1 && _mockHeight != -1; + return _mockX != -1 || _mockY != -1 || _mockWidth != -1 || _mockHeight != -1; } internal virtual void OnConstraintChanged(LayoutConstraint oldConstraint, LayoutConstraint newConstraint) => ComputeConstrainsForChildren(); From bf2e827819144c8541eef07b1de09ce4f885b03c Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Wed, 11 May 2022 19:36:50 -0600 Subject: [PATCH 4/6] Fix iOS tests --- src/Controls/tests/DeviceTests/HandlerTestBase.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Controls/tests/DeviceTests/HandlerTestBase.cs b/src/Controls/tests/DeviceTests/HandlerTestBase.cs index f5f965598b89..53ffc05ab4bc 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 From 911c603117d3b0fc6e0a38f982af3f4c2927e490 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Wed, 11 May 2022 20:20:28 -0600 Subject: [PATCH 5/6] Fix Blazor tests? --- src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs b/src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs index c24759505a4b..ce849bb51234 100644 --- a/src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs +++ b/src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs @@ -59,7 +59,11 @@ protected THandler CreateHandler(IView view) handler.SetVirtualView(view); view.Handler = handler; +#if IOS + view.Arrange(new Rect(0, 0, double.PositiveInfinity, double.PositiveInfinity)); +#else view.Arrange(new Rect(0, 0, view.Width, view.Height)); +#endif handler.PlatformArrange(view.Frame); return handler; From 7e26d91cec3ee5162d7d4925f58d2a110c3a0c71 Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Wed, 11 May 2022 23:18:00 -0600 Subject: [PATCH 6/6] No, for real this time --- src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs b/src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs index ce849bb51234..58b6a872b951 100644 --- a/src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs +++ b/src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs @@ -59,11 +59,8 @@ protected THandler CreateHandler(IView view) handler.SetVirtualView(view); view.Handler = handler; -#if IOS - view.Arrange(new Rect(0, 0, double.PositiveInfinity, double.PositiveInfinity)); -#else - view.Arrange(new Rect(0, 0, view.Width, view.Height)); -#endif + var size = view.Measure(double.PositiveInfinity, double.PositiveInfinity); + view.Arrange(new Rect(0, 0, size.Width, size.Height)); handler.PlatformArrange(view.Frame); return handler;