Application.Lifecycle.cs 2.9 KB

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