ClipboardPage.xaml.cs 5.0 KB

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