Responder.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 respond to user interaction.</summary>
  22. public virtual bool Enabled { get; set; } = true;
  23. /// <summary>Gets or sets a value indicating whether this <see cref="Responder"/> has focus.</summary>
  24. /// <value><c>true</c> if has focus; otherwise, <c>false</c>.</value>
  25. public virtual bool HasFocus { get; }
  26. /// <summary>Gets or sets a value indicating whether this <see cref="Responder"/> and all its child controls are displayed.</summary>
  27. public virtual bool Visible { get; set; } = true;
  28. /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resource.</summary>
  29. public void Dispose ()
  30. {
  31. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  32. Disposing?.Invoke (this, EventArgs.Empty);
  33. Dispose (true);
  34. GC.SuppressFinalize (this);
  35. #if DEBUG_IDISPOSABLE
  36. WasDisposed = true;
  37. foreach (Responder instance in Instances.Where (x => x.WasDisposed).ToList ())
  38. {
  39. Instances.Remove (instance);
  40. }
  41. #endif
  42. }
  43. /// <summary>Event raised when <see cref="Dispose()"/> has been called to signal that this object is being disposed.</summary>
  44. public event EventHandler Disposing;
  45. /// <summary>Method invoked when the <see cref="CanFocus"/> property from a view is changed.</summary>
  46. public virtual void OnCanFocusChanged () { }
  47. /// <summary>Method invoked when the <see cref="Enabled"/> property from a view is changed.</summary>
  48. public virtual void OnEnabledChanged () { }
  49. /// <summary>Method invoked when a view gets focus.</summary>
  50. /// <param name="view">The view that is losing focus.</param>
  51. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  52. public virtual bool OnEnter (View view) { return false; }
  53. /// <summary>Method invoked when a view loses focus.</summary>
  54. /// <param name="view">The view that is getting focus.</param>
  55. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  56. public virtual bool OnLeave (View view) { return false; }
  57. /// <summary>Method invoked when the <see cref="Visible"/> property from a view is changed.</summary>
  58. public virtual void OnVisibleChanged () { }
  59. /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
  60. /// <remarks>
  61. /// If disposing equals true, the method has been called directly or indirectly by a user's code. Managed and
  62. /// unmanaged resources can be disposed. If disposing equals false, the method has been called by the runtime from
  63. /// inside the finalizer and you should not reference other objects. Only unmanaged resources can be disposed.
  64. /// </remarks>
  65. /// <param name="disposing"></param>
  66. protected virtual void Dispose (bool disposing)
  67. {
  68. if (!disposedValue)
  69. {
  70. if (disposing)
  71. {
  72. // TODO: dispose managed state (managed objects)
  73. }
  74. disposedValue = true;
  75. }
  76. }
  77. // TODO: v2 - nuke this
  78. /// <summary>Utilty function to determine <paramref name="method"/> is overridden in the <paramref name="subclass"/>.</summary>
  79. /// <param name="subclass">The view.</param>
  80. /// <param name="method">The method name.</param>
  81. /// <returns><see langword="true"/> if it's overridden, <see langword="false"/> otherwise.</returns>
  82. internal static bool IsOverridden (Responder subclass, string method)
  83. {
  84. MethodInfo m = subclass.GetType ()
  85. .GetMethod (
  86. method,
  87. BindingFlags.Instance
  88. | BindingFlags.Public
  89. | BindingFlags.NonPublic
  90. | BindingFlags.DeclaredOnly
  91. );
  92. if (m is null)
  93. {
  94. return false;
  95. }
  96. return m.GetBaseDefinition ().DeclaringType != m.DeclaringType;
  97. }
  98. #if DEBUG_IDISPOSABLE
  99. /// <summary>For debug purposes to verify objects are being disposed properly</summary>
  100. public bool WasDisposed;
  101. /// <summary>For debug purposes to verify objects are being disposed properly</summary>
  102. public int DisposedCount = 0;
  103. /// <summary>For debug purposes</summary>
  104. public static List<Responder> Instances = new ();
  105. /// <summary>For debug purposes</summary>
  106. public Responder () { Instances.Add (this); }
  107. #endif
  108. }