feat: replace mask with pure rectangle to reduce layout calculation

This commit is contained in:
rabbitism
2024-02-05 13:54:58 +08:00
parent 2ac5dfa170
commit 35b3a2f659
6 changed files with 99 additions and 18 deletions

View File

@@ -0,0 +1,35 @@
using Avalonia;
using Avalonia.Controls.Shapes;
using Avalonia.Media;
namespace Ursa.Controls.Shapes;
/// <summary>
/// A rectangle, with no corner radius.
/// </summary>
public class PureRectangle: Shape
{
static PureRectangle()
{
FocusableProperty.OverrideDefaultValue<PureRectangle>(false);
AffectsGeometry<PureRectangle>(BoundsProperty);
}
protected override Geometry? CreateDefiningGeometry()
{
StreamGeometry geometry = new StreamGeometry();
Rect rect = new Rect(this.Bounds.Size).Deflate(this.StrokeThickness / 2.0);
using StreamGeometryContext context = geometry.Open();
context.BeginFigure(new Point(rect.Left, rect.Top), true);
context.LineTo(new Point(rect.Right, rect.Top));
context.LineTo(new Point(rect.Right, rect.Bottom));
context.LineTo(new Point(rect.Left, rect.Bottom));
context.LineTo(new Point(rect.Left, rect.Top));
context.EndFigure(true);
return geometry;
}
protected override Size MeasureOverride(Size availableSize)
{
return new Size(this.StrokeThickness, this.StrokeThickness);
}
}