IApplicationMainLoop.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System.Collections.Concurrent;
  2. namespace Terminal.Gui.App;
  3. /// <summary>
  4. /// Interface for the main application loop that runs the core Terminal.Gui UI rendering and event processing.
  5. /// </summary>
  6. /// <remarks>
  7. /// This interface defines the contract for the main loop that coordinates:
  8. /// <list type="bullet">
  9. /// <item>Processing input events from the console</item>
  10. /// <item>Running user timeout callbacks</item>
  11. /// <item>Detecting UI changes that need redrawing</item>
  12. /// <item>Rendering UI updates to the console</item>
  13. /// </list>
  14. /// </remarks>
  15. /// <typeparam name="TInputRecord">Type of raw input events processed by the loop, e.g. <see cref="ConsoleKeyInfo"/> for cross-platform .NET driver</typeparam>
  16. public interface IApplicationMainLoop<TInputRecord> : IDisposable where TInputRecord : struct
  17. {
  18. /// <summary>
  19. /// Gets the <see cref="ITimedEvents"/> implementation that manages user-defined timeouts and periodic events.
  20. /// </summary>
  21. public ITimedEvents TimedEvents { get; }
  22. /// <summary>
  23. /// Gets the <see cref="IOutputBuffer"/> representing the desired screen state for console rendering.
  24. /// </summary>
  25. public IOutputBuffer OutputBuffer { get; }
  26. /// <summary>
  27. /// Gets the <see cref="IOutput"/> implementation responsible for rendering the <see cref="OutputBuffer"/> to the console using platform specific methods.
  28. /// </summary>
  29. public IOutput Output { get; }
  30. /// <summary>
  31. /// Gets <see cref="InputProcessor"/> implementation that processes the mouse and keyboard input populated by <see cref="IInput{TInputRecord}"/>
  32. /// implementations on the input thread and translating to events on the UI thread.
  33. /// </summary>
  34. public IInputProcessor InputProcessor { get; }
  35. /// <summary>
  36. /// Gets the class responsible for sending ANSI escape requests which expect a response
  37. /// from the remote terminal e.g. Device Attribute Request
  38. /// </summary>
  39. public AnsiRequestScheduler AnsiRequestScheduler { get; }
  40. /// <summary>
  41. /// Gets the <see cref="ISizeMonitor"/> implementation that tracks terminal size changes.
  42. /// </summary>
  43. public ISizeMonitor SizeMonitor { get; }
  44. /// <summary>
  45. /// Initializes the main loop with its required dependencies.
  46. /// </summary>
  47. /// <param name="timedEvents">
  48. /// The <see cref="ITimedEvents"/> implementation for managing user-defined timeouts and periodic callbacks
  49. /// (e.g., <see cref="Application.AddTimeout"/>).
  50. /// </param>
  51. /// <param name="inputQueue">
  52. /// The thread-safe queue containing raw input events populated by <see cref="IInput{TInputRecord}"/> on
  53. /// the input thread. This queue is drained by <see cref="InputProcessor"/> during each <see cref="Iteration"/>.
  54. /// </param>
  55. /// <param name="inputProcessor">
  56. /// The <see cref="IInputProcessor"/> that translates raw input records (e.g., <see cref="ConsoleKeyInfo"/>)
  57. /// into Terminal.Gui events (<see cref="Key"/>, <see cref="MouseEventArgs"/>) and raises them on the main UI thread.
  58. /// </param>
  59. /// <param name="output">
  60. /// The <see cref="IOutput"/> implementation responsible for rendering the <see cref="OutputBuffer"/> to the
  61. /// console using platform-specific methods (e.g., Win32 APIs, ANSI escape sequences).
  62. /// </param>
  63. /// <param name="componentFactory">
  64. /// The factory for creating driver-specific components. Used here to create the <see cref="ISizeMonitor"/>
  65. /// that tracks terminal size changes.
  66. /// </param>
  67. /// <remarks>
  68. /// <para>
  69. /// This method is called by <see cref="MainLoopCoordinator{TInputRecord}"/> during application startup
  70. /// to wire up all the components needed for the main loop to function. It must be called before
  71. /// <see cref="Iteration"/> can be invoked.
  72. /// </para>
  73. /// <para>
  74. /// <b>Initialization order:</b>
  75. /// </para>
  76. /// <list type="number">
  77. /// <item>Store references to <paramref name="timedEvents"/>, <paramref name="inputQueue"/>,
  78. /// <paramref name="inputProcessor"/>, and <paramref name="output"/></item>
  79. /// <item>Create <see cref="AnsiRequestScheduler"/> for managing ANSI requests/responses</item>
  80. /// <item>Initialize <see cref="OutputBuffer"/> size to match current console dimensions</item>
  81. /// <item>Create <see cref="ISizeMonitor"/> using the <paramref name="componentFactory"/></item>
  82. /// </list>
  83. /// <para>
  84. /// After initialization, the main loop is ready to process events via <see cref="Iteration"/>.
  85. /// </para>
  86. /// </remarks>
  87. void Initialize (
  88. ITimedEvents timedEvents,
  89. ConcurrentQueue<TInputRecord> inputQueue,
  90. IInputProcessor inputProcessor,
  91. IOutput output,
  92. IComponentFactory<TInputRecord> componentFactory
  93. );
  94. /// <summary>
  95. /// Perform a single iteration of the main loop then blocks for a fixed length
  96. /// of time, this method is designed to be run in a loop.
  97. /// </summary>
  98. public void Iteration ();
  99. }