ClipboardPage.xaml.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Avalonia;
  5. using Avalonia.Controls;
  6. using Avalonia.Controls.Notifications;
  7. using Avalonia.Input;
  8. using Avalonia.Interactivity;
  9. using Avalonia.Markup.Xaml;
  10. using Avalonia.Platform;
  11. using Avalonia.Platform.Storage;
  12. using Avalonia.Platform.Storage.FileIO;
  13. namespace ControlCatalog.Pages
  14. {
  15. public partial class ClipboardPage : UserControl
  16. {
  17. private INotificationManager? _notificationManager;
  18. private INotificationManager NotificationManager => _notificationManager
  19. ??= new WindowNotificationManager(TopLevel.GetTopLevel(this)!);
  20. public ClipboardPage()
  21. {
  22. InitializeComponent();
  23. }
  24. private TextBox ClipboardContent => this.Get<TextBox>("ClipboardContent");
  25. private void InitializeComponent()
  26. {
  27. AvaloniaXamlLoader.Load(this);
  28. }
  29. private async void CopyText(object? sender, RoutedEventArgs args)
  30. {
  31. if (TopLevel.GetTopLevel(this)?.Clipboard is { } clipboard && ClipboardContent is { } clipboardContent)
  32. await clipboard.SetTextAsync(clipboardContent.Text ?? String.Empty);
  33. }
  34. private async void PasteText(object? sender, RoutedEventArgs args)
  35. {
  36. if (TopLevel.GetTopLevel(this)?.Clipboard is { } clipboard)
  37. {
  38. ClipboardContent.Text = await clipboard.GetTextAsync();
  39. }
  40. }
  41. private async void CopyTextDataObject(object? sender, RoutedEventArgs args)
  42. {
  43. if (TopLevel.GetTopLevel(this)?.Clipboard is { } clipboard)
  44. {
  45. var dataObject = new DataObject();
  46. dataObject.Set(DataFormats.Text, ClipboardContent.Text ?? string.Empty);
  47. await clipboard.SetDataObjectAsync(dataObject);
  48. }
  49. }
  50. private async void PasteTextDataObject(object? sender, RoutedEventArgs args)
  51. {
  52. if (TopLevel.GetTopLevel(this)?.Clipboard is { } clipboard)
  53. {
  54. ClipboardContent.Text = await clipboard.GetDataAsync(DataFormats.Text) as string ?? string.Empty;
  55. }
  56. }
  57. private async void CopyFilesDataObject(object? sender, RoutedEventArgs args)
  58. {
  59. if (TopLevel.GetTopLevel(this)?.Clipboard is { } clipboard)
  60. {
  61. var storageProvider = TopLevel.GetTopLevel(this)!.StorageProvider;
  62. var filesPath = (ClipboardContent.Text ?? string.Empty)
  63. .Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
  64. if (filesPath.Length == 0)
  65. {
  66. return;
  67. }
  68. List<string> invalidFile = new(filesPath.Length);
  69. List<IStorageFile> files = new(filesPath.Length);
  70. for (int i = 0; i < filesPath.Length; i++)
  71. {
  72. var file = await storageProvider.TryGetFileFromPathAsync(filesPath[i]);
  73. if (file is null)
  74. {
  75. invalidFile.Add(filesPath[i]);
  76. }
  77. else
  78. {
  79. files.Add(file);
  80. }
  81. }
  82. if (invalidFile.Count > 0)
  83. {
  84. NotificationManager.Show(new Notification("Warning", "There is one o more invalid path.", NotificationType.Warning));
  85. }
  86. if (files.Count > 0)
  87. {
  88. var dataObject = new DataObject();
  89. dataObject.Set(DataFormats.Files, files);
  90. await clipboard.SetDataObjectAsync(dataObject);
  91. NotificationManager.Show(new Notification("Success", "Copy completated.", NotificationType.Success));
  92. }
  93. else
  94. {
  95. NotificationManager.Show(new Notification("Warning", "Any files to copy in Clipboard.", NotificationType.Warning));
  96. }
  97. }
  98. }
  99. private async void PasteFilesDataObject(object? sender, RoutedEventArgs args)
  100. {
  101. if (TopLevel.GetTopLevel(this)?.Clipboard is { } clipboard)
  102. {
  103. var files = await clipboard.GetDataAsync(DataFormats.Files) as IEnumerable<Avalonia.Platform.Storage.IStorageItem>;
  104. ClipboardContent.Text = files != null ? string.Join(Environment.NewLine, files.Select(f => f.TryGetLocalPath() ?? f.Name)) : string.Empty;
  105. }
  106. }
  107. private async void GetFormats(object sender, RoutedEventArgs args)
  108. {
  109. if (TopLevel.GetTopLevel(this)?.Clipboard is { } clipboard)
  110. {
  111. var formats = await clipboard.GetFormatsAsync();
  112. ClipboardContent.Text = string.Join(Environment.NewLine, formats);
  113. }
  114. }
  115. private async void Clear(object sender, RoutedEventArgs args)
  116. {
  117. if (TopLevel.GetTopLevel(this)?.Clipboard is { } clipboard)
  118. {
  119. await clipboard.ClearAsync();
  120. }
  121. }
  122. }
  123. }