Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,7 @@ protected override Size ArrangeOverride(Size finalSize)
var columnIndex = Grid.GetColumn(child);
var rowIndex = Grid.GetRow(child);

var rect = new Rect(0, 0, 0, 0)
{
X = _borderThickness
};
var rect = new Rect(_borderThickness, 0, 0, 0);

for (int col = 0; col < columnIndex; col++)
{
Expand Down
3 changes: 2 additions & 1 deletion Microsoft.Toolkit.Uwp.UI.Controls/Carousel/CarouselPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using Microsoft.Toolkit.Uwp.Extensions;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Windows.Foundation;
using Windows.UI;
Expand Down Expand Up @@ -283,7 +284,7 @@ protected override Size ArrangeOverride(Size finalSize)
double centerLeft = 0;
double centerTop = 0;

Clip = new RectangleGeometry { Rect = new Rect(0, 0, finalSize.Width, finalSize.Height) };
Clip = new RectangleGeometry { Rect = finalSize.ToRect() };

for (int i = 0; i < Children.Count; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Graphics.Canvas;
using Microsoft.Toolkit.Uwp.Extensions;
using Windows.Foundation;
using Windows.Graphics.DirectX;
using Windows.Graphics.Display;
Expand Down Expand Up @@ -98,7 +99,7 @@ private void UpdatePreview(int centerX, int centerY)
for (var j = colorStartX; j < colorEndX; j++)
{
var color = colors[((i - colorStartY) * width) + (j - colorStartX)];
drawingSession.FillRectangle(new Rect(startPoint, size), color);
drawingSession.FillRectangle(startPoint.ToRect(size), color);
startPoint.X += PreviewPixelsPerRawPixel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.Extensions;
using Windows.Foundation;
using Windows.UI.Core;
using Windows.UI.Xaml;
Expand Down Expand Up @@ -216,8 +217,8 @@ private async Task UpdateEyedropperWorkAreaAsync()
}

var transform = TargetElement.TransformToVisual(content);
var position = transform.TransformPoint(default(Point));
_eyedropper.WorkArea = new Rect(position, new Size(TargetElement.ActualWidth, TargetElement.ActualHeight));
var position = transform.TransformPoint(default);
_eyedropper.WorkArea = position.ToRect(TargetElement.ActualWidth, TargetElement.ActualHeight);
if (ControlHelpers.IsXamlRootAvailable && XamlRoot != null)
{
_eyedropper.XamlRoot = XamlRoot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using Microsoft.Toolkit.Uwp.Extensions;
using Windows.Foundation;
using Windows.UI.Composition;
using Windows.UI.Xaml;
Expand Down Expand Up @@ -117,7 +118,7 @@ private static List<DiscreteObjectKeyFrame> GetRectKeyframes(Rect from, Rect to,
rectKeyframes.Add(new DiscreteObjectKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(time),
Value = new Rect(startPoint, endPoint)
Value = startPoint.ToRect(endPoint)
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using Microsoft.Toolkit.Uwp.Extensions;
using Windows.Foundation;
using Windows.System;
using Windows.UI.Core;
Expand Down Expand Up @@ -98,7 +99,7 @@ private void ImageCropperThumb_KeyDown(object sender, KeyRoutedEventArgs e)

private void ImageCropperThumb_KeyUp(object sender, KeyRoutedEventArgs e)
{
var selectedRect = new Rect(new Point(_startX, _startY), new Point(_endX, _endY));
var selectedRect = new Point(_startX, _startY).ToRect(new Point(_endX, _endY));
var croppedRect = _inverseImageTransform.TransformBounds(selectedRect);
if (croppedRect.Width > MinCropSize.Width && croppedRect.Height > MinCropSize.Height)
{
Expand All @@ -111,7 +112,7 @@ private void ImageCropperThumb_KeyUp(object sender, KeyRoutedEventArgs e)

private void ImageCropperThumb_ManipulationCompleted(object sender, ManipulationCompletedRoutedEventArgs e)
{
var selectedRect = new Rect(new Point(_startX, _startY), new Point(_endX, _endY));
var selectedRect = new Point(_startX, _startY).ToRect(new Point(_endX, _endY));
var croppedRect = _inverseImageTransform.TransformBounds(selectedRect);
if (croppedRect.Width > MinCropSize.Width && croppedRect.Height > MinCropSize.Height)
{
Expand Down Expand Up @@ -155,7 +156,7 @@ private void SourceImage_ManipulationDelta(object sender, ManipulationDeltaRoute
offsetY = Math.Max(offsetY, _restrictedSelectRect.Y - _startY);
}

var selectedRect = new Rect(new Point(_startX, _startY), new Point(_endX, _endY));
var selectedRect = new Point(_startX, _startY).ToRect(new Point(_endX, _endY));
selectedRect.X += offsetX;
selectedRect.Y += offsetY;
var croppedRect = _inverseImageTransform.TransformBounds(selectedRect);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Effects;
using Microsoft.Graphics.Canvas.Geometry;
using Microsoft.Toolkit.Uwp.Extensions;
using Windows.Foundation;
using Windows.Graphics.Imaging;
using Windows.Storage.Streams;
Expand Down Expand Up @@ -299,7 +300,7 @@ private static Rect GetSafeRect(Point startPoint, Point endPoint, Size minSize,
break;
}

return new Rect(startPoint, endPoint);
return startPoint.ToRect(endPoint);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Numerics;
using Microsoft.Toolkit.Uwp.Extensions;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Hosting;
Expand Down Expand Up @@ -111,7 +112,7 @@ private void UpdateCroppedRect(ThumbPosition position, Point diffPos)

var startPoint = new Point(_startX, _startY);
var endPoint = new Point(_endX, _endY);
var currentSelectedRect = new Rect(startPoint, endPoint);
var currentSelectedRect = startPoint.ToRect(endPoint);
switch (position)
{
case ThumbPosition.Top:
Expand Down Expand Up @@ -251,7 +252,7 @@ private void UpdateCroppedRect(ThumbPosition position, Point diffPos)

var isEffectiveRegion = IsSafePoint(_restrictedSelectRect, startPoint) &&
IsSafePoint(_restrictedSelectRect, endPoint);
var selectedRect = new Rect(startPoint, endPoint);
var selectedRect = startPoint.ToRect(endPoint);
if (!isEffectiveRegion)
{
if (!IsCornerThumb(position) && TryGetContainedRect(_restrictedSelectRect, ref selectedRect))
Expand All @@ -268,8 +269,7 @@ private void UpdateCroppedRect(ThumbPosition position, Point diffPos)
selectedRect.Union(CanvasRect);
if (selectedRect != CanvasRect)
{
var croppedRect = _inverseImageTransform.TransformBounds(
new Rect(startPoint, endPoint));
var croppedRect = _inverseImageTransform.TransformBounds(startPoint.ToRect(endPoint));
croppedRect.Intersect(_restrictedCropRect);
_currentCroppedRect = croppedRect;
var viewportRect = GetUniformRect(CanvasRect, selectedRect.Width / selectedRect.Height);
Expand Down Expand Up @@ -460,7 +460,7 @@ private void UpdateMaskArea(bool animate = false)
case CropShape.Rectangular:
if (_innerGeometry is RectangleGeometry rectangleGeometry)
{
var to = new Rect(new Point(_startX, _startY), new Point(_endX, _endY));
var to = new Point(_startX, _startY).ToRect(new Point(_endX, _endY));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sergio0694 noticed in this case we don't have a way to construct a rect from x, y, x2, y2. Would it be useful for us to create a helper for this scenario too and avoid creating both two new points just to create a Rect? This pattern is used 3 times in the other file as well. Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michael-hawker That would be the ideal use case for static extension methods 😢
As in, ideally we'd just add a Rect.FromCoordinates static method taking x1, y1, x2, x2.
Withough that (C# doesn't support that yet unfortunately), not sure about this - I mean we could add a RectHelper class with that method, but having a whole new helper class just for that single method might be a bit overkill 🤔
If you like that idea just let me know and I can create another PR with that though!

if (animate)
{
var storyboard = new Storyboard();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

using System;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.Extensions;
using Windows.Foundation;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
Expand Down Expand Up @@ -120,7 +120,7 @@ private Size MinSelectSize
{
get
{
var realMinSelectSize = _imageTransform.TransformBounds(new Rect(default(Point), MinCropSize));
var realMinSelectSize = _imageTransform.TransformBounds(MinCropSize.ToRect());
var minLength = Math.Min(realMinSelectSize.Width, realMinSelectSize.Height);
if (minLength < MinSelectedLength)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Toolkit.Uwp.Extensions;
using Microsoft.Toolkit.Uwp.UI.Extensions;
using Windows.Foundation;
using Windows.UI.Xaml;
Expand Down Expand Up @@ -131,7 +132,7 @@ protected override Size ArrangeOverride(Size finalSize)
var y_normalized = (finalSize.Height / 2) - y - (element.DesiredSize.Height / 2);
var point = new Point(x_normalized, y_normalized);

element.Arrange(new Rect(point, element.DesiredSize));
element.Arrange(point.ToRect(element.DesiredSize));

var elementProperties = new OrbitViewElementProperties()
{
Expand Down
4 changes: 2 additions & 2 deletions Microsoft.Toolkit.Uwp.UI.Controls/RotatorTile/RotatorTile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
using System;
using System.Collections;
using System.Collections.Specialized;
using Microsoft.Toolkit.Uwp.Extensions;
using Microsoft.Toolkit.Uwp.Helpers;
using Windows.Foundation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
Expand Down Expand Up @@ -143,7 +143,7 @@ private void RotatorTile_SizeChanged(object sender, SizeChangedEventArgs e)
}

// Set clip to control
Clip = new RectangleGeometry() { Rect = new Rect(default(Point), e.NewSize) };
Clip = new RectangleGeometry { Rect = e.NewSize.ToRect() };
}

private void RotatorTile_Loaded(object sender, RoutedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ protected override Size ArrangeOverride(Size finalSize)
// Make sure all overflown elements have no size.
foreach (var child in _overflow)
{
child.Arrange(new Rect(0, 0, 0, 0));
child.Arrange(default);
}

_overflow = new List<UIElement>(); // Reset for next time.
Expand Down
58 changes: 58 additions & 0 deletions Microsoft.Toolkit.Uwp/Extensions/PointExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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"/> ending at the specified point, starting at the given coordinates.
/// </summary>
/// <param name="point">The input <see cref="Point"/> value to convert.</param>
/// <param name="end">The ending position for the rectangle.</param>
/// <returns>A <see cref="Rect"/> value between the two specified points.</returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rect ToRect(this Point point, Point end)
{
return new Rect(point, end);
}

/// <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);
}
}
}
Loading