ClipboardBase.cs 3.8 KB

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