Responder.cs 5.1 KB

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