namespace Terminal.Gui;
/// The execution state for a view.
public class RunState : IDisposable
{
/// Initializes a new class.
///
public RunState (Toplevel view) { Toplevel = view; }
/// The belonging to this .
public Toplevel Toplevel { get; internal set; }
/// Releases all resource used by the object.
/// Call when you are finished using the .
///
/// method leaves the in an unusable state. After calling
/// , you must release all references to the so the garbage collector can
/// reclaim the memory that the was occupying.
///
public void Dispose ()
{
Dispose (true);
GC.SuppressFinalize (this);
#if DEBUG_IDISPOSABLE
WasDisposed = true;
#endif
}
/// Releases all resource used by the object.
/// If set to we are disposing and should dispose held objects.
protected virtual void Dispose (bool disposing)
{
if (Toplevel is { } && disposing)
{
// Previously we were requiring Toplevel be disposed here.
// But that is not correct becaue `Begin` didn't create the TopLevel, `Init` did; thus
// disposing should be done by `Shutdown`, not `End`.
throw new InvalidOperationException (
"Toplevel must be null before calling Application.RunState.Dispose"
);
}
}
#if DEBUG_IDISPOSABLE
/// For debug (see DEBUG_IDISPOSABLE define) purposes to verify objects are being disposed properly
public bool WasDisposed;
/// For debug (see DEBUG_IDISPOSABLE define) purposes to verify objects are being disposed properly
public int DisposedCount = 0;
/// For debug (see DEBUG_IDISPOSABLE define) purposes; the runstate instances that have been created
public static List Instances = new ();
/// Creates a new RunState object.
public RunState () { Instances.Add (this); }
#endif
}