RunState.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. namespace Terminal.Gui;
  2. /// <summary>The execution state for a <see cref="Toplevel"/> view.</summary>
  3. public class RunState : IDisposable
  4. {
  5. /// <summary>Initializes a new <see cref="RunState"/> class.</summary>
  6. /// <param name="view"></param>
  7. public RunState (Toplevel view) { Toplevel = view; }
  8. /// <summary>The <see cref="Toplevel"/> belonging to this <see cref="RunState"/>.</summary>
  9. public Toplevel Toplevel { get; internal set; }
  10. /// <summary>Releases all resource used by the <see cref="RunState"/> object.</summary>
  11. /// <remarks>Call <see cref="Dispose()"/> when you are finished using the <see cref="RunState"/>.</remarks>
  12. /// <remarks>
  13. /// <see cref="Dispose()"/> method leaves the <see cref="RunState"/> in an unusable state. After calling
  14. /// <see cref="Dispose()"/>, you must release all references to the <see cref="RunState"/> so the garbage collector can
  15. /// reclaim the memory that the <see cref="RunState"/> was occupying.
  16. /// </remarks>
  17. public void Dispose ()
  18. {
  19. Dispose (true);
  20. GC.SuppressFinalize (this);
  21. #if DEBUG_IDISPOSABLE
  22. WasDisposed = true;
  23. #endif
  24. }
  25. /// <summary>Releases all resource used by the <see cref="RunState"/> object.</summary>
  26. /// <param name="disposing">If set to <see langword="true"/> we are disposing and should dispose held objects.</param>
  27. protected virtual void Dispose (bool disposing)
  28. {
  29. if (Toplevel is { } && disposing)
  30. {
  31. // Previously we were requiring Toplevel be disposed here.
  32. // But that is not correct becaue `Begin` didn't create the TopLevel, `Init` did; thus
  33. // disposing should be done by `Shutdown`, not `End`.
  34. throw new InvalidOperationException (
  35. "Toplevel must be null before calling Application.RunState.Dispose"
  36. );
  37. }
  38. }
  39. #if DEBUG_IDISPOSABLE
  40. /// <summary>For debug (see DEBUG_IDISPOSABLE define) purposes to verify objects are being disposed properly</summary>
  41. public bool WasDisposed;
  42. /// <summary>For debug (see DEBUG_IDISPOSABLE define) purposes to verify objects are being disposed properly</summary>
  43. public int DisposedCount = 0;
  44. /// <summary>For debug (see DEBUG_IDISPOSABLE define) purposes; the runstate instances that have been created</summary>
  45. public static List<RunState> Instances = new ();
  46. /// <summary>Creates a new RunState object.</summary>
  47. public RunState () { Instances.Add (this); }
  48. #endif
  49. }