Browse Source

Refactor, clean-up, miscellaneous.

Ruben 1 year ago
parent
commit
f5d095a857

+ 2 - 2
src/PicView.Avalonia/CustomControls/FuncTextBox.cs

@@ -10,12 +10,12 @@ namespace PicView.Avalonia.CustomControls
     {
         public FuncTextBox()
         {
-            if (!Application.Current.TryGetResource("MainTextColor", ThemeVariant.Default, out var MainTextColor))
+            if (!Application.Current.TryGetResource("MainTextColor", ThemeVariant.Default, out var mainTextColor))
             {
                 return;
             }
 
-            var iconBrush = new SolidColorBrush((Color)(MainTextColor ?? Brushes.White));
+            var iconBrush = new SolidColorBrush((Color)(mainTextColor ?? Brushes.White));
             if (!Application.Current.TryGetResource("CopyGeometry", ThemeVariant.Default, out var copyGeometry))
             {
                 return;

+ 4 - 2
src/PicView.Avalonia/Gallery/GalleryFunctions.cs

@@ -308,7 +308,7 @@ public static class GalleryFunctions
          }
 
          GalleryItem? galleryItem;
-         var imageModel = await GetImageModel.GetImageModelAsync(fileInfo, true, (uint)vm.GetGalleryItemHeight);
+         var thumb = await GetThumbnails.GetThumbAsync(fileInfo.FullName, (uint)vm.GetGalleryItemHeight, fileInfo);
          var galleryThumbInfo = GalleryThumbInfo.GalleryThumbHolder.GetThumbData(fileInfo);
          try
          {
@@ -342,7 +342,9 @@ public static class GalleryFunctions
                      await vm.ImageIterator.IterateToIndex(vm.ImageIterator.ImagePaths.IndexOf(fileInfo.FullName)).ConfigureAwait(false);
                  };
                  galleryListBox.Items.Insert(index, galleryItem);
-                 ImageFunctions.SetImage(imageModel.Image, galleryItem.GalleryImage, imageModel.ImageType);
+                 ImageFunctions.SetImage(thumb, galleryItem.GalleryImage,
+                     fileInfo.Extension.Equals("svg", StringComparison.OrdinalIgnoreCase) ||
+                     fileInfo.Extension.Equals("svgz", StringComparison.OrdinalIgnoreCase) ? ImageType.Svg : ImageType.Bitmap);
              }, DispatcherPriority.Render);
              return true;
          }

+ 8 - 5
src/PicView.Avalonia/Gallery/GalleryLoad.cs

@@ -16,6 +16,9 @@ public static class GalleryLoad
 
     public static async Task LoadGallery(MainViewModel vm, string currentDirectory)
     {
+        // TODO: Lazy load this when scrolling instead. Figure out how to support virtualization. 
+        
+        
         if (vm.ImageIterator?.ImagePaths.Count == 0 || IsLoading || vm.ImageIterator is null)
         {
             return;
@@ -174,8 +177,7 @@ public static class GalleryLoad
                     return;
                 }
 
-                var thumbImageModel = await GetImageModel.GetImageModelAsync(fileInfos[i], isThumb: true,
-                    (uint)galleryItemSize);
+                var thumb = await GetThumbnails.GetThumbAsync(fileInfos[i].FullName, (uint)galleryItemSize, fileInfos[i]);
                 var thumbData = GalleryThumbInfo.GalleryThumbHolder.GetThumbData(fileInfos[i]);
 
                 await Dispatcher.UIThread.InvokeAsync(() =>
@@ -189,10 +191,11 @@ public static class GalleryLoad
                         return;
                     }
 
-                    if (thumbImageModel?.Image is not null)
+                    if (thumb is not null)
                     {
-                        ImageFunctions.SetImage(thumbImageModel.Image, galleryItem.GalleryImage,
-                            thumbImageModel.ImageType);
+                        ImageFunctions.SetImage(thumb, galleryItem.GalleryImage,
+                            fileInfos[i].Extension.Equals("svg", StringComparison.OrdinalIgnoreCase) ||
+                            fileInfos[i].Extension.Equals("svgz", StringComparison.OrdinalIgnoreCase) ? ImageType.Svg : ImageType.Bitmap);
                     }
 
                     galleryItem.FileLocation.Text = thumbData.FileLocation;

+ 16 - 56
src/PicView.Avalonia/ImageHandling/GetImageModel.cs

@@ -6,7 +6,7 @@ namespace PicView.Avalonia.ImageHandling;
 
 public static class GetImageModel
 {
-    public static async Task<ImageModel?> GetImageModelAsync(FileInfo fileInfo, bool isThumb = false, uint height = 0)
+    public static async Task<ImageModel?> GetImageModelAsync(FileInfo fileInfo)
     {
         if (fileInfo is null)
         {
@@ -39,34 +39,18 @@ public static class GetImageModel
             switch (ext)
             {
                 case ".webp":
-                    if (isThumb)
+                    await AddImageAsync(fileInfo, imageModel).ConfigureAwait(false);
+                    if (ImageFunctions.IsAnimated(fileInfo))
                     {
-                        var thumb = await GetThumbnails.GetThumbAsync(fileInfo.FullName, height, fileInfo).ConfigureAwait(false);
-                        SetModel(thumb, fileInfo, imageModel);
-                    }
-                    else
-                    {
-                        await AddImageAsync(fileInfo, imageModel).ConfigureAwait(false);
-                        if (ImageFunctions.IsAnimated(fileInfo))
-                        {
-                            imageModel.ImageType = ImageType.AnimatedWebp;
-                        }
+                        imageModel.ImageType = ImageType.AnimatedWebp;
                     }
 
                     break;
                 case ".gif":
-                    if (isThumb)
+                    await AddImageAsync(fileInfo, imageModel).ConfigureAwait(false);
+                    if (ImageFunctions.IsAnimated(fileInfo))
                     {
-                        var thumb = await GetThumbnails.GetThumbAsync(fileInfo.FullName, height, fileInfo).ConfigureAwait(false);
-                        SetModel(thumb, fileInfo, imageModel);
-                    }
-                    else
-                    {
-                        await AddImageAsync(fileInfo, imageModel).ConfigureAwait(false);
-                        if (ImageFunctions.IsAnimated(fileInfo))
-                        {
-                            imageModel.ImageType = ImageType.AnimatedGif;
-                        }
+                        imageModel.ImageType = ImageType.AnimatedGif;
                     }
 
                     break;
@@ -78,15 +62,7 @@ public static class GetImageModel
                 case ".jfif":
                 case ".ico":
                 case ".wbmp":
-                    if (isThumb)
-                    {
-                        var thumb = await GetThumbnails.GetThumbAsync(fileInfo.FullName, height, fileInfo).ConfigureAwait(false);
-                        SetModel(thumb, fileInfo, imageModel);
-                    }
-                    else
-                    {
-                        await AddImageAsync(fileInfo, imageModel).ConfigureAwait(false);
-                    }
+                    await AddImageAsync(fileInfo, imageModel).ConfigureAwait(false);
 
                     break;
 
@@ -96,11 +72,11 @@ public static class GetImageModel
                     break;
 
                 case ".b64":
-                    await AddBase64ImageAsync(fileInfo, imageModel, isThumb, height).ConfigureAwait(false);
+                    await AddBase64ImageAsync(fileInfo, imageModel).ConfigureAwait(false);
                     break;
 
                 default:
-                    await AddDefaultImageAsync(fileInfo, imageModel, isThumb, height).ConfigureAwait(false);
+                    await AddDefaultImageAsync(fileInfo, imageModel).ConfigureAwait(false);
                     break;
             }
         }
@@ -123,18 +99,10 @@ public static class GetImageModel
         return imageModel;
     }
 
-    private static async Task AddDefaultImageAsync(FileInfo fileInfo, ImageModel imageModel, bool isThumb, uint height)
+    private static async Task AddDefaultImageAsync(FileInfo fileInfo, ImageModel imageModel)
     {
-        if (isThumb)
-        {
-            var thumb = await GetThumbnails.GetThumbAsync(fileInfo.FullName, height, fileInfo).ConfigureAwait(false);
-            SetModel(thumb, fileInfo, imageModel);
-        }
-        else
-        {
-            var bitmap = await GetImage.GetDefaultBitmapAsync(fileInfo).ConfigureAwait(false);
-            SetModel(bitmap, fileInfo, imageModel);
-        }
+        var bitmap = await GetImage.GetDefaultBitmapAsync(fileInfo).ConfigureAwait(false);
+        SetModel(bitmap, fileInfo, imageModel);
     }
 
 
@@ -173,18 +141,10 @@ public static class GetImageModel
     
     #region Base64
 
-    private static async Task AddBase64ImageAsync(FileInfo fileInfo, ImageModel imageModel, bool isThumb, uint height)
+    private static async Task AddBase64ImageAsync(FileInfo fileInfo, ImageModel imageModel)
     {
-        if (isThumb)
-        {
-            var thumb = await GetThumbnails.GetThumbAsync(fileInfo.FullName, height, fileInfo).ConfigureAwait(false);
-            SetModel(thumb, fileInfo, imageModel);
-        }
-        else
-        {
-            var base64 = await GetImage.GetBase64ImageAsync(fileInfo).ConfigureAwait(false);
-            SetModel(base64, fileInfo, imageModel);
-        }
+        var base64 = await GetImage.GetBase64ImageAsync(fileInfo).ConfigureAwait(false);
+        SetModel(base64, fileInfo, imageModel);
     }
 
     #endregion

+ 7 - 1
src/PicView.Avalonia/ImageHandling/GetThumbnails.cs

@@ -37,6 +37,11 @@ public static class GetThumbnails
     public static async Task<Bitmap?> CreateThumbAsync(MagickImage magick, string path, uint height,
         FileInfo? fileInfo = null)
     {
+        // TODO: extract thumbnails from PlatformService and convert to Avalonia image,
+        // I.E. https://boldena.com/article/64006
+        // https://github.com/AvaloniaUI/Avalonia/discussions/16703
+        // https://stackoverflow.com/a/42178963/2923736 convert to DLLImport to LibraryImport, source generation & AOT support
+        
         fileInfo ??= new FileInfo(path);
         await using var fileStream = FileHelper.GetOptimizedFileStream(fileInfo);
         if (fileInfo.Length >= 2147483648)
@@ -58,4 +63,5 @@ public static class GetThumbnails
         memoryStream.Position = 0;
         return WriteableBitmap.Decode(memoryStream);
     }
-}
+}
+

+ 1 - 1
src/PicView.Avalonia/UI/FunctionsHelper.cs

@@ -973,7 +973,7 @@ public static class FunctionsHelper
         {
             await SetAsWallpaperFilled();
         }
-        // TODO: Add support for macOS
+        // TODO: Add setting wallpaper support for macOS
     }
 
     public static async Task SetAsWallpaperTiled()

+ 3 - 10
src/PicView.Avalonia/UI/WindowHelper.cs

@@ -109,8 +109,6 @@ public static class WindowHelper
 
     public static void CenterWindowOnScreen(bool horizontal = true)
     {
-        // TODO: Add support for multiple screens
-        
         if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
         {
             return;
@@ -147,14 +145,9 @@ public static class WindowHelper
             var centeredY = screenBounds.Y + (screenHeight - windowSize.Height) / 2;
 
             // Set the window's new position
-            if (horizontal)
-            {
-                window.Position = new PixelPoint((int)centeredX, (int)centeredY);
-            }
-            else
-            {
-                window.Position = new PixelPoint(window.Position.X, (int)centeredY);
-            }
+            window.Position = horizontal ?
+                new PixelPoint((int)centeredX, (int)centeredY) :
+                new PixelPoint(window.Position.X, (int)centeredY);
         });
     }
 

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

@@ -419,7 +419,7 @@ public partial class ImageViewer : UserControl
 
         if (SettingsHelper.Settings.WindowProperties.Fullscreen)
         {
-            // TODO: figure out how to pan in fullscreen
+            // TODO: figure out how to pan in fullscreen while keeping it in bounds
             _translateTransform.Transitions = null;
             _translateTransform.X = newXproperty;
             _translateTransform.Y = newYproperty;