Clipboard.cs 3.1 KB

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