IApplication.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #nullable enable
  2. using System.Diagnostics.CodeAnalysis;
  3. namespace Terminal.Gui.App;
  4. /// <summary>
  5. /// Interface for instances that provide backing functionality to static
  6. /// gateway class <see cref="Application"/>.
  7. /// </summary>
  8. public interface IApplication
  9. {
  10. /// <summary>Adds a timeout to the application.</summary>
  11. /// <remarks>
  12. /// When time specified passes, the callback will be invoked. If the callback returns true, the timeout will be
  13. /// reset, repeating the invocation. If it returns false, the timeout will stop and be removed. The returned value is a
  14. /// token that can be used to stop the timeout by calling <see cref="RemoveTimeout(object)"/>.
  15. /// </remarks>
  16. object AddTimeout (TimeSpan time, Func<bool> callback);
  17. /// <summary>
  18. /// Handles keyboard input and key bindings at the Application level.
  19. /// </summary>
  20. IKeyboard Keyboard { get; set; }
  21. /// <summary>
  22. /// Handles mouse event state and processing.
  23. /// </summary>
  24. IMouse Mouse { get; set; }
  25. /// <summary>Gets or sets the console driver being used.</summary>
  26. IConsoleDriver? Driver { get; set; }
  27. /// <summary>Gets or sets whether the application has been initialized.</summary>
  28. bool Initialized { get; set; }
  29. /// <summary>Gets or sets the popover manager.</summary>
  30. ApplicationPopover? Popover { get; set; }
  31. /// <summary>Gets or sets the navigation manager.</summary>
  32. ApplicationNavigation? Navigation { get; set; }
  33. /// <summary>Gets the currently active Toplevel.</summary>
  34. Toplevel? Top { get; set; }
  35. /// <summary>Gets the stack of all Toplevels.</summary>
  36. System.Collections.Concurrent.ConcurrentStack<Toplevel> TopLevels { get; }
  37. /// <summary>Requests that the application stop running.</summary>
  38. void RequestStop ();
  39. /// <summary>
  40. /// Causes any Toplevels that need layout to be laid out. Then draws any Toplevels that need display. Only Views that
  41. /// need to be laid out (see <see cref="View.NeedsLayout"/>) will be laid out.
  42. /// Only Views that need to be drawn (see <see cref="View.NeedsDraw"/>) will be drawn.
  43. /// </summary>
  44. /// <param name="forceRedraw">
  45. /// If <see langword="true"/> the entire View hierarchy will be redrawn. The default is <see langword="false"/> and
  46. /// should only be overriden for testing.
  47. /// </param>
  48. public void LayoutAndDraw (bool forceRedraw = false);
  49. /// <summary>Initializes a new instance of <see cref="Terminal.Gui"/> Application.</summary>
  50. /// <para>Call this method once per instance (or after <see cref="Shutdown"/> has been called).</para>
  51. /// <para>
  52. /// This function loads the right <see cref="IConsoleDriver"/> for the platform, Creates a <see cref="Toplevel"/>. and
  53. /// assigns it to <see cref="Application.Top"/>
  54. /// </para>
  55. /// <para>
  56. /// <see cref="Shutdown"/> must be called when the application is closing (typically after
  57. /// <see cref="Run{T}"/> has returned) to ensure resources are cleaned up and
  58. /// terminal settings
  59. /// restored.
  60. /// </para>
  61. /// <para>
  62. /// The <see cref="Run{T}"/> function combines
  63. /// <see cref="Init(IConsoleDriver,string)"/> and <see cref="Run(Toplevel, Func{Exception, bool})"/>
  64. /// into a single
  65. /// call. An application cam use <see cref="Run{T}"/> without explicitly calling
  66. /// <see cref="Init(IConsoleDriver,string)"/>.
  67. /// </para>
  68. /// <param name="driver">
  69. /// The <see cref="IConsoleDriver"/> to use. If neither <paramref name="driver"/> or
  70. /// <paramref name="driverName"/> are specified the default driver for the platform will be used.
  71. /// </param>
  72. /// <param name="driverName">
  73. /// The driver name (e.g. "dotnet", "windows", "fake", or "unix") of the
  74. /// <see cref="IConsoleDriver"/> to use. If neither <paramref name="driver"/> or <paramref name="driverName"/> are
  75. /// specified the default driver for the platform will be used.
  76. /// </param>
  77. [RequiresUnreferencedCode ("AOT")]
  78. [RequiresDynamicCode ("AOT")]
  79. public void Init (IConsoleDriver? driver = null, string? driverName = null);
  80. /// <summary>Runs <paramref name="action"/> on the main UI loop thread</summary>
  81. /// <param name="action">the action to be invoked on the main processing thread.</param>
  82. void Invoke (Action action);
  83. /// <summary>
  84. /// <see langword="true"/> if implementation is 'old'. <see langword="false"/> if implementation
  85. /// is cutting edge.
  86. /// </summary>
  87. bool IsLegacy { get; }
  88. /// <summary>Removes a previously scheduled timeout</summary>
  89. /// <remarks>The token parameter is the value returned by <see cref="AddTimeout"/>.</remarks>
  90. /// <returns>
  91. /// <see langword="true"/>
  92. /// if the timeout is successfully removed; otherwise,
  93. /// <see langword="false"/>
  94. /// .
  95. /// This method also returns
  96. /// <see langword="false"/>
  97. /// if the timeout is not found.
  98. /// </returns>
  99. bool RemoveTimeout (object token);
  100. /// <summary>Stops the provided <see cref="Toplevel"/>, causing or the <paramref name="top"/> if provided.</summary>
  101. /// <param name="top">The <see cref="Toplevel"/> to stop.</param>
  102. /// <remarks>
  103. /// <para>This will cause <see cref="Application.Run(Toplevel, Func{Exception, bool})"/> to return.</para>
  104. /// <para>
  105. /// Calling <see cref="RequestStop(Toplevel)"/> is equivalent to setting the <see cref="Toplevel.Running"/>
  106. /// property on the currently running <see cref="Toplevel"/> to false.
  107. /// </para>
  108. /// </remarks>
  109. void RequestStop (Toplevel? top);
  110. /// <summary>
  111. /// Runs the application by creating a <see cref="Toplevel"/> object and calling
  112. /// <see cref="Run(Toplevel, Func{Exception, bool})"/>.
  113. /// </summary>
  114. /// <remarks>
  115. /// <para>Calling <see cref="Init"/> first is not needed as this function will initialize the application.</para>
  116. /// <para>
  117. /// <see cref="Shutdown"/> must be called when the application is closing (typically after Run> has returned) to
  118. /// ensure resources are cleaned up and terminal settings restored.
  119. /// </para>
  120. /// <para>
  121. /// The caller is responsible for disposing the object returned by this method.
  122. /// </para>
  123. /// </remarks>
  124. /// <returns>The created <see cref="Toplevel"/> object. The caller is responsible for disposing this object.</returns>
  125. [RequiresUnreferencedCode ("AOT")]
  126. [RequiresDynamicCode ("AOT")]
  127. public Toplevel Run (Func<Exception, bool>? errorHandler = null, IConsoleDriver? driver = null);
  128. /// <summary>
  129. /// Runs the application by creating a <see cref="Toplevel"/>-derived object of type <c>T</c> and calling
  130. /// <see cref="Run(Toplevel, Func{Exception, bool})"/>.
  131. /// </summary>
  132. /// <remarks>
  133. /// <para>Calling <see cref="Init"/> first is not needed as this function will initialize the application.</para>
  134. /// <para>
  135. /// <see cref="Shutdown"/> must be called when the application is closing (typically after Run> has returned) to
  136. /// ensure resources are cleaned up and terminal settings restored.
  137. /// </para>
  138. /// <para>
  139. /// The caller is responsible for disposing the object returned by this method.
  140. /// </para>
  141. /// </remarks>
  142. /// <param name="errorHandler"></param>
  143. /// <param name="driver">
  144. /// The <see cref="IConsoleDriver"/> to use. If not specified the default driver for the platform will
  145. /// be used. Must be
  146. /// <see langword="null"/> if <see cref="Init"/> has already been called.
  147. /// </param>
  148. /// <returns>The created T object. The caller is responsible for disposing this object.</returns>
  149. [RequiresUnreferencedCode ("AOT")]
  150. [RequiresDynamicCode ("AOT")]
  151. public T Run<T> (Func<Exception, bool>? errorHandler = null, IConsoleDriver? driver = null)
  152. where T : Toplevel, new();
  153. /// <summary>Runs the Application using the provided <see cref="Toplevel"/> view.</summary>
  154. /// <remarks>
  155. /// <para>
  156. /// This method is used to start processing events for the main application, but it is also used to run other
  157. /// modal <see cref="View"/>s such as <see cref="Dialog"/> boxes.
  158. /// </para>
  159. /// <para>
  160. /// To make a <see cref="Run(Toplevel,System.Func{System.Exception,bool})"/> stop execution, call
  161. /// <see cref="Application.RequestStop"/>.
  162. /// </para>
  163. /// <para>
  164. /// Calling <see cref="Run(Toplevel,System.Func{System.Exception,bool})"/> is equivalent to calling
  165. /// <see cref="Application.Begin(Toplevel)"/>, followed by <see cref="Application.RunLoop(RunState)"/>, and then
  166. /// calling
  167. /// <see cref="Application.End(RunState)"/>.
  168. /// </para>
  169. /// <para>
  170. /// Alternatively, to have a program control the main loop and process events manually, call
  171. /// <see cref="Application.Begin(Toplevel)"/> to set things up manually and then repeatedly call
  172. /// <see cref="Application.RunLoop(RunState)"/> with the wait parameter set to false. By doing this the
  173. /// <see cref="Application.RunLoop(RunState)"/> method will only process any pending events, timers handlers and
  174. /// then
  175. /// return control immediately.
  176. /// </para>
  177. /// <para>
  178. /// When using <see cref="Run{T}"/> or
  179. /// <see cref="Run(System.Func{System.Exception,bool},IConsoleDriver)"/>
  180. /// <see cref="Init"/> will be called automatically.
  181. /// </para>
  182. /// <para>
  183. /// RELEASE builds only: When <paramref name="errorHandler"/> is <see langword="null"/> any exceptions will be
  184. /// rethrown. Otherwise, if <paramref name="errorHandler"/> will be called. If <paramref name="errorHandler"/>
  185. /// returns <see langword="true"/> the <see cref="Application.RunLoop(RunState)"/> will resume; otherwise this
  186. /// method will
  187. /// exit.
  188. /// </para>
  189. /// </remarks>
  190. /// <param name="view">The <see cref="Toplevel"/> to run as a modal.</param>
  191. /// <param name="errorHandler">
  192. /// RELEASE builds only: Handler for any unhandled exceptions (resumes when returns true,
  193. /// rethrows when null).
  194. /// </param>
  195. public void Run (Toplevel view, Func<Exception, bool>? errorHandler = null);
  196. /// <summary>Shutdown an application initialized with <see cref="Init"/>.</summary>
  197. /// <remarks>
  198. /// Shutdown must be called for every call to <see cref="Init"/> or
  199. /// <see cref="Application.Run(Toplevel, Func{Exception, bool})"/> to ensure all resources are cleaned
  200. /// up (Disposed)
  201. /// and terminal settings are restored.
  202. /// </remarks>
  203. public void Shutdown ();
  204. /// <summary>
  205. /// Handles recurring events. These are invoked on the main UI thread - allowing for
  206. /// safe updates to <see cref="View"/> instances.
  207. /// </summary>
  208. ITimedEvents? TimedEvents { get; }
  209. }