Application.Lifecycle.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. ApplicationImpl.Instance.Init (driverName ?? ForceDriver);
  34. }
  35. /// <summary>
  36. /// Gets or sets the main thread ID for the application.
  37. /// </summary>
  38. [Obsolete ("The legacy static Application object is going away.")]
  39. public static int? MainThreadId
  40. {
  41. get => ((ApplicationImpl)ApplicationImpl.Instance).MainThreadId;
  42. set => ((ApplicationImpl)ApplicationImpl.Instance).MainThreadId = value;
  43. }
  44. /// <inheritdoc cref="IApplication.Shutdown"/>
  45. [Obsolete ("The legacy static Application object is going away.")]
  46. public static void Shutdown () => ApplicationImpl.Instance.Shutdown ();
  47. /// <inheritdoc cref="IApplication.Initialized"/>
  48. [Obsolete ("The legacy static Application object is going away.")]
  49. public static bool Initialized
  50. {
  51. get => ApplicationImpl.Instance.Initialized;
  52. internal set => ApplicationImpl.Instance.Initialized = value;
  53. }
  54. /// <inheritdoc cref="IApplication.InitializedChanged"/>
  55. [Obsolete ("The legacy static Application object is going away.")]
  56. public static event EventHandler<EventArgs<bool>>? InitializedChanged
  57. {
  58. add => ApplicationImpl.Instance.InitializedChanged += value;
  59. remove => ApplicationImpl.Instance.InitializedChanged -= value;
  60. }
  61. // IMPORTANT: Ensure all property/fields are reset here. See Init_ResetState_Resets_Properties unit test.
  62. // Encapsulate all setting of initial state for Application; Having
  63. // this in a function like this ensures we don't make mistakes in
  64. // guaranteeing that the state of this singleton is deterministic when Init
  65. // starts running and after Shutdown returns.
  66. [Obsolete ("The legacy static Application object is going away.")]
  67. internal static void ResetState (bool ignoreDisposed = false)
  68. {
  69. // Reset the model usage tracking first to allow access to Instance if needed
  70. ApplicationImpl.ResetModelUsageTracking ();
  71. // Now safe to access Instance for cleanup
  72. ApplicationImpl.Instance?.ResetState (ignoreDisposed);
  73. }
  74. }