IApplication.cs 14 KB

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