ApplicationImpl.cs 7.5 KB

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