Ruben 1 سال پیش
والد
کامیت
5f6aedc02c

+ 5 - 2
src/PicView.Avalonia/Crop/CropDragHandler.cs

@@ -10,6 +10,9 @@ public class CropDragHandler(CropControl control)
     private Point _dragStart;
     private bool _isDragging;
     private Rect _originalRect;
+    
+    private const double ButtonLeftOffset = 11;
+    private const double ButtonTopOffset = 3;
 
     public void OnDragStart(object? sender, PointerPressedEventArgs e)
     {
@@ -78,8 +81,8 @@ public class CropDragHandler(CropControl control)
         Canvas.SetLeft(control.MainRectangle, newLeft);
         Canvas.SetTop(control.MainRectangle, newTop);
         
-        Canvas.SetLeft(control.SizeBorder, newLeft + 11);
-        Canvas.SetTop(control.SizeBorder, newTop - control.SizeBorder.Bounds.Height - 3);
+        Canvas.SetLeft(control.SizeBorder, newLeft + ButtonLeftOffset);
+        Canvas.SetTop(control.SizeBorder, newTop - control.SizeBorder.Bounds.Height - ButtonTopOffset);
 
         // Update view model values
         vm.SelectionX = Convert.ToInt32(newLeft);

+ 37 - 37
src/PicView.Avalonia/Crop/CropLayoutManager.cs

@@ -6,45 +6,45 @@ namespace PicView.Avalonia.Crop;
 
 public class CropLayoutManager(CropControl control)
 {
+    private const int DefaultSelectionSize = 200;
+    
     public void InitializeLayout()
     {
+        if (control.DataContext is not ImageCropperViewModel vm)
+        {
+            return;
+        }
+
+        // Ensure image dimensions are valid before proceeding
+        if (vm.ImageWidth <= 0 || vm.ImageHeight <= 0)
+        {
+            return;
+        }
+
+        // Set initial width and height for the crop rectangle
+        var pixelWidth = vm.ImageWidth / vm.AspectRatio;
+        var pixelHeight = vm.ImageHeight / vm.AspectRatio;
 
-            if (control.DataContext is not ImageCropperViewModel vm)
-            {
-                return;
-            }
-
-            // Ensure image dimensions are valid before proceeding
-            if (vm.ImageWidth <= 0 || vm.ImageHeight <= 0)
-            {
-                return;
-            }
-
-            // Set initial width and height for the crop rectangle
-            var pixelWidth = vm.ImageWidth / vm.AspectRatio;
-            var pixelHeight = vm.ImageHeight / vm.AspectRatio;
-
-            if (pixelWidth >= 400 || pixelHeight >= 400)
-            {
-                vm.SelectionWidth = 200;
-                vm.SelectionHeight = 200;
-            }
-            else if (pixelWidth <= 200 || pixelHeight <= 200)
-            {
-                vm.SelectionWidth = pixelWidth / 2;
-                vm.SelectionHeight = pixelHeight / 2;
-            }
-
-
-            // Calculate centered position
-            vm.SelectionX = Convert.ToInt32((vm.ImageWidth - vm.SelectionWidth) / 2);
-            vm.SelectionY = Convert.ToInt32((vm.ImageHeight - vm.SelectionHeight) / 2);
-
-            // Apply the calculated position to the MainRectangle
-            Canvas.SetLeft(control.MainRectangle, vm.SelectionX);
-            Canvas.SetTop(control.MainRectangle, vm.SelectionY);
-
-            UpdateLayout();
+        if (pixelWidth >= DefaultSelectionSize * 2 || pixelHeight >= DefaultSelectionSize * 2)
+        {
+            vm.SelectionWidth = DefaultSelectionSize;
+            vm.SelectionHeight = DefaultSelectionSize;
+        }
+        else if (pixelWidth <= DefaultSelectionSize || pixelHeight <= DefaultSelectionSize)
+        {
+            vm.SelectionWidth = pixelWidth / 2;
+            vm.SelectionHeight = pixelHeight / 2;
+        }
+
+        // Calculate centered position
+        vm.SelectionX = Convert.ToInt32((vm.ImageWidth - vm.SelectionWidth) / 2);
+        vm.SelectionY = Convert.ToInt32((vm.ImageHeight - vm.SelectionHeight) / 2);
+
+        // Apply the calculated position to the MainRectangle
+        Canvas.SetLeft(control.MainRectangle, vm.SelectionX);
+        Canvas.SetTop(control.MainRectangle, vm.SelectionY);
+
+        UpdateLayout();
     }
 
     public void UpdateLayout()
@@ -201,7 +201,7 @@ public class CropLayoutManager(CropControl control)
         Canvas.SetTop(control.RightMiddleButton, rightMiddleY);
 
         Canvas.SetLeft(control.SizeBorder, topLeftX + control.TopLeftButton.Bounds.Width + 2);
-        
+
         if (topLeftY != 0)
         {
             Canvas.SetTop(control.SizeBorder, topLeftY - control.TopLeftButton.Bounds.Height);

+ 15 - 0
src/PicView.Avalonia/ImageHandling/ImageHelper.cs

@@ -1,4 +1,6 @@
 using System.Diagnostics;
+using Avalonia;
+using Avalonia.Media.Imaging;
 using ImageMagick;
 using PicView.Avalonia.Navigation;
 using PicView.Avalonia.UI;
@@ -116,4 +118,17 @@ public static class ImageHelper
         });
         SetTitleHelper.RefreshTitle(vm);
     }
+    
+    public static RenderTargetBitmap ConvertCroppedBitmapToBitmap(CroppedBitmap croppedBitmap)
+    {
+        var renderTargetBitmap = new RenderTargetBitmap(new PixelSize(
+            croppedBitmap.SourceRect.Width,
+            croppedBitmap.SourceRect.Height));
+
+        using var context = renderTargetBitmap.CreateDrawingContext();
+        context.DrawImage(croppedBitmap,
+            new Rect(0, 0, croppedBitmap.SourceRect.Width, croppedBitmap.SourceRect.Height));
+
+        return renderTargetBitmap;
+    }
 }

+ 2 - 14
src/PicView.Avalonia/ViewModels/ImageCropperViewModel.cs

@@ -4,6 +4,7 @@ using Avalonia.Media.Imaging;
 using ImageMagick;
 using PicView.Avalonia.Crop;
 using PicView.Avalonia.FileSystem;
+using PicView.Avalonia.ImageHandling;
 using PicView.Avalonia.Navigation;
 using PicView.Avalonia.UI;
 using PicView.Core.Localization;
@@ -155,7 +156,7 @@ public class ImageCropperViewModel : ViewModelBase
         var width = (int)PixelSelectionWidth;
         var height = (int)PixelSelectionHeight;
         var croppedBitmap = new CroppedBitmap(Bitmap, new PixelRect(x, y, width, height));
-        var bitmap = ConvertCroppedBitmapToBitmap(croppedBitmap);
+        var bitmap = ImageHelper.ConvertCroppedBitmapToBitmap(croppedBitmap);
         return (fileName, new FileInfo(fileName), bitmap);
     }
 
@@ -180,17 +181,4 @@ public class ImageCropperViewModel : ViewModelBase
         image.Crop(geometry);
         await image.WriteAsync(saveFilePath);
     }
-
-    private static RenderTargetBitmap ConvertCroppedBitmapToBitmap(CroppedBitmap croppedBitmap)
-    {
-        var renderTargetBitmap = new RenderTargetBitmap(new PixelSize(
-            croppedBitmap.SourceRect.Width,
-            croppedBitmap.SourceRect.Height));
-
-        using var context = renderTargetBitmap.CreateDrawingContext();
-        context.DrawImage(croppedBitmap,
-            new Rect(0, 0, croppedBitmap.SourceRect.Width, croppedBitmap.SourceRect.Height));
-
-        return renderTargetBitmap;
-    }
 }

+ 30 - 27
src/PicView.Avalonia/Views/UC/CropControl.axaml.cs

@@ -20,40 +20,43 @@ public partial class CropControl : UserControl
         _resizeHandler = new CropResizeHandler(this);
         _layoutManager = new CropLayoutManager(this);
 
-        Loaded += delegate
-        {
-            MainRectangle.PointerPressed += _dragHandler.OnDragStart;
-            MainRectangle.PointerReleased += _dragHandler.OnDragEnd;
-            MainRectangle.PointerMoved += _dragHandler.OnDragMove;
-            MainRectangle.PointerMoved += (_, _) => _layoutManager.UpdateLayout();
-
-            TopLeftButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
-            TopRightButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
-            BottomLeftButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
-            BottomRightButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
-            LeftMiddleButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
-            RightMiddleButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
-            TopMiddleButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
-            BottomMiddleButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
-
-            TopLeftButton.PointerReleased += _resizeHandler.OnResizeEnd;
-            TopRightButton.PointerReleased += _resizeHandler.OnResizeEnd;
-            BottomLeftButton.PointerReleased += _resizeHandler.OnResizeEnd;
-            BottomRightButton.PointerReleased += _resizeHandler.OnResizeEnd;
-            LeftMiddleButton.PointerReleased += _resizeHandler.OnResizeEnd;
-            RightMiddleButton.PointerReleased += _resizeHandler.OnResizeEnd;
-            TopMiddleButton.PointerReleased += _resizeHandler.OnResizeEnd;
-            BottomMiddleButton.PointerReleased += _resizeHandler.OnResizeEnd;
-        };
-
         Loaded += OnControlLoaded;
-        LostFocus += OnControlLostFocus;
     }
 
     private void OnControlLoaded(object? sender, RoutedEventArgs e)
+    {
+        InitLoaded();
+    }
+
+    private void InitLoaded()
     {
         InitializeResizeHandlers();
         _layoutManager.InitializeLayout();
+            
+        MainRectangle.PointerPressed += _dragHandler.OnDragStart;
+        MainRectangle.PointerReleased += _dragHandler.OnDragEnd;
+        MainRectangle.PointerMoved += _dragHandler.OnDragMove;
+        MainRectangle.PointerMoved += (_, _) => _layoutManager.UpdateLayout();
+
+        TopLeftButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
+        TopRightButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
+        BottomLeftButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
+        BottomRightButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
+        LeftMiddleButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
+        RightMiddleButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
+        TopMiddleButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
+        BottomMiddleButton.PointerPressed += (_, e) => _resizeHandler.OnResizeStart(e);
+
+        TopLeftButton.PointerReleased += _resizeHandler.OnResizeEnd;
+        TopRightButton.PointerReleased += _resizeHandler.OnResizeEnd;
+        BottomLeftButton.PointerReleased += _resizeHandler.OnResizeEnd;
+        BottomRightButton.PointerReleased += _resizeHandler.OnResizeEnd;
+        LeftMiddleButton.PointerReleased += _resizeHandler.OnResizeEnd;
+        RightMiddleButton.PointerReleased += _resizeHandler.OnResizeEnd;
+        TopMiddleButton.PointerReleased += _resizeHandler.OnResizeEnd;
+        BottomMiddleButton.PointerReleased += _resizeHandler.OnResizeEnd;
+            
+        LostFocus += OnControlLostFocus;
     }
 
     private void OnControlLostFocus(object? sender, RoutedEventArgs e)