Responder.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  50. /// <param name="mouseEvent">Contains the details about the mouse event.</param>
  51. public virtual bool MouseEvent (MouseEvent mouseEvent) { return false; }
  52. /// <summary>Method invoked when the <see cref="CanFocus"/> property from a view is changed.</summary>
  53. public virtual void OnCanFocusChanged () { }
  54. /// <summary>Method invoked when the <see cref="Enabled"/> property from a view is changed.</summary>
  55. public virtual void OnEnabledChanged () { }
  56. /// <summary>Method invoked when a view gets focus.</summary>
  57. /// <param name="view">The view that is losing focus.</param>
  58. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  59. public virtual bool OnEnter (View view) { return false; }
  60. /// <summary>Method invoked when a view loses focus.</summary>
  61. /// <param name="view">The view that is getting focus.</param>
  62. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  63. public virtual bool OnLeave (View view) { return false; }
  64. /// <summary>
  65. /// Called when the mouse first enters the view; the view will now receives mouse events until the mouse leaves
  66. /// the view. At which time, <see cref="OnMouseLeave(Gui.MouseEvent)"/> will be called.
  67. /// </summary>
  68. /// <param name="mouseEvent"></param>
  69. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  70. public virtual bool OnMouseEnter (MouseEvent mouseEvent) { return false; }
  71. /// <summary>
  72. /// Called when the mouse has moved outside of the view; the view will no longer receive mouse events (until the
  73. /// mouse moves within the view again and <see cref="OnMouseEnter(Gui.MouseEvent)"/> is called).
  74. /// </summary>
  75. /// <param name="mouseEvent"></param>
  76. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  77. public virtual bool OnMouseLeave (MouseEvent mouseEvent) { return false; }
  78. /// <summary>Method invoked when the <see cref="Visible"/> property from a view is changed.</summary>
  79. public virtual void OnVisibleChanged () { }
  80. /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
  81. /// <remarks>
  82. /// If disposing equals true, the method has been called directly or indirectly by a user's code. Managed and
  83. /// unmanaged resources can be disposed. If disposing equals false, the method has been called by the runtime from
  84. /// inside the finalizer and you should not reference other objects. Only unmanaged resources can be disposed.
  85. /// </remarks>
  86. /// <param name="disposing"></param>
  87. protected virtual void Dispose (bool disposing)
  88. {
  89. if (!disposedValue)
  90. {
  91. if (disposing)
  92. {
  93. // TODO: dispose managed state (managed objects)
  94. }
  95. disposedValue = true;
  96. }
  97. }
  98. // TODO: v2 - nuke this
  99. /// <summary>Utilty function to determine <paramref name="method"/> is overridden in the <paramref name="subclass"/>.</summary>
  100. /// <param name="subclass">The view.</param>
  101. /// <param name="method">The method name.</param>
  102. /// <returns><see langword="true"/> if it's overridden, <see langword="false"/> otherwise.</returns>
  103. internal static bool IsOverridden (Responder subclass, string method)
  104. {
  105. MethodInfo m = subclass.GetType ()
  106. .GetMethod (
  107. method,
  108. BindingFlags.Instance
  109. | BindingFlags.Public
  110. | BindingFlags.NonPublic
  111. | BindingFlags.DeclaredOnly
  112. );
  113. if (m == null)
  114. {
  115. return false;
  116. }
  117. return m.GetBaseDefinition ().DeclaringType != m.DeclaringType;
  118. }
  119. #if DEBUG_IDISPOSABLE
  120. /// <summary>For debug purposes to verify objects are being disposed properly</summary>
  121. public bool WasDisposed;
  122. /// <summary>For debug purposes to verify objects are being disposed properly</summary>
  123. public int DisposedCount = 0;
  124. /// <summary>For debug purposes</summary>
  125. public static List<Responder> Instances = new ();
  126. /// <summary>For debug purposes</summary>
  127. public Responder () { Instances.Add (this); }
  128. #endif
  129. }