ApplicationImpl.Driver.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. using System.Collections.Concurrent;
  2. namespace Terminal.Gui.App;
  3. public partial class ApplicationImpl
  4. {
  5. /// <inheritdoc/>
  6. public IDriver? Driver { get; set; }
  7. /// <inheritdoc/>
  8. public bool Force16Colors { get; set; }
  9. /// <inheritdoc/>
  10. public string ForceDriver { get; set; } = string.Empty;
  11. /// <inheritdoc/>
  12. public List<SixelToRender> Sixel { get; } = new ();
  13. /// <summary>
  14. /// Creates the appropriate <see cref="IDriver"/> based on platform and driverName.
  15. /// </summary>
  16. /// <param name="driverName"></param>
  17. /// <returns></returns>
  18. /// <exception cref="Exception"></exception>
  19. /// <exception cref="InvalidOperationException"></exception>
  20. private void CreateDriver (string? driverName)
  21. {
  22. PlatformID p = Environment.OSVersion.Platform;
  23. // Check component factory type first - this takes precedence over driverName
  24. bool factoryIsWindows = _componentFactory is IComponentFactory<WindowsConsole.InputRecord>;
  25. bool factoryIsDotNet = _componentFactory is IComponentFactory<ConsoleKeyInfo>;
  26. bool factoryIsUnix = _componentFactory is IComponentFactory<char>;
  27. bool factoryIsFake = _componentFactory is IComponentFactory<ConsoleKeyInfo>;
  28. // Then check driverName
  29. bool nameIsWindows = driverName?.Contains ("win", StringComparison.OrdinalIgnoreCase) ?? false;
  30. bool nameIsDotNet = driverName?.Contains ("dotnet", StringComparison.OrdinalIgnoreCase) ?? false;
  31. bool nameIsUnix = driverName?.Contains ("unix", StringComparison.OrdinalIgnoreCase) ?? false;
  32. bool nameIsFake = driverName?.Contains ("fake", StringComparison.OrdinalIgnoreCase) ?? false;
  33. // Decide which driver to use - component factory type takes priority
  34. if (factoryIsFake || (!factoryIsWindows && !factoryIsDotNet && !factoryIsUnix && nameIsFake))
  35. {
  36. Coordinator = CreateSubcomponents (() => new FakeComponentFactory ());
  37. _driverName = "fake";
  38. }
  39. else if (factoryIsWindows || (!factoryIsDotNet && !factoryIsUnix && nameIsWindows))
  40. {
  41. Coordinator = CreateSubcomponents (() => new WindowsComponentFactory ());
  42. _driverName = "windows";
  43. }
  44. else if (factoryIsDotNet || (!factoryIsWindows && !factoryIsUnix && nameIsDotNet))
  45. {
  46. Coordinator = CreateSubcomponents (() => new NetComponentFactory ());
  47. _driverName = "dotnet";
  48. }
  49. else if (factoryIsUnix || (!factoryIsWindows && !factoryIsDotNet && nameIsUnix))
  50. {
  51. Coordinator = CreateSubcomponents (() => new UnixComponentFactory ());
  52. _driverName = "unix";
  53. }
  54. else if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
  55. {
  56. Coordinator = CreateSubcomponents (() => new WindowsComponentFactory ());
  57. _driverName = "windows";
  58. }
  59. else if (p == PlatformID.Unix)
  60. {
  61. Coordinator = CreateSubcomponents (() => new UnixComponentFactory ());
  62. _driverName = "unix";
  63. }
  64. else
  65. {
  66. Logging.Information($"Falling back to dotnet driver.");
  67. Coordinator = CreateSubcomponents (() => new NetComponentFactory ());
  68. _driverName = "dotnet";
  69. }
  70. Logging.Trace ($"Created Subcomponents: {Coordinator}");
  71. Coordinator.StartInputTaskAsync (this).Wait ();
  72. if (Driver == null)
  73. {
  74. throw new ("Driver was null even after booting MainLoopCoordinator");
  75. }
  76. }
  77. private readonly IComponentFactory? _componentFactory;
  78. /// <summary>
  79. /// INTERNAL: Gets or sets the main loop coordinator that orchestrates the application's event processing,
  80. /// input handling, and rendering pipeline.
  81. /// </summary>
  82. /// <remarks>
  83. /// <para>
  84. /// The <see cref="IMainLoopCoordinator"/> is the central component responsible for:
  85. /// <list type="bullet">
  86. /// <item>Managing the platform-specific input thread that reads from the console</item>
  87. /// <item>Coordinating the main application loop via <see cref="IMainLoopCoordinator.RunIteration"/></item>
  88. /// <item>Processing queued input events and translating them to Terminal.Gui events</item>
  89. /// <item>Managing the <see cref="ApplicationMainLoop{TInputRecord}"/> that handles rendering</item>
  90. /// <item>Executing scheduled timeouts and callbacks via <see cref="ITimedEvents"/></item>
  91. /// </list>
  92. /// </para>
  93. /// <para>
  94. /// The coordinator is created in <see cref="CreateDriver"/> based on the selected driver
  95. /// (Windows, Unix, .NET, or Fake) and is started by calling
  96. /// <see cref="IMainLoopCoordinator.StartInputTaskAsync"/>.
  97. /// </para>
  98. /// </remarks>
  99. internal IMainLoopCoordinator? Coordinator { get; private set; }
  100. /// <summary>
  101. /// INTERNAL: Creates a <see cref="MainLoopCoordinator{TInputRecord}"/> with the appropriate component factory
  102. /// for the specified input record type.
  103. /// </summary>
  104. /// <typeparam name="TInputRecord">
  105. /// Platform-specific input type: <see cref="ConsoleKeyInfo"/> (.NET/Fake),
  106. /// <see cref="WindowsConsole.InputRecord"/> (Windows), or <see cref="char"/> (Unix).
  107. /// </typeparam>
  108. /// <param name="fallbackFactory">
  109. /// Factory function to create the component factory if <see cref="_componentFactory"/>
  110. /// is not of type <see cref="IComponentFactory{TInputRecord}"/>.
  111. /// </param>
  112. /// <returns>
  113. /// A <see cref="MainLoopCoordinator{TInputRecord}"/> configured with the input queue,
  114. /// main loop, timed events, and selected component factory.
  115. /// </returns>
  116. private IMainLoopCoordinator CreateSubcomponents<TInputRecord> (Func<IComponentFactory<TInputRecord>> fallbackFactory) where TInputRecord : struct
  117. {
  118. ConcurrentQueue<TInputRecord> inputQueue = new ();
  119. ApplicationMainLoop<TInputRecord> loop = new ();
  120. IComponentFactory<TInputRecord> cf;
  121. if (_componentFactory is IComponentFactory<TInputRecord> typedFactory)
  122. {
  123. cf = typedFactory;
  124. }
  125. else
  126. {
  127. cf = fallbackFactory ();
  128. }
  129. return new MainLoopCoordinator<TInputRecord> (_timedEvents, inputQueue, loop, cf);
  130. }
  131. internal void SubscribeDriverEvents ()
  132. {
  133. ArgumentNullException.ThrowIfNull (Driver);
  134. Driver.SizeChanged += Driver_SizeChanged;
  135. Driver.KeyDown += Driver_KeyDown;
  136. Driver.KeyUp += Driver_KeyUp;
  137. Driver.MouseEvent += Driver_MouseEvent;
  138. }
  139. internal void UnsubscribeDriverEvents ()
  140. {
  141. ArgumentNullException.ThrowIfNull (Driver);
  142. Driver.SizeChanged -= Driver_SizeChanged;
  143. Driver.KeyDown -= Driver_KeyDown;
  144. Driver.KeyUp -= Driver_KeyUp;
  145. Driver.MouseEvent -= Driver_MouseEvent;
  146. }
  147. private void Driver_KeyDown (object? sender, Key e) { Keyboard?.RaiseKeyDownEvent (e); }
  148. private void Driver_KeyUp (object? sender, Key e) { Keyboard?.RaiseKeyUpEvent (e); }
  149. private void Driver_MouseEvent (object? sender, MouseEventArgs e) { Mouse?.RaiseMouseEvent (e); }
  150. }