Ver código fonte

Fix incorrect image disposal at startup and fix printing shortcut not working properly.

Refine file handling in `FunctionsMapper` and improve eviction logic in `Preloader`

- Fixed `null` handling for `CurrentValue?.FullName` in multiple `FunctionsMapper` methods (`Print`, `OpenWith`, `OpenInExplorer`, etc.).
- Updated `Preloader` to conditionally invoke `ImageDisposalHelper` only on successful addition (`TryAdd` eviction handling).
- Removed unnecessary validation in `FileManager.Print`.
- Corrected eviction logic in `EvictingDictionary` (`_dictionary.Count > _maxSize`).
Ruben 1 semana atrás
pai
commit
d12c756dd9

+ 0 - 5
src/PicView.Avalonia/FileSystem/FileManager.cs

@@ -88,11 +88,6 @@ public static class FileManager
     /// </summary>
     public static async Task Print(string? path, MainViewModel vm)
     {
-        if (!ValidateParameters(path, vm.PlatformService))
-        {
-            return;
-        }
-
         try
         {
             vm.MainWindow.IsLoadingIndicatorShown.Value = true;

+ 11 - 5
src/PicView.Avalonia/Functions/FunctionsMapper.cs

@@ -527,7 +527,7 @@ public static class FunctionsMapper
     
     /// <inheritdoc cref="FileManager.Print(string, MainViewModel)" />
     public static async ValueTask Print() =>
-        await FileManager.Print(null, Vm).ConfigureAwait(false);
+        await FileManager.Print(Vm.PicViewer.FileInfo?.CurrentValue?.FullName, Vm).ConfigureAwait(false);
 
     /// <inheritdoc cref="FilePicker.SelectAndLoadFile(MainViewModel)" />
     public static async ValueTask Open() =>
@@ -535,11 +535,13 @@ public static class FunctionsMapper
 
     /// <inheritdoc cref="FileManager.OpenWith(string, MainViewModel)" />
     public static async ValueTask OpenWith() =>
-        await Task.Run(() => Vm?.PlatformService?.OpenWith(Vm.PicViewer.FileInfo?.CurrentValue.FullName)).ConfigureAwait(false);
+        await Task.Run(() => Vm?.PlatformService?.OpenWith(Vm.PicViewer.FileInfo?.CurrentValue?.FullName))
+            .ConfigureAwait(false);
     
     /// <inheritdoc cref="FileManager.LocateOnDisk(string, MainViewModel)" />
     public static async ValueTask OpenInExplorer()=>
-        await Task.Run(() => Vm?.PlatformService?.LocateOnDisk(Vm.PicViewer.FileInfo?.CurrentValue.FullName)).ConfigureAwait(false);
+        await Task.Run(() => Vm?.PlatformService?.LocateOnDisk(Vm.PicViewer.FileInfo?.CurrentValue?.FullName))
+            .ConfigureAwait(false);
 
     /// <inheritdoc cref="FileSaverHelper.SaveCurrentFile(MainViewModel)" />
     public static async ValueTask Save() =>
@@ -551,11 +553,15 @@ public static class FunctionsMapper
     
     /// <inheritdoc cref="FileManager.DeleteFileWithOptionalDialog" />
     public static async ValueTask DeleteFile() =>
-        await FileManager.DeleteFileWithOptionalDialog(true, Vm.PicViewer?.FileInfo?.CurrentValue.FullName, Vm.PlatformService).ConfigureAwait(false);
+        await FileManager
+            .DeleteFileWithOptionalDialog(true, Vm.PicViewer?.FileInfo?.CurrentValue?.FullName, Vm.PlatformService)
+            .ConfigureAwait(false);
     
     /// <inheritdoc cref="FileManager.DeleteFileWithOptionalDialog" />
     public static async ValueTask DeleteFilePermanently() =>
-        await FileManager.DeleteFileWithOptionalDialog(false, Vm.PicViewer?.FileInfo?.CurrentValue.FullName, Vm.PlatformService).ConfigureAwait(false);
+        await FileManager
+            .DeleteFileWithOptionalDialog(false, Vm.PicViewer?.FileInfo?.CurrentValue?.FullName, Vm.PlatformService)
+            .ConfigureAwait(false);
 
     public static async ValueTask Rename()
     {

+ 3 - 3
src/PicView.Core/Preloading/EvictingDictionary.cs

@@ -1,5 +1,4 @@
-using System.Buffers;
-using System.Collections;
+using System.Collections;
 using System.Diagnostics.CodeAnalysis;
 using PicView.Core.DebugTools;
 
@@ -94,7 +93,8 @@ public class EvictingDictionary<TValue> : IEnumerable<KeyValuePair<int, TValue>>
                 evictedValue = default;
                 return false;
             }
-            if (_dictionary.Count >= _maxSize)
+
+            if (_dictionary.Count > _maxSize)
             {
                 // Looping Eviction Logic: Find the key farthest away from the current index.
                 var keyToEvict = -1;

+ 5 - 2
src/PicView.Core/Preloading/Preloader.cs

@@ -59,11 +59,14 @@ public class PreLoader(Func<FileInfo, ValueTask<ImageModel>> imageModelLoader) :
         var fileInfo = list[index];
         var preLoadValue = new PreLoadValue(new ImageModel { FileInfo = fileInfo }, isLoading: true);
         ct.ThrowIfCancellationRequested();
-        _preLoadList.TryAdd(index, preLoadValue, list.Count, isReverse, out var evictedValue);
+        var evicted = _preLoadList.TryAdd(index, preLoadValue, list.Count, isReverse, out var evictedValue);
 
         try
         {
-            ImageDisposalHelper(evictedValue);
+            if (evicted)
+            {
+                ImageDisposalHelper(evictedValue);
+            }
 
             var imageModel = await imageModelLoader(fileInfo).ConfigureAwait(false);