Skip to content

Commit

Permalink
Avoid having every access of Bounds or Frame allocate a new Rect (#7049)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hartez committed May 12, 2022
1 parent a5ac726 commit 34e224d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/BlazorWebView/tests/MauiDeviceTests/HandlerTestBase.cs
Expand Up @@ -59,7 +59,8 @@ protected THandler CreateHandler<THandler>(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;
Expand Down
Expand Up @@ -15,26 +15,18 @@ public partial class VisualElement : IView
EventHandler? _unloaded;
bool _watchingPlatformLoaded;

Rect _frame = new Rect(0, 0, -1, -1);

/// <include file="../../../../docs/Microsoft.Maui.Controls/VisualElement.xml" path="//Member[@MemberName='Frame']/Docs" />
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);
}
}

Expand Down
32 changes: 18 additions & 14 deletions src/Controls/src/Core/VisualElement.cs
Expand Up @@ -441,16 +441,10 @@ public IList<Behavior> Behaviors
/// <include file="../../docs/Microsoft.Maui.Controls/VisualElement.xml" path="//Member[@MemberName='Bounds']/Docs" />
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;
}
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/Controls/tests/DeviceTests/HandlerTestBase.cs
Expand Up @@ -134,6 +134,10 @@ protected THandler CreateHandler<THandler>(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
Expand Down

0 comments on commit 34e224d

Please sign in to comment.