HistoryTextItemEventArgs.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Event arguments for <see cref="HistoryText"/> item events. Used by <see cref="TextField"/> and <see cref="TextView"/>.
  5. /// </summary>
  6. /// <remarks>
  7. /// This class encapsulates information about changes to text, such as the affected lines, cursor positions, and the type of change.
  8. /// It is primarily used to support undo/redo functionality in text editing controls like <see cref="TextView"/> and <see cref="TextField"/>.
  9. /// </remarks>
  10. public class HistoryTextItemEventArgs : EventArgs
  11. {
  12. // TODO: these should all be properties
  13. /// <summary>
  14. /// Gets or sets the cursor position at the time of the change.
  15. /// </summary>
  16. public Point CursorPosition;
  17. /// <summary>
  18. /// Gets or sets the final cursor position after the change is applied.
  19. /// </summary>
  20. public Point FinalCursorPosition;
  21. /// <summary>
  22. /// Gets or sets a value indicating whether the change is part of an undo operation.
  23. /// </summary>
  24. public bool IsUndoing;
  25. /// <summary>
  26. /// Gets or sets the lines of text affected by the change.
  27. /// </summary>
  28. /// <remarks>
  29. /// Each line is represented as a list of <see cref="Cell"/> objects, which include the text and its attributes.
  30. /// </remarks>
  31. public List<List<Cell>> Lines;
  32. /// <summary>
  33. /// Gets or sets the status of the line(s) affected by the change.
  34. /// </summary>
  35. /// <seealso cref="TextEditingLineStatus"/>
  36. public TextEditingLineStatus LineStatus;
  37. /// <summary>
  38. /// Gets or sets the associated <see cref="HistoryTextItemEventArgs"/> for a removed line when an added line replaces it.
  39. /// </summary>
  40. /// <remarks>
  41. /// This property is used to track the relationship between added and removed lines during undo/redo operations.
  42. /// </remarks>
  43. public HistoryTextItemEventArgs? RemovedOnAdded;
  44. /// <summary>
  45. /// Initializes a new instance of the <see cref="HistoryTextItemEventArgs"/> class with the specified lines, cursor position, line status, and associated removed line.
  46. /// </summary>
  47. /// <param name="lines">The lines of text affected by the change.</param>
  48. /// <param name="curPos">The cursor position at the time of the change.</param>
  49. /// <param name="linesStatus">The status of the line(s) affected by the change.</param>
  50. /// <param name="removedOnAdded">The associated <see cref="HistoryTextItemEventArgs"/> for a removed line when an added line replaces it.</param>
  51. public HistoryTextItemEventArgs (List<List<Cell>> lines, Point curPos, TextEditingLineStatus linesStatus, HistoryTextItemEventArgs removedOnAdded)
  52. {
  53. Lines = lines;
  54. CursorPosition = curPos;
  55. LineStatus = linesStatus;
  56. RemovedOnAdded = removedOnAdded;
  57. }
  58. /// <summary>
  59. /// Initializes a new instance of the <see cref="HistoryTextItemEventArgs"/> class by copying an existing instance and associating it with a removed line.
  60. /// </summary>
  61. /// <param name="historyTextItem">The existing <see cref="HistoryTextItemEventArgs"/> to copy.</param>
  62. /// <param name="removedOnAdded">The associated <see cref="HistoryTextItemEventArgs"/> for a removed line when an added line replaces it.</param>
  63. public HistoryTextItemEventArgs (HistoryTextItemEventArgs historyTextItem, HistoryTextItemEventArgs removedOnAdded)
  64. {
  65. RemovedOnAdded = removedOnAdded;
  66. Lines = [.. historyTextItem.Lines];
  67. CursorPosition = new Point (historyTextItem.CursorPosition.X, historyTextItem.CursorPosition.Y);
  68. LineStatus = historyTextItem.LineStatus;
  69. }
  70. /// <summary>
  71. /// Initializes a new instance of the <see cref="HistoryTextItemEventArgs"/> class with the specified lines, cursor position, and line status.
  72. /// </summary>
  73. /// <param name="lines">The lines of text affected by the change.</param>
  74. /// <param name="curPos">The cursor position at the time of the change.</param>
  75. /// <param name="linesStatus">The status of the line(s) affected by the change.</param>
  76. public HistoryTextItemEventArgs (List<List<Cell>> lines, Point curPos, TextEditingLineStatus linesStatus)
  77. {
  78. Lines = lines;
  79. CursorPosition = curPos;
  80. LineStatus = linesStatus;
  81. }
  82. /// <summary>
  83. /// Initializes a new instance of the <see cref="HistoryTextItemEventArgs"/> class by copying an existing instance.
  84. /// </summary>
  85. /// <param name="historyTextItem">The existing <see cref="HistoryTextItemEventArgs"/> to copy.</param>
  86. public HistoryTextItemEventArgs (HistoryTextItemEventArgs historyTextItem)
  87. {
  88. Lines = [.. historyTextItem.Lines];
  89. CursorPosition = new Point (historyTextItem.CursorPosition.X, historyTextItem.CursorPosition.Y);
  90. LineStatus = historyTextItem.LineStatus;
  91. }
  92. /// <summary>
  93. /// Returns a string representation of the <see cref="HistoryTextItemEventArgs"/> instance.
  94. /// </summary>
  95. /// <returns>A string containing the count of lines, cursor position, and line status.</returns>
  96. public override string ToString ()
  97. {
  98. return $"(Count: {Lines.Count}, Cursor: {CursorPosition}, Status: {LineStatus})";
  99. }
  100. }