浏览代码

Refactor setting and updating titles

Ruben 7 月之前
父节点
当前提交
a38adc0985

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

@@ -75,7 +75,7 @@ public static class CropFunctions
 
         vm.CurrentView = vm.ImageViewer;
         IsCropping = false;
-        SetTitleHelper.RefreshTitle(vm);
+        SetTitleHelper.SetTitle(vm);
     }
 
     public static bool DetermineIfShouldBeEnabled(MainViewModel vm)

+ 1 - 1
src/PicView.Avalonia/ImageHandling/ImageHelper.cs

@@ -147,6 +147,6 @@ public static class ImageHelper
                 Trace.WriteLine(e);
             }
         });
-        SetTitleHelper.RefreshTitle(vm);
+        SetTitleHelper.SetTitle(vm);
     }
 }

+ 1 - 1
src/PicView.Avalonia/Navigation/ErrorHandling.cs

@@ -46,7 +46,7 @@ public static class ErrorHandling
             }
             else
             {
-                SetTitleHelper.ResetTitle(vm);
+                SetTitleHelper.SetNoImageTitle(vm);
             }
 
             vm.GalleryMode = GalleryMode.Closed;

+ 1 - 1
src/PicView.Avalonia/SettingsManagement/SettingsUpdater.cs

@@ -69,7 +69,7 @@ public static class SettingsUpdater
         }
         finally
         {
-            SetTitleHelper.RefreshTitle(vm);
+            SetTitleHelper.SetTitle(vm);
             vm.IsLoading = false;
         }
     }

+ 11 - 2
src/PicView.Avalonia/StartUp/QuickLoad.cs

@@ -64,8 +64,17 @@ public static class QuickLoad
             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;
+                if (!size.HasValue)
+                {
+#if DEBUG
+         Console.WriteLine($"{nameof(QuickLoadAsync)} {nameof(size)} is null");           
+#endif
+                    ErrorHandling.ShowStartUpMenu(vm);
+                    return;
+                }
+                WindowResizing.SetSize(size.Value, vm);
+                vm.ImageViewer.MainBorder.Height = size.Value.Width;
+                vm.ImageViewer.MainBorder.Width = size.Value.Height;
             }
             else if (imageModel.PixelWidth <= UIHelper.GetMainView.Bounds.Width && imageModel.PixelHeight <= UIHelper.GetMainView.Bounds.Height)
             {

+ 142 - 57
src/PicView.Avalonia/UI/SetTitleHelper.cs

@@ -1,4 +1,5 @@
-using PicView.Avalonia.ImageHandling;
+using ImageMagick;
+using PicView.Avalonia.ImageHandling;
 using PicView.Avalonia.Navigation;
 using PicView.Avalonia.ViewModels;
 using PicView.Core.FileHandling;
@@ -10,6 +11,12 @@ namespace PicView.Avalonia.UI;
 
 public static class SetTitleHelper
 {
+    /// <summary>
+    ///     Sets the title of the window and the title displayed in the UI to the appropriate
+    ///     value based on the current state of the application.
+    /// </summary>
+    /// <param name="vm">The main view model instance.</param>
+    /// <remarks>Can be used to refresh the title when files are added or removed.</remarks>
     public static void SetTitle(MainViewModel vm)
     {
         if (!NavigationManager.CanNavigate(vm))
@@ -22,45 +29,75 @@ public static class SetTitleHelper
             }
             else if (s.Contains(TranslationHelper.Translation.Base64Image))
             {
-                title = TranslationHelper.Translation.Base64Image ?? "Base64 Image";
+                title = TranslationHelper.Translation.Base64Image;
             }
             else
             {
-                title = TranslationHelper.Translation.ClipboardImage ?? "Clipboard Image";
+                title = TranslationHelper.Translation.ClipboardImage!;
             }
-            
-            var singeImageWindowTitles = ImageTitleFormatter.GenerateTitleForSingleImage(vm.PixelWidth, vm.PixelWidth, title, vm.ZoomValue);
-            vm.WindowTitle = singeImageWindowTitles.BaseTitle;
-            vm.Title = singeImageWindowTitles.TitleWithAppName;
-            vm.TitleTooltip = singeImageWindowTitles.TitleWithAppName;
+
+            var singleImageWindowTitles =
+                ImageTitleFormatter.GenerateTitleForSingleImage(vm.PixelWidth, vm.PixelWidth, title, vm.ZoomValue);
+            vm.WindowTitle = singleImageWindowTitles.BaseTitle;
+            vm.Title = singleImageWindowTitles.TitleWithAppName;
+            vm.TitleTooltip = singleImageWindowTitles.TitleWithAppName;
+            return;
+        }
+
+        if (NavigationManager.TiffNavigationInfo is not null)
+        {
+            SetTiffTitle(NavigationManager.TiffNavigationInfo, vm.PixelWidth, vm.PixelHeight,
+                NavigationManager.GetCurrentIndex, vm.FileInfo, vm);
+            return;
+        }
+
+        if (Settings.ImageScaling.ShowImageSideBySide)
+        {
+            var imageModel1 = new ImageModel
+            {
+                FileInfo = vm.FileInfo,
+                PixelWidth = vm.PixelWidth,
+                PixelHeight = vm.PixelHeight
+            };
+            var nextFileName = NavigationManager.GetNextFileName;
+            using var magickImage = new MagickImage();
+            magickImage.Ping(nextFileName);
+            var imageModel2 = new ImageModel
+            {
+                FileInfo = new FileInfo(nextFileName),
+                PixelWidth = (int)magickImage.Width,
+                PixelHeight = (int)magickImage.Height
+            };
+            SetSideBySideTitle(vm, imageModel1, imageModel2);
             return;
         }
 
-        var windowTitles = ImageTitleFormatter.GenerateTitleStrings(vm.PixelWidth, vm.PixelHeight, NavigationManager.GetCurrentIndex,
+        var windowTitles = ImageTitleFormatter.GenerateTitleStrings(vm.PixelWidth, vm.PixelHeight,
+            NavigationManager.GetCurrentIndex,
             vm.FileInfo, vm.ZoomValue, NavigationManager.GetCollection);
         vm.WindowTitle = windowTitles.TitleWithAppName;
         vm.Title = windowTitles.BaseTitle;
         vm.TitleTooltip = windowTitles.FilePathTitle;
     }
 
-    public static void RefreshTitle(MainViewModel vm)
-    {
-        if (vm.FileInfo == null)
-        {
-            return;
-        }
-        var path = vm.FileInfo.FullName;
-        vm.FileInfo = new FileInfo(path);
-        SetTitle(vm);
-    }
-    
+    /// <summary>
+    ///     Sets the title of the window and the title displayed in the UI
+    ///     to a temporary "Loading..." title while the application is loading current image.
+    /// </summary>
+    /// <param name="vm">The main view model instance.</param>
     public static void SetLoadingTitle(MainViewModel vm)
     {
         vm.WindowTitle = $"{TranslationHelper.Translation.Loading} - PicView";
         vm.Title = TranslationHelper.Translation.Loading;
         vm.TitleTooltip = vm.Title;
     }
-    
+
+    /// <summary>
+    ///     Sets the window title and UI title based on the provided image model.
+    /// </summary>
+    /// <param name="vm">The main view model instance.</param>
+    /// <param name="imageModel">The image model containing information about the current image.</param>
+    /// <remarks>If the image model or its file info is null, an error message is set as the title.</remarks>
     public static void SetTitle(MainViewModel vm, ImageModel? imageModel)
     {
         if (imageModel is null)
@@ -75,35 +112,59 @@ public static class SetTitleHelper
             return;
         }
 
-        var windowTitles = ImageTitleFormatter.GenerateTitleStrings(imageModel.PixelWidth, imageModel.PixelHeight,  NavigationManager.GetCurrentIndex,
-            imageModel.FileInfo,  vm.ZoomValue,  NavigationManager.GetCollection);
+        var windowTitles = ImageTitleFormatter.GenerateTitleStrings(imageModel.PixelWidth, imageModel.PixelHeight,
+            NavigationManager.GetCurrentIndex,
+            imageModel.FileInfo, vm.ZoomValue, NavigationManager.GetCollection);
         vm.WindowTitle = windowTitles.TitleWithAppName;
         vm.Title = windowTitles.BaseTitle;
         vm.TitleTooltip = windowTitles.FilePathTitle;
     }
-    
-    public static void SetTiffTitle(TiffManager.TiffNavigationInfo tiffNavigationInfo, int width, int height, int index, FileInfo fileInfo, MainViewModel vm)
+
+    /// <summary>
+    ///     Sets the title of the window and the title displayed in the UI
+    ///     based on the provided TIFF navigation info and image model.
+    /// </summary>
+    /// <param name="tiffNavigationInfo">The TIFF navigation info object.</param>
+    /// <param name="width">The width of the image in pixels.</param>
+    /// <param name="height">The height of the image in pixels.</param>
+    /// <param name="index">The index of the image in the list.</param>
+    /// <param name="fileInfo">The FileInfo object representing the image file.</param>
+    /// <param name="vm">The main view model instance.</param>
+    /// <remarks>
+    ///     This method is used to set the title of the window and the title displayed in the UI
+    ///     for TIFF images. The image title is generated using the TIFF navigation info and image model.
+    /// </remarks>
+    public static void SetTiffTitle(TiffManager.TiffNavigationInfo tiffNavigationInfo, int width, int height, int index,
+        FileInfo fileInfo, MainViewModel vm)
     {
-        var singeImageWindowTitles = ImageTitleFormatter.GenerateTiffTitleStrings(width, height, index, fileInfo, tiffNavigationInfo, 1, NavigationManager.GetCollection);
+        var singeImageWindowTitles = ImageTitleFormatter.GenerateTiffTitleStrings(width, height, index, fileInfo,
+            tiffNavigationInfo, 1, NavigationManager.GetCollection);
         vm.WindowTitle = singeImageWindowTitles.TitleWithAppName;
-        vm.Title = singeImageWindowTitles.BaseTitle; 
+        vm.Title = singeImageWindowTitles.BaseTitle;
         vm.TitleTooltip = singeImageWindowTitles.BaseTitle;
     }
-    
+
+    /// <summary>
+    ///     Sets the title of the window and the title displayed in the UI
+    ///     based on the provided image model and main view model instance.
+    ///     If the image is a TIFF with multiple pages, the title is generated
+    ///     using the TIFF navigation info and image model. Otherwise, the single
+    ///     image title is generated using the image model.
+    /// </summary>
+    /// <param name="imageModel">The image model containing the image information.</param>
+    /// <param name="vm">The main view model instance.</param>
+    /// <remarks>
+    ///     This method is used to set the title of the window and the title displayed in the UI
+    ///     for TIFF images with multiple pages. If the image is not a TIFF with multiple pages,
+    ///     the single image title is generated using the image model.
+    /// </remarks>
     public static void TrySetTiffTitle(ImageModel? imageModel, MainViewModel vm)
     {
-        if (imageModel is null)
+        if (!ValidateImageModel(imageModel, vm))
         {
-            ReturnError(vm);
             return;
         }
 
-        if (imageModel.FileInfo is null)
-        {
-            ReturnError(vm);
-            return;
-        }
-        
         if (TiffManager.GetTiffPageCount(imageModel.FileInfo.FullName) is { } pageCount and > 1)
         {
             var tiffNavigationInfo = new TiffManager.TiffNavigationInfo
@@ -120,25 +181,30 @@ public static class SetTitleHelper
             SetTitle(vm, imageModel);
         }
     }
-    
+
+    /// <summary>
+    ///     Sets the title of the window and the title displayed in the UI when showing two images side by side.
+    /// </summary>
+    /// <param name="vm">The main view model instance.</param>
+    /// <param name="imageModel1">The first image model containing the image information.</param>
+    /// <param name="imageModel2">The second image model containing the image information.</param>
+    /// <remarks>
+    ///     This method is used to set the title of the window and the title displayed in the UI.
+    ///     The title is a combination of the titles of the two images.
+    /// </remarks>
     public static void SetSideBySideTitle(MainViewModel vm, ImageModel? imageModel1, ImageModel? imageModel2)
     {
-        if (imageModel1 is null || imageModel2 is null)
+        if (!ValidateImageModel(imageModel1, vm) || !ValidateImageModel(imageModel2, vm))
         {
-            ReturnError(vm);
             return;
         }
-
-        if (imageModel1.FileInfo is null || imageModel2.FileInfo is null)
-        {
-            ReturnError(vm);
-            return;
-        }
-
-        var firstWindowTitles = ImageTitleFormatter.GenerateTitleStrings(imageModel1.PixelWidth, imageModel1.PixelHeight,  NavigationManager.GetCurrentIndex,
-            imageModel1.FileInfo,  vm.ZoomValue,  NavigationManager.GetCollection);
-        var secondWindowTitles = ImageTitleFormatter.GenerateTitleStrings(imageModel2.PixelWidth, imageModel2.PixelHeight,  NavigationManager.GetNextIndex,
-            imageModel2.FileInfo,  vm.ZoomValue,  NavigationManager.GetCollection);
+        
+        var firstWindowTitles = ImageTitleFormatter.GenerateTitleStrings(imageModel1.PixelWidth,
+            imageModel1.PixelHeight, NavigationManager.GetCurrentIndex,
+            imageModel1.FileInfo, vm.ZoomValue, NavigationManager.GetCollection);
+        var secondWindowTitles = ImageTitleFormatter.GenerateTitleStrings(imageModel2.PixelWidth,
+            imageModel2.PixelHeight, NavigationManager.GetNextIndex,
+            imageModel2.FileInfo, vm.ZoomValue, NavigationManager.GetCollection);
         var windowTitle = $"{firstWindowTitles.BaseTitle} \u21dc || \u21dd {secondWindowTitles.BaseTitle} - PicView";
         var title = $"{firstWindowTitles.BaseTitle} \u21dc || \u21dd  {secondWindowTitles.BaseTitle}";
         var titleTooltip = $"{firstWindowTitles.FilePathTitle} \u21dc || \u21dd  {secondWindowTitles.FilePathTitle}";
@@ -146,18 +212,37 @@ public static class SetTitleHelper
         vm.Title = title;
         vm.TitleTooltip = titleTooltip;
     }
-    
+
+    /// <summary>
+    ///     Sets the window title and UI title to indicate no image is available.
+    /// </summary>
+    /// <param name="vm">The main view model instance.</param>
+    /// <remarks>
+    ///     This method sets the title, window title, and tooltip to a default message
+    ///     indicating that no image is currently loaded or available.
+    /// </remarks>
+    public static void SetNoImageTitle(MainViewModel vm)
+    {
+        vm.Title = TranslationHelper.Translation.NoImage;
+        vm.WindowTitle = TranslationHelper.Translation.NoImage + " - PicView";
+        vm.TitleTooltip = TranslationHelper.Translation.NoImage;
+    }
+
     private static void ReturnError(MainViewModel vm)
     {
         vm.WindowTitle =
             vm.Title =
                 vm.TitleTooltip = TranslationHelper.GetTranslation("UnableToRender");
-    }   
+    }
 
-    public static void ResetTitle(MainViewModel vm)
+    private static bool ValidateImageModel(ImageModel? imageModel, MainViewModel vm)
     {
-        vm.Title = TranslationHelper.Translation.NoImage;
-        vm.WindowTitle = TranslationHelper.Translation.NoImage + " - PicView";
-        vm.TitleTooltip = TranslationHelper.Translation.NoImage;
+        if (imageModel?.FileInfo is not null)
+        {
+            return true;
+        }
+
+        ReturnError(vm);
+        return false;
     }
-}
+}

+ 1 - 1
src/PicView.Avalonia/Views/StartUpMenu.axaml.cs

@@ -88,7 +88,7 @@ public partial class StartUpMenu : UserControl
         if (DataContext is not MainViewModel vm)
             return;
 
-        SetTitleHelper.ResetTitle(vm);
+        SetTitleHelper.SetNoImageTitle(vm);
     }
 
     public void ResponsiveSize(double width, double height)