ApplicationImpl.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /// Configures the singleton instance of <see cref="Application"/> to use the specified backend implementation.
  21. /// </summary>
  22. /// <param name="app"></param>
  23. public static void SetInstance (IApplication? app) { _instance = app; }
  24. // Private static readonly Lazy instance of Application
  25. private static IApplication? _instance;
  26. /// <summary>
  27. /// Gets the currently configured backend implementation of <see cref="Application"/> gateway methods.
  28. /// </summary>
  29. public static IApplication Instance => _instance ??= new ApplicationImpl ();
  30. #endregion Singleton
  31. private string? _driverName;
  32. #region Input
  33. private IMouse? _mouse;
  34. /// <summary>
  35. /// Handles mouse event state and processing.
  36. /// </summary>
  37. public IMouse Mouse
  38. {
  39. get
  40. {
  41. _mouse ??= new MouseImpl { App = this };
  42. return _mouse;
  43. }
  44. set => _mouse = value ?? throw new ArgumentNullException (nameof (value));
  45. }
  46. private IKeyboard? _keyboard;
  47. /// <summary>
  48. /// Handles keyboard input and key bindings at the Application level
  49. /// </summary>
  50. public IKeyboard Keyboard
  51. {
  52. get
  53. {
  54. _keyboard ??= new KeyboardImpl { App = this };
  55. return _keyboard;
  56. }
  57. set => _keyboard = value ?? throw new ArgumentNullException (nameof (value));
  58. }
  59. #endregion Input
  60. #region View Management
  61. private ApplicationPopover? _popover;
  62. /// <inheritdoc/>
  63. public ApplicationPopover? Popover
  64. {
  65. get
  66. {
  67. _popover ??= new () { App = this };
  68. return _popover;
  69. }
  70. set => _popover = value;
  71. }
  72. private ApplicationNavigation? _navigation;
  73. /// <inheritdoc/>
  74. public ApplicationNavigation? Navigation
  75. {
  76. get
  77. {
  78. _navigation ??= new () { App = this };
  79. return _navigation;
  80. }
  81. set => _navigation = value ?? throw new ArgumentNullException (nameof (value));
  82. }
  83. private Toplevel? _topRunnable;
  84. /// <inheritdoc/>
  85. public Toplevel? TopRunnable
  86. {
  87. get => _topRunnable;
  88. set
  89. {
  90. _topRunnable = value;
  91. if (_topRunnable is { })
  92. {
  93. _topRunnable.App = this;
  94. }
  95. }
  96. }
  97. // BUGBUG: Technically, this is not the full lst of sessions. There be dragons here, e.g. see how Toplevel.Id is used. What
  98. /// <inheritdoc/>
  99. public ConcurrentStack<Toplevel> SessionStack { get; } = new ();
  100. /// <inheritdoc/>
  101. public Toplevel? CachedSessionTokenToplevel { get; set; }
  102. /// <inheritdoc/>
  103. public ConcurrentStack<RunnableSessionToken>? RunnableSessionStack { get; } = new ();
  104. /// <inheritdoc/>
  105. public IRunnable? FrameworkOwnedRunnable { get; set; }
  106. #endregion View Management
  107. /// <inheritdoc/>
  108. public new string ToString () => Driver?.ToString () ?? string.Empty;
  109. }