IApplication.cs 14 KB

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