Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid having every access of Bounds or Frame allocate a new Rect #7049

Merged
merged 6 commits into from May 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -1021,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()
Expand Down Expand Up @@ -1199,16 +1198,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();
PureWeen marked this conversation as resolved.
Show resolved Hide resolved

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