ApplicationImpl.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. // Only check the fence when creating a new instance
  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. _modelUsage = ApplicationModelUsage.LegacyStatic;
  51. return _instance = new ApplicationImpl ();
  52. }
  53. }
  54. /// <summary>
  55. /// INTERNAL: Marks that the instance-based model has been used. Called by Application.Create().
  56. /// </summary>
  57. internal static void MarkInstanceBasedModelUsed ()
  58. {
  59. if (_modelUsage == ApplicationModelUsage.LegacyStatic)
  60. {
  61. throw new InvalidOperationException (
  62. "Cannot use modern instance-based model (Application.Create) after using legacy static Application model (Application.Init/ApplicationImpl.Instance). " +
  63. "Use only one model per process.");
  64. }
  65. _modelUsage = ApplicationModelUsage.InstanceBased;
  66. }
  67. /// <summary>
  68. /// INTERNAL: Resets the model usage tracking. Only for testing purposes.
  69. /// </summary>
  70. internal static void ResetModelUsageTracking ()
  71. {
  72. _modelUsage = ApplicationModelUsage.None;
  73. _instance = null;
  74. }
  75. #endregion Singleton
  76. /// <summary>
  77. /// Defines the different application usage models.
  78. /// </summary>
  79. private enum ApplicationModelUsage
  80. {
  81. /// <summary>No model has been used yet.</summary>
  82. None,
  83. /// <summary>Legacy static model (Application.Init/ApplicationImpl.Instance).</summary>
  84. LegacyStatic,
  85. /// <summary>Modern instance-based model (Application.Create).</summary>
  86. InstanceBased
  87. }
  88. private string? _driverName;
  89. #region Input
  90. private IMouse? _mouse;
  91. /// <summary>
  92. /// Handles mouse event state and processing.
  93. /// </summary>
  94. public IMouse Mouse
  95. {
  96. get
  97. {
  98. _mouse ??= new MouseImpl { App = this };
  99. return _mouse;
  100. }
  101. set => _mouse = value ?? throw new ArgumentNullException (nameof (value));
  102. }
  103. private IKeyboard? _keyboard;
  104. /// <summary>
  105. /// Handles keyboard input and key bindings at the Application level
  106. /// </summary>
  107. public IKeyboard Keyboard
  108. {
  109. get
  110. {
  111. _keyboard ??= new KeyboardImpl { App = this };
  112. return _keyboard;
  113. }
  114. set => _keyboard = value ?? throw new ArgumentNullException (nameof (value));
  115. }
  116. #endregion Input
  117. #region View Management
  118. private ApplicationPopover? _popover;
  119. /// <inheritdoc/>
  120. public ApplicationPopover? Popover
  121. {
  122. get
  123. {
  124. _popover ??= new () { App = this };
  125. return _popover;
  126. }
  127. set => _popover = value;
  128. }
  129. private ApplicationNavigation? _navigation;
  130. /// <inheritdoc/>
  131. public ApplicationNavigation? Navigation
  132. {
  133. get
  134. {
  135. _navigation ??= new () { App = this };
  136. return _navigation;
  137. }
  138. set => _navigation = value ?? throw new ArgumentNullException (nameof (value));
  139. }
  140. private Toplevel? _topRunnable;
  141. /// <inheritdoc/>
  142. public Toplevel? TopRunnable
  143. {
  144. get => _topRunnable;
  145. set
  146. {
  147. _topRunnable = value;
  148. if (_topRunnable is { })
  149. {
  150. _topRunnable.App = this;
  151. }
  152. }
  153. }
  154. // BUGBUG: Technically, this is not the full lst of sessions. There be dragons here, e.g. see how Toplevel.Id is used. What
  155. /// <inheritdoc/>
  156. public ConcurrentStack<Toplevel> SessionStack { get; } = new ();
  157. /// <inheritdoc/>
  158. public Toplevel? CachedSessionTokenToplevel { get; set; }
  159. /// <inheritdoc/>
  160. public ConcurrentStack<RunnableSessionToken>? RunnableSessionStack { get; } = new ();
  161. /// <inheritdoc/>
  162. public IRunnable? FrameworkOwnedRunnable { get; set; }
  163. #endregion View Management
  164. /// <inheritdoc/>
  165. public new string ToString () => Driver?.ToString () ?? string.Empty;
  166. }