Clipboard.cs 3.3 KB

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