From 6390e7c32ff586dee883213394c6d62bf29928fc Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Thu, 10 Oct 2024 23:05:55 +0100 Subject: [PATCH] fix 2 --- .../ContentCropper/ContentCropper.cs | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/Toolkit.UI.Controls.Avalonia/ContentCropper/ContentCropper.cs b/Toolkit.UI.Controls.Avalonia/ContentCropper/ContentCropper.cs index 6cee526..6006de2 100644 --- a/Toolkit.UI.Controls.Avalonia/ContentCropper/ContentCropper.cs +++ b/Toolkit.UI.Controls.Avalonia/ContentCropper/ContentCropper.cs @@ -137,25 +137,29 @@ public class ContentCropper : ContentControl canvas.Width = newContentWidth; canvas.Height = newContentHeight; - // Calculate new positions and sizes based on updated ratios - double newCropLeft = cropLeftRatio * newContentWidth; - double newCropTop = cropTopRatio * newContentHeight; - double newCropWidth = cropWidthRatio * newContentWidth; - double newCropHeight = cropHeightRatio * newContentHeight; - - // Check if the crop rectangle was resized or moved, and update accordingly - if (border.Width != newCropWidth || border.Height != newCropHeight) + if (border.Width > 0 && border.Height > 0) { + double newCropLeft = cropLeftRatio * newContentWidth; + double newCropTop = cropTopRatio * newContentHeight; + double newCropWidth = cropWidthRatio * newContentWidth; + double newCropHeight = cropHeightRatio * newContentHeight; + border.Width = newCropWidth; border.Height = newCropHeight; + Canvas.SetLeft(border, newCropLeft); Canvas.SetTop(border, newCropTop); } + else + { + InitializeCropRect(); + } PositionThumbs(); RenderOverLays(); } + private void InitializeCropRect() { if (canvas is null || Content is not Control content) @@ -177,6 +181,9 @@ public class ContentCropper : ContentControl canvas.Height = height; UpdateCropArea(width, height); + UpdateCropRatios(); + + PositionThumbs(); RenderOverLays(); } @@ -198,7 +205,8 @@ public class ContentCropper : ContentControl RenderOverLays(); } - private void OnBorderPointerPressed(object? sender, PointerPressedEventArgs args) + private void OnBorderPointerPressed(object? sender, + PointerPressedEventArgs args) { if (!isDragging && border is not null) { @@ -210,13 +218,15 @@ public class ContentCropper : ContentControl } } - private void OnBorderPointerReleased(object? sender, PointerReleasedEventArgs args) + private void OnBorderPointerReleased(object? sender, + PointerReleasedEventArgs args) { isDragging = false; UpdateCropRatios(); } - private void OnThumbDragDelta(object? sender, VectorEventArgs args) + private void OnThumbDragDelta(object? sender, + VectorEventArgs args) { if (canvas is null || border is null || sender is not Thumb thumb) { @@ -343,7 +353,6 @@ public class ContentCropper : ContentControl rectangleBottom.Height = Math.Max(0, canvas.Height - bottomY); Canvas.SetTop(rectangleBottom, bottomY); } - private void UpdateCropRatios() { if (canvas == null || border == null) @@ -357,6 +366,7 @@ public class ContentCropper : ContentControl cropHeightRatio = border.Height / canvas.Height; } + private void UpdateCropArea(double width, double height) { if (canvas == null || border == null)