Skip to content

Commit 05d77c4

Browse files
author
msftbot[bot]
authored
New SizeExtensions for Windows.Foundation.Size (#3489)
## Closes #3317 <!-- Add the relevant issue number after the "#" mentioned above (for ex: Fixes #1234) which will automatically close the issue once the PR is merged. --> <!-- Add a brief overview here of the feature/bug & fix. --> ## PR Type What kind of change does this PR introduce? <!-- Please uncomment one or more that apply to this PR. --> <!-- - Bugfix --> - Feature <!-- - Code style update (formatting) --> <!-- - Refactoring (no functional changes, no api changes) --> <!-- - Build or CI related changes --> <!-- - Documentation content changes --> <!-- - Sample app changes --> <!-- - Other... Please describe: --> ## What is the new behavior? <!-- Describe how was this issue resolved or changed? --> This PR adds new extensions to easily convert `Windows.Foundation.Size` and `Point` values to `Rect`: ```csharp namespace Microsoft.Toolkit.Uwp.Extensions { public static class SizeExtensions { public static Rect ToRect(this Size size); public static Rect ToRect(this Size size, double x, double y); public static Rect ToRect(this Size size, Point point); } public static class PointExtensions { public static Rect ToRect(this Point point, double width, double height); public static Rect ToRect(this Point point, Point end); public static Rect ToRect(this Point point, Size size); } } ``` ## PR Checklist Please check if your PR fulfills the following requirements: - [X] Tested code with current [supported SDKs](../readme.md#supported) - [ ] ~~Pull Request has been submitted to the documentation repository [instructions](..\contributing.md#docs). Link: <!-- docs PR link -->~~ - [ ] ~~Sample in sample app has been added / updated (for bug fixes / features)~~ - [ ] ~~Icon has been created (if new sample) following the [Thumbnail Style Guide and templates](https://github.com/windows-toolkit/WindowsCommunityToolkit-design-assets)~~ - [X] Tests for the changes have been added (for bug fixes / features) (if applicable) - [X] Header has been added to all new source files (run *build/UpdateHeaders.bat*) - [X] Contains **NO** breaking changes
2 parents 0b25083 + 0941b5e commit 05d77c4

File tree

17 files changed

+272
-24
lines changed

17 files changed

+272
-24
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Markdown/Render/MarkdownTable.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,7 @@ protected override Size ArrangeOverride(Size finalSize)
155155
var columnIndex = Grid.GetColumn(child);
156156
var rowIndex = Grid.GetRow(child);
157157

158-
var rect = new Rect(0, 0, 0, 0)
159-
{
160-
X = _borderThickness
161-
};
158+
var rect = new Rect(_borderThickness, 0, 0, 0);
162159

163160
for (int col = 0; col < columnIndex; col++)
164161
{

Microsoft.Toolkit.Uwp.UI.Controls/Carousel/CarouselPanel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using Microsoft.Toolkit.Uwp.Extensions;
67
using Microsoft.Toolkit.Uwp.UI.Extensions;
78
using Windows.Foundation;
89
using Windows.UI;
@@ -283,7 +284,7 @@ protected override Size ArrangeOverride(Size finalSize)
283284
double centerLeft = 0;
284285
double centerTop = 0;
285286

286-
Clip = new RectangleGeometry { Rect = new Rect(0, 0, finalSize.Width, finalSize.Height) };
287+
Clip = new RectangleGeometry { Rect = finalSize.ToRect() };
287288

288289
for (int i = 0; i < Children.Count; i++)
289290
{

Microsoft.Toolkit.Uwp.UI.Controls/Eyedropper/Eyedropper.Logic.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Threading.Tasks;
88
using Microsoft.Graphics.Canvas;
9+
using Microsoft.Toolkit.Uwp.Extensions;
910
using Windows.Foundation;
1011
using Windows.Graphics.DirectX;
1112
using Windows.Graphics.Display;
@@ -98,7 +99,7 @@ private void UpdatePreview(int centerX, int centerY)
9899
for (var j = colorStartX; j < colorEndX; j++)
99100
{
100101
var color = colors[((i - colorStartY) * width) + (j - colorStartX)];
101-
drawingSession.FillRectangle(new Rect(startPoint, size), color);
102+
drawingSession.FillRectangle(startPoint.ToRect(size), color);
102103
startPoint.X += PreviewPixelsPerRawPixel;
103104
}
104105

Microsoft.Toolkit.Uwp.UI.Controls/Eyedropper/EyedropperToolButton.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Threading.Tasks;
7+
using Microsoft.Toolkit.Uwp.Extensions;
78
using Windows.Foundation;
89
using Windows.UI.Core;
910
using Windows.UI.Xaml;
@@ -216,8 +217,8 @@ private async Task UpdateEyedropperWorkAreaAsync()
216217
}
217218

218219
var transform = TargetElement.TransformToVisual(content);
219-
var position = transform.TransformPoint(default(Point));
220-
_eyedropper.WorkArea = new Rect(position, new Size(TargetElement.ActualWidth, TargetElement.ActualHeight));
220+
var position = transform.TransformPoint(default);
221+
_eyedropper.WorkArea = position.ToRect(TargetElement.ActualWidth, TargetElement.ActualHeight);
221222
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
222223
{
223224
_eyedropper.XamlRoot = XamlRoot;

Microsoft.Toolkit.Uwp.UI.Controls/ImageCropper/ImageCropper.Animations.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Numerics;
8+
using Microsoft.Toolkit.Uwp.Extensions;
89
using Windows.Foundation;
910
using Windows.UI.Composition;
1011
using Windows.UI.Xaml;
@@ -117,7 +118,7 @@ private static List<DiscreteObjectKeyFrame> GetRectKeyframes(Rect from, Rect to,
117118
rectKeyframes.Add(new DiscreteObjectKeyFrame
118119
{
119120
KeyTime = KeyTime.FromTimeSpan(time),
120-
Value = new Rect(startPoint, endPoint)
121+
Value = startPoint.ToRect(endPoint)
121122
});
122123
}
123124

Microsoft.Toolkit.Uwp.UI.Controls/ImageCropper/ImageCropper.Events.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using Microsoft.Toolkit.Uwp.Extensions;
67
using Windows.Foundation;
78
using Windows.System;
89
using Windows.UI.Core;
@@ -98,7 +99,7 @@ private void ImageCropperThumb_KeyDown(object sender, KeyRoutedEventArgs e)
9899

99100
private void ImageCropperThumb_KeyUp(object sender, KeyRoutedEventArgs e)
100101
{
101-
var selectedRect = new Rect(new Point(_startX, _startY), new Point(_endX, _endY));
102+
var selectedRect = new Point(_startX, _startY).ToRect(new Point(_endX, _endY));
102103
var croppedRect = _inverseImageTransform.TransformBounds(selectedRect);
103104
if (croppedRect.Width > MinCropSize.Width && croppedRect.Height > MinCropSize.Height)
104105
{
@@ -111,7 +112,7 @@ private void ImageCropperThumb_KeyUp(object sender, KeyRoutedEventArgs e)
111112

112113
private void ImageCropperThumb_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
113114
{
114-
var selectedRect = new Rect(new Point(_startX, _startY), new Point(_endX, _endY));
115+
var selectedRect = new Point(_startX, _startY).ToRect(new Point(_endX, _endY));
115116
var croppedRect = _inverseImageTransform.TransformBounds(selectedRect);
116117
if (croppedRect.Width > MinCropSize.Width && croppedRect.Height > MinCropSize.Height)
117118
{
@@ -155,7 +156,7 @@ private void SourceImage_ManipulationDelta(object sender, ManipulationDeltaRoute
155156
offsetY = Math.Max(offsetY, _restrictedSelectRect.Y - _startY);
156157
}
157158

158-
var selectedRect = new Rect(new Point(_startX, _startY), new Point(_endX, _endY));
159+
var selectedRect = new Point(_startX, _startY).ToRect(new Point(_endX, _endY));
159160
selectedRect.X += offsetX;
160161
selectedRect.Y += offsetY;
161162
var croppedRect = _inverseImageTransform.TransformBounds(selectedRect);

Microsoft.Toolkit.Uwp.UI.Controls/ImageCropper/ImageCropper.Helpers.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.Graphics.Canvas;
1010
using Microsoft.Graphics.Canvas.Effects;
1111
using Microsoft.Graphics.Canvas.Geometry;
12+
using Microsoft.Toolkit.Uwp.Extensions;
1213
using Windows.Foundation;
1314
using Windows.Graphics.Imaging;
1415
using Windows.Storage.Streams;
@@ -299,7 +300,7 @@ private static Rect GetSafeRect(Point startPoint, Point endPoint, Size minSize,
299300
break;
300301
}
301302

302-
return new Rect(startPoint, endPoint);
303+
return startPoint.ToRect(endPoint);
303304
}
304305

305306
/// <summary>

Microsoft.Toolkit.Uwp.UI.Controls/ImageCropper/ImageCropper.Logic.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Numerics;
7+
using Microsoft.Toolkit.Uwp.Extensions;
78
using Windows.Foundation;
89
using Windows.UI.Xaml;
910
using Windows.UI.Xaml.Hosting;
@@ -111,7 +112,7 @@ private void UpdateCroppedRect(ThumbPosition position, Point diffPos)
111112

112113
var startPoint = new Point(_startX, _startY);
113114
var endPoint = new Point(_endX, _endY);
114-
var currentSelectedRect = new Rect(startPoint, endPoint);
115+
var currentSelectedRect = startPoint.ToRect(endPoint);
115116
switch (position)
116117
{
117118
case ThumbPosition.Top:
@@ -251,7 +252,7 @@ private void UpdateCroppedRect(ThumbPosition position, Point diffPos)
251252

252253
var isEffectiveRegion = IsSafePoint(_restrictedSelectRect, startPoint) &&
253254
IsSafePoint(_restrictedSelectRect, endPoint);
254-
var selectedRect = new Rect(startPoint, endPoint);
255+
var selectedRect = startPoint.ToRect(endPoint);
255256
if (!isEffectiveRegion)
256257
{
257258
if (!IsCornerThumb(position) && TryGetContainedRect(_restrictedSelectRect, ref selectedRect))
@@ -268,8 +269,7 @@ private void UpdateCroppedRect(ThumbPosition position, Point diffPos)
268269
selectedRect.Union(CanvasRect);
269270
if (selectedRect != CanvasRect)
270271
{
271-
var croppedRect = _inverseImageTransform.TransformBounds(
272-
new Rect(startPoint, endPoint));
272+
var croppedRect = _inverseImageTransform.TransformBounds(startPoint.ToRect(endPoint));
273273
croppedRect.Intersect(_restrictedCropRect);
274274
_currentCroppedRect = croppedRect;
275275
var viewportRect = GetUniformRect(CanvasRect, selectedRect.Width / selectedRect.Height);
@@ -460,7 +460,7 @@ private void UpdateMaskArea(bool animate = false)
460460
case CropShape.Rectangular:
461461
if (_innerGeometry is RectangleGeometry rectangleGeometry)
462462
{
463-
var to = new Rect(new Point(_startX, _startY), new Point(_endX, _endY));
463+
var to = new Point(_startX, _startY).ToRect(new Point(_endX, _endY));
464464
if (animate)
465465
{
466466
var storyboard = new Storyboard();

Microsoft.Toolkit.Uwp.UI.Controls/ImageCropper/ImageCropper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
using System;
66
using System.Threading.Tasks;
7+
using Microsoft.Toolkit.Uwp.Extensions;
78
using Windows.Foundation;
8-
using Windows.Graphics.Imaging;
99
using Windows.Storage;
1010
using Windows.Storage.Streams;
1111
using Windows.UI.Xaml;
@@ -120,7 +120,7 @@ private Size MinSelectSize
120120
{
121121
get
122122
{
123-
var realMinSelectSize = _imageTransform.TransformBounds(new Rect(default(Point), MinCropSize));
123+
var realMinSelectSize = _imageTransform.TransformBounds(MinCropSize.ToRect());
124124
var minLength = Math.Min(realMinSelectSize.Width, realMinSelectSize.Height);
125125
if (minLength < MinSelectedLength)
126126
{

Microsoft.Toolkit.Uwp.UI.Controls/OrbitView/OrbitViewPanel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8+
using Microsoft.Toolkit.Uwp.Extensions;
89
using Microsoft.Toolkit.Uwp.UI.Extensions;
910
using Windows.Foundation;
1011
using Windows.UI.Xaml;
@@ -131,7 +132,7 @@ protected override Size ArrangeOverride(Size finalSize)
131132
var y_normalized = (finalSize.Height / 2) - y - (element.DesiredSize.Height / 2);
132133
var point = new Point(x_normalized, y_normalized);
133134

134-
element.Arrange(new Rect(point, element.DesiredSize));
135+
element.Arrange(point.ToRect(element.DesiredSize));
135136

136137
var elementProperties = new OrbitViewElementProperties()
137138
{

Microsoft.Toolkit.Uwp.UI.Controls/RotatorTile/RotatorTile.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
using System;
66
using System.Collections;
77
using System.Collections.Specialized;
8+
using Microsoft.Toolkit.Uwp.Extensions;
89
using Microsoft.Toolkit.Uwp.Helpers;
9-
using Windows.Foundation;
1010
using Windows.UI.Xaml;
1111
using Windows.UI.Xaml.Controls;
1212
using Windows.UI.Xaml.Media;
@@ -143,7 +143,7 @@ private void RotatorTile_SizeChanged(object sender, SizeChangedEventArgs e)
143143
}
144144

145145
// Set clip to control
146-
Clip = new RectangleGeometry() { Rect = new Rect(default(Point), e.NewSize) };
146+
Clip = new RectangleGeometry { Rect = e.NewSize.ToRect() };
147147
}
148148

149149
private void RotatorTile_Loaded(object sender, RoutedEventArgs e)

Microsoft.Toolkit.Uwp.UI.Controls/UniformGrid/UniformGrid.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ protected override Size ArrangeOverride(Size finalSize)
152152
// Make sure all overflown elements have no size.
153153
foreach (var child in _overflow)
154154
{
155-
child.Arrange(new Rect(0, 0, 0, 0));
155+
child.Arrange(default);
156156
}
157157

158158
_overflow = new List<UIElement>(); // Reset for next time.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics.Contracts;
6+
using System.Runtime.CompilerServices;
7+
using Point = Windows.Foundation.Point;
8+
using Rect = Windows.Foundation.Rect;
9+
using Size = Windows.Foundation.Size;
10+
11+
namespace Microsoft.Toolkit.Uwp.Extensions
12+
{
13+
/// <summary>
14+
/// Extensions for the <see cref="Point"/> type.
15+
/// </summary>
16+
public static class PointExtensions
17+
{
18+
/// <summary>
19+
/// Creates a new <see cref="Rect"/> of the specified size, starting at a given point.
20+
/// </summary>
21+
/// <param name="point">The input <see cref="Point"/> value to convert.</param>
22+
/// <param name="width">The width of the rectangle.</param>
23+
/// <param name="height">The height of the rectangle.</param>
24+
/// <returns>A <see cref="Rect"/> value of the specified size, starting at the given point.</returns>
25+
[Pure]
26+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
27+
public static Rect ToRect(this Point point, double width, double height)
28+
{
29+
return new Rect(point.X, point.Y, width, height);
30+
}
31+
32+
/// <summary>
33+
/// Creates a new <see cref="Rect"/> ending at the specified point, starting at the given coordinates.
34+
/// </summary>
35+
/// <param name="point">The input <see cref="Point"/> value to convert.</param>
36+
/// <param name="end">The ending position for the rectangle.</param>
37+
/// <returns>A <see cref="Rect"/> value between the two specified points.</returns>
38+
[Pure]
39+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
40+
public static Rect ToRect(this Point point, Point end)
41+
{
42+
return new Rect(point, end);
43+
}
44+
45+
/// <summary>
46+
/// Creates a new <see cref="Rect"/> of the specified size, starting at the given coordinates.
47+
/// </summary>
48+
/// <param name="point">The input <see cref="Point"/> value to convert.</param>
49+
/// <param name="size">The size of the rectangle to create.</param>
50+
/// <returns>A <see cref="Rect"/> value of the specified size, starting at the given coordinates.</returns>
51+
[Pure]
52+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
53+
public static Rect ToRect(this Point point, Size size)
54+
{
55+
return new Rect(point, size);
56+
}
57+
}
58+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics.Contracts;
6+
using System.Runtime.CompilerServices;
7+
using Point = Windows.Foundation.Point;
8+
using Rect = Windows.Foundation.Rect;
9+
using Size = Windows.Foundation.Size;
10+
11+
namespace Microsoft.Toolkit.Uwp.Extensions
12+
{
13+
/// <summary>
14+
/// Extensions for the <see cref="Size"/> type.
15+
/// </summary>
16+
public static class SizeExtensions
17+
{
18+
/// <summary>
19+
/// Creates a new <see cref="Rect"/> of the specified size, starting at the origin.
20+
/// </summary>
21+
/// <param name="size">The input <see cref="Size"/> value to convert.</param>
22+
/// <returns>A <see cref="Rect"/> value of the specified size, starting at the origin.</returns>
23+
[Pure]
24+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
25+
public static Rect ToRect(this Size size)
26+
{
27+
return new Rect(0, 0, size.Width, size.Height);
28+
}
29+
30+
/// <summary>
31+
/// Creates a new <see cref="Rect"/> of the specified size, starting at the given coordinates.
32+
/// </summary>
33+
/// <param name="size">The input <see cref="Size"/> value to convert.</param>
34+
/// <param name="x">The horizontal offset.</param>
35+
/// <param name="y">The vertical offset.</param>
36+
/// <returns>A <see cref="Rect"/> value of the specified size, starting at the given coordinates.</returns>
37+
[Pure]
38+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
39+
public static Rect ToRect(this Size size, double x, double y)
40+
{
41+
return new Rect(x, y, size.Width, size.Height);
42+
}
43+
44+
/// <summary>
45+
/// Creates a new <see cref="Rect"/> of the specified size, starting at the given position.
46+
/// </summary>
47+
/// <param name="size">The input <see cref="Size"/> value to convert.</param>
48+
/// <param name="point">The starting position to use.</param>
49+
/// <returns>A <see cref="Rect"/> value of the specified size, starting at the given position.</returns>
50+
[Pure]
51+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
52+
public static Rect ToRect(this Size size, Point point)
53+
{
54+
return new Rect(point, size);
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)