ClipboardBase.cs 4.1 KB

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