ApplicationImpl.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. public static ApplicationModelUsage ModelUsage { get; private set; } = ApplicationModelUsage.None;
  34. /// <summary>
  35. /// Error message for when trying to use modern model after legacy static model.
  36. /// </summary>
  37. internal const string ERROR_MODERN_AFTER_LEGACY =
  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. internal const string ERROR_LEGACY_AFTER_MODERN =
  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)
  51. {
  52. ModelUsage = ApplicationModelUsage.LegacyStatic;
  53. _instance = app;
  54. }
  55. // Private static readonly Lazy instance of Application
  56. private static IApplication? _instance;
  57. /// <summary>
  58. /// Gets the currently configured backend implementation of <see cref="Application"/> gateway methods.
  59. /// </summary>
  60. public static IApplication Instance
  61. {
  62. get
  63. {
  64. // If an instance already exists, return it without fence checking
  65. // This allows for cleanup/reset operations
  66. if (_instance is { })
  67. {
  68. return _instance;
  69. }
  70. // Check if the instance-based model has already been used
  71. if (ModelUsage == ApplicationModelUsage.InstanceBased)
  72. {
  73. throw new InvalidOperationException (ERROR_LEGACY_AFTER_MODERN);
  74. }
  75. // Mark the usage and create the instance
  76. ModelUsage = ApplicationModelUsage.LegacyStatic;
  77. return _instance = new ApplicationImpl ();
  78. }
  79. }
  80. /// <summary>
  81. /// INTERNAL: Marks that the instance-based model has been used. Called by Application.Create().
  82. /// </summary>
  83. internal static void MarkInstanceBasedModelUsed ()
  84. {
  85. // Check if the legacy static model has already been initialized
  86. if (ModelUsage == ApplicationModelUsage.LegacyStatic && _instance?.Initialized == true)
  87. {
  88. throw new InvalidOperationException (ERROR_MODERN_AFTER_LEGACY);
  89. }
  90. ModelUsage = ApplicationModelUsage.InstanceBased;
  91. }
  92. /// <summary>
  93. /// INTERNAL: Resets the model usage tracking. Only for testing purposes.
  94. /// </summary>
  95. internal static void ResetModelUsageTracking ()
  96. {
  97. ModelUsage = ApplicationModelUsage.None;
  98. _instance = null;
  99. }
  100. /// <summary>
  101. /// INTERNAL: Resets state without going through the fence-checked Instance property.
  102. /// Used by Application.ResetState() to allow cleanup regardless of which model was used.
  103. /// </summary>
  104. internal static void ResetStateStatic (bool ignoreDisposed = false)
  105. {
  106. // If an instance exists, reset it
  107. _instance?.ResetState (ignoreDisposed);
  108. // Reset Application static properties to their defaults
  109. // This ensures tests start with clean state
  110. Application.ForceDriver = string.Empty;
  111. Application.Force16Colors = false;
  112. Application.IsMouseDisabled = false;
  113. Application.QuitKey = Key.Esc;
  114. Application.ArrangeKey = Key.F5.WithCtrl;
  115. Application.NextTabGroupKey = Key.F6;
  116. Application.NextTabKey = Key.Tab;
  117. Application.PrevTabGroupKey = Key.F6.WithShift;
  118. Application.PrevTabKey = Key.Tab.WithShift;
  119. // Always reset the model tracking to allow tests to use either model after reset
  120. ResetModelUsageTracking ();
  121. }
  122. #endregion Singleton
  123. private string? _driverName;
  124. #region Input
  125. private IMouse? _mouse;
  126. /// <summary>
  127. /// Handles mouse event state and processing.
  128. /// </summary>
  129. public IMouse Mouse
  130. {
  131. get
  132. {
  133. _mouse ??= new MouseImpl { App = this };
  134. return _mouse;
  135. }
  136. set => _mouse = value ?? throw new ArgumentNullException (nameof (value));
  137. }
  138. private IKeyboard? _keyboard;
  139. /// <summary>
  140. /// Handles keyboard input and key bindings at the Application level
  141. /// </summary>
  142. public IKeyboard Keyboard
  143. {
  144. get
  145. {
  146. _keyboard ??= new KeyboardImpl { App = this };
  147. return _keyboard;
  148. }
  149. set => _keyboard = value ?? throw new ArgumentNullException (nameof (value));
  150. }
  151. #endregion Input
  152. #region View Management
  153. private ApplicationPopover? _popover;
  154. /// <inheritdoc/>
  155. public ApplicationPopover? Popover
  156. {
  157. get
  158. {
  159. _popover ??= new () { App = this };
  160. return _popover;
  161. }
  162. set => _popover = value;
  163. }
  164. private ApplicationNavigation? _navigation;
  165. /// <inheritdoc/>
  166. public ApplicationNavigation? Navigation
  167. {
  168. get
  169. {
  170. _navigation ??= new () { App = this };
  171. return _navigation;
  172. }
  173. set => _navigation = value ?? throw new ArgumentNullException (nameof (value));
  174. }
  175. private Toplevel? _topRunnable;
  176. /// <inheritdoc/>
  177. public Toplevel? TopRunnable
  178. {
  179. get => _topRunnable;
  180. set
  181. {
  182. _topRunnable = value;
  183. if (_topRunnable is { })
  184. {
  185. _topRunnable.App = this;
  186. }
  187. }
  188. }
  189. // BUGBUG: Technically, this is not the full lst of sessions. There be dragons here, e.g. see how Toplevel.Id is used. What
  190. /// <inheritdoc/>
  191. public ConcurrentStack<Toplevel> SessionStack { get; } = new ();
  192. /// <inheritdoc/>
  193. public Toplevel? CachedSessionTokenToplevel { get; set; }
  194. /// <inheritdoc/>
  195. public ConcurrentStack<RunnableSessionToken>? RunnableSessionStack { get; } = new ();
  196. /// <inheritdoc/>
  197. public IRunnable? FrameworkOwnedRunnable { get; set; }
  198. #endregion View Management
  199. /// <inheritdoc/>
  200. public new string ToString () => Driver?.ToString () ?? string.Empty;
  201. }