IApplication.cs 15 KB

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