ClipboardBase.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. return GetClipboardDataImpl ();
  24. } catch (NotSupportedException ex) {
  25. throw new NotSupportedException ("Failed to copy from the OS clipboard.", ex);
  26. }
  27. }
  28. /// <summary>
  29. /// Returns the contents of the OS clipboard if possible. Implemented by <see cref="ConsoleDriver"/>-specific subclasses.
  30. /// </summary>
  31. /// <returns>The contents of the OS clipboard if successful.</returns>
  32. /// <exception cref="NotSupportedException">Thrown if it was not possible to copy from the OS clipboard.</exception>
  33. protected abstract string GetClipboardDataImpl ();
  34. /// <summary>
  35. /// Pastes the <paramref name="text"/> to the OS clipboard if possible.
  36. /// </summary>
  37. /// <param name="text">The text to paste to the OS clipboard.</param>
  38. /// <exception cref="NotSupportedException">Thrown if it was not possible to paste to the OS clipboard.</exception>
  39. public void SetClipboardData (string text)
  40. {
  41. try {
  42. SetClipboardDataImpl (text);
  43. } catch (NotSupportedException ex) {
  44. throw new NotSupportedException ("Failed to paste to the OS clipboard.", ex);
  45. }
  46. }
  47. /// <summary>
  48. /// Pastes the <paramref name="text"/> to the OS clipboard if possible. Implemented by <see cref="ConsoleDriver"/>-specific subclasses.
  49. /// </summary>
  50. /// <param name="text">The text to paste to the OS clipboard.</param>
  51. /// <exception cref="NotSupportedException">Thrown if it was not possible to paste to the OS clipboard.</exception>
  52. protected abstract void SetClipboardDataImpl (string text);
  53. /// <summary>
  54. /// Copies the contents of the OS clipboard to <paramref name="result"/> if possible.
  55. /// </summary>
  56. /// <param name="result">The contents of the OS clipboard if successful, <see cref="string.Empty"/> if not.</param>
  57. /// <returns><see langword="true"/> the OS clipboard was retrieved, <see langword="false"/> otherwise.</returns>
  58. public bool TryGetClipboardData (out string result)
  59. {
  60. // Don't even try to read because environment is not set up.
  61. if (!IsSupported) {
  62. result = null;
  63. return false;
  64. }
  65. try {
  66. result = GetClipboardDataImpl ();
  67. while (result == null) {
  68. result = GetClipboardDataImpl ();
  69. }
  70. return true;
  71. } catch (NotSupportedException ex) {
  72. System.Diagnostics.Debug.WriteLine ($"TryGetClipboardData: {ex.Message}");
  73. result = null;
  74. return false;
  75. }
  76. }
  77. /// <summary>
  78. /// Pastes the <paramref name="text"/> to the OS clipboard if possible.
  79. /// </summary>
  80. /// <param name="text">The text to paste to the OS clipboard.</param>
  81. /// <returns><see langword="true"/> the OS clipboard was set, <see langword="false"/> otherwise.</returns>
  82. public bool TrySetClipboardData (string text)
  83. {
  84. // Don't even try to set because environment is not set up
  85. if (!IsSupported) {
  86. return false;
  87. }
  88. try {
  89. SetClipboardDataImpl (text);
  90. return true;
  91. } catch (NotSupportedException ex) {
  92. System.Diagnostics.Debug.WriteLine ($"TrySetClipboardData: {ex.Message}");
  93. return false;
  94. }
  95. }
  96. }
  97. }