TextView.Clipboard.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. namespace Terminal.Gui.Views;
  2. public partial class TextView
  3. {
  4. private void SetClipboard (string text)
  5. {
  6. if (text is { })
  7. {
  8. Clipboard.Contents = text;
  9. }
  10. }
  11. /// <summary>Copy the selected text to the clipboard contents.</summary>
  12. public void Copy ()
  13. {
  14. SetWrapModel ();
  15. if (IsSelecting)
  16. {
  17. _copiedText = GetRegion (out _copiedCellsList);
  18. SetClipboard (_copiedText);
  19. _copyWithoutSelection = false;
  20. }
  21. else
  22. {
  23. List<Cell> currentLine = GetCurrentLine ();
  24. _copiedCellsList.Add (currentLine);
  25. _copiedText = Cell.ToString (currentLine);
  26. SetClipboard (_copiedText);
  27. _copyWithoutSelection = true;
  28. }
  29. UpdateWrapModel ();
  30. DoNeededAction ();
  31. }
  32. /// <summary>Cut the selected text to the clipboard contents.</summary>
  33. public void Cut ()
  34. {
  35. SetWrapModel ();
  36. _copiedText = GetRegion (out _copiedCellsList);
  37. SetClipboard (_copiedText);
  38. if (!_isReadOnly)
  39. {
  40. ClearRegion ();
  41. _historyText.Add (
  42. [new (GetCurrentLine ())],
  43. CursorPosition,
  44. TextEditingLineStatus.Replaced
  45. );
  46. }
  47. UpdateWrapModel ();
  48. IsSelecting = false;
  49. DoNeededAction ();
  50. OnContentsChanged ();
  51. }
  52. /// <summary>Paste the clipboard contents into the current selected position.</summary>
  53. public void Paste ()
  54. {
  55. if (_isReadOnly)
  56. {
  57. return;
  58. }
  59. SetWrapModel ();
  60. string? contents = Clipboard.Contents;
  61. if (_copyWithoutSelection && contents!.FirstOrDefault (x => x is '\n' or '\r') == 0)
  62. {
  63. List<Cell> runeList = contents is null ? [] : Cell.ToCellList (contents);
  64. List<Cell> currentLine = GetCurrentLine ();
  65. _historyText.Add ([new (currentLine)], CursorPosition);
  66. List<List<Cell>> addedLine = [new (currentLine), runeList];
  67. _historyText.Add (
  68. [.. addedLine],
  69. CursorPosition,
  70. TextEditingLineStatus.Added
  71. );
  72. _model.AddLine (CurrentRow, runeList);
  73. CurrentRow++;
  74. _historyText.Add (
  75. [new (GetCurrentLine ())],
  76. CursorPosition,
  77. TextEditingLineStatus.Replaced
  78. );
  79. SetNeedsDraw ();
  80. OnContentsChanged ();
  81. }
  82. else
  83. {
  84. if (IsSelecting)
  85. {
  86. ClearRegion ();
  87. }
  88. _copyWithoutSelection = false;
  89. InsertAllText (contents!, true);
  90. if (IsSelecting)
  91. {
  92. _historyText.ReplaceLast (
  93. [new (GetCurrentLine ())],
  94. CursorPosition,
  95. TextEditingLineStatus.Original
  96. );
  97. }
  98. SetNeedsDraw ();
  99. }
  100. UpdateWrapModel ();
  101. IsSelecting = false;
  102. DoNeededAction ();
  103. }
  104. private void ProcessCopy ()
  105. {
  106. ResetColumnTrack ();
  107. Copy ();
  108. }
  109. private void ProcessCut ()
  110. {
  111. ResetColumnTrack ();
  112. Cut ();
  113. }
  114. private void ProcessPaste ()
  115. {
  116. ResetColumnTrack ();
  117. if (_isReadOnly)
  118. {
  119. return;
  120. }
  121. Paste ();
  122. }
  123. private void AppendClipboard (string text) { Clipboard.Contents += text; }
  124. }