Responder.cs 6.6 KB

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