Application.Lifecycle.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Diagnostics;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Reflection;
  4. using Microsoft.VisualBasic;
  5. using Terminal.Gui.App;
  6. using Terminal.Gui.Drivers;
  7. using Terminal.Gui.Views;
  8. namespace Terminal.Gui.App;
  9. public static partial class Application // Lifecycle (Init/Shutdown)
  10. {
  11. /// <summary>
  12. /// Creates a new <see cref="IApplication"/> instance.
  13. /// </summary>
  14. /// <remarks>
  15. /// The recommended pattern is for developers to call <c>Application.Create()</c> and then use the returned
  16. /// <see cref="IApplication"/> instance for all subsequent application operations.
  17. /// </remarks>
  18. /// <returns>A new <see cref="IApplication"/> instance.</returns>
  19. /// <exception cref="InvalidOperationException">
  20. /// Thrown if the legacy static Application model has already been used in this process.
  21. /// </exception>
  22. public static IApplication Create ()
  23. {
  24. ApplicationImpl.MarkInstanceBasedModelUsed ();
  25. return new ApplicationImpl ();
  26. }
  27. /// <inheritdoc cref="IApplication.Init"/>
  28. [RequiresUnreferencedCode ("AOT")]
  29. [RequiresDynamicCode ("AOT")]
  30. [Obsolete ("The legacy static Application object is going away.")]
  31. public static void Init (string? driverName = null)
  32. {
  33. //Debug.Fail ("Application.Init() called - parallelizable tests should not use legacy static Application model");
  34. ApplicationImpl.Instance.Init (driverName ?? ForceDriver);
  35. }
  36. /// <summary>
  37. /// Gets or sets the main thread ID for the application.
  38. /// </summary>
  39. [Obsolete ("The legacy static Application object is going away.")]
  40. public static int? MainThreadId
  41. {
  42. get => ApplicationImpl.Instance.MainThreadId;
  43. internal set => ApplicationImpl.Instance.MainThreadId = value;
  44. }
  45. /// <inheritdoc cref="IApplication.Shutdown"/>
  46. [Obsolete ("The legacy static Application object is going away.")]
  47. public static void Shutdown () => ApplicationImpl.Instance.Shutdown ();
  48. /// <inheritdoc cref="IApplication.Initialized"/>
  49. [Obsolete ("The legacy static Application object is going away.")]
  50. public static bool Initialized
  51. {
  52. get => ApplicationImpl.Instance.Initialized;
  53. internal set => ApplicationImpl.Instance.Initialized = value;
  54. }
  55. /// <inheritdoc cref="IApplication.InitializedChanged"/>
  56. [Obsolete ("The legacy static Application object is going away.")]
  57. public static event EventHandler<EventArgs<bool>>? InitializedChanged
  58. {
  59. add => ApplicationImpl.Instance.InitializedChanged += value;
  60. remove => ApplicationImpl.Instance.InitializedChanged -= value;
  61. }
  62. // IMPORTANT: Ensure all property/fields are reset here. See Init_ResetState_Resets_Properties unit test.
  63. // Encapsulate all setting of initial state for Application; Having
  64. // this in a function like this ensures we don't make mistakes in
  65. // guaranteeing that the state of this singleton is deterministic when Init
  66. // starts running and after Shutdown returns.
  67. [Obsolete ("The legacy static Application object is going away.")]
  68. internal static void ResetState (bool ignoreDisposed = false)
  69. {
  70. // Use the static reset method to bypass the fence check
  71. ApplicationImpl.ResetStateStatic (ignoreDisposed);
  72. }
  73. }