ClipboardBase.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /// Get the operation system clipboard.
  17. /// </summary>
  18. /// <exception cref="NotSupportedException">Thrown if it was not possible to read the clipboard contents</exception>
  19. public string GetClipboardData ()
  20. {
  21. try {
  22. return GetClipboardDataImpl ();
  23. } catch (Exception ex) {
  24. throw new NotSupportedException ("Failed to read clipboard.", ex);
  25. }
  26. }
  27. /// <summary>
  28. /// Get the operation system clipboard.
  29. /// </summary>
  30. protected abstract string GetClipboardDataImpl ();
  31. /// <summary>
  32. /// Sets the operation system clipboard.
  33. /// </summary>
  34. /// <param name="text"></param>
  35. /// <exception cref="NotSupportedException">Thrown if it was not possible to set the clipboard contents</exception>
  36. public void SetClipboardData (string text)
  37. {
  38. try {
  39. SetClipboardDataImpl (text);
  40. } catch (Exception ex) {
  41. throw new NotSupportedException ("Failed to write to clipboard.", ex);
  42. }
  43. }
  44. /// <summary>
  45. /// Sets the operation system clipboard.
  46. /// </summary>
  47. /// <param name="text"></param>
  48. protected abstract void SetClipboardDataImpl (string text);
  49. /// <summary>
  50. /// Gets the operation system clipboard if possible.
  51. /// </summary>
  52. /// <param name="result">Clipboard contents read</param>
  53. /// <returns>true if it was possible to read the OS clipboard.</returns>
  54. public bool TryGetClipboardData (out string result)
  55. {
  56. // Don't even try to read because environment is not set up.
  57. if (!IsSupported) {
  58. result = null;
  59. return false;
  60. }
  61. try {
  62. result = GetClipboardDataImpl ();
  63. while (result == null) {
  64. result = GetClipboardDataImpl ();
  65. }
  66. return true;
  67. } catch (Exception) {
  68. result = null;
  69. return false;
  70. }
  71. }
  72. /// <summary>
  73. /// Sets the operation system clipboard if possible.
  74. /// </summary>
  75. /// <param name="text"></param>
  76. /// <returns>True if the clipboard content was set successfully</returns>
  77. public bool TrySetClipboardData (string text)
  78. {
  79. // Don't even try to set because environment is not set up
  80. if (!IsSupported) {
  81. return false;
  82. }
  83. try {
  84. SetClipboardDataImpl (text);
  85. return true;
  86. } catch (Exception) {
  87. return false;
  88. }
  89. }
  90. }
  91. }