ApplicationImpl.Driver.cs 7.2 KB

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