IApplication.cs 10 KB

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