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

New SizeExtensions for Windows.Foundation.Size #3489

Merged
9 commits merged into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Microsoft.Toolkit.Uwp/Extensions/PointExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using Point = Windows.Foundation.Point;
using Rect = Windows.Foundation.Rect;
using Size = Windows.Foundation.Size;

namespace Microsoft.Toolkit.Uwp.Extensions
{
/// <summary>
/// Extensions for the <see cref="Point"/> type.
/// </summary>
public static class PointExtensions
{
/// <summary>
/// Creates a new <see cref="Rect"/> of the specified size, starting at a given point.
/// </summary>
/// <param name="point">The input <see cref="Point"/> value to convert.</param>
/// <param name="width">The width of the rectangle.</param>
/// <param name="height">The height of the rectangle.</param>
/// <returns>A <see cref="Rect"/> value of the specified size, starting at the given point.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rect ToRect(this Point point, double width, double height)
{
return new Rect(point.X, point.Y, width, height);
}

/// <summary>
/// Creates a new <see cref="Rect"/> of the specified size, starting at the given coordinates.
/// </summary>
/// <param name="point">The input <see cref="Point"/> value to convert.</param>
/// <param name="size">The size of the rectangle to create.</param>
/// <returns>A <see cref="Rect"/> value of the specified size, starting at the given coordinates.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rect ToRect(this Point point, Size size)
{
return new Rect(point, size);
}
}
}
57 changes: 57 additions & 0 deletions Microsoft.Toolkit.Uwp/Extensions/SizeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using Point = Windows.Foundation.Point;
using Rect = Windows.Foundation.Rect;
using Size = Windows.Foundation.Size;

namespace Microsoft.Toolkit.Uwp.Extensions
{
/// <summary>
/// Extensions for the <see cref="Size"/> type.
/// </summary>
public static class SizeExtensions
{
/// <summary>
/// Creates a new <see cref="Rect"/> of the specified size, starting at the origin.
/// </summary>
/// <param name="size">The input <see cref="Size"/> value to convert.</param>
/// <returns>A <see cref="Rect"/> value of the specified size, starting at the origin.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rect ToRect(this Size size)
{
return new Rect(0, 0, size.Width, size.Height);
}

/// <summary>
/// Creates a new <see cref="Rect"/> of the specified size, starting at the given coordinates.
/// </summary>
/// <param name="size">The input <see cref="Size"/> value to convert.</param>
/// <param name="x">The horizontal offset.</param>
/// <param name="y">The vertical offset.</param>
/// <returns>A <see cref="Rect"/> value of the specified size, starting at the given coordinates.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rect ToRect(this Size size, double x, double y)
{
return new Rect(x, y, size.Width, size.Height);
}

/// <summary>
/// Creates a new <see cref="Rect"/> of the specified size, starting at the given position.
/// </summary>
/// <param name="size">The input <see cref="Size"/> value to convert.</param>
/// <param name="point">The starting position to use.</param>
/// <returns>A <see cref="Rect"/> value of the specified size, starting at the given position.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rect ToRect(this Size size, Point point)
{
return new Rect(point, size);
}
}
}
47 changes: 47 additions & 0 deletions UnitTests/UnitTests.UWP/Extensions/Test_PointExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Windows.Foundation;
using Microsoft.Toolkit.Uwp.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTests.Extensions
{
[TestClass]
public class Test_PointExtensions
{
[TestCategory("PointExtensions")]
[TestMethod]
[DataRow(0d, 0d, 0, 0)]
[DataRow(0d, 0d, 22, 6.89d)]
[DataRow(3.14d, 6.55f, 3838d, 3.24724928d)]
[DataRow(double.MinValue, double.Epsilon, 22, 0.3248d)]
public static void Test_PointExtensions_ToRect_FromWidthHeight(double width, double height, int x, int y)
{
Point p = new Point(x, y);
Rect
a = p.ToRect(width, height),
b = new Rect(x, y, width, height);

Assert.AreEqual(a, b);
}

[TestCategory("SizeExtensions")]
[TestMethod]
[DataRow(0d, 0d, 0, 0)]
[DataRow(0d, 0d, 22, 6.89d)]
[DataRow(3.14d, 6.55f, 3838d, 3.24724928d)]
[DataRow(double.MinValue, double.Epsilon, 22, 0.3248d)]
public static void Test_PointExtensions_ToRect_FromPoint(double width, double height, int x, int y)
{
Point p = new Point(x, y);
Size s = new Size(width, height);
Rect
a = p.ToRect(s),
b = new Rect(p, s);

Assert.AreEqual(a, b);
}
}
}
62 changes: 62 additions & 0 deletions UnitTests/UnitTests.UWP/Extensions/Test_SizeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Windows.Foundation;
using Microsoft.Toolkit.Uwp.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTests.Extensions
{
[TestClass]
public class Test_SizeExtensions
{
[TestCategory("SizeExtensions")]
[TestMethod]
[DataRow(0d, 0d)]
[DataRow(3.14d, 6.55f)]
[DataRow(double.MinValue, double.Epsilon)]
public static void Test_SizeExtensions_ToRect_FromSize(double width, double height)
{
Size s = new Size(width, height);
Rect
a = s.ToRect(),
b = new Rect(0, 0, s.Width, s.Height);

Assert.AreEqual(a, b);
}

[TestCategory("SizeExtensions")]
[TestMethod]
[DataRow(0d, 0d, 0, 0)]
[DataRow(0d, 0d, 22, 6.89d)]
[DataRow(3.14d, 6.55f, 3838d, 3.24724928d)]
[DataRow(double.MinValue, double.Epsilon, 22, 0.3248d)]
public static void Test_SizeExtensions_ToRect_FromSizeAndPosition(double width, double height, int x, int y)
{
Size s = new Size(width, height);
Rect
a = s.ToRect(x, y),
b = new Rect(x, y, s.Width, s.Height);

Assert.AreEqual(a, b);
}

[TestCategory("SizeExtensions")]
[TestMethod]
[DataRow(0d, 0d, 0, 0)]
[DataRow(0d, 0d, 22, 6.89d)]
[DataRow(3.14d, 6.55f, 3838d, 3.24724928d)]
[DataRow(double.MinValue, double.Epsilon, 22, 0.3248d)]
public static void Test_SizeExtensions_ToRect_FromSizeAndPoint(double width, double height, int x, int y)
{
Point p = new Point(x, y);
Size s = new Size(width, height);
Rect
a = s.ToRect(p),
b = new Rect(p, s);

Assert.AreEqual(a, b);
}
}
}
2 changes: 2 additions & 0 deletions UnitTests/UnitTests.UWP/UnitTests.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@
<Compile Include="Converters\Test_StringFormatConverter.cs" />
<Compile Include="Converters\Test_TypeToObjectConverter.cs" />
<Compile Include="Extensions\Helpers\ObjectWithNullableBoolProperty.cs" />
<Compile Include="Extensions\Test_PointExtensions.cs" />
<Compile Include="Extensions\Test_SizeExtensions.cs" />
<Compile Include="Extensions\Test_BitmapIconExtensionMarkupExtension.cs" />
<Compile Include="Extensions\Test_FontIconSourceExtensionMarkupExtension.cs" />
<Compile Include="Extensions\Test_FontIconExtensionMarkupExtension.cs" />
Expand Down