Browse Source

Merge branch 'master' into fixes/setterbindinginstance-leak

pull/3958/head
Dariusz Komosiński 6 years ago
committed by GitHub
parent
commit
6dc6f4f63a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/Avalonia.Visuals/Media/DrawingContext.cs
  2. 49
      src/Avalonia.Visuals/Rect.cs
  3. 3
      src/Avalonia.Visuals/Rendering/SceneGraph/DrawOperation.cs
  4. 19
      tests/Avalonia.Visuals.UnitTests/RectTests.cs

2
src/Avalonia.Visuals/Media/DrawingContext.cs

@ -350,7 +350,7 @@ namespace Avalonia.Media
/// </summary>
/// <param name="matrix">The matrix</param>
/// <returns>A disposable used to undo the transformation.</returns>
PushedState PushSetTransform(Matrix matrix)
public PushedState PushSetTransform(Matrix matrix)
{
var oldMatrix = CurrentTransform;
CurrentTransform = matrix;

49
src/Avalonia.Visuals/Rect.cs

@ -421,11 +421,50 @@ namespace Avalonia
}
/// <summary>
/// Gets the union of two rectangles.
/// </summary>
/// <param name="rect">The other rectangle.</param>
/// <returns>The union.</returns>
public Rect Union(Rect rect)
/// Normalizes the rectangle so both the <see cref="Width"/> and <see
/// cref="Height"/> are positive, without changing the location of the rectangle
/// </summary>
/// <returns>Normalized Rect</returns>
/// <remarks>
/// Empty rect will be return when Rect contains invalid values. Like NaN.
/// </remarks>
public Rect Normalize()
{
Rect rect = this;
if(double.IsNaN(rect.Right) || double.IsNaN(rect.Bottom) ||
double.IsNaN(rect.X) || double.IsNaN(rect.Y) ||
double.IsNaN(Height) || double.IsNaN(Width))
{
return Rect.Empty;
}
if (rect.Width < 0)
{
var x = X + Width;
var width = X - x;
rect = rect.WithX(x).WithWidth(width);
}
if (rect.Height < 0)
{
var y = Y + Height;
var height = Y - y;
rect = rect.WithY(y).WithHeight(height);
}
return rect;
}
/// <summary>
/// Gets the union of two rectangles.
/// </summary>
/// <param name="rect">The other rectangle.</param>
/// <returns>The union.</returns>
public Rect Union(Rect rect)
{
if (IsEmpty)
{

3
src/Avalonia.Visuals/Rendering/SceneGraph/DrawOperation.cs

@ -11,7 +11,8 @@ namespace Avalonia.Rendering.SceneGraph
{
public DrawOperation(Rect bounds, Matrix transform)
{
bounds = bounds.TransformToAABB(transform);
bounds = bounds.Normalize().TransformToAABB(transform);
Bounds = new Rect(
new Point(Math.Floor(bounds.X), Math.Floor(bounds.Y)),
new Point(Math.Ceiling(bounds.Right), Math.Ceiling(bounds.Bottom)));

19
tests/Avalonia.Visuals.UnitTests/RectTests.cs

@ -35,5 +35,24 @@ namespace Avalonia.Visuals.UnitTests
Assert.Equal(new Rect(0, 0, 100, 100), result);
}
[Fact]
public void Normalize_Should_Reverse_Negative_Size()
{
var result = new Rect(new Point(100, 100), new Point(0, 0)).Normalize();
Assert.Equal(new Rect(0, 0, 100, 100), result);
}
[Fact]
public void Normalize_Should_Make_Invalid_Rects_Empty()
{
var result = new Rect(
double.NegativeInfinity, double.PositiveInfinity,
double.PositiveInfinity, double.PositiveInfinity)
.Normalize();
Assert.Equal(Rect.Empty, result);
}
}
}

Loading…
Cancel
Save