Clipboard.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using NStack;
  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 ustring contents;
  28. /// <summary>
  29. /// Gets (copies from) or sets (pastes to) the contents of the OS clipboard.
  30. /// </summary>
  31. public static ustring Contents {
  32. get {
  33. return contents = ustring.Make (Application.Driver.Clipboard.GetClipboardData ());
  34. //try {
  35. // if (IsSupported) {
  36. // return contents = ustring.Make (Application.Driver.Clipboard.GetClipboardData ());
  37. // } else {
  38. // return ustring.Make ("Clipboard not supported"); // contents;
  39. // }
  40. //} catch (Exception) {
  41. // return contents;
  42. //}
  43. }
  44. set {
  45. Application.Driver.Clipboard.SetClipboardData (value.ToString ());
  46. //try {
  47. // if (IsSupported) {
  48. // if (value == null) {
  49. // value = string.Empty;
  50. // }
  51. // Application.Driver.Clipboard.SetClipboardData (value.ToString ());
  52. // }
  53. // contents = value;
  54. //} catch (NotSupportedException e) {
  55. // throw e;
  56. //} catch (Exception) {
  57. // contents = value;
  58. //}
  59. }
  60. }
  61. /// <summary>
  62. /// Returns true if the environmental dependencies are in place to interact with the OS clipboard.
  63. /// </summary>
  64. /// <remarks>
  65. /// </remarks>
  66. public static bool IsSupported { get => Application.Driver.Clipboard.IsSupported; }
  67. /// <summary>
  68. /// Copies the contents of the OS clipboard to <paramref name="result"/> if possible.
  69. /// </summary>
  70. /// <param name="result">The contents of the OS clipboard if successful, <see cref="string.Empty"/> if not.</param>
  71. /// <returns><see langword="true"/> the OS clipboard was retrieved, <see langword="false"/> otherwise.</returns>
  72. public static bool TryGetClipboardData (out string result)
  73. {
  74. if (IsSupported && Application.Driver.Clipboard.TryGetClipboardData (out result)) {
  75. if (contents != result) {
  76. contents = result;
  77. }
  78. return true;
  79. }
  80. result = string.Empty;
  81. return false;
  82. }
  83. /// <summary>
  84. /// Pastes the <paramref name="text"/> to the OS clipboard if possible.
  85. /// </summary>
  86. /// <param name="text">The text to paste to the OS clipboard.</param>
  87. /// <returns><see langword="true"/> the OS clipboard was set, <see langword="false"/> otherwise.</returns>
  88. public static bool TrySetClipboardData (string text)
  89. {
  90. if (IsSupported && Application.Driver.Clipboard.TrySetClipboardData (text)) {
  91. contents = text;
  92. return true;
  93. }
  94. return false;
  95. }
  96. }
  97. }