IApplication.cs 10 KB

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