Sfoglia il codice sorgente

Fix renaming and resynchronizing when renaming

Ruben 4 mesi fa
parent
commit
ca39d0e708

+ 13 - 3
src/PicView.Avalonia/Gallery/GalleryFunctions.cs

@@ -59,9 +59,19 @@ public static class GalleryFunctions
             }
             galleryItem.FileName.Text = newName;
             galleryItem.FileLocation.Text = newFileLocation;
-            galleryListBox.Items.RemoveAt(oldIndex);
-            galleryListBox.Items.Insert(newIndex, galleryItem);
-            return true;
+            if (oldIndex == newIndex)
+            {
+                galleryListBox.Items[oldIndex] = galleryItem;
+                return true;
+            }
+            if (newIndex >= 0 && newIndex < galleryListBox.Items.Count)
+            {
+                galleryListBox.Items.RemoveAt(oldIndex);
+                galleryListBox.Items.Insert(newIndex, galleryItem);
+                return true;
+            }
+
+            return false;
         }
     }
     

+ 20 - 24
src/PicView.Avalonia/Navigation/ImageIterator.cs

@@ -329,67 +329,63 @@ public class ImageIterator : IAsyncDisposable
     {
         try
         {
-            var index = ImagePaths.FindIndex(x => x.FullName.Equals(e.FullPath));
-            if (index < 0)
+            if (e.FullPath.IsSupported() == false)
             {
                 return;
             }
-            if (e.FullPath.IsSupported() == false)
+            
+            var oldIndex = ImagePaths.FindIndex(x => x.FullName.Equals(e.OldFullPath));
+            if (oldIndex < 0)
             {
-                ImagePaths.RemoveAt(index);
                 return;
             }
 
             _isRunning = true;
-
-            var oldIndex = ImagePaths.FindIndex(x => x.FullName.Equals(e.OldFullPath));;
-            var currentIndex = CurrentIndex;
-            var sameFile = currentIndex == oldIndex;
-            var fileInfo = new FileInfo(e.FullPath);
-            if (fileInfo.Exists == false)
+            
+            var sameFile = CurrentIndex == oldIndex;
+            var newFileInfo = new FileInfo(e.FullPath);
+            if (newFileInfo.Exists == false)
             {
                 return;
             }
 
-            var sourceFileInfo = Settings.Sorting.IncludeSubDirectories
-                ? new FileInfo(_watcher.Path)
-                : fileInfo;
-            var newList = _vm.PlatformService.GetFiles(sourceFileInfo);
+            var newList = _vm.PlatformService.GetFiles(newFileInfo);
             if (newList.Count == 0)
             {
                 return;
             }
 
-            if (fileInfo.Exists == false)
+            if (newFileInfo.Exists == false)
             {
                 return;
             }
 
             ImagePaths = newList;
-
-            if (fileInfo.Exists == false)
-            {
-                return;
-            }
+            var newIndex = ImagePaths.FindIndex(x => x.FullName.Equals(e.FullPath));
 
             if (sameFile)
             {
-                _vm.PicViewer.FileInfo = fileInfo;
+                _vm.PicViewer.FileInfo = newFileInfo;
+                CurrentIndex = newIndex;
             }
 
             TitleManager.SetTitle(_vm);
-            PreLoader.RefreshFileInfo(oldIndex, fileInfo, ImagePaths);
+
+            PreLoader.RefreshFileInfo(newIndex, newFileInfo, ImagePaths);
             Resynchronize();
 
+
+
             _isRunning = false;
             FileHistoryManager.Rename(e.OldFullPath, e.FullPath);
+
             await Dispatcher.UIThread.InvokeAsync(() =>
-                GalleryFunctions.RenameGalleryItem(oldIndex, index, Path.GetFileNameWithoutExtension(e.Name),
+                GalleryFunctions.RenameGalleryItem(oldIndex, newIndex, Path.GetFileNameWithoutExtension(e.Name),
                     e.FullPath,
                     _vm));
             if (sameFile)
             {
-                _vm.SelectedGalleryItemIndex = index;
+                _vm.SelectedGalleryItemIndex = newIndex;
                 GalleryFunctions.CenterGallery(_vm);
             }
         }

+ 8 - 15
src/PicView.Avalonia/Views/UC/EditableTitlebar.axaml.cs

@@ -122,7 +122,14 @@ public partial class EditableTitlebar : UserControl
                 MainKeyboardShortcuts.IsKeysEnabled = true;
                 if (isFileRenamed)
                 {
-                    await FinalizeRenameAsync(vm, newPath);
+                    vm.IsLoading = false;
+                    vm.IsEditableTitlebarOpen = false;
+                    await Dispatcher.UIThread.InvokeAsync(() =>
+                    {
+                        TextBox.ClearSelection();
+                        Cursor = new Cursor(StandardCursorType.Arrow);
+                        UIHelper.GetMainView.Focus();
+                    });
                 }
             });
         }
@@ -159,18 +166,4 @@ public partial class EditableTitlebar : UserControl
         Cursor = new Cursor(StandardCursorType.Ibeam);
         TextBox.Focus();
     }
-    
-    private async Task FinalizeRenameAsync(MainViewModel vm, string newPath)
-    {
-        vm.IsLoading = false;
-        vm.IsEditableTitlebarOpen = false;
-        MainKeyboardShortcuts.IsKeysEnabled = true;
-        await Dispatcher.UIThread.InvokeAsync(() =>
-        {
-            TextBox.ClearSelection();
-            Cursor = new Cursor(StandardCursorType.Arrow);
-            UIHelper.GetMainView.Focus();
-        });
-        await NavigationManager.LoadPicFromFile(newPath, vm);
-    }
 }

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

@@ -159,7 +159,6 @@ public class PreLoader(Func<FileInfo, Task<ImageModel>> imageModelLoader) : IAsy
     ///     Resynchronizes the preload list with the given list of image file paths.
     ///     Moves or removes entries as needed to match the new ordering or contents.
     /// </summary>
-    /// <param name="list">The list of image file paths to sync with.</param>
     /// <remarks>
     ///     Call this method after the file watcher detects changes, or the list is resorted.
     /// </remarks>
@@ -179,10 +178,11 @@ public class PreLoader(Func<FileInfo, Task<ImageModel>> imageModelLoader) : IAsy
         _cancellationTokenSource?.Cancel();
 
         // Create a reverse lookup from file path to current index
-        var reverseLookup = new Dictionary<FileInfo, int>(list.Count);
+        var reverseLookup = new Dictionary<string, int>(list.Count);
+        
         for (var i = 0; i < list.Count; i++)
         {
-            reverseLookup[list[i]] = i;
+            reverseLookup[list[i].FullName] = i;
         }
 
         // Snapshot of current keys to avoid modification during iteration
@@ -202,7 +202,7 @@ public class PreLoader(Func<FileInfo, Task<ImageModel>> imageModelLoader) : IAsy
                 continue;
             }
 
-            if (!reverseLookup.TryGetValue(file, out var newIndex))
+            if (!reverseLookup.TryGetValue(file.FullName, out var newIndex))
             {
                 // File no longer exists in the list
                 Remove(oldIndex, list);
@@ -456,10 +456,7 @@ public class PreLoader(Func<FileInfo, Task<ImageModel>> imageModelLoader) : IAsy
         _preLoadList.Clear();
 
 #if DEBUG
-        if (_showAddRemove)
-        {
-            Trace.WriteLine("Preloader cleared");
-        }
+        Trace.WriteLine("Preloader cleared");
 #endif
     }