IRunnable.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. namespace Terminal.Gui.App;
  2. /// <summary>
  3. /// Defines a view that can be run as an independent session with <see cref="IApplication.Run"/>.
  4. /// </summary>
  5. /// <remarks>
  6. /// <para>
  7. /// A runnable view can execute as a self-contained session with its own lifecycle,
  8. /// event loop iteration, and focus management.
  9. /// </para>
  10. /// <para>
  11. /// Implementing <see cref="IRunnable"/> does not require any specific view hierarchy
  12. /// (e.g., deriving from Toplevel) or layout mode (e.g., Overlapped).
  13. /// </para>
  14. /// <para>
  15. /// For exclusive input capture (modal behavior), implement <see cref="IModalRunnable{TResult}"/>.
  16. /// </para>
  17. /// <para>
  18. /// This interface follows the Terminal.Gui Cancellable Work Pattern (CWP) for lifecycle events
  19. /// where cancellation makes sense (Stopping, Starting).
  20. /// </para>
  21. /// </remarks>
  22. public interface IRunnable
  23. {
  24. /// <summary>
  25. /// Gets or sets whether this runnable session is currently running.
  26. /// </summary>
  27. /// <remarks>
  28. /// This property is set by the framework during session lifecycle. Setting this property
  29. /// directly is discouraged. Use <see cref="IApplication.RequestStop"/> instead.
  30. /// </remarks>
  31. bool Running { get; set; }
  32. #region Lifecycle Methods (Called by IApplication)
  33. /// <summary>
  34. /// Raises the <see cref="Stopping"/> and <see cref="Stopped"/> events.
  35. /// Called by <see cref="IApplication.RequestStop"/>.
  36. /// </summary>
  37. /// <remarks>
  38. /// <para>
  39. /// This method implements the Cancellable Work Pattern for stopping:
  40. /// 1. Calls virtual method (can cancel)
  41. /// 2. Raises <see cref="Stopping"/> event (can cancel)
  42. /// 3. If not canceled, sets <see cref="Running"/> = false
  43. /// 4. Calls post-notification virtual method
  44. /// 5. Raises <see cref="Stopped"/> event
  45. /// </para>
  46. /// <para>
  47. /// Implementations should follow this pattern. See <see cref="Runnable"/> for reference implementation.
  48. /// </para>
  49. /// </remarks>
  50. void RaiseStoppingEvent ();
  51. /// <summary>
  52. /// Raises the <see cref="Starting"/> event.
  53. /// Called by <see cref="IApplication.Begin"/> or <see cref="IApplication.Run"/> when this runnable session is starting.
  54. /// </summary>
  55. /// <returns><see langword="true"/> if starting was canceled; otherwise <see langword="false"/>.</returns>
  56. /// <remarks>
  57. /// <para>
  58. /// This method implements the Cancellable Work Pattern for starting:
  59. /// 1. Calls virtual method (can cancel)
  60. /// 2. Raises <see cref="Starting"/> event (can cancel)
  61. /// 3. If canceled, returns true
  62. /// 4. If not canceled, calls <see cref="RaiseStartedEvent"/> and returns false
  63. /// </para>
  64. /// </remarks>
  65. bool RaiseStartingEvent ();
  66. /// <summary>
  67. /// Raises the <see cref="Started"/> event.
  68. /// Called by <see cref="RaiseStartingEvent"/> after starting succeeds.
  69. /// </summary>
  70. /// <remarks>
  71. /// This is the post-notification phase of starting. Implementations should raise the
  72. /// <see cref="Started"/> event.
  73. /// </remarks>
  74. void RaiseStartedEvent ();
  75. #endregion
  76. #region Lifecycle Events
  77. /// <summary>
  78. /// Raised during <see cref="ISupportInitialize.EndInit"/> before initialization completes.
  79. /// Can be canceled by setting <see cref="System.ComponentModel.CancelEventArgs.Cancel"/> to <see langword="true"/>.
  80. /// </summary>
  81. /// <remarks>
  82. /// <para>
  83. /// This event is from the <see cref="ISupportInitialize"/> pattern and is raised by <see cref="View.EndInit"/>.
  84. /// Subscribe to this event to perform pre-initialization work or to cancel initialization.
  85. /// </para>
  86. /// <para>
  87. /// This event follows the Terminal.Gui Cancellable Work Pattern.
  88. /// </para>
  89. /// </remarks>
  90. event EventHandler<System.ComponentModel.CancelEventArgs>? Initializing;
  91. /// <summary>
  92. /// Raised after the runnable has been initialized (via <see cref="ISupportInitialize.BeginInit"/>/<see cref="ISupportInitialize.EndInit"/>).
  93. /// The view is laid out and ready to be drawn.
  94. /// </summary>
  95. /// <remarks>
  96. /// <para>
  97. /// This event is from the <see cref="ISupportInitialize"/> pattern and is raised by <see cref="View.EndInit"/>.
  98. /// Subscribe to this event to perform initialization that requires the view to be fully laid out.
  99. /// </para>
  100. /// <para>
  101. /// This is the post-notification event in the Cancellable Work Pattern pair with <see cref="Initializing"/>.
  102. /// </para>
  103. /// </remarks>
  104. event EventHandler? Initialized;
  105. /// <summary>
  106. /// Raised when <see cref="IApplication.RequestStop"/> is called on this runnable.
  107. /// Can be canceled by setting <see cref="System.ComponentModel.CancelEventArgs.Cancel"/> to <see langword="true"/>.
  108. /// </summary>
  109. /// <remarks>
  110. /// Subscribe to this event to prevent the runnable from stopping (e.g., to prompt the user
  111. /// to save changes). If canceled, <see cref="Running"/> will remain <see langword="true"/>.
  112. /// </remarks>
  113. event EventHandler<System.ComponentModel.CancelEventArgs>? Stopping;
  114. /// <summary>
  115. /// Raised after the runnable session has stopped (<see cref="Running"/> = <see langword="false"/>).
  116. /// </summary>
  117. /// <remarks>
  118. /// This is the post-notification event in the Cancellable Work Pattern pair with <see cref="Stopping"/>.
  119. /// Subscribe to this event for cleanup work that should occur after the session stops.
  120. /// </remarks>
  121. event EventHandler? Stopped;
  122. /// <summary>
  123. /// Raised when the runnable session is about to start running.
  124. /// Can be canceled by setting <see cref="System.ComponentModel.CancelEventArgs.Cancel"/> to <see langword="true"/>.
  125. /// </summary>
  126. /// <remarks>
  127. /// Subscribe to this event to prevent starting or to perform pre-start work.
  128. /// This aligns with <see cref="Running"/> and <see cref="IApplication.Run"/>.
  129. /// </remarks>
  130. event EventHandler<System.ComponentModel.CancelEventArgs>? Starting;
  131. /// <summary>
  132. /// Raised when the runnable session has started running.
  133. /// </summary>
  134. /// <remarks>
  135. /// This is the post-notification event in the Cancellable Work Pattern pair with <see cref="Starting"/>.
  136. /// Subscribe to this event for post-start logic (e.g., setting focus).
  137. /// This aligns with <see cref="Running"/> and <see cref="IApplication.Run"/>.
  138. /// </remarks>
  139. event EventHandler? Started;
  140. #endregion
  141. }