Application.Lifecycle.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. //Debug.Fail ("Application.Create() called");
  25. ApplicationImpl.MarkInstanceBasedModelUsed ();
  26. return new ApplicationImpl ();
  27. }
  28. /// <inheritdoc cref="IApplication.Init"/>
  29. [RequiresUnreferencedCode ("AOT")]
  30. [RequiresDynamicCode ("AOT")]
  31. [Obsolete ("The legacy static Application object is going away.")]
  32. public static void Init (string? driverName = null)
  33. {
  34. //Debug.Fail ("Application.Init() called - parallelizable tests should not use legacy static Application model");
  35. ApplicationImpl.Instance.Init (driverName ?? ForceDriver);
  36. }
  37. /// <summary>
  38. /// Gets or sets the main thread ID for the application.
  39. /// </summary>
  40. [Obsolete ("The legacy static Application object is going away.")]
  41. public static int? MainThreadId
  42. {
  43. get => ApplicationImpl.Instance.MainThreadId;
  44. internal set => ApplicationImpl.Instance.MainThreadId = value;
  45. }
  46. /// <inheritdoc cref="IApplication.Dispose"/>
  47. [Obsolete ("The legacy static Application object is going away.")]
  48. public static void Shutdown () => ApplicationImpl.Instance.Dispose ();
  49. /// <inheritdoc cref="IApplication.Initialized"/>
  50. [Obsolete ("The legacy static Application object is going away.")]
  51. public static bool Initialized
  52. {
  53. get => ApplicationImpl.Instance.Initialized;
  54. internal set => ApplicationImpl.Instance.Initialized = value;
  55. }
  56. /// <inheritdoc cref="IApplication.InitializedChanged"/>
  57. [Obsolete ("The legacy static Application object is going away.")]
  58. public static event EventHandler<EventArgs<bool>>? InitializedChanged
  59. {
  60. add => ApplicationImpl.Instance.InitializedChanged += value;
  61. remove => ApplicationImpl.Instance.InitializedChanged -= value;
  62. }
  63. // IMPORTANT: Ensure all property/fields are reset here. See Init_ResetState_Resets_Properties unit test.
  64. // Encapsulate all setting of initial state for Application; Having
  65. // this in a function like this ensures we don't make mistakes in
  66. // guaranteeing that the state of this singleton is deterministic when Init
  67. // starts running and after Shutdown returns.
  68. [Obsolete ("The legacy static Application object is going away.")]
  69. internal static void ResetState (bool ignoreDisposed = false)
  70. {
  71. // Use the static reset method to bypass the fence check
  72. ApplicationImpl.ResetStateStatic (ignoreDisposed);
  73. }
  74. }