Pārlūkot izejas kodu

Optimization and bug fixes when navigating with key held down

Ruben 4 gadi atpakaļ
vecāks
revīzija
e5202cf5f1

+ 42 - 13
PicView/ChangeImage/FastPic.cs

@@ -1,9 +1,7 @@
 using PicView.ImageHandling;
-using PicView.UILogic;
 using System.IO;
 using System.Threading.Tasks;
 using System.Windows.Media.Imaging;
-using System.Windows.Threading;
 using static PicView.ChangeImage.Navigation;
 
 namespace PicView.ChangeImage
@@ -11,6 +9,7 @@ namespace PicView.ChangeImage
     internal static class FastPic
     {
         static System.Timers.Timer? timer;
+        static bool updateSource;
 
         internal static async Task Run(int index)
         {
@@ -31,7 +30,7 @@ namespace PicView.ChangeImage
             timer.Start();
             FileInfo? fileInfo = null;
             BitmapSource? pic = null;
-            bool fitImage = true;
+            updateSource = false;
 
             var preloadValue = Preloader.Get(Pics[index]);
             if (preloadValue != null)
@@ -63,7 +62,7 @@ namespace PicView.ChangeImage
                 else
                 {
                     pic = Thumbnails.GetBitmapSourceThumb(fileInfo);
-                    fitImage = false;
+                    updateSource = true; // Update it when key released
                 }
             }
 
@@ -76,22 +75,52 @@ namespace PicView.ChangeImage
                     return;
                 }
             }
+            LoadPic.UpdatePic(FolderIndex, pic);
 
-            await ConfigureWindows.GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Render, () =>
+            if (Preloader.IsRunning is false)
             {
-                SetTitle.SetTitleString(pic.PixelWidth, pic.PixelHeight, index, fileInfo);
-                ConfigureWindows.GetMainWindow.MainImage.Source = pic;
+                await Preloader.PreLoad(FolderIndex).ConfigureAwait(false);
+            }
+        }
+
+        /// <summary>
+        /// Update after FastPic() was used
+        /// </summary>
+        internal static async Task FastPicUpdateAsync()
+        {
+            if (updateSource == false) { return; }
+
+            // Update picture in case it didn't load. Won't happen normally
 
-                if (fitImage)
+            timer = null;
+            BitmapSource? pic = null;
+            Preloader.PreloadValue? preloadValue = null;
+
+            var exists = await Preloader.AddAsync(FolderIndex).ConfigureAwait(false);
+            if (exists)
+            {
+                preloadValue = Preloader.Get(Pics[FolderIndex]);
+                if (preloadValue is null)
                 {
-                    UILogic.Sizing.ScaleImage.FitImage(pic.PixelWidth, pic.PixelHeight);
+                    await Error_Handling.ReloadAsync().ConfigureAwait(false);
+                    return;
                 }
-            });
-
-            if (Preloader.IsRunning is false)
+                if (preloadValue.bitmapSource is not null)
+                {
+                    pic = preloadValue.bitmapSource;
+                }
+                else
+                {
+                    pic = ImageFunctions.ImageErrorMessage();
+                }
+            }
+            else
             {
-                await Preloader.PreLoad(FolderIndex).ConfigureAwait(false);
+                await Error_Handling.ReloadAsync().ConfigureAwait(false);
+                return;
             }
+
+            LoadPic.UpdatePic(FolderIndex, pic, preloadValue.fileInfo);
         }
     }
 }

+ 5 - 3
PicView/ChangeImage/Preloader.cs

@@ -40,9 +40,9 @@ namespace PicView.ChangeImage
         /// Add file to preloader from index
         /// </summary>
         /// <param name="i">Index of Pics</param>
-        internal static async Task AddAsync(int i, FileInfo? fileInfo = null, BitmapSource? bitmapSource = null)
+        internal static async Task<bool> AddAsync(int i, FileInfo? fileInfo = null, BitmapSource? bitmapSource = null)
         {
-            if (i >= Pics?.Count) { return; }
+            if (i >= Pics?.Count) { return false; }
 
             if (i < 0)
             {
@@ -50,7 +50,7 @@ namespace PicView.ChangeImage
             }
 
             var preloadValue = new PreloadValue(null, true, null);
-            if (preloadValue is null) { return; }
+            if (preloadValue is null) { return false; }
 
             if (Sources.TryAdd(Navigation.Pics[i], preloadValue))
             {
@@ -68,7 +68,9 @@ namespace PicView.ChangeImage
                     preloadValue.isLoading = false;
                     preloadValue.fileInfo = fileInfo;
                 }).ConfigureAwait(false);
+                return true;
             }
+            return false;
         }
 
         /// <summary>

+ 6 - 8
PicView/ImageHandling/ImageFunctions.cs

@@ -21,18 +21,16 @@ namespace PicView.ImageHandling
 
             try
             {
-                using (MagickImage image = new MagickImage(Navigation.Pics[Navigation.FolderIndex]))
-                {
-                    var profile = image.GetExifProfile();
-                    profile.SetValue(ExifTag.Rating, rating);
+                using MagickImage image = new MagickImage(Navigation.Pics[Navigation.FolderIndex]);
+                var profile = image.GetExifProfile();
+                profile.SetValue(ExifTag.Rating, rating);
 
-                    image.SetProfile(profile);
+                image.SetProfile(profile);
 
-                    image.Write(Navigation.Pics[Navigation.FolderIndex]);
-                }
+                image.Write(Navigation.Pics[Navigation.FolderIndex]);
                 return true;
             }
-            catch (System.Exception)
+            catch (MagickException)
             {
                 return false;
             }

+ 16 - 0
PicView/Shortcuts/MainKeyboardShortcuts.cs

@@ -538,6 +538,22 @@ namespace PicView.Shortcuts
             // Don't allow keys when typing in text
             if (GetMainWindow.TitleText.IsKeyboardFocusWithin) { return; }
 
+            switch (e.Key)
+            {
+                case Key.A:
+                case Key.Right:
+                case Key.Left:
+                case Key.D:
+                    if (FolderIndex <= 0 || Pics?.Count < FolderIndex)
+                    {
+                        return;
+                    }
+                    _ = ChangeImage.FastPic.FastPicUpdateAsync().ConfigureAwait(false);
+                    return;
+
+                default: break;
+            }
+
             var altDown = (Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt;
             if (altDown && !e.IsRepeat)
             {