Clipboard.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. namespace Terminal.Gui.App;
  2. /// <summary>Provides cut, copy, and paste support for the OS clipboard.</summary>
  3. /// <remarks>
  4. /// <para>
  5. /// <b>DEPRECATED:</b> This static class is obsolete. Use <see cref="IApplication.Clipboard"/> instead.
  6. /// </para>
  7. /// <para>On Windows, the <see cref="Clipboard"/> class uses the Windows Clipboard APIs via P/Invoke.</para>
  8. /// <para>
  9. /// On Linux, when not running under Windows Subsystem for Linux (WSL), the <see cref="Clipboard"/> class uses
  10. /// the xclip command line tool. If xclip is not installed, the clipboard will not work.
  11. /// </para>
  12. /// <para>
  13. /// On Linux, when running under Windows Subsystem for Linux (WSL), the <see cref="Clipboard"/> class launches
  14. /// Windows' powershell.exe via WSL interop and uses the "Set-Clipboard" and "Get-Clipboard" Powershell CmdLets.
  15. /// </para>
  16. /// <para>
  17. /// On the Mac, the <see cref="Clipboard"/> class uses the MacO OS X pbcopy and pbpaste command line tools and
  18. /// the Mac clipboard APIs vai P/Invoke.
  19. /// </para>
  20. /// </remarks>
  21. [Obsolete ("Use IApplication.Clipboard instead. The static Clipboard class will be removed in a future release.")]
  22. public static class Clipboard
  23. {
  24. private static string? _contents = string.Empty;
  25. /// <summary>Gets (copies from) or sets (pastes to) the contents of the OS clipboard.</summary>
  26. public static string? Contents
  27. {
  28. get
  29. {
  30. try
  31. {
  32. if (IsSupported)
  33. {
  34. // throw new InvalidOperationException ($"{Application.Driver?.GetType ().Name}.GetClipboardData returned null instead of string.Empty");
  35. string clipData = Application.Driver?.Clipboard?.GetClipboardData () ?? string.Empty;
  36. _contents = clipData;
  37. }
  38. }
  39. catch (Exception)
  40. {
  41. _contents = string.Empty;
  42. }
  43. return _contents;
  44. }
  45. set
  46. {
  47. try
  48. {
  49. if (IsSupported)
  50. {
  51. value ??= string.Empty;
  52. Application.Driver?.Clipboard?.SetClipboardData (value);
  53. }
  54. _contents = value;
  55. }
  56. catch (Exception)
  57. {
  58. _contents = value;
  59. }
  60. }
  61. }
  62. /// <summary>Returns true if the environmental dependencies are in place to interact with the OS clipboard.</summary>
  63. /// <remarks></remarks>
  64. public static bool IsSupported => Application.Driver?.Clipboard?.IsSupported ?? false;
  65. /// <summary>Gets the OS clipboard data if possible.</summary>
  66. /// <param name="result">The clipboard data if successful.</param>
  67. /// <returns><see langword="true"/> if the clipboard data was retrieved successfully; otherwise, <see langword="false"/>.</returns>
  68. public static bool TryGetClipboardData (out string result)
  69. {
  70. result = string.Empty;
  71. if (IsSupported && Application.Driver?.Clipboard is { })
  72. {
  73. return Application.Driver.Clipboard.TryGetClipboardData (out result);
  74. }
  75. return false;
  76. }
  77. /// <summary>Sets the OS clipboard data if possible.</summary>
  78. /// <param name="text">The text to set.</param>
  79. /// <returns><see langword="true"/> if the clipboard data was set successfully; otherwise, <see langword="false"/>.</returns>
  80. public static bool TrySetClipboardData (string text)
  81. {
  82. if (IsSupported && Application.Driver?.Clipboard is { })
  83. {
  84. return Application.Driver.Clipboard.TrySetClipboardData (text);
  85. }
  86. return false;
  87. }
  88. }