Clipboard.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using NStack;
  2. using System;
  3. namespace Terminal.Gui {
  4. /// <summary>
  5. /// Provides cut, copy, and paste support for the clipboard with OS interaction.
  6. /// </summary>
  7. public static class Clipboard {
  8. static ustring contents;
  9. /// <summary>
  10. /// Get or sets the operation system clipboard, otherwise the contents field.
  11. /// </summary>
  12. public static ustring Contents {
  13. get {
  14. try {
  15. return Application.Driver.Clipboard.GetClipboardData ();
  16. } catch (Exception) {
  17. return contents;
  18. }
  19. }
  20. set {
  21. try {
  22. Application.Driver.Clipboard.SetClipboardData (value.ToString ());
  23. contents = value;
  24. } catch (Exception) {
  25. contents = value;
  26. }
  27. }
  28. }
  29. /// <summary>
  30. /// Returns true if the environmental dependencies are in place to interact with the OS clipboard.
  31. /// </summary>
  32. public static bool IsSupported { get; } = Application.Driver.Clipboard.IsSupported;
  33. /// <summary>
  34. /// Gets the operation system clipboard if possible.
  35. /// </summary>
  36. /// <param name="result">Clipboard contents read</param>
  37. /// <returns>true if it was possible to read the OS clipboard.</returns>
  38. public static bool TryGetClipboardData (out string result)
  39. {
  40. if (Application.Driver.Clipboard.TryGetClipboardData (out result)) {
  41. if (contents != result) {
  42. contents = result;
  43. }
  44. return true;
  45. }
  46. return false;
  47. }
  48. /// <summary>
  49. /// Sets the operation system clipboard if possible.
  50. /// </summary>
  51. /// <param name="text"></param>
  52. /// <returns>True if the clipboard content was set successfully.</returns>
  53. public static bool TrySetClipboardData (string text)
  54. {
  55. if (Application.Driver.Clipboard.TrySetClipboardData (text)) {
  56. contents = text;
  57. return true;
  58. }
  59. return false;
  60. }
  61. }
  62. }