Responder.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. [CanBeNull]
  24. public event EventHandler Disposing;
  25. /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
  26. /// <remarks>
  27. /// If disposing equals true, the method has been called directly or indirectly by a user's code. Managed and
  28. /// unmanaged resources can be disposed. If disposing equals false, the method has been called by the runtime from
  29. /// inside the finalizer and you should not reference other objects. Only unmanaged resources can be disposed.
  30. /// </remarks>
  31. /// <param name="disposing"></param>
  32. protected virtual void Dispose (bool disposing)
  33. {
  34. if (!_disposedValue)
  35. {
  36. if (disposing)
  37. {
  38. // TODO: dispose managed state (managed objects)
  39. }
  40. _disposedValue = true;
  41. }
  42. }
  43. // TODO: v2 - nuke this
  44. /// <summary>Utilty function to determine <paramref name="method"/> is overridden in the <paramref name="subclass"/>.</summary>
  45. /// <param name="subclass">The view.</param>
  46. /// <param name="method">The method name.</param>
  47. /// <returns><see langword="true"/> if it's overridden, <see langword="false"/> otherwise.</returns>
  48. internal static bool IsOverridden (Responder subclass, string method)
  49. {
  50. MethodInfo m = subclass.GetType ()
  51. .GetMethod (
  52. method,
  53. BindingFlags.Instance
  54. | BindingFlags.Public
  55. | BindingFlags.NonPublic
  56. | BindingFlags.DeclaredOnly
  57. );
  58. if (m is null)
  59. {
  60. return false;
  61. }
  62. return m.GetBaseDefinition ().DeclaringType != m.DeclaringType;
  63. }
  64. #if DEBUG_IDISPOSABLE
  65. /// <summary>For debug purposes to verify objects are being disposed properly</summary>
  66. public bool WasDisposed;
  67. /// <summary>For debug purposes to verify objects are being disposed properly</summary>
  68. public int DisposedCount = 0;
  69. /// <summary>For debug purposes</summary>
  70. public static List<Responder> Instances = new ();
  71. /// <summary>For debug purposes</summary>
  72. public Responder () { Instances.Add (this); }
  73. #endif
  74. }