Browse Source

[Avalonia] SVG image drag and drop preview

Ruben 1 year ago
parent
commit
9d18a08168

+ 17 - 5
src/PicView.Avalonia/Views/MainView.axaml.cs

@@ -250,12 +250,24 @@ public partial class MainView : UserControl
             }
             else if (path.IsSupported())
             {
-                var thumb = await ImageHelper.GetThumbAsync(path, SizeDefaults.WindowMinSize - 30).ConfigureAwait(false);
-
-                await Dispatcher.UIThread.InvokeAsync(() =>
+                var ext = Path.GetExtension(path);
+                if (ext.Equals(".svg", StringComparison.InvariantCultureIgnoreCase) || ext.Equals(".svgz", StringComparison.InvariantCultureIgnoreCase))
+                {
+                    await Dispatcher.UIThread.InvokeAsync(() =>
+                    {
+                        _dragDropView?.UpdateSvgThumbnail(path);
+                    });
+                }
+                else
                 {
-                    _dragDropView?.UpdateThumbnail(thumb);
-                });
+                    var thumb = await ImageHelper.GetThumbAsync(path, SizeDefaults.WindowMinSize - 30).ConfigureAwait(false);
+
+                    await Dispatcher.UIThread.InvokeAsync(() =>
+                    {
+                        _dragDropView?.UpdateThumbnail(thumb);
+                    });
+                }
+
             }
             else
             {

+ 42 - 0
src/PicView.Avalonia/Views/UC/DragDrogView.axaml.cs

@@ -1,6 +1,7 @@
 using Avalonia.Controls;
 using Avalonia.Media;
 using Avalonia.Media.Imaging;
+using Avalonia.Svg.Skia;
 using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
 using PicView.Core.Calculations;
@@ -31,6 +32,7 @@ public partial class DragDropView : UserControl
         }
         ContentHolder.Background = null;
         ContentHolder.IsVisible = false;
+        ContentHolder.Child = null;
     }
 
     public void AddDirectoryIcon()
@@ -43,6 +45,7 @@ public partial class DragDropView : UserControl
         }
         ContentHolder.Background = null;
         ContentHolder.IsVisible = false;
+        ContentHolder.Child = null;
     }
 
     public void AddZipIcon()
@@ -55,6 +58,7 @@ public partial class DragDropView : UserControl
         }
         ContentHolder.Background = null;
         ContentHolder.IsVisible = false;
+        ContentHolder.Child = null;
     }
 
     public void UpdateThumbnail(Bitmap image)
@@ -83,6 +87,44 @@ public partial class DragDropView : UserControl
             Opacity = 0.95,
             Source = image
         };
+        ContentHolder.Child = null;
+        ContentHolder.IsVisible = true;
+    }
+    
+    public void UpdateSvgThumbnail(object svg)
+    {
+        TxtDragToView.Text = TranslationHelper.Translation.DropToLoad;
+        Width = UIHelper.GetMainView.Bounds.Width;
+        Height = UIHelper.GetMainView.Bounds.Height;
+        
+        if (DataContext is not MainViewModel vm)
+        {
+            return;
+        }
+        
+        var svgSource = SvgSource.Load(svg as string);
+        var svgImage = new SvgImage { Source = svgSource };
+
+        var screen = ScreenHelper.ScreenSize;
+        const int maxSize = SizeDefaults.WindowMinSize - 30;
+        var padding = vm.BottombarHeight + vm.TitlebarHeight + 50;
+        var boxedWidth = UIHelper.GetMainView.Bounds.Width * screen.Scaling - padding;
+        var boxedHeight = UIHelper.GetMainView.Bounds.Height * screen.Scaling - padding;
+        var scaledWidth = boxedWidth / svgImage?.Size. Width ?? maxSize;
+        var scaledHeight = boxedHeight / svgImage?.Size.Height ?? maxSize;
+        var scale = Math.Min(scaledWidth, scaledHeight);
+        ContentHolder.Width = svgImage?.Size.Width * scale ?? maxSize;
+        ContentHolder.Height = svgImage?.Size.Height * scale ?? maxSize; 
+        ContentHolder.Background = new ImageBrush
+        {
+            Opacity = 0.95,
+        };
+        ContentHolder.Child = new Image
+        {
+            Source = svgImage,
+            Width = svgImage?.Size.Width * scale ?? maxSize,
+            Height = svgImage?.Size.Height * scale ?? maxSize
+        };
         ContentHolder.IsVisible = true;
     }