#nullable enable
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
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 (IConsoleDriver? driver = null, string? driverName = null)
{
ApplicationImpl.Instance.Init (driver, driverName ?? ForceDriver);
}
internal static int MainThreadId
{
get => ((ApplicationImpl)ApplicationImpl.Instance).MainThreadId;
set => ((ApplicationImpl)ApplicationImpl.Instance).MainThreadId = value;
}
internal static void SubscribeDriverEvents ()
{
ArgumentNullException.ThrowIfNull (Driver);
Driver.SizeChanged += Driver_SizeChanged;
Driver.KeyDown += Driver_KeyDown;
Driver.KeyUp += Driver_KeyUp;
Driver.MouseEvent += Driver_MouseEvent;
}
internal static void UnsubscribeDriverEvents ()
{
ArgumentNullException.ThrowIfNull (Driver);
Driver.SizeChanged -= Driver_SizeChanged;
Driver.KeyDown -= Driver_KeyDown;
Driver.KeyUp -= Driver_KeyUp;
Driver.MouseEvent -= Driver_MouseEvent;
}
private static void Driver_SizeChanged (object? sender, SizeChangedEventArgs e)
{
RaiseScreenChangedEvent (new Rectangle (new (0, 0), e.Size!.Value));
}
private static void Driver_KeyDown (object? sender, Key e) { RaiseKeyDownEvent (e); }
private static void Driver_KeyUp (object? sender, Key e) { RaiseKeyUpEvent (e); }
private static void Driver_MouseEvent (object? sender, MouseEventArgs e) { RaiseMouseEvent (e); }
/// Gets a list of types and type names that are available.
///
[RequiresUnreferencedCode ("AOT")]
public static (List, List) GetDriverTypes ()
{
// use reflection to get the list of drivers
List driverTypes = new ();
// Only inspect the IConsoleDriver assembly
var asm = typeof (IConsoleDriver).Assembly;
foreach (Type? type in asm.GetTypes ())
{
if (typeof (IConsoleDriver).IsAssignableFrom (type) &&
type is { IsAbstract: false, IsClass: true })
{
driverTypes.Add (type);
}
}
List driverTypeNames = driverTypes
.Where (d => !typeof (IConsoleDriverFacade).IsAssignableFrom (d))
.Select (d => d!.Name)
.Union (["dotnet", "windows", "unix", "fake"])
.ToList ()!;
return (driverTypes, driverTypeNames);
}
/// 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;
}
///
/// This event is raised after the and methods have been called.
///
///
/// Intended to support unit tests that need to know when the application has been initialized.
///
public static event EventHandler>? InitializedChanged;
///
/// Raises the event.
///
internal static void OnInitializedChanged (object sender, EventArgs e)
{
Application.InitializedChanged?.Invoke (sender, e);
}
}