Responder.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Reflection;
  2. namespace Terminal.Gui;
  3. /// <summary>Responder base class implemented by objects that want to participate on keyboard and mouse input.</summary>
  4. public class Responder : IDisposable
  5. {
  6. private bool _disposedValue;
  7. /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resource.</summary>
  8. public void Dispose ()
  9. {
  10. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  11. Disposing?.Invoke (this, EventArgs.Empty);
  12. Dispose (true);
  13. GC.SuppressFinalize (this);
  14. #if DEBUG_IDISPOSABLE
  15. WasDisposed = true;
  16. foreach (Responder instance in Instances.Where (x => x.WasDisposed).ToList ())
  17. {
  18. Instances.Remove (instance);
  19. }
  20. #endif
  21. }
  22. /// <summary>Event raised when <see cref="Dispose()"/> has been called to signal that this object is being disposed.</summary>
  23. public event EventHandler Disposing;
  24. /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
  25. /// <remarks>
  26. /// If disposing equals true, the method has been called directly or indirectly by a user's code. Managed and
  27. /// unmanaged resources can be disposed. If disposing equals false, the method has been called by the runtime from
  28. /// inside the finalizer and you should not reference other objects. Only unmanaged resources can be disposed.
  29. /// </remarks>
  30. /// <param name="disposing"></param>
  31. protected virtual void Dispose (bool disposing)
  32. {
  33. if (!_disposedValue)
  34. {
  35. if (disposing)
  36. {
  37. // TODO: dispose managed state (managed objects)
  38. }
  39. _disposedValue = true;
  40. }
  41. }
  42. // TODO: v2 - nuke this
  43. /// <summary>Utilty function to determine <paramref name="method"/> is overridden in the <paramref name="subclass"/>.</summary>
  44. /// <param name="subclass">The view.</param>
  45. /// <param name="method">The method name.</param>
  46. /// <returns><see langword="true"/> if it's overridden, <see langword="false"/> otherwise.</returns>
  47. internal static bool IsOverridden (Responder subclass, string method)
  48. {
  49. MethodInfo m = subclass.GetType ()
  50. .GetMethod (
  51. method,
  52. BindingFlags.Instance
  53. | BindingFlags.Public
  54. | BindingFlags.NonPublic
  55. | BindingFlags.DeclaredOnly
  56. );
  57. if (m is null)
  58. {
  59. return false;
  60. }
  61. return m.GetBaseDefinition ().DeclaringType != m.DeclaringType;
  62. }
  63. #if DEBUG_IDISPOSABLE
  64. /// <summary>For debug purposes to verify objects are being disposed properly</summary>
  65. public bool WasDisposed;
  66. /// <summary>For debug purposes to verify objects are being disposed properly</summary>
  67. public int DisposedCount = 0;
  68. /// <summary>For debug purposes</summary>
  69. public static List<Responder> Instances = new ();
  70. /// <summary>For debug purposes</summary>
  71. public Responder () { Instances.Add (this); }
  72. #endif
  73. }