ApplicationImpl.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System.Collections.Concurrent;
  2. namespace Terminal.Gui.App;
  3. /// <summary>
  4. /// Implementation of core <see cref="Application"/> methods using the modern
  5. /// main loop architecture with component factories for different platforms.
  6. /// </summary>
  7. public partial class ApplicationImpl : IApplication
  8. {
  9. /// <summary>
  10. /// INTERNAL: Creates a new instance of the Application backend.
  11. /// </summary>
  12. internal ApplicationImpl () { }
  13. /// <summary>
  14. /// INTERNAL: Creates a new instance of the Application backend.
  15. /// </summary>
  16. /// <param name="componentFactory"></param>
  17. internal ApplicationImpl (IComponentFactory componentFactory) { _componentFactory = componentFactory; }
  18. #region Singleton
  19. /// <summary>
  20. /// Tracks which application model has been used in this process.
  21. /// </summary>
  22. private static ApplicationModelUsage _modelUsage = ApplicationModelUsage.None;
  23. /// <summary>
  24. /// Configures the singleton instance of <see cref="Application"/> to use the specified backend implementation.
  25. /// </summary>
  26. /// <param name="app"></param>
  27. public static void SetInstance (IApplication? app) { _instance = app; }
  28. // Private static readonly Lazy instance of Application
  29. private static IApplication? _instance;
  30. /// <summary>
  31. /// Gets the currently configured backend implementation of <see cref="Application"/> gateway methods.
  32. /// </summary>
  33. public static IApplication Instance
  34. {
  35. get
  36. {
  37. // If an instance already exists, return it without fence checking
  38. // This allows for cleanup/reset operations
  39. if (_instance is { })
  40. {
  41. return _instance;
  42. }
  43. // Check if the instance-based model has already been used
  44. if (_modelUsage == ApplicationModelUsage.InstanceBased)
  45. {
  46. throw new InvalidOperationException (
  47. "Cannot use legacy static Application model (Application.Init/ApplicationImpl.Instance) after using modern instance-based model (Application.Create). " +
  48. "Use only one model per process.");
  49. }
  50. // Mark the usage and create the instance
  51. _modelUsage = ApplicationModelUsage.LegacyStatic;
  52. return _instance = new ApplicationImpl ();
  53. }
  54. }
  55. /// <summary>
  56. /// INTERNAL: Marks that the instance-based model has been used. Called by Application.Create().
  57. /// </summary>
  58. internal static void MarkInstanceBasedModelUsed ()
  59. {
  60. // Check if the legacy static model has already been initialized
  61. if (_modelUsage == ApplicationModelUsage.LegacyStatic && _instance?.Initialized == true)
  62. {
  63. throw new InvalidOperationException (
  64. "Cannot use modern instance-based model (Application.Create) after using legacy static Application model (Application.Init/ApplicationImpl.Instance). " +
  65. "Use only one model per process.");
  66. }
  67. _modelUsage = ApplicationModelUsage.InstanceBased;
  68. }
  69. /// <summary>
  70. /// INTERNAL: Resets the model usage tracking. Only for testing purposes.
  71. /// </summary>
  72. internal static void ResetModelUsageTracking ()
  73. {
  74. _modelUsage = ApplicationModelUsage.None;
  75. _instance = null;
  76. }
  77. /// <summary>
  78. /// INTERNAL: Resets state without going through the fence-checked Instance property.
  79. /// Used by Application.ResetState() to allow cleanup regardless of which model was used.
  80. /// </summary>
  81. internal static void ResetStateStatic (bool ignoreDisposed = false)
  82. {
  83. // If an instance exists, reset it
  84. _instance?.ResetState (ignoreDisposed);
  85. // Always reset the model tracking to allow tests to use either model after reset
  86. ResetModelUsageTracking ();
  87. }
  88. #endregion Singleton
  89. /// <summary>
  90. /// Defines the different application usage models.
  91. /// </summary>
  92. private enum ApplicationModelUsage
  93. {
  94. /// <summary>No model has been used yet.</summary>
  95. None,
  96. /// <summary>Legacy static model (Application.Init/ApplicationImpl.Instance).</summary>
  97. LegacyStatic,
  98. /// <summary>Modern instance-based model (Application.Create).</summary>
  99. InstanceBased
  100. }
  101. private string? _driverName;
  102. #region Input
  103. private IMouse? _mouse;
  104. /// <summary>
  105. /// Handles mouse event state and processing.
  106. /// </summary>
  107. public IMouse Mouse
  108. {
  109. get
  110. {
  111. _mouse ??= new MouseImpl { App = this };
  112. return _mouse;
  113. }
  114. set => _mouse = value ?? throw new ArgumentNullException (nameof (value));
  115. }
  116. private IKeyboard? _keyboard;
  117. /// <summary>
  118. /// Handles keyboard input and key bindings at the Application level
  119. /// </summary>
  120. public IKeyboard Keyboard
  121. {
  122. get
  123. {
  124. _keyboard ??= new KeyboardImpl { App = this };
  125. return _keyboard;
  126. }
  127. set => _keyboard = value ?? throw new ArgumentNullException (nameof (value));
  128. }
  129. #endregion Input
  130. #region View Management
  131. private ApplicationPopover? _popover;
  132. /// <inheritdoc/>
  133. public ApplicationPopover? Popover
  134. {
  135. get
  136. {
  137. _popover ??= new () { App = this };
  138. return _popover;
  139. }
  140. set => _popover = value;
  141. }
  142. private ApplicationNavigation? _navigation;
  143. /// <inheritdoc/>
  144. public ApplicationNavigation? Navigation
  145. {
  146. get
  147. {
  148. _navigation ??= new () { App = this };
  149. return _navigation;
  150. }
  151. set => _navigation = value ?? throw new ArgumentNullException (nameof (value));
  152. }
  153. private Toplevel? _topRunnable;
  154. /// <inheritdoc/>
  155. public Toplevel? TopRunnable
  156. {
  157. get => _topRunnable;
  158. set
  159. {
  160. _topRunnable = value;
  161. if (_topRunnable is { })
  162. {
  163. _topRunnable.App = this;
  164. }
  165. }
  166. }
  167. // BUGBUG: Technically, this is not the full lst of sessions. There be dragons here, e.g. see how Toplevel.Id is used. What
  168. /// <inheritdoc/>
  169. public ConcurrentStack<Toplevel> SessionStack { get; } = new ();
  170. /// <inheritdoc/>
  171. public Toplevel? CachedSessionTokenToplevel { get; set; }
  172. /// <inheritdoc/>
  173. public ConcurrentStack<RunnableSessionToken>? RunnableSessionStack { get; } = new ();
  174. /// <inheritdoc/>
  175. public IRunnable? FrameworkOwnedRunnable { get; set; }
  176. #endregion View Management
  177. /// <inheritdoc/>
  178. public new string ToString () => Driver?.ToString () ?? string.Empty;
  179. }