ClipboardPasteOperations.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using Avalonia.Input;
  2. using Avalonia.Threading;
  3. using PicView.Avalonia.Navigation;
  4. using PicView.Avalonia.ViewModels;
  5. using PicView.Core.DebugTools;
  6. namespace PicView.Avalonia.Clipboard;
  7. public static class ClipboardPasteOperations
  8. {
  9. /// <summary>
  10. /// Pastes content from the clipboard
  11. /// </summary>
  12. /// <param name="vm">The main view model</param>
  13. public static async Task Paste(MainViewModel vm)
  14. {
  15. var clipboard = ClipboardService.GetClipboard();
  16. if (clipboard == null)
  17. {
  18. return;
  19. }
  20. try
  21. {
  22. // Need to use dispatcher to access clipboard in this instance
  23. var files = await Dispatcher.UIThread.InvokeAsync(async () => await clipboard.GetDataAsync(DataFormats.Files));
  24. if (files != null)
  25. {
  26. await ClipboardFileOperations.PasteFiles(files, vm);
  27. return;
  28. }
  29. // Try to paste text (URLs, file paths)
  30. var text = await clipboard.GetTextAsync();
  31. if (!string.IsNullOrWhiteSpace(text))
  32. {
  33. await NavigationManager.LoadPicFromStringAsync(text, vm).ConfigureAwait(false);
  34. return;
  35. }
  36. // Try to paste image data
  37. await ClipboardImageOperations.PasteClipboardImage(vm);
  38. }
  39. catch (Exception ex)
  40. {
  41. DebugHelper.LogDebug(nameof(ClipboardPasteOperations), nameof(Paste), ex);
  42. }
  43. }
  44. }