IApplication.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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>
  30. /// Gets or sets whether <see cref="Driver"/> will be forced to output only the 16 colors defined in
  31. /// <see cref="ColorName16"/>. The default is <see langword="false"/>, meaning 24-bit (TrueColor) colors will be output
  32. /// as long as the selected <see cref="IConsoleDriver"/> supports TrueColor.
  33. /// </summary>
  34. bool Force16Colors { get; set; }
  35. /// <summary>
  36. /// Forces the use of the specified driver (one of "fake", "dotnet", "windows", or "unix"). If not
  37. /// specified, the driver is selected based on the platform.
  38. /// </summary>
  39. string ForceDriver { get; set; }
  40. /// <summary>
  41. /// Collection of sixel images to write out to screen when updating.
  42. /// Only add to this collection if you are sure terminal supports sixel format.
  43. /// </summary>
  44. List<SixelToRender> Sixel { get; }
  45. /// <summary>
  46. /// Gets or sets the size of the screen. By default, this is the size of the screen as reported by the <see cref="IConsoleDriver"/>.
  47. /// </summary>
  48. Rectangle Screen { get; set; }
  49. /// <summary>
  50. /// Gets or sets whether the screen will be cleared, and all Views redrawn, during the next Application iteration.
  51. /// </summary>
  52. bool ClearScreenNextIteration { get; set; }
  53. /// <summary>Gets or sets the popover manager.</summary>
  54. ApplicationPopover? Popover { get; set; }
  55. /// <summary>Gets or sets the navigation manager.</summary>
  56. ApplicationNavigation? Navigation { get; set; }
  57. /// <summary>Gets the currently active Toplevel.</summary>
  58. Toplevel? Top { get; set; }
  59. /// <summary>Gets the stack of all Toplevels.</summary>
  60. System.Collections.Concurrent.ConcurrentStack<Toplevel> TopLevels { get; }
  61. /// <summary>
  62. /// Caches the Toplevel associated with the current RunState.
  63. /// </summary>
  64. Toplevel? CachedRunStateToplevel { get; set; }
  65. /// <summary>Requests that the application stop running.</summary>
  66. void RequestStop ();
  67. /// <summary>
  68. /// Causes any Toplevels that need layout to be laid out. Then draws any Toplevels that need display. Only Views that
  69. /// need to be laid out (see <see cref="View.NeedsLayout"/>) will be laid out.
  70. /// Only Views that need to be drawn (see <see cref="View.NeedsDraw"/>) will be drawn.
  71. /// </summary>
  72. /// <param name="forceRedraw">
  73. /// If <see langword="true"/> the entire View hierarchy will be redrawn. The default is <see langword="false"/> and
  74. /// should only be overriden for testing.
  75. /// </param>
  76. public void LayoutAndDraw (bool forceRedraw = false);
  77. /// <summary>Initializes a new instance of <see cref="Terminal.Gui"/> Application.</summary>
  78. /// <para>Call this method once per instance (or after <see cref="Shutdown"/> has been called).</para>
  79. /// <para>
  80. /// This function loads the right <see cref="IConsoleDriver"/> for the platform, Creates a <see cref="Toplevel"/>. and
  81. /// assigns it to <see cref="Application.Top"/>
  82. /// </para>
  83. /// <para>
  84. /// <see cref="Shutdown"/> must be called when the application is closing (typically after
  85. /// <see cref="Run{T}"/> has returned) to ensure resources are cleaned up and
  86. /// terminal settings
  87. /// restored.
  88. /// </para>
  89. /// <para>
  90. /// The <see cref="Run{T}"/> function combines
  91. /// <see cref="Init(IConsoleDriver,string)"/> and <see cref="Run(Toplevel, Func{Exception, bool})"/>
  92. /// into a single
  93. /// call. An application cam use <see cref="Run{T}"/> without explicitly calling
  94. /// <see cref="Init(IConsoleDriver,string)"/>.
  95. /// </para>
  96. /// <param name="driver">
  97. /// The <see cref="IConsoleDriver"/> to use. If neither <paramref name="driver"/> or
  98. /// <paramref name="driverName"/> are specified the default driver for the platform will be used.
  99. /// </param>
  100. /// <param name="driverName">
  101. /// The driver name (e.g. "dotnet", "windows", "fake", or "unix") of the
  102. /// <see cref="IConsoleDriver"/> to use. If neither <paramref name="driver"/> or <paramref name="driverName"/> are
  103. /// specified the default driver for the platform will be used.
  104. /// </param>
  105. [RequiresUnreferencedCode ("AOT")]
  106. [RequiresDynamicCode ("AOT")]
  107. public void Init (IConsoleDriver? driver = null, string? driverName = null);
  108. /// <summary>Runs <paramref name="action"/> on the main UI loop thread</summary>
  109. /// <param name="action">the action to be invoked on the main processing thread.</param>
  110. void Invoke (Action action);
  111. /// <summary>
  112. /// <see langword="true"/> if implementation is 'old'. <see langword="false"/> if implementation
  113. /// is cutting edge.
  114. /// </summary>
  115. bool IsLegacy { get; }
  116. /// <summary>Removes a previously scheduled timeout</summary>
  117. /// <remarks>The token parameter is the value returned by <see cref="AddTimeout"/>.</remarks>
  118. /// <returns>
  119. /// <see langword="true"/>
  120. /// if the timeout is successfully removed; otherwise,
  121. /// <see langword="false"/>
  122. /// .
  123. /// This method also returns
  124. /// <see langword="false"/>
  125. /// if the timeout is not found.
  126. /// </returns>
  127. bool RemoveTimeout (object token);
  128. /// <summary>Stops the provided <see cref="Toplevel"/>, causing or the <paramref name="top"/> if provided.</summary>
  129. /// <param name="top">The <see cref="Toplevel"/> to stop.</param>
  130. /// <remarks>
  131. /// <para>This will cause <see cref="Application.Run(Toplevel, Func{Exception, bool})"/> to return.</para>
  132. /// <para>
  133. /// Calling <see cref="RequestStop(Toplevel)"/> is equivalent to setting the <see cref="Toplevel.Running"/>
  134. /// property on the currently running <see cref="Toplevel"/> to false.
  135. /// </para>
  136. /// </remarks>
  137. void RequestStop (Toplevel? top);
  138. /// <summary>
  139. /// Runs the application by creating a <see cref="Toplevel"/> object and calling
  140. /// <see cref="Run(Toplevel, Func{Exception, bool})"/>.
  141. /// </summary>
  142. /// <remarks>
  143. /// <para>Calling <see cref="Init"/> first is not needed as this function will initialize the application.</para>
  144. /// <para>
  145. /// <see cref="Shutdown"/> must be called when the application is closing (typically after Run> has returned) to
  146. /// ensure resources are cleaned up and terminal settings restored.
  147. /// </para>
  148. /// <para>
  149. /// The caller is responsible for disposing the object returned by this method.
  150. /// </para>
  151. /// </remarks>
  152. /// <returns>The created <see cref="Toplevel"/> object. The caller is responsible for disposing this object.</returns>
  153. [RequiresUnreferencedCode ("AOT")]
  154. [RequiresDynamicCode ("AOT")]
  155. public Toplevel Run (Func<Exception, bool>? errorHandler = null, IConsoleDriver? driver = null);
  156. /// <summary>
  157. /// Runs the application by creating a <see cref="Toplevel"/>-derived object of type <c>T</c> and calling
  158. /// <see cref="Run(Toplevel, Func{Exception, bool})"/>.
  159. /// </summary>
  160. /// <remarks>
  161. /// <para>Calling <see cref="Init"/> first is not needed as this function will initialize the application.</para>
  162. /// <para>
  163. /// <see cref="Shutdown"/> must be called when the application is closing (typically after Run> has returned) to
  164. /// ensure resources are cleaned up and terminal settings restored.
  165. /// </para>
  166. /// <para>
  167. /// The caller is responsible for disposing the object returned by this method.
  168. /// </para>
  169. /// </remarks>
  170. /// <param name="errorHandler"></param>
  171. /// <param name="driver">
  172. /// The <see cref="IConsoleDriver"/> to use. If not specified the default driver for the platform will
  173. /// be used. Must be
  174. /// <see langword="null"/> if <see cref="Init"/> has already been called.
  175. /// </param>
  176. /// <returns>The created T object. The caller is responsible for disposing this object.</returns>
  177. [RequiresUnreferencedCode ("AOT")]
  178. [RequiresDynamicCode ("AOT")]
  179. public T Run<T> (Func<Exception, bool>? errorHandler = null, IConsoleDriver? driver = null)
  180. where T : Toplevel, new();
  181. /// <summary>Runs the Application using the provided <see cref="Toplevel"/> view.</summary>
  182. /// <remarks>
  183. /// <para>
  184. /// This method is used to start processing events for the main application, but it is also used to run other
  185. /// modal <see cref="View"/>s such as <see cref="Dialog"/> boxes.
  186. /// </para>
  187. /// <para>
  188. /// To make a <see cref="Run(Toplevel,System.Func{System.Exception,bool})"/> stop execution, call
  189. /// <see cref="Application.RequestStop"/>.
  190. /// </para>
  191. /// <para>
  192. /// Calling <see cref="Run(Toplevel,System.Func{System.Exception,bool})"/> is equivalent to calling
  193. /// <see cref="Application.Begin(Toplevel)"/>, followed by <see cref="Application.RunLoop(RunState)"/>, and then
  194. /// calling
  195. /// <see cref="Application.End(RunState)"/>.
  196. /// </para>
  197. /// <para>
  198. /// Alternatively, to have a program control the main loop and process events manually, call
  199. /// <see cref="Application.Begin(Toplevel)"/> to set things up manually and then repeatedly call
  200. /// <see cref="Application.RunLoop(RunState)"/> with the wait parameter set to false. By doing this the
  201. /// <see cref="Application.RunLoop(RunState)"/> method will only process any pending events, timers handlers and
  202. /// then
  203. /// return control immediately.
  204. /// </para>
  205. /// <para>
  206. /// When using <see cref="Run{T}"/> or
  207. /// <see cref="Run(System.Func{System.Exception,bool},IConsoleDriver)"/>
  208. /// <see cref="Init"/> will be called automatically.
  209. /// </para>
  210. /// <para>
  211. /// RELEASE builds only: When <paramref name="errorHandler"/> is <see langword="null"/> any exceptions will be
  212. /// rethrown. Otherwise, if <paramref name="errorHandler"/> will be called. If <paramref name="errorHandler"/>
  213. /// returns <see langword="true"/> the <see cref="Application.RunLoop(RunState)"/> will resume; otherwise this
  214. /// method will
  215. /// exit.
  216. /// </para>
  217. /// </remarks>
  218. /// <param name="view">The <see cref="Toplevel"/> to run as a modal.</param>
  219. /// <param name="errorHandler">
  220. /// RELEASE builds only: Handler for any unhandled exceptions (resumes when returns true,
  221. /// rethrows when null).
  222. /// </param>
  223. public void Run (Toplevel view, Func<Exception, bool>? errorHandler = null);
  224. /// <summary>Shutdown an application initialized with <see cref="Init"/>.</summary>
  225. /// <remarks>
  226. /// Shutdown must be called for every call to <see cref="Init"/> or
  227. /// <see cref="Application.Run(Toplevel, Func{Exception, bool})"/> to ensure all resources are cleaned
  228. /// up (Disposed)
  229. /// and terminal settings are restored.
  230. /// </remarks>
  231. public void Shutdown ();
  232. /// <summary>
  233. /// Handles recurring events. These are invoked on the main UI thread - allowing for
  234. /// safe updates to <see cref="View"/> instances.
  235. /// </summary>
  236. ITimedEvents? TimedEvents { get; }
  237. }