HistoryTextItemEventArgs.cs 5.1 KB

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