Parcourir la source

[Avalonia] Implement logic for the setting: `Show file saving dialog`, refactor, misc.

Ruben il y a 1 an
Parent
commit
3ae930072e

+ 7 - 0
src/PicView.Avalonia.MacOS/Views/SettingsWindow.axaml.cs

@@ -1,5 +1,6 @@
 using Avalonia.Controls;
 using Avalonia.Input;
+using PicView.Core.Config;
 using PicView.Core.Localization;
 
 namespace PicView.Avalonia.MacOS.Views;
@@ -22,6 +23,12 @@ public partial class SettingsWindow : Window
                 Close();
             }
         };
+        
+        Closing += async delegate
+        {
+            Hide();
+            await SettingsHelper.SaveSettingsAsync();
+        };
     }
 
     private void MoveWindow(object? sender, PointerPressedEventArgs e)

+ 7 - 0
src/PicView.Avalonia.Win32/Views/SettingsWindow.axaml.cs

@@ -1,6 +1,7 @@
 using Avalonia.Controls;
 using Avalonia.Input;
 using Avalonia.Interactivity;
+using PicView.Core.Config;
 using PicView.Core.Localization;
 
 namespace PicView.Avalonia.Win32.Views;
@@ -23,6 +24,12 @@ public partial class SettingsWindow : Window
                 Close();
             }
         };
+
+        Closing += async delegate
+        {
+            Hide();
+            await SettingsHelper.SaveSettingsAsync();
+        };
     }
 
     private void MoveWindow(object? sender, PointerPressedEventArgs e)

+ 7 - 51
src/PicView.Avalonia/FileSystem/FilePickerHelper.cs

@@ -1,13 +1,10 @@
 using System.Runtime.InteropServices;
 using Avalonia;
 using Avalonia.Controls.ApplicationLifetimes;
-using Avalonia.Media.Imaging;
 using Avalonia.Platform.Storage;
-using PicView.Avalonia.Navigation;
 using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
 using PicView.Core.FileHandling;
-using PicView.Core.ImageDecoding;
 using PicView.Core.Localization;
 
 namespace PicView.Avalonia.FileSystem;
@@ -66,7 +63,7 @@ public static class FilePickerHelper
         MimeTypes = new[] { "archive/*" }
     };
 
-    public static async Task SaveFileAsync(string? fileName, MainViewModel vm)
+    public static async Task PickAndSaveFileAsAsync(string? fileName, MainViewModel vm)
     {
         if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop ||
             desktop.MainWindow?.StorageProvider is not { } provider)
@@ -90,54 +87,13 @@ public static class FilePickerHelper
             file = await provider.SaveFilePickerAsync(options);
         }
 
-        var path = file.Path.AbsolutePath;
-        if (!string.IsNullOrWhiteSpace(fileName))
+        if (file is null)
         {
-            await SaveImageFileHelper.SaveImageAsync(null,
-                fileName,
-                path,
-                null,
-                null,
-                null,
-                Path.GetExtension(path),
-                vm.RotationAngle);
-        }
-        else
-        {
-            switch (vm.ImageType)
-            {
-                case ImageType.AnimatedGif:
-                case ImageType.AnimatedWebp:
-                    throw new ArgumentOutOfRangeException();
-                case ImageType.Bitmap:
-                    if (vm.ImageSource is not Bitmap bitmap)
-                    {
-                        throw new ArgumentOutOfRangeException();
-                    }
-                    var stream = new FileStream(path, FileMode.Create);
-                    const uint quality = 100;
-                    bitmap.Save(stream, (int)quality);
-                    await stream.DisposeAsync();
-                    var ext = Path.GetExtension(path);
-                    if (ext != ".jpg" || ext != ".jpeg" || ext != ".png" || ext != ".bmp" || vm.RotationAngle != 0)
-                    {
-                        await SaveImageFileHelper.SaveImageAsync(
-                            null,
-                            path,
-                            destination:path,
-                            width: null,
-                            height: null,
-                            quality,
-                            ext,
-                            vm.RotationAngle);
-                    }
-                    
-                    break;
-                case ImageType.Svg:
-                    throw new ArgumentOutOfRangeException();
-                default:
-                    throw new ArgumentOutOfRangeException();
-            }
+            // User exited
+            return;
         }
+
+        var destination = file.Path.AbsolutePath;
+        await FileSaverHelper.SaveFileAsync(fileName, destination, vm);
     }
 }

+ 94 - 0
src/PicView.Avalonia/FileSystem/FileSaverHelper.cs

@@ -0,0 +1,94 @@
+using Avalonia.Media.Imaging;
+using PicView.Avalonia.Navigation;
+using PicView.Avalonia.ViewModels;
+using PicView.Core.Config;
+using PicView.Core.ImageDecoding;
+
+namespace PicView.Avalonia.FileSystem;
+
+public static class FileSaverHelper
+{
+    public static async Task SaveCurrentFile(MainViewModel vm)
+    {
+        if (vm is null)
+        {
+            return;
+        }
+        
+        if (SettingsHelper.Settings.UIProperties.ShowFileSavingDialog)
+        {
+            if (vm.FileInfo is null)
+            {
+                await SaveFileAsync(null, vm.FileInfo.FullName, vm);
+            }
+            else
+            {
+                await FilePickerHelper.PickAndSaveFileAsAsync(vm.FileInfo.FullName, vm);
+            }
+            
+        }
+        else
+        {
+            if (vm.FileInfo is null)
+            {
+                await SaveFileAsync(null, vm.FileInfo.FullName, vm);
+            }
+            else
+            {
+                await SaveFileAsync(vm.FileInfo.FullName, vm.FileInfo.FullName, vm);
+            }
+        }
+    }
+
+    public static async Task SaveFileAsync(string? filename, string destination, MainViewModel vm)
+    {
+        if (!string.IsNullOrWhiteSpace(filename))
+        {
+            await SaveImageFileHelper.SaveImageAsync(null,
+                filename,
+                destination,
+                null,
+                null,
+                null,
+                Path.GetExtension(destination),
+                vm.RotationAngle);
+        }
+        else
+        {
+            switch (vm.ImageType)
+            {
+                case ImageType.AnimatedGif:
+                case ImageType.AnimatedWebp:
+                    throw new ArgumentOutOfRangeException();
+                case ImageType.Bitmap:
+                    if (vm.ImageSource is not Bitmap bitmap)
+                    {
+                        throw new ArgumentOutOfRangeException();
+                    }
+                    var stream = new FileStream(destination, FileMode.Create);
+                    const uint quality = 100;
+                    bitmap.Save(stream, (int)quality);
+                    await stream.DisposeAsync();
+                    var ext = Path.GetExtension(destination);
+                    if (ext != ".jpg" || ext != ".jpeg" || ext != ".png" || ext != ".bmp" || vm.RotationAngle != 0)
+                    {
+                        await SaveImageFileHelper.SaveImageAsync(
+                            null,
+                            destination,
+                            destination:destination,
+                            width: null,
+                            height: null,
+                            quality,
+                            ext,
+                            vm.RotationAngle);
+                    }
+                    
+                    break;
+                case ImageType.Svg:
+                    throw new ArgumentOutOfRangeException();
+                default:
+                    throw new ArgumentOutOfRangeException();
+            }
+        }
+    }
+}

+ 1 - 5
src/PicView.Avalonia/UI/FunctionsHelper.cs

@@ -605,11 +605,7 @@ public static class FunctionsHelper
 
     public static async Task Save()
     {
-        if (Vm is null)
-        {
-            return;
-        }
-        await FilePickerHelper.SaveFileAsync(Vm.FileInfo?.FullName, Vm);
+        await FileSaverHelper.SaveCurrentFile(Vm);
     }
     
     public static async Task DeleteFile()

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

@@ -616,7 +616,6 @@ public class MainViewModel : ViewModelBase
             this.RaiseAndSetIfChanged(ref _isStretched, value);
             SettingsHelper.Settings.ImageScaling.StretchImage = value;
             WindowHelper.SetSize(this);
-            _ = SettingsHelper.SaveSettingsAsync();
         }
     }
 
@@ -645,7 +644,6 @@ public class MainViewModel : ViewModelBase
         {
             this.RaiseAndSetIfChanged(ref _isCtrlToZoomEnabled, value);
             SettingsHelper.Settings.Zoom.CtrlZoom = value;
-            _ = SettingsHelper.SaveSettingsAsync();
         }
     }
 
@@ -658,7 +656,6 @@ public class MainViewModel : ViewModelBase
         {
             this.RaiseAndSetIfChanged(ref _isNavigatingInReverse, value);
             SettingsHelper.Settings.Zoom.HorizontalReverseScroll = value;
-            _ = SettingsHelper.SaveSettingsAsync();
         }
     }
 
@@ -671,7 +668,6 @@ public class MainViewModel : ViewModelBase
         {
             this.RaiseAndSetIfChanged(ref _isOpeningLastFileOnStartup, value);
             SettingsHelper.Settings.StartUp.OpenLastFile = value;
-            _ = SettingsHelper.SaveSettingsAsync();
         }
     }
 
@@ -684,7 +680,6 @@ public class MainViewModel : ViewModelBase
         {
             this.RaiseAndSetIfChanged(ref _isStayingCentered, value);
             SettingsHelper.Settings.WindowProperties.KeepCentered = value;
-            _ = SettingsHelper.SaveSettingsAsync();
         }
     }
 
@@ -697,7 +692,6 @@ public class MainViewModel : ViewModelBase
         {
             this.RaiseAndSetIfChanged(ref _isFileSavingDialogShown, value);
             SettingsHelper.Settings.UIProperties.ShowFileSavingDialog = value;
-            _ = SettingsHelper.SaveSettingsAsync();
         }
     }
 

+ 2 - 4
src/PicView.Avalonia/Views/GeneralSettingsView.axaml

@@ -98,8 +98,7 @@
             Background="Transparent"
             BorderThickness="0"
             Classes="altHover"
-            IsChecked="{CompiledBinding IsFileSavingDialogShown,
-                                        Mode=OneWay}"
+            IsChecked="{CompiledBinding IsFileSavingDialogShown}"
             Margin="0,0,0,3"
             Width="270">
             <TextBlock
@@ -167,8 +166,7 @@
             <ComboBoxItem
                 Content="{CompiledBinding Reverse,
                                           Mode=OneWay}"
-                IsSelected="{CompiledBinding IsNavigatingInReverse,
-                                             Mode=OneWay}"
+                IsSelected="{CompiledBinding IsNavigatingInReverse}"
                 x:Name="ReverseDirectionBoxItem" />
             <ComboBoxItem Content="{CompiledBinding Forward, Mode=OneWay}" x:Name="ForwardDirectionBoxItem" />
         </ComboBox>