Responder.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. //
  2. // Core.cs: The core engine for gui.cs
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // Pending:
  8. // - Check for NeedDisplay on the hierarchy and repaint
  9. // - Layout support
  10. // - "Colors" type or "Attributes" type?
  11. // - What to surface as "BackgroundCOlor" when clearing a window, an attribute or colors?
  12. //
  13. // Optimziations
  14. // - Add rendering limitation to the exposed area
  15. using System.Reflection;
  16. namespace Terminal.Gui;
  17. /// <summary>Responder base class implemented by objects that want to participate on keyboard and mouse input.</summary>
  18. public class Responder : IDisposable
  19. {
  20. private bool disposedValue;
  21. /// <summary>Gets or sets a value indicating whether this <see cref="Responder"/> can focus.</summary>
  22. /// <value><c>true</c> if can focus; otherwise, <c>false</c>.</value>
  23. public virtual bool CanFocus { get; set; }
  24. /// <summary>Gets or sets a value indicating whether this <see cref="Responder"/> can respond to user interaction.</summary>
  25. public virtual bool Enabled { get; set; } = true;
  26. /// <summary>Gets or sets a value indicating whether this <see cref="Responder"/> has focus.</summary>
  27. /// <value><c>true</c> if has focus; otherwise, <c>false</c>.</value>
  28. public virtual bool HasFocus { get; }
  29. /// <summary>Gets or sets a value indicating whether this <see cref="Responder"/> and all its child controls are displayed.</summary>
  30. public virtual bool Visible { get; set; } = true;
  31. /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resource.</summary>
  32. public void Dispose ()
  33. {
  34. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  35. Disposing?.Invoke (this, EventArgs.Empty);
  36. Dispose (true);
  37. GC.SuppressFinalize (this);
  38. #if DEBUG_IDISPOSABLE
  39. WasDisposed = true;
  40. foreach (Responder instance in Instances.Where (x => x.WasDisposed).ToList ())
  41. {
  42. Instances.Remove (instance);
  43. }
  44. #endif
  45. }
  46. /// <summary>Event raised when <see cref="Dispose()"/> has been called to signal that this object is being disposed.</summary>
  47. public event EventHandler Disposing;
  48. /// <summary>Method invoked when a mouse event is generated</summary>
  49. /// <remarks>
  50. /// The coordinates are relative to <see cref="View.Bounds"/>.
  51. /// </remarks>
  52. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  53. /// <param name="mouseEvent">Contains the details about the mouse event.</param>
  54. public virtual bool MouseEvent (MouseEvent mouseEvent) { return false; }
  55. /// <summary>Method invoked when the <see cref="CanFocus"/> property from a view is changed.</summary>
  56. public virtual void OnCanFocusChanged () { }
  57. /// <summary>Method invoked when the <see cref="Enabled"/> property from a view is changed.</summary>
  58. public virtual void OnEnabledChanged () { }
  59. /// <summary>Method invoked when a view gets focus.</summary>
  60. /// <param name="view">The view that is losing focus.</param>
  61. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  62. public virtual bool OnEnter (View view) { return false; }
  63. /// <summary>Method invoked when a view loses focus.</summary>
  64. /// <param name="view">The view that is getting focus.</param>
  65. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  66. public virtual bool OnLeave (View view) { return false; }
  67. /// <summary>
  68. /// Called when the mouse first enters the view; the view will now receives mouse events until the mouse leaves
  69. /// the view. At which time, <see cref="OnMouseLeave(Gui.MouseEvent)"/> will be called.
  70. /// </summary>
  71. /// <remarks>
  72. /// The coordinates are relative to <see cref="View.Bounds"/>.
  73. /// </remarks>
  74. /// <param name="mouseEvent"></param>
  75. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  76. public virtual bool OnMouseEnter (MouseEvent mouseEvent) { return false; }
  77. /// <summary>
  78. /// Called when the mouse has moved outside of the view; the view will no longer receive mouse events (until the
  79. /// mouse moves within the view again and <see cref="OnMouseEnter(Gui.MouseEvent)"/> is called).
  80. /// </summary>
  81. /// <remarks>
  82. /// The coordinates are relative to <see cref="View.Bounds"/>.
  83. /// </remarks>
  84. /// <param name="mouseEvent"></param>
  85. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  86. public virtual bool OnMouseLeave (MouseEvent mouseEvent) { return false; }
  87. /// <summary>Method invoked when the <see cref="Visible"/> property from a view is changed.</summary>
  88. public virtual void OnVisibleChanged () { }
  89. /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
  90. /// <remarks>
  91. /// If disposing equals true, the method has been called directly or indirectly by a user's code. Managed and
  92. /// unmanaged resources can be disposed. If disposing equals false, the method has been called by the runtime from
  93. /// inside the finalizer and you should not reference other objects. Only unmanaged resources can be disposed.
  94. /// </remarks>
  95. /// <param name="disposing"></param>
  96. protected virtual void Dispose (bool disposing)
  97. {
  98. if (!disposedValue)
  99. {
  100. if (disposing)
  101. {
  102. // TODO: dispose managed state (managed objects)
  103. }
  104. disposedValue = true;
  105. }
  106. }
  107. // TODO: v2 - nuke this
  108. /// <summary>Utilty function to determine <paramref name="method"/> is overridden in the <paramref name="subclass"/>.</summary>
  109. /// <param name="subclass">The view.</param>
  110. /// <param name="method">The method name.</param>
  111. /// <returns><see langword="true"/> if it's overridden, <see langword="false"/> otherwise.</returns>
  112. internal static bool IsOverridden (Responder subclass, string method)
  113. {
  114. MethodInfo m = subclass.GetType ()
  115. .GetMethod (
  116. method,
  117. BindingFlags.Instance
  118. | BindingFlags.Public
  119. | BindingFlags.NonPublic
  120. | BindingFlags.DeclaredOnly
  121. );
  122. if (m is null)
  123. {
  124. return false;
  125. }
  126. return m.GetBaseDefinition ().DeclaringType != m.DeclaringType;
  127. }
  128. #if DEBUG_IDISPOSABLE
  129. /// <summary>For debug purposes to verify objects are being disposed properly</summary>
  130. public bool WasDisposed;
  131. /// <summary>For debug purposes to verify objects are being disposed properly</summary>
  132. public int DisposedCount = 0;
  133. /// <summary>For debug purposes</summary>
  134. public static List<Responder> Instances = new ();
  135. /// <summary>For debug purposes</summary>
  136. public Responder () { Instances.Add (this); }
  137. #endif
  138. }