ApplicationImpl.Driver.cs 7.2 KB

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