ClipboardBase.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #nullable disable
  2. using System.Diagnostics;
  3. namespace Terminal.Gui.App;
  4. /// <summary>Shared abstract class to enforce rules from the implementation of the <see cref="IClipboard"/> interface.</summary>
  5. public abstract class ClipboardBase : IClipboard
  6. {
  7. /// <summary>Returns true if the environmental dependencies are in place to interact with the OS clipboard</summary>
  8. public abstract bool IsSupported { get; }
  9. /// <summary>Returns the contents of the OS clipboard if possible.</summary>
  10. /// <returns>The contents of the OS clipboard if successful.</returns>
  11. /// <exception cref="NotSupportedException">Thrown if it was not possible to copy from the OS clipboard.</exception>
  12. public string GetClipboardData ()
  13. {
  14. try
  15. {
  16. string result = GetClipboardDataImpl ();
  17. if (result is null)
  18. {
  19. return string.Empty;
  20. }
  21. return result;
  22. }
  23. catch (NotSupportedException ex)
  24. {
  25. throw new NotSupportedException ("Failed to copy from the OS clipboard.", ex);
  26. }
  27. }
  28. /// <summary>Pastes the <paramref name="text"/> to the OS clipboard if possible.</summary>
  29. /// <param name="text">The text to paste to the OS clipboard.</param>
  30. /// <exception cref="NotSupportedException">Thrown if it was not possible to paste to the OS clipboard.</exception>
  31. public void SetClipboardData (string text)
  32. {
  33. if (text is null)
  34. {
  35. throw new ArgumentNullException (nameof (text));
  36. }
  37. try
  38. {
  39. SetClipboardDataImpl (text);
  40. }
  41. catch (NotSupportedException ex)
  42. {
  43. throw new NotSupportedException ("Failed to paste to the OS clipboard.", ex);
  44. }
  45. }
  46. /// <summary>Copies the contents of the OS clipboard to <paramref name="result"/> if possible.</summary>
  47. /// <param name="result">The contents of the OS clipboard if successful.</param>
  48. /// <returns><see langword="true"/> the OS clipboard was retrieved, <see langword="false"/> otherwise.</returns>
  49. public bool TryGetClipboardData (out string result)
  50. {
  51. result = string.Empty;
  52. // Don't even try to read because environment is not set up.
  53. if (!IsSupported)
  54. {
  55. return false;
  56. }
  57. try
  58. {
  59. result = GetClipboardDataImpl ();
  60. return true;
  61. }
  62. catch (NotSupportedException ex)
  63. {
  64. Debug.WriteLine ($"TryGetClipboardData: {ex.Message}");
  65. return false;
  66. }
  67. }
  68. /// <summary>Pastes the <paramref name="text"/> to the OS clipboard if possible.</summary>
  69. /// <param name="text">The text to paste to the OS clipboard.</param>
  70. /// <returns><see langword="true"/> the OS clipboard was set, <see langword="false"/> otherwise.</returns>
  71. public bool TrySetClipboardData (string text)
  72. {
  73. // Don't even try to set because environment is not set up
  74. if (!IsSupported)
  75. {
  76. return false;
  77. }
  78. try
  79. {
  80. SetClipboardDataImpl (text);
  81. return true;
  82. }
  83. catch (NotSupportedException ex)
  84. {
  85. Debug.WriteLine ($"TrySetClipboardData: {ex.Message}");
  86. return false;
  87. }
  88. }
  89. /// <summary>
  90. /// Returns the contents of the OS clipboard if possible. Implemented by <see cref="IDriver"/>-specific
  91. /// subclasses.
  92. /// </summary>
  93. /// <returns>The contents of the OS clipboard if successful.</returns>
  94. /// <exception cref="NotSupportedException">Thrown if it was not possible to copy from the OS clipboard.</exception>
  95. protected abstract string GetClipboardDataImpl ();
  96. /// <summary>
  97. /// Pastes the <paramref name="text"/> to the OS clipboard if possible. Implemented by <see cref="IDriver"/>
  98. /// -specific subclasses.
  99. /// </summary>
  100. /// <param name="text">The text to paste to the OS clipboard.</param>
  101. /// <exception cref="NotSupportedException">Thrown if it was not possible to paste to the OS clipboard.</exception>
  102. protected abstract void SetClipboardDataImpl (string text);
  103. }