Application.Lifecycle.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. /// Gets the singleton <see cref="IApplication"/> instance used by the legacy static Application model.
  13. /// </summary>
  14. /// <remarks>
  15. /// <para>
  16. /// For new code, prefer using <see cref="Create"/> to get an instance-based application.
  17. /// This property is provided for backward compatibility and internal use.
  18. /// </para>
  19. /// <para>
  20. /// This property returns the same singleton instance used by the legacy static <see cref="Application"/>
  21. /// methods like <see cref="Init"/> and <see cref="Run(IRunnable, Func{Exception, bool}?)"/>.
  22. /// </para>
  23. /// </remarks>
  24. [Obsolete ("The legacy static Application object is going away. Use Application.Create() for new code.")]
  25. public static IApplication Instance => ApplicationImpl.Instance;
  26. /// <summary>
  27. /// Creates a new <see cref="IApplication"/> instance.
  28. /// </summary>
  29. /// <remarks>
  30. /// The recommended pattern is for developers to call <c>Application.Create()</c> and then use the returned
  31. /// <see cref="IApplication"/> instance for all subsequent application operations.
  32. /// </remarks>
  33. /// <returns>A new <see cref="IApplication"/> instance.</returns>
  34. /// <exception cref="InvalidOperationException">
  35. /// Thrown if the legacy static Application model has already been used in this process.
  36. /// </exception>
  37. public static IApplication Create ()
  38. {
  39. //Debug.Fail ("Application.Create() called");
  40. ApplicationImpl.MarkInstanceBasedModelUsed ();
  41. return new ApplicationImpl ();
  42. }
  43. /// <inheritdoc cref="IApplication.Init"/>
  44. [RequiresUnreferencedCode ("AOT")]
  45. [RequiresDynamicCode ("AOT")]
  46. [Obsolete ("The legacy static Application object is going away.")]
  47. public static void Init (string? driverName = null)
  48. {
  49. //Debug.Fail ("Application.Init() called - parallelizable tests should not use legacy static Application model");
  50. ApplicationImpl.Instance.Init (driverName ?? ForceDriver);
  51. }
  52. /// <summary>
  53. /// Gets or sets the main thread ID for the application.
  54. /// </summary>
  55. [Obsolete ("The legacy static Application object is going away.")]
  56. public static int? MainThreadId
  57. {
  58. get => ApplicationImpl.Instance.MainThreadId;
  59. internal set => ApplicationImpl.Instance.MainThreadId = value;
  60. }
  61. /// <inheritdoc cref="IDisposable.Dispose"/>
  62. [Obsolete ("The legacy static Application object is going away.")]
  63. public static void Shutdown () => ApplicationImpl.Instance.Dispose ();
  64. /// <inheritdoc cref="IApplication.Initialized"/>
  65. [Obsolete ("The legacy static Application object is going away.")]
  66. public static bool Initialized
  67. {
  68. get => ApplicationImpl.Instance.Initialized;
  69. internal set => ApplicationImpl.Instance.Initialized = value;
  70. }
  71. /// <inheritdoc cref="IApplication.InitializedChanged"/>
  72. [Obsolete ("The legacy static Application object is going away.")]
  73. public static event EventHandler<EventArgs<bool>>? InitializedChanged
  74. {
  75. add => ApplicationImpl.Instance.InitializedChanged += value;
  76. remove => ApplicationImpl.Instance.InitializedChanged -= value;
  77. }
  78. // IMPORTANT: Ensure all property/fields are reset here. See Init_ResetState_Resets_Properties unit test.
  79. // Encapsulate all setting of initial state for Application; Having
  80. // this in a function like this ensures we don't make mistakes in
  81. // guaranteeing that the state of this singleton is deterministic when Init
  82. // starts running and after Shutdown returns.
  83. [Obsolete ("The legacy static Application object is going away.")]
  84. internal static void ResetState (bool ignoreDisposed = false)
  85. {
  86. // Use the static reset method to bypass the fence check
  87. ApplicationImpl.ResetStateStatic (ignoreDisposed);
  88. }
  89. }