IApplicationMainLoop.cs 5.4 KB

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