Improved resized logics

This commit is contained in:
TheXamlGuy
2024-10-05 23:43:18 +01:00
parent 247acd2609
commit d434b917ec
@@ -9,7 +9,8 @@ using Avalonia.Media.Imaging;
namespace Toolkit.UI.Controls.Avalonia; namespace Toolkit.UI.Controls.Avalonia;
public class ImageCropper : TemplatedControl public class ImageCropper :
TemplatedControl
{ {
public static readonly StyledProperty<IImage?> CurrentAreaBitmapProperty = public static readonly StyledProperty<IImage?> CurrentAreaBitmapProperty =
AvaloniaProperty.Register<ImageCropper, IImage?>(nameof(CurrentAreaBitmap)); AvaloniaProperty.Register<ImageCropper, IImage?>(nameof(CurrentAreaBitmap));
@@ -225,20 +226,33 @@ public class ImageCropper : TemplatedControl
case "TopLeftButton": case "TopLeftButton":
newWidth = Math.Max(0, border.Width - deltaX); newWidth = Math.Max(0, border.Width - deltaX);
newHeight = Math.Max(0, border.Height - deltaY); newHeight = Math.Max(0, border.Height - deltaY);
leftPosition += deltaX; if (newWidth > 0)
topPosition += deltaY; {
leftPosition += deltaX;
}
if (newHeight > 0)
{
topPosition += deltaY;
}
break; break;
case "TopRightButton": case "TopRightButton":
newWidth = Math.Max(0, border.Width + deltaX); newWidth = Math.Max(0, border.Width + deltaX);
newHeight = Math.Max(0, border.Height - deltaY); newHeight = Math.Max(0, border.Height - deltaY);
topPosition += deltaY; if (newHeight > 0)
{
topPosition += deltaY;
}
break; break;
case "BottomLeftButton": case "BottomLeftButton":
newWidth = Math.Max(0, border.Width - deltaX); newWidth = Math.Max(0, border.Width - deltaX);
newHeight = Math.Max(0, border.Height + deltaY); newHeight = Math.Max(0, border.Height + deltaY);
leftPosition += deltaX; if (newWidth > 0)
{
leftPosition += deltaX;
}
break; break;
case "BottomRightButton": case "BottomRightButton":
@@ -255,23 +269,27 @@ public class ImageCropper : TemplatedControl
if (thumb.Name == "TopLeftButton" || thumb.Name == "BottomLeftButton") if (thumb.Name == "TopLeftButton" || thumb.Name == "BottomLeftButton")
{ {
leftPosition = Math.Max(0, leftPosition); leftPosition = Math.Max(0, leftPosition);
newWidth = Math.Max(0, border.Width - (leftPosition - Canvas.GetLeft(border))); if (newWidth > 0)
{
newWidth = Math.Max(0, border.Width - (leftPosition - Canvas.GetLeft(border)));
}
} }
else if (thumb.Name == "TopRightButton" || thumb.Name == "BottomRightButton") else if (thumb.Name == "TopRightButton" || thumb.Name == "BottomRightButton")
{ {
double rightBoundary = canvas.Width; newWidth = Math.Min(newWidth, canvas.Width - leftPosition);
newWidth = Math.Min(newWidth, rightBoundary - leftPosition);
} }
if (thumb.Name == "TopLeftButton" || thumb.Name == "TopRightButton") if (thumb.Name == "TopLeftButton" || thumb.Name == "TopRightButton")
{ {
topPosition = Math.Max(0, topPosition); topPosition = Math.Max(0, topPosition);
newHeight = Math.Max(0, border.Height - (topPosition - Canvas.GetTop(border))); if (newHeight > 0)
{
newHeight = Math.Max(0, border.Height - (topPosition - Canvas.GetTop(border)));
}
} }
else if (thumb.Name == "BottomLeftButton" || thumb.Name == "BottomRightButton") else if (thumb.Name == "BottomLeftButton" || thumb.Name == "BottomRightButton")
{ {
double bottomBoundary = canvas.Height; newHeight = Math.Min(newHeight, canvas.Height - topPosition);
newHeight = Math.Min(newHeight, bottomBoundary - topPosition);
} }
border.Width = newWidth; border.Width = newWidth;
@@ -284,7 +302,6 @@ public class ImageCropper : TemplatedControl
Render(); Render();
} }
private void PositionThumbs() private void PositionThumbs()
{ {
if (border == null || if (border == null ||
@@ -343,7 +360,7 @@ public class ImageCropper : TemplatedControl
Canvas.SetTop(rectangleLeft, borderTop); Canvas.SetTop(rectangleLeft, borderTop);
rectangleTop.Width = Math.Max(0, canvas.Width); rectangleTop.Width = Math.Max(0, canvas.Width);
rectangleTop.Height = Math.Max(0, borderTop); rectangleTop.Height = Math.Max(0, borderTop - 0.5);
double rightX = borderLeft + border.Width; double rightX = borderLeft + border.Width;