1
1
Ruben 10 сар өмнө
parent
commit
b28da714f9

+ 2 - 1
src/PicView.Avalonia/Crop/CropFunctions.cs

@@ -26,7 +26,8 @@ public static class CropFunctions
         var cropperViewModel = new ImageCropperViewModel(bitmap)
         {
             ImageWidth = size.Width,
-            ImageHeight = size.Height
+            ImageHeight = size.Height,
+            AspectRatio = vm.AspectRatio
         };
         var cropControl = new CropControl
         {

+ 38 - 1
src/PicView.Avalonia/ViewModels/ImageCropperViewModel.cs

@@ -2,6 +2,8 @@
 using Avalonia;
 using Avalonia.Media.Imaging;
 using PicView.Avalonia.Crop;
+using PicView.Avalonia.FileSystem;
+using PicView.Avalonia.Navigation;
 using PicView.Avalonia.UI;
 using PicView.Core.Localization;
 using ReactiveUI;
@@ -15,6 +17,8 @@ public class ImageCropperViewModel : ViewModelBase
         Bitmap = bitmap;
         CropImageCommand  = ReactiveCommand.CreateFromTask(async () =>
         {
+            var croppedBitmap = GetCroppedBitmap();
+            await SaveCroppedImageAsync(croppedBitmap);
             
         });
         CloseCropCommand  = ReactiveCommand.Create(() =>
@@ -73,12 +77,45 @@ public class ImageCropperViewModel : ViewModelBase
         get;
         set => this.RaiseAndSetIfChanged(ref field, value);
     }
+    
+    public double AspectRatio
+    {
+        get;
+        init => this.RaiseAndSetIfChanged(ref field, value);
+    }
 
-    // Call this method when the user completes the selection
     public CroppedBitmap GetCroppedBitmap()
     {
         var sourceRect = new PixelRect((int)SelectionX, (int)SelectionY, (int)SelectionWidth, (int)SelectionHeight);
         var croppedBitmap = new CroppedBitmap(Bitmap, sourceRect);
         return croppedBitmap;
     }
+
+    private async Task SaveCroppedImageAsync(CroppedBitmap croppedBitmap)
+    {
+        if (UIHelper.GetMainView.DataContext is not MainViewModel vm)
+        {
+            return;
+        }
+
+        string fileName;
+        FileInfo fileInfo;
+        if (!NavigationHelper.CanNavigate(vm))
+        {
+            var random = new Random();
+            fileName = $"{TranslationHelper.Translation.Crop} {random.Next(9999)}.png";
+            fileInfo = new FileInfo(fileName);
+        }
+        else
+        {
+            fileName = vm.FileInfo.FullName;
+            fileInfo = vm.FileInfo;
+        }
+
+        var saveFileDialog = await FilePicker.PickFileForSavingAsync(fileName);
+        if (saveFileDialog is null)
+        {
+            return;
+        }
+    }
 }

+ 6 - 0
src/PicView.Avalonia/ViewModels/MainViewModel.cs

@@ -91,6 +91,12 @@ public class MainViewModel : ViewModelBase
         get;
         set => this.RaiseAndSetIfChanged(ref field, value);
     } = double.NaN;
+    
+    public double AspectRatio
+    {
+        get;
+        set => this.RaiseAndSetIfChanged(ref field, value);
+    }
 
     #endregion
 

+ 2 - 2
src/PicView.Avalonia/ViewModels/ViewModelBase.cs

@@ -76,7 +76,7 @@ public class ViewModelBase : ReactiveObject
         Apply = TranslationHelper.Translation.Apply;
         Cancel = TranslationHelper.Translation.Cancel;
         BitDepth = TranslationHelper.Translation.BitDepth;
-        AspectRatio = TranslationHelper.Translation.AspectRatio;
+        ReadAbleAspectRatio = TranslationHelper.Translation.AspectRatio;
         Width = TranslationHelper.Translation.Width;
         Height = TranslationHelper.Translation.Height;
         SizeMp = TranslationHelper.Translation.SizeMp;
@@ -1583,7 +1583,7 @@ public class ViewModelBase : ReactiveObject
         set => this.RaiseAndSetIfChanged(ref field, value);
     }
 
-    public string? AspectRatio
+    public string? ReadAbleAspectRatio
     {
         get;
         set => this.RaiseAndSetIfChanged(ref field, value);

+ 1 - 1
src/PicView.Avalonia/Views/ExifView.axaml

@@ -422,7 +422,7 @@
                         <TextBlock
                             Classes="txt"
                             Margin="0,8,0,0"
-                            Text="{CompiledBinding AspectRatio,
+                            Text="{CompiledBinding ReadAbleAspectRatio,
                                                    Mode=OneWay}"
                             Width="100" />
                         <customControls:FuncTextBox

+ 6 - 5
src/PicView.Avalonia/WindowBehavior/WindowResizing.cs

@@ -159,11 +159,10 @@ public static class WindowResizing
 
         const int padding = 45;
         var screenSize = ScreenHelper.ScreenSize;
-        double desktopMinWidth = 0, desktopMinHeight = 0, containerWidth = 0, containerHeight = 0;
-        desktopMinWidth = desktop.MainWindow.MinWidth;
-        desktopMinHeight = desktop.MainWindow.MinHeight;
-        containerWidth = mainView.Bounds.Width;
-        containerHeight = mainView.Bounds.Height;
+        var desktopMinWidth = desktop.MainWindow.MinWidth;
+        var desktopMinHeight = desktop.MainWindow.MinHeight;
+        var containerWidth = mainView.Bounds.Width;
+        var containerHeight = mainView.Bounds.Height;
 
         if (double.IsNaN(containerWidth) || double.IsNaN(containerHeight) || double.IsNaN(width) ||
             double.IsNaN(height))
@@ -240,6 +239,8 @@ public static class WindowResizing
         {
             vm.GalleryWidth = double.NaN;
         }
+        
+        vm.AspectRatio = size.AspectRatio;
     }
 
     public static void SaveSize(Window window)