Browse Source

Refactor clipboard operations to use `TryGetFilesAsync` and `TryGetTextAsync`.

- Replaced deprecated `GetDataAsync` and `GetTextAsync` calls for clipboard data retrieval.
- Added comprehensive image format handling for platform-specific clipboard operations.
- Improved clipboard data transfer for non-Windows platforms using `DataTransferItem` and `DataTransfer`.
Ruben 2 days ago
parent
commit
129fa2885c

+ 50 - 5
src/PicView.Avalonia/Clipboard/ClipboardImageOperations.cs

@@ -2,6 +2,7 @@ using System.Runtime.InteropServices;
 using Avalonia.Input;
 using Avalonia.Input;
 using Avalonia.Input.Platform;
 using Avalonia.Input.Platform;
 using Avalonia.Media.Imaging;
 using Avalonia.Media.Imaging;
+using ImageMagick;
 using PicView.Avalonia.Navigation;
 using PicView.Avalonia.Navigation;
 using PicView.Avalonia.ViewModels;
 using PicView.Avalonia.ViewModels;
 using PicView.Core.DebugTools;
 using PicView.Core.DebugTools;
@@ -31,7 +32,7 @@ public static class ClipboardImageOperations
         return await ClipboardService.ExecuteClipboardOperation(async () =>
         return await ClipboardService.ExecuteClipboardOperation(async () =>
         {
         {
             await clipboard.ClearAsync();
             await clipboard.ClearAsync();
-
+            
             // Handle for Windows
             // Handle for Windows
             if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
             if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
             {
             {
@@ -40,12 +41,55 @@ public static class ClipboardImageOperations
             }
             }
 
 
             // Handle for other platforms
             // Handle for other platforms
+            const string pngIdentifier = "image/png";
+            const string jpegIdentifier = "image/jpeg";
+            const string gifIdentifier = "image/gif";
+            const string tiffIdentifier = "image/tiff";
+            string identifier;
+            switch (vm.PicViewer.Format.Value)
+            {
+                case MagickFormat.Tif:
+                case MagickFormat.Tiff:
+                case MagickFormat.Tiff64:
+                    identifier = tiffIdentifier;
+                    break;
+                case MagickFormat.Gif:
+                case MagickFormat.Gif87:
+                    identifier = gifIdentifier;
+                    break;
+                case MagickFormat.Hdr:
+                case MagickFormat.Heic:
+                case MagickFormat.Heif:
+                case MagickFormat.J2c:
+                case MagickFormat.J2k:
+                case MagickFormat.Jng:
+                case MagickFormat.Jnx:
+                case MagickFormat.Jp2:
+                case MagickFormat.Jpc:
+                case MagickFormat.Jpe:
+                case MagickFormat.Jpeg:
+                case MagickFormat.Jpg:
+                case MagickFormat.Jpm:
+                case MagickFormat.Jps:
+                    identifier = jpegIdentifier;
+                    break;
+                case MagickFormat.Svg:
+                case MagickFormat.Svgz:
+                case MagickFormat.Text:
+                case null:
+                    return false;
+                default:
+                    identifier = pngIdentifier;
+                    break;
+            }
+
             using var ms = new MemoryStream();
             using var ms = new MemoryStream();
             bitmap.Save(ms);
             bitmap.Save(ms);
+            var dataItem = DataTransferItem.Create(DataFormat.CreateBytesPlatformFormat(identifier), ms.ToArray());
+            var dataTransfer = new DataTransfer();
+            dataTransfer.Add(dataItem);
+            await clipboard.SetDataAsync(dataTransfer);
 
 
-            var dataObject = new DataObject();
-            dataObject.Set("image/png", ms.ToArray());
-            await clipboard.SetDataObjectAsync(dataObject);
             return true;
             return true;
         });
         });
     }
     }
@@ -113,7 +157,8 @@ public static class ClipboardImageOperations
                 return string.Empty;
                 return string.Empty;
 
 
             default:
             default:
-                throw new ArgumentOutOfRangeException(nameof(vm.PicViewer.ImageType), $"Unsupported image type: {vm.PicViewer.ImageType}");
+                throw new ArgumentOutOfRangeException(nameof(vm.PicViewer.ImageType),
+                    $"Unsupported image type: {vm.PicViewer.ImageType}");
         }
         }
     }
     }
 
 

+ 3 - 3
src/PicView.Avalonia/Clipboard/ClipboardPasteOperations.cs

@@ -1,4 +1,4 @@
-using Avalonia.Input;
+using Avalonia.Input.Platform;
 using Avalonia.Threading;
 using Avalonia.Threading;
 using PicView.Avalonia.Navigation;
 using PicView.Avalonia.Navigation;
 using PicView.Avalonia.ViewModels;
 using PicView.Avalonia.ViewModels;
@@ -23,7 +23,7 @@ public static class ClipboardPasteOperations
         try
         try
         {
         {
             // Need to use dispatcher to access clipboard in this instance
             // Need to use dispatcher to access clipboard in this instance
-            var files = await Dispatcher.UIThread.InvokeAsync(async () => await clipboard.GetDataAsync(DataFormats.Files));
+            var files = await Dispatcher.UIThread.InvokeAsync(async () => await clipboard.TryGetFilesAsync());
             if (files != null)
             if (files != null)
             {
             {
                 await ClipboardFileOperations.PasteFiles(files, vm);
                 await ClipboardFileOperations.PasteFiles(files, vm);
@@ -31,7 +31,7 @@ public static class ClipboardPasteOperations
             }
             }
 
 
             // Try to paste text (URLs, file paths)
             // Try to paste text (URLs, file paths)
-            var text = await clipboard.GetTextAsync();
+            var text = await clipboard.TryGetTextAsync();
             if (!string.IsNullOrWhiteSpace(text))
             if (!string.IsNullOrWhiteSpace(text))
             {
             {
                 await NavigationManager.LoadPicFromStringAsync(text, vm).ConfigureAwait(false);
                 await NavigationManager.LoadPicFromStringAsync(text, vm).ConfigureAwait(false);