#nullable enable using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Reflection; using Microsoft.VisualBasic; using Terminal.Gui.App; using Terminal.Gui.Drivers; using Terminal.Gui.Views; namespace Terminal.Gui.App; public static partial class Application // Lifecycle (Init/Shutdown) { /// Initializes a new instance of a Terminal.Gui Application. must be called when the application is closing. /// Call this method once per instance (or after has been called). /// /// This function loads the right for the platform, Creates a . and /// assigns it to /// /// /// must be called when the application is closing (typically after /// has returned) to ensure resources are cleaned up and /// terminal settings /// restored. /// /// /// The function combines /// and /// into a single /// call. An application can use without explicitly calling /// . /// /// /// The to use. If neither or /// are specified the default driver for the platform will be used. /// /// /// The short name (e.g. "dotnet", "windows", "unix", or "fake") of the /// to use. If neither or are /// specified the default driver for the platform will be used. /// [RequiresUnreferencedCode ("AOT")] [RequiresDynamicCode ("AOT")] public static void Init (IDriver? driver = null, string? driverName = null) { ApplicationImpl.Instance.Init (driver, driverName ?? ForceDriver); } /// /// Gets or sets the main thread ID for the application. /// public static int? MainThreadId { get => ((ApplicationImpl)ApplicationImpl.Instance).MainThreadId; set => ((ApplicationImpl)ApplicationImpl.Instance).MainThreadId = value; } /// Shutdown an application initialized with . /// /// Shutdown must be called for every call to or /// to ensure all resources are cleaned /// up (Disposed) /// and terminal settings are restored. /// public static void Shutdown () => ApplicationImpl.Instance.Shutdown (); /// /// Gets whether the application has been initialized with and not yet shutdown with . /// /// /// /// The event is raised after the and methods have been called. /// /// public static bool Initialized { get => ApplicationImpl.Instance.Initialized; internal set => ApplicationImpl.Instance.Initialized = value; } /// public static event EventHandler>? InitializedChanged { add => ApplicationImpl.Instance.InitializedChanged += value; remove => ApplicationImpl.Instance.InitializedChanged -= value; } // IMPORTANT: Ensure all property/fields are reset here. See Init_ResetState_Resets_Properties unit test. // Encapsulate all setting of initial state for Application; Having // this in a function like this ensures we don't make mistakes in // guaranteeing that the state of this singleton is deterministic when Init // starts running and after Shutdown returns. internal static void ResetState (bool ignoreDisposed = false) => ApplicationImpl.Instance?.ResetState (ignoreDisposed); }