Forráskód Böngészése

Fix incorrect startup size when image has a 1:1 ratio, and certain circumstances of incorrect size at startup as well. Refactor.

Ruben 7 hónapja
szülő
commit
cae1bb07cb

+ 40 - 33
src/PicView.Avalonia/StartUp/QuickLoad.cs

@@ -49,14 +49,28 @@ public static class QuickLoad
             vm.SecondaryImageSource = secondaryPreloadValue?.ImageModel?.Image;
         }
         
+        // When width and height are the same, it renders image incorrectly at startup,
+        // so need to handle it specially
+        var is1To1 = imageModel.PixelWidth == imageModel.PixelHeight;
+        
         await Dispatcher.UIThread.InvokeAsync(() =>
         {
             vm.ImageViewer.SetTransform(imageModel.EXIFOrientation);
-            SetSize();
-            if (Settings.WindowProperties.AutoFit)
+            if (Settings.WindowProperties.AutoFit && !Settings.Zoom.ScrollEnabled)
             {
+                SetSize();
                 WindowFunctions.CenterWindowOnScreen();
             }
+            else if (is1To1)
+            {
+                var size = WindowResizing.GetSize(imageModel.PixelWidth, imageModel.PixelHeight, secondaryPreloadValue?.ImageModel?.PixelWidth ?? 0, secondaryPreloadValue?.ImageModel?.PixelHeight ?? 0, vm.RotationAngle, vm);
+                vm.ImageViewer.MainBorder.Height = size?.Width ?? 0;
+                vm.ImageViewer.MainBorder.Width = size?.Height ?? 0;
+            }
+            else if (imageModel.PixelWidth <= UIHelper.GetMainView.Bounds.Width && imageModel.PixelHeight <= UIHelper.GetMainView.Bounds.Height)
+            {
+                SetSize();
+            }
         }, DispatcherPriority.Send);
 
         vm.IsLoading = false;
@@ -79,7 +93,7 @@ public static class QuickLoad
         {
             if (TiffManager.IsTiff(imageModel.FileInfo.FullName))
             {
-                SetTitleHelper.TrySetTiffTitle(imageModel.PixelWidth, imageModel.PixelHeight, NavigationManager.GetCurrentIndex, fileInfo, vm);
+                SetTitleHelper.TrySetTiffTitle(imageModel, vm);
             }
             else
             {
@@ -87,48 +101,41 @@ public static class QuickLoad
             }
         }
         
-        await Dispatcher.UIThread.InvokeAsync(() =>
+        // Fixes weird bug where the image is not rendered correctly
+        // TODO: check if this will still be needed in future Avalonia versions
+        if (!Settings.WindowProperties.AutoFit && !Settings.Zoom.ScrollEnabled)
         {
-            if (vm.PixelWidth > vm.ImageViewer.Bounds.Width && !Settings.Zoom.ScrollEnabled)
+            if (!is1To1)
             {
-                // Fixes weird bug where the image is not rendered correctly
-                // TODO: check if this will still be needed in future Avalonia versions
-                if (vm.PixelHeight > vm.ImageViewer.Bounds.Height)
+                await Dispatcher.UIThread.InvokeAsync(() =>
                 {
-                    vm.ImageViewer.MainBorder.Height = vm.ImageViewer.ImageScrollViewer.Bounds.Height;
-                }
-
-                vm.ImageViewer.MainBorder.Width = vm.ImageViewer.ImageScrollViewer.Bounds.Width;
-
-                WindowResizing.SetSize(1, 1, 0, 0, 0, vm);
-            }
-        });
-        
-        await Dispatcher.UIThread.InvokeAsync(() =>
-        {
-            if (vm.PixelWidth > vm.ImageViewer.Bounds.Width && !Settings.Zoom.ScrollEnabled)
-            {
-                vm.ImageViewer.MainBorder.Height = double.NaN;
-                vm.ImageViewer.MainBorder.Width = double.NaN;
+                    if (imageModel.PixelWidth > UIHelper.GetMainView.Bounds.Width || imageModel.PixelHeight > UIHelper.GetMainView.Bounds.Height
+                        || imageModel.PixelWidth == imageModel.PixelHeight)
+                    {
+                        WindowResizing.SetSize(1, 1, 0, 0, 0, vm);
+                    }
+                });
+                await Dispatcher.UIThread.InvokeAsync(() =>
+                {
+                    if (imageModel.PixelWidth > UIHelper.GetMainView.Bounds.Width || imageModel.PixelHeight > UIHelper.GetMainView.Bounds.Height)
+                    {
+                        vm.ImageViewer.MainBorder.Height = double.NaN;
+                        vm.ImageViewer.MainBorder.Width = double.NaN;
 
-                SetSize();
+                        SetSize();
+                    }
+                }, DispatcherPriority.Send);
             }
-        }, DispatcherPriority.Send);
+        }
 
         if (Settings.Zoom.ScrollEnabled)
         {
             // Bad fix for scrolling
             // TODO: Implement proper startup scrolling fix
             Settings.Zoom.ScrollEnabled = false;
-            await Dispatcher.UIThread.InvokeAsync(() =>
-            {
-                WindowResizing.SetSize(imageModel.PixelWidth, imageModel.PixelHeight, secondaryPreloadValue?.ImageModel?.PixelWidth ?? 0, secondaryPreloadValue?.ImageModel?.PixelHeight ?? 0, imageModel.Rotation, vm);
-            }, DispatcherPriority.Background);
+            await Dispatcher.UIThread.InvokeAsync(SetSize, DispatcherPriority.Background);
             Settings.Zoom.ScrollEnabled = true;
-            await Dispatcher.UIThread.InvokeAsync(() =>
-            {
-                WindowResizing.SetSize(imageModel.PixelWidth, imageModel.PixelHeight, secondaryPreloadValue?.ImageModel?.PixelWidth ?? 0, secondaryPreloadValue?.ImageModel?.PixelHeight ?? 0, imageModel.Rotation, vm);
-            }, DispatcherPriority.Send);
+            await Dispatcher.UIThread.InvokeAsync(SetSize, DispatcherPriority.Send);
         }
 
         vm.ExifOrientation = imageModel.EXIFOrientation;

+ 94 - 76
src/PicView.Avalonia/WindowBehavior/WindowResizing.cs

@@ -49,6 +49,71 @@ public static class WindowResizing
     #region Set Window Size
 
     public static void SetSize(MainViewModel vm)
+    {
+        var size = GetSize(vm);
+
+        if (size is null)
+        {
+            return;
+        }
+        
+        if (Dispatcher.UIThread.CheckAccess())
+        {
+            SetSize(size.Value, vm);
+        }
+        else
+        {
+            Dispatcher.UIThread.InvokeAsync(() => SetSize(size.Value, vm));
+        }
+    }
+
+    public static void SetSize(double width, double height, double secondWidth, double secondHeight, double rotation,
+        MainViewModel vm)
+    {
+        var size = GetSize(width, height, secondWidth, secondHeight, rotation, vm);
+
+        if (size is null)
+        {
+            return;
+        }
+
+        SetSize(size.Value, vm);
+    }
+    
+    public static void SetSize(ImageSizeCalculationHelper.ImageSize size, MainViewModel vm)
+    {
+        vm.TitleMaxWidth = size.TitleMaxWidth;
+        vm.ImageWidth = size.Width;
+        vm.SecondaryImageWidth = size.SecondaryWidth;
+        vm.ImageHeight = size.Height;
+        vm.GalleryMargin = new Thickness(0, 0, 0, size.Margin);
+
+        vm.ScrollViewerWidth = size.ScrollViewerWidth;
+        vm.ScrollViewerHeight = size.ScrollViewerHeight;
+
+        if (Settings.WindowProperties.AutoFit)
+        {
+            if (Settings.WindowProperties.Fullscreen ||
+                Settings.WindowProperties.Maximized)
+            {
+                vm.GalleryWidth = double.NaN;
+            }
+            else
+            {
+                vm.GalleryWidth = vm.RotationAngle is 90 or 270
+                    ? Math.Max(size.Height, SizeDefaults.WindowMinSize)
+                    : Math.Max(size.Width, SizeDefaults.WindowMinSize);
+            }
+        }
+        else
+        {
+            vm.GalleryWidth = double.NaN;
+        }
+        
+        vm.AspectRatio = size.AspectRatio;
+    }
+
+    public static ImageSizeCalculationHelper.ImageSize? GetSize(MainViewModel vm)
     {
         double firstWidth, firstHeight;
         var preloadValue = NavigationManager.GetCurrentPreLoadValue();
@@ -63,7 +128,7 @@ public static class WindowResizing
                 }
                 else
                 {
-                    return;
+                    return null;
                 }
             }
             else if (vm.FileInfo?.Exists != null)
@@ -75,7 +140,7 @@ public static class WindowResizing
             }
             else
             {
-                return;
+                return null;
             }
         }
         else
@@ -84,52 +149,33 @@ public static class WindowResizing
             firstHeight = GetHeight();
         }
 
-        if (Settings.ImageScaling.ShowImageSideBySide)
+        if (!Settings.ImageScaling.ShowImageSideBySide)
         {
-            var secondaryPreloadValue = NavigationManager.GetNextPreLoadValue();
-            double secondWidth, secondHeight;
-            if (secondaryPreloadValue != null)
-            {
-                secondWidth = GetWidth();
-                secondHeight = GetHeight();
-            }
-            else if (NavigationManager.CanNavigate(vm))
-            {
-                var nextFileName = NavigationManager.GetNextFileName;
-                var magickImage = new MagickImage();
-                magickImage.Ping(nextFileName);
-                secondWidth = magickImage.Width;
-                secondHeight = magickImage.Height;
-            }
-            else
-            {
-                secondWidth = 0;
-                secondHeight = 0;
-            }
+            return GetSize(firstWidth, firstHeight, 0, 0, vm.RotationAngle, vm);
+        }
 
-            if (Dispatcher.UIThread.CheckAccess())
-            {
-                SetSize(firstWidth, firstHeight, secondWidth, secondHeight, vm.RotationAngle, vm);
-            }
-            else
-            {
-                Dispatcher.UIThread.InvokeAsync(() =>
-                    SetSize(firstWidth, firstHeight, secondWidth, secondHeight, vm.RotationAngle, vm));
-            }
+        var secondaryPreloadValue = NavigationManager.GetNextPreLoadValue();
+        double secondWidth, secondHeight;
+        if (secondaryPreloadValue != null)
+        {
+            secondWidth = GetWidth();
+            secondHeight = GetHeight();
+        }
+        else if (NavigationManager.CanNavigate(vm))
+        {
+            var nextFileName = NavigationManager.GetNextFileName;
+            var magickImage = new MagickImage();
+            magickImage.Ping(nextFileName);
+            secondWidth = magickImage.Width;
+            secondHeight = magickImage.Height;
         }
         else
         {
-            if (Dispatcher.UIThread.CheckAccess())
-            {
-                SetSize(firstWidth, firstHeight, 0, 0, vm.RotationAngle, vm);
-            }
-            else
-            {
-                Dispatcher.UIThread.InvokeAsync(() => SetSize(firstWidth, firstHeight, 0, 0, vm.RotationAngle, vm));
-            }
+            secondWidth = 0;
+            secondHeight = 0;
         }
-
-        return;
+            
+        return GetSize(firstWidth, firstHeight, secondWidth, secondHeight, vm.RotationAngle, vm);
 
         double GetWidth()
         {
@@ -141,21 +187,21 @@ public static class WindowResizing
             return preloadValue?.ImageModel?.PixelHeight ?? vm.ImageHeight;
         }
     }
-
-    public static void SetSize(double width, double height, double secondWidth, double secondHeight, double rotation,
+    
+    public static ImageSizeCalculationHelper.ImageSize? GetSize(double width, double height, double secondWidth, double secondHeight, double rotation,
         MainViewModel vm)
     {
         width = width == 0 ? vm.ImageWidth : width;
         height = height == 0 ? vm.ImageHeight : height;
         if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
         {
-            return;
+            return null;
         }
 
         var mainView = UIHelper.GetMainView;
         if (mainView is null)
         {
-            return;
+            return null;
         }
 
         const int padding = 45;
@@ -168,7 +214,7 @@ public static class WindowResizing
         if (double.IsNaN(containerWidth) || double.IsNaN(containerHeight) || double.IsNaN(width) ||
             double.IsNaN(height))
         {
-            return;
+            return null;
         }
 
         ImageSizeCalculationHelper.ImageSize size;
@@ -213,35 +259,7 @@ public static class WindowResizing
                 containerHeight);
         }
 
-        vm.TitleMaxWidth = size.TitleMaxWidth;
-        vm.ImageWidth = size.Width;
-        vm.SecondaryImageWidth = size.SecondaryWidth;
-        vm.ImageHeight = size.Height;
-        vm.GalleryMargin = new Thickness(0, 0, 0, size.Margin);
-
-        vm.ScrollViewerWidth = size.ScrollViewerWidth;
-        vm.ScrollViewerHeight = size.ScrollViewerHeight;
-
-        if (Settings.WindowProperties.AutoFit)
-        {
-            if (Settings.WindowProperties.Fullscreen ||
-                Settings.WindowProperties.Maximized)
-            {
-                vm.GalleryWidth = double.NaN;
-            }
-            else
-            {
-                vm.GalleryWidth = vm.RotationAngle is 90 or 270
-                    ? Math.Max(size.Height, desktopMinHeight)
-                    : Math.Max(size.Width, desktopMinWidth);
-            }
-        }
-        else
-        {
-            vm.GalleryWidth = double.NaN;
-        }
-        
-        vm.AspectRatio = size.AspectRatio;
+        return size;
     }
 
     public static void SaveSize(Window window)