Browse Source

Fix `Optimize image` button not properly disabling in the image info view, when unsupported.

Refactor bindings for `ShouldCropBeEnabled` and `ShouldOptimizeImageBeEnabled`, relocate properties from `GlobalSettingsViewModel` to `PicViewerModel`. Enhance image optimization workflow with loading indicator and update file size display logic.
Ruben 3 months ago
parent
commit
801b385daf

+ 2 - 0
src/PicView.Avalonia.MacOS/Views/ExifWindow.axaml

@@ -61,6 +61,8 @@
                 Icon="{StaticResource PortalImage}"
                 IconHeight="17"
                 IconWidth="17"
+                IsEnabled="{CompiledBinding PicViewer.ShouldOptimizeImageBeEnabled.Value,
+                                            Mode=OneWay}"
                 ToolTip.Tip="{CompiledBinding Translation.OptimizeImage.Value,
                                               Mode=OneWay}"
                 Width="45"

+ 2 - 0
src/PicView.Avalonia.Win32/Views/ExifWindow.axaml

@@ -121,6 +121,8 @@
                     Icon="{StaticResource PortalImage}"
                     IconHeight="17"
                     IconWidth="17"
+                    IsEnabled="{CompiledBinding PicViewer.ShouldOptimizeImageBeEnabled.Value,
+                                                Mode=OneWay}"
                     ToolTip.Tip="{CompiledBinding Translation.OptimizeImage.Value,
                                                   Mode=OneWay}"
                     Width="45"

+ 4 - 4
src/PicView.Avalonia/Converters/ConversionHelper.cs

@@ -30,7 +30,7 @@ internal static class ConversionHelper
     {
         if (vm.PicViewer.FileInfo is null)
         {
-            vm.GlobalSettings.ShouldOptimizeImageBeEnabled.Value = false;
+            vm.PicViewer.ShouldOptimizeImageBeEnabled.Value = false;
             return;
         }
 
@@ -41,17 +41,17 @@ internal static class ConversionHelper
                 || vm.PicViewer.FileInfo.CurrentValue.Extension.Equals(".png", StringComparison.InvariantCultureIgnoreCase)
                 || vm.PicViewer.FileInfo.CurrentValue.Extension.Equals(".gif", StringComparison.InvariantCultureIgnoreCase))
             {
-                vm.GlobalSettings.ShouldOptimizeImageBeEnabled.Value = true;
+                vm.PicViewer.ShouldOptimizeImageBeEnabled.Value = true;
             }
             else
             {
-                vm.GlobalSettings.ShouldOptimizeImageBeEnabled.Value = false;
+                vm.PicViewer.ShouldOptimizeImageBeEnabled.Value = false;
             }
         }
         catch (Exception e)
         {
             DebugHelper.LogDebug(nameof(ConversionHelper), nameof(DetermineIfOptimizeImageShouldBeEnabled), e);
-            vm.GlobalSettings.ShouldOptimizeImageBeEnabled.Value = false;
+            vm.PicViewer.ShouldOptimizeImageBeEnabled.Value = false;
         }
     }
 }

+ 3 - 3
src/PicView.Avalonia/Crop/CropFunctions.cs

@@ -119,7 +119,7 @@ public static class CropFunctions
 
         if (vm?.PicViewer.ImageSource.CurrentValue is not Bitmap || Settings.ImageScaling.ShowImageSideBySide)
         {
-            vm.GlobalSettings.ShouldCropBeEnabled.Value = false;
+            vm.PicViewer.ShouldCropBeEnabled.Value = false;
             return false;
         }
 
@@ -135,11 +135,11 @@ public static class CropFunctions
 
         if (vm.GlobalSettings.RotationAngle.CurrentValue is 0 && vm.PicViewer.ScaleX.CurrentValue is 1)
         {
-            vm.GlobalSettings.ShouldCropBeEnabled.Value = true;
+            vm.PicViewer.ShouldCropBeEnabled.Value = true;
             return true;
         }
 
-        vm.GlobalSettings.ShouldCropBeEnabled.Value = false;
+        vm.PicViewer.ShouldCropBeEnabled.Value = false;
         return false;
     }
 }

+ 23 - 16
src/PicView.Avalonia/ImageHandling/ImageOptimizer.cs

@@ -20,24 +20,31 @@ public static class ImageOptimizer
         {
             return;
         }
-        
-        await Task.Run(() =>
+
+        try
         {
-            try
+            vm.MainWindow.IsLoadingIndicatorShown.Value = true;
+            await Task.Run(() =>
             {
-                var optimizer = new ImageMagick.ImageOptimizer
+                try
                 {
-                    OptimalCompression = true
-                };
-                optimizer.LosslessCompress(vm.PicViewer.FileInfo.CurrentValue.FullName);
-            }
-            catch (Exception ex)
-            {
-                DebugHelper.LogDebug(nameof(ImageOptimizer), nameof(OptimizeImageAsync), ex);
-            }
-        });
-        await NavigationManager.QuickReload();
-        
-        TitleManager.SetTitle(vm);
+                    var optimizer = new ImageMagick.ImageOptimizer
+                    {
+                        OptimalCompression = true
+                    };
+                    optimizer.LosslessCompress(vm.PicViewer.FileInfo.CurrentValue.FullName);
+                }
+                catch (Exception ex)
+                {
+                    DebugHelper.LogDebug(nameof(ImageOptimizer), nameof(OptimizeImageAsync), ex);
+                }
+            });
+            await NavigationManager.QuickReload();
+        }
+        finally
+        {
+            TitleManager.SetTitle(vm);
+            vm.MainWindow.IsLoadingIndicatorShown.Value = false;
+        }
     }
 }

+ 3 - 1
src/PicView.Avalonia/Navigation/ImageIterator.cs

@@ -528,7 +528,9 @@ public class ImageIterator : IAsyncDisposable
     public async Task QuickReload()
     {
         RemoveCurrentItemFromPreLoader();
-        _vm.PicViewer.FileInfo.Value = new FileInfo(_vm.PicViewer.FileInfo.CurrentValue.FullName);
+        var newFileInfo = new FileInfo(_vm.PicViewer.FileInfo.CurrentValue.FullName);
+        ImagePaths[CurrentIndex] = newFileInfo;
+        _vm.PicViewer.FileInfo.Value = newFileInfo;
         await IterateToIndex(CurrentIndex, new CancellationTokenSource()).ConfigureAwait(false);
     }
 

+ 30 - 1
src/PicView.Avalonia/Views/ImageInfoView.axaml

@@ -247,6 +247,7 @@
                     <Image
                         Height="12"
                         Source="{StaticResource FileUserImage}"
+                        Stretch="Fill"
                         Width="12" />
                 </MenuItem.Icon>
             </MenuItem>
@@ -256,7 +257,7 @@
                 Command="{CompiledBinding Tools.OptimizeImageCommand}"
                 Header="{CompiledBinding Translation.OptimizeImage.Value,
                                          Mode=OneWay}"
-                IsEnabled="{CompiledBinding GlobalSettings.ShouldOptimizeImageBeEnabled.Value,
+                IsEnabled="{CompiledBinding PicViewer.ShouldOptimizeImageBeEnabled.Value,
                                             Mode=OneWay}">
                 <MenuItem.Icon>
                     <Image
@@ -522,6 +523,32 @@
                         </customControls:CopyButton>
                     </StackPanel>
 
+                    <!--  File Size  -->
+                    <StackPanel Margin="15,10" Orientation="Horizontal">
+                        <TextBlock
+                            Classes="txt"
+                            Margin="0,8,0,0"
+                            Text="{CompiledBinding Translation.FileSize.Value,
+                                                   Mode=OneWay}"
+                            Width="100" />
+                        <customControls:FuncTextBox
+                            Classes="TStyle x"
+                            IsReadOnly="True"
+                            x:Name="FileSizeBox" />
+                        <customControls:CopyButton
+                            Classes="altHover BorderStyle"
+                            CopyText="{Binding Path=Text, ElementName=FileSizeBox}"
+                            ToolTip.Tip="{CompiledBinding Translation.Copy.Value,
+                                                          Mode=OneWay}">
+                            <Path
+                                Data="{StaticResource CopyGeometry}"
+                                Fill="{StaticResource SecondaryTextColor}"
+                                Height="16"
+                                Stretch="Fill"
+                                Width="16" />
+                        </customControls:CopyButton>
+                    </StackPanel>
+
                     <!--  Created  -->
                     <StackPanel Margin="15,10" Orientation="Horizontal">
                         <TextBlock
@@ -633,6 +660,7 @@
                                 Width="16" />
                         </customControls:CopyButton>
                     </StackPanel>
+
                 </StackPanel>
 
                 <StackPanel Width="347">
@@ -845,6 +873,7 @@
                                 Width="16" />
                         </customControls:CopyButton>
                     </StackPanel>
+
                 </StackPanel>
 
                 <!--  Convert To  -->

+ 3 - 0
src/PicView.Avalonia/Views/ImageInfoView.axaml.cs

@@ -8,6 +8,7 @@ using PicView.Avalonia.Navigation;
 using PicView.Avalonia.Resizing;
 using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
+using PicView.Core.Extensions;
 using PicView.Core.Titles;
 using R3;
 
@@ -73,6 +74,8 @@ public partial class ImageInfoView : UserControl
                 {
                     DirectoryNameTextBox.Text = x.DirectoryName;
                 }
+                FileSizeBox.Text = vm.PicViewer.FileInfo?.CurrentValue?.Length.GetReadableFileSize();
+                ConversionHelper.DetermineIfOptimizeImageShouldBeEnabled(vm);
             }).AddTo(_disposables);
             
             ResetButton.Click += (_, _) =>

+ 2 - 2
src/PicView.Avalonia/Views/MainView.axaml

@@ -768,7 +768,7 @@
                     Command="{CompiledBinding Tools.OptimizeImageCommand}"
                     Header="{CompiledBinding Translation.OptimizeImage.Value,
                                              Mode=OneWay}"
-                    IsEnabled="{CompiledBinding GlobalSettings.ShouldOptimizeImageBeEnabled.Value,
+                    IsEnabled="{CompiledBinding PicViewer.ShouldOptimizeImageBeEnabled.Value,
                                                 Mode=OneWay}">
                     <MenuItem.Icon>
                         <Image
@@ -785,7 +785,7 @@
                     Command="{CompiledBinding Tools.CropCommand}"
                     Header="{CompiledBinding Translation.Crop.Value,
                                              Mode=OneWay}"
-                    IsEnabled="{CompiledBinding GlobalSettings.ShouldCropBeEnabled.Value,
+                    IsEnabled="{CompiledBinding PicViewer.ShouldCropBeEnabled.Value,
                                                 Mode=OneWay}"
                     x:Name="CropMenuItem">
                     <MenuItem.Icon>

+ 1 - 1
src/PicView.Avalonia/Views/UC/Menus/ImageMenu.axaml

@@ -244,7 +244,7 @@
                     Classes="ButtonBorder altHover"
                     Command="{CompiledBinding Tools.CropCommand}"
                     Height="46"
-                    IsEnabled="{CompiledBinding GlobalSettings.ShouldCropBeEnabled.Value,
+                    IsEnabled="{CompiledBinding PicViewer.ShouldCropBeEnabled.Value,
                                                 Mode=OneWay}"
                     ToolTip.Tip="{CompiledBinding Translation.Crop,
                                                   Mode=OneWay}">

+ 1 - 1
src/PicView.Avalonia/Views/UC/Menus/ToolsMenu.axaml

@@ -155,7 +155,7 @@
                     Classes="ButtonBorder altHover"
                     Command="{CompiledBinding Tools.OptimizeImageCommand}"
                     Height="46"
-                    IsEnabled="{CompiledBinding GlobalSettings.ShouldOptimizeImageBeEnabled.Value,
+                    IsEnabled="{CompiledBinding PicViewer.ShouldOptimizeImageBeEnabled.Value,
                                                 Mode=OneWay}"
                     ToolTip.Tip="{CompiledBinding Translation.OptimizeImage.Value,
                                                   Mode=OneWay}"

+ 3 - 3
src/PicView.Core/ViewModels/ExifViewModel.cs

@@ -18,7 +18,7 @@ public class ExifViewModel : IDisposable
         SetExifRating4Command = new ReactiveCommand<string>(Set4Star);
         SetExifRating5Command = new ReactiveCommand<string>(Set5Star);            
     }
-
+    
     private void Set0Star(string value)
     {
         EXIFHelper.SetEXIFRating(value, 0);
@@ -108,8 +108,8 @@ public class ExifViewModel : IDisposable
             LensMaker,
             LensModel);
     }
-    public ReactiveCommand? OpenGoogleLinkCommand { get; set; }
-    public ReactiveCommand? OpenBingLinkCommand { get; set; }
+    public ReactiveCommand? OpenGoogleLinkCommand { get; }
+    public ReactiveCommand? OpenBingLinkCommand { get; }
     
     public ReactiveCommand<string>? SetExifRating0Command { get; set; }
     public ReactiveCommand<string>? SetExifRating1Command { get; set; }

+ 0 - 4
src/PicView.Core/ViewModels/GlobalSettingsViewModel.cs

@@ -21,9 +21,5 @@ public class GlobalSettingsViewModel
 
     public BindableReactiveProperty<double> ZoomValue { get; } = new();
     
-    public BindableReactiveProperty<bool> ShouldCropBeEnabled { get; } = new();
-    
-    public BindableReactiveProperty<bool> ShouldOptimizeImageBeEnabled { get; } = new();
-    
     public BindableReactiveProperty<bool> IsShowingTaskbarProgress { get; } = new(Settings.UIProperties.IsTaskbarProgressEnabled);
 }

+ 4 - 0
src/PicView.Core/ViewModels/PicViewerModel.cs

@@ -71,4 +71,8 @@ public class PicViewerModel : IDisposable
     public BindableReactiveProperty<int> GetIndex { get; } = new();
     
     public BindableReactiveProperty<bool> IsSingleImage { get; } = new();
+    
+    public BindableReactiveProperty<bool> ShouldCropBeEnabled { get; } = new();
+    
+    public BindableReactiveProperty<bool> ShouldOptimizeImageBeEnabled { get; } = new();
 }