IApplication.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. using System.Collections.Concurrent;
  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. /// <remarks>
  9. /// <para>
  10. /// Implements <see cref="IDisposable"/> to support automatic resource cleanup via using statements.
  11. /// Call <see cref="IDisposable.Dispose"/> or use a using statement to properly clean up resources.
  12. /// </para>
  13. /// </remarks>
  14. public interface IApplication : IDisposable
  15. {
  16. #region Lifecycle - App Initialization and Shutdown
  17. /// <summary>
  18. /// Gets or sets the managed thread ID of the application's main UI thread, which is set during
  19. /// <see cref="Init"/> and used to determine if code is executing on the main thread.
  20. /// </summary>
  21. /// <value>
  22. /// The managed thread ID of the main UI thread, or <see langword="null"/> if the application is not initialized.
  23. /// </value>
  24. public int? MainThreadId { get; internal set; }
  25. /// <summary>Initializes a new instance of <see cref="Terminal.Gui"/> Application.</summary>
  26. /// <param name="driverName">
  27. /// The short name (e.g. "dotnet", "windows", "unix", or "fake") of the
  28. /// <see cref="IDriver"/> to use. If not specified the default driver for the platform will be used.
  29. /// </param>
  30. /// <returns>This instance for fluent API chaining.</returns>
  31. /// <remarks>
  32. /// <para>Call this method once per instance (or after <see cref="IDisposable.Dispose"/> has been called).</para>
  33. /// <para>
  34. /// This function loads the right <see cref="IDriver"/> for the platform, creates a main loop coordinator,
  35. /// initializes keyboard and mouse handlers, and subscribes to driver events.
  36. /// </para>
  37. /// <para>
  38. /// <see cref="IDisposable.Dispose"/> must be called when the application is closing (typically after
  39. /// <see cref="Run{TRunnable}"/> has returned) to ensure all resources are cleaned up (disposed) and
  40. /// terminal settings are restored.
  41. /// </para>
  42. /// <para>
  43. /// Supports fluent API with automatic resource management:
  44. /// </para>
  45. /// <para>
  46. /// Recommended pattern (using statement):
  47. /// <code>
  48. /// using (var app = Application.Create().Init())
  49. /// {
  50. /// app.Run&lt;MyDialog&gt;();
  51. /// var result = app.GetResult&lt;MyResultType&gt;();
  52. /// } // app.Dispose() called automatically
  53. /// </code>
  54. /// </para>
  55. /// <para>
  56. /// Alternative pattern (manual disposal):
  57. /// <code>
  58. /// var app = Application.Create().Init();
  59. /// app.Run&lt;MyDialog&gt;();
  60. /// var result = app.GetResult&lt;MyResultType&gt;();
  61. /// app.Dispose(); // Must call explicitly
  62. /// </code>
  63. /// </para>
  64. /// <para>
  65. /// Note: Runnables created by <see cref="Run{TRunnable}"/> are automatically disposed when
  66. /// that method returns. Runnables passed to <see cref="Run(IRunnable, Func{Exception, bool})"/>
  67. /// must be disposed by the caller.
  68. /// </para>
  69. /// </remarks>
  70. [RequiresUnreferencedCode ("AOT")]
  71. [RequiresDynamicCode ("AOT")]
  72. public IApplication Init (string? driverName = null);
  73. /// <summary>
  74. /// This event is raised after the <see cref="Init"/> and <see cref="IDisposable.Dispose"/> methods have been called.
  75. /// </summary>
  76. /// <remarks>
  77. /// Intended to support unit tests that need to know when the application has been initialized.
  78. /// </remarks>
  79. public event EventHandler<EventArgs<bool>>? InitializedChanged;
  80. /// <summary>Gets or sets whether the application has been initialized.</summary>
  81. bool Initialized { get; set; }
  82. /// <summary>
  83. /// Gets or sets a value indicating whether this application is running in example mode.
  84. /// When <see langword="true"/>, metadata is collected and demo keys are automatically sent.
  85. /// </summary>
  86. bool IsExample { get; set; }
  87. /// <summary>
  88. /// INTERNAL: Resets the state of this instance. Called by Dispose.
  89. /// </summary>
  90. /// <param name="ignoreDisposed">If true, ignores disposed state checks during reset.</param>
  91. /// <remarks>
  92. /// <para>
  93. /// Encapsulates all setting of initial state for Application; having this in a function like this ensures we
  94. /// don't make mistakes in guaranteeing that the state of this singleton is deterministic when <see cref="Init"/>
  95. /// starts running and after <see cref="IDisposable.Dispose"/> returns.
  96. /// </para>
  97. /// <para>
  98. /// IMPORTANT: Ensure all property/fields are reset here. See Init_ResetState_Resets_Properties unit test.
  99. /// </para>
  100. /// </remarks>
  101. internal void ResetState (bool ignoreDisposed = false);
  102. #endregion App Initialization and Shutdown
  103. #region Session Management - Begin->Run->Iteration->Stop->End
  104. /// <summary>
  105. /// Gets the stack of all active runnable session tokens.
  106. /// Sessions execute serially - the top of stack is the currently modal session.
  107. /// </summary>
  108. /// <remarks>
  109. /// <para>
  110. /// Session tokens are pushed onto the stack when <see cref="Run(IRunnable, Func{Exception, bool})"/> is called and
  111. /// popped when
  112. /// <see cref="RequestStop(IRunnable)"/> completes. The stack grows during nested modal calls and
  113. /// shrinks as they complete.
  114. /// </para>
  115. /// <para>
  116. /// Only the top session (<see cref="TopRunnableView"/>) has exclusive keyboard/mouse input (
  117. /// <see cref="IRunnable.IsModal"/> = true).
  118. /// All other sessions on the stack continue to be laid out, drawn, and receive iteration events (
  119. /// <see cref="IRunnable.IsRunning"/> = true),
  120. /// but they don't receive user input.
  121. /// </para>
  122. /// <example>
  123. /// Stack during nested modals:
  124. /// <code>
  125. /// RunnableSessionStack (top to bottom):
  126. /// - MessageBox (TopRunnable, IsModal=true, IsRunning=true, has input)
  127. /// - FileDialog (IsModal=false, IsRunning=true, continues to update/draw)
  128. /// - MainWindow (IsModal=false, IsRunning=true, continues to update/draw)
  129. /// </code>
  130. /// </example>
  131. /// </remarks>
  132. ConcurrentStack<SessionToken>? SessionStack { get; }
  133. /// <summary>
  134. /// Raised when <see cref="Begin(IRunnable)"/> has been called and has created a new <see cref="SessionToken"/>.
  135. /// </summary>
  136. /// <remarks>
  137. /// If <see cref="StopAfterFirstIteration"/> is <see langword="true"/>, callers to <see cref="Begin(IRunnable)"/>
  138. /// must also subscribe to <see cref="SessionEnded"/> and manually dispose of the <see cref="SessionToken"/> token
  139. /// when the application is done.
  140. /// </remarks>
  141. public event EventHandler<SessionTokenEventArgs>? SessionBegun;
  142. #region TopRunnable Properties
  143. /// <summary>Gets the Runnable that is on the top of the <see cref="SessionStack"/>.</summary>
  144. /// <remarks>
  145. /// <para>
  146. /// The top runnable in the session stack captures all mouse and keyboard input.
  147. /// This is set by <see cref="Begin(IRunnable)"/> and cleared by <see cref="End(SessionToken)"/>.
  148. /// </para>
  149. /// </remarks>
  150. IRunnable? TopRunnable { get; }
  151. /// <summary>Gets the View that is on the top of the <see cref="SessionStack"/>.</summary>
  152. /// <remarks>
  153. /// <para>
  154. /// This is a convenience property that casts <see cref="TopRunnable"/> to a <see cref="View"/>.
  155. /// </para>
  156. /// </remarks>
  157. View? TopRunnableView { get; }
  158. #endregion TopRunnable Properties
  159. /// <summary>
  160. /// Building block API: Creates a <see cref="SessionToken"/> and prepares the provided <see cref="IRunnable"/>
  161. /// for
  162. /// execution. Not usually called directly by applications. Use <see cref="Run(IRunnable, Func{Exception, bool})"/>
  163. /// instead.
  164. /// </summary>
  165. /// <param name="runnable">The <see cref="IRunnable"/> to prepare execution for.</param>
  166. /// <returns>
  167. /// The <see cref="SessionToken"/> that needs to be passed to the <see cref="End(SessionToken)"/>
  168. /// method upon
  169. /// completion.
  170. /// </returns>
  171. /// <remarks>
  172. /// <para>
  173. /// This method prepares the provided <see cref="IRunnable"/> for running. It adds this to the
  174. /// <see cref="SessionStack"/>, lays out the SubViews, focuses the first element, and draws the
  175. /// runnable on the screen. This is usually followed by starting the main loop, and then the
  176. /// <see cref="End(SessionToken)"/> method upon termination which will undo these changes.
  177. /// </para>
  178. /// <para>
  179. /// Raises the <see cref="IRunnable.IsRunningChanging"/>, <see cref="IRunnable.IsRunningChanged"/>,
  180. /// and <see cref="IRunnable.IsModalChanged"/> events.
  181. /// </para>
  182. /// </remarks>
  183. /// <returns>The session token. <see langword="null"/> if the operation was cancelled.</returns>
  184. SessionToken? Begin (IRunnable runnable);
  185. /// <summary>
  186. /// Runs a new Session with the provided runnable view.
  187. /// </summary>
  188. /// <param name="runnable">The runnable to execute.</param>
  189. /// <param name="errorHandler">Optional handler for unhandled exceptions (resumes when returns true, rethrows when null).</param>
  190. /// <remarks>
  191. /// <para>
  192. /// This method is used to start processing events for the main application, but it is also used to run other
  193. /// modal views such as dialogs.
  194. /// </para>
  195. /// <para>
  196. /// To make <see cref="Run(IRunnable, Func{Exception, bool})"/> stop execution, call
  197. /// <see cref="RequestStop()"/> or <see cref="RequestStop(IRunnable)"/>.
  198. /// </para>
  199. /// <para>
  200. /// Calling <see cref="Run(IRunnable, Func{Exception, bool})"/> is equivalent to calling
  201. /// <see cref="Begin(IRunnable)"/>, followed by starting the main loop, and then calling
  202. /// <see cref="End(SessionToken)"/>.
  203. /// </para>
  204. /// <para>
  205. /// In RELEASE builds: When <paramref name="errorHandler"/> is <see langword="null"/> any exceptions will be
  206. /// rethrown. Otherwise, <paramref name="errorHandler"/> will be called. If <paramref name="errorHandler"/>
  207. /// returns <see langword="true"/> the main loop will resume; otherwise this method will exit.
  208. /// </para>
  209. /// </remarks>
  210. object? Run (IRunnable runnable, Func<Exception, bool>? errorHandler = null);
  211. /// <summary>
  212. /// Runs a new Session creating a <see cref="IRunnable"/>-derived object of type <typeparamref name="TRunnable"/>
  213. /// and calling <see cref="Run(IRunnable, Func{Exception, bool})"/>. When the session is stopped,
  214. /// <see cref="End(SessionToken)"/> will be called.
  215. /// </summary>
  216. /// <typeparam name="TRunnable"></typeparam>
  217. /// <param name="errorHandler">Handler for any unhandled exceptions (resumes when returns true, rethrows when null).</param>
  218. /// <param name="driverName">
  219. /// The driver name. If not specified the default driver for the platform will be used. Must be
  220. /// <see langword="null"/> if <see cref="Init"/> has already been called.
  221. /// </param>
  222. /// <returns>
  223. /// The created <see name="IApplication"/> object. The caller is responsible for calling
  224. /// <see cref="IDisposable.Dispose"/> on this
  225. /// object.
  226. /// </returns>
  227. /// <remarks>
  228. /// <para>
  229. /// This method is used to start processing events for the main application, but it is also used to run other
  230. /// modal <see cref="View"/>s such as <see cref="Dialog"/> boxes.
  231. /// </para>
  232. /// <para>
  233. /// To make <see cref="Run(IRunnable, Func{Exception, bool})"/> stop execution, call
  234. /// <see cref="RequestStop()"/> or <see cref="RequestStop(IRunnable)"/>.
  235. /// </para>
  236. /// <para>
  237. /// In RELEASE builds: When <paramref name="errorHandler"/> is <see langword="null"/> any exceptions will be
  238. /// rethrown. Otherwise, <paramref name="errorHandler"/> will be called. If <paramref name="errorHandler"/>
  239. /// returns <see langword="true"/> the main loop will resume; otherwise this method will exit.
  240. /// </para>
  241. /// <para>
  242. /// <see cref="IDisposable.Dispose"/> must be called when the application is closing (typically after Run has
  243. /// returned) to
  244. /// ensure resources are cleaned up and terminal settings restored.
  245. /// </para>
  246. /// <para>
  247. /// In RELEASE builds: When <paramref name="errorHandler"/> is <see langword="null"/> any exceptions will be
  248. /// rethrown. Otherwise, <paramref name="errorHandler"/> will be called. If <paramref name="errorHandler"/>
  249. /// returns <see langword="true"/> the main loop will resume; otherwise this method will exit.
  250. /// </para>
  251. /// <para>
  252. /// The caller is responsible for disposing the object returned by this method.
  253. /// </para>
  254. /// </remarks>
  255. [RequiresUnreferencedCode ("AOT")]
  256. [RequiresDynamicCode ("AOT")]
  257. public IApplication Run<TRunnable> (Func<Exception, bool>? errorHandler = null, string? driverName = null)
  258. where TRunnable : IRunnable, new ();
  259. #region Iteration & Invoke
  260. /// <summary>
  261. /// Raises the <see cref="Iteration"/> event.
  262. /// </summary>
  263. /// <remarks>
  264. /// This is called once per main loop iteration, before processing input, timeouts, or rendering.
  265. /// </remarks>
  266. public void RaiseIteration ();
  267. /// <summary>This event is raised on each iteration of the main loop.</summary>
  268. /// <remarks>
  269. /// <para>
  270. /// This event is raised before input processing, timeout callbacks, and rendering occur each iteration.
  271. /// </para>
  272. /// <para>The event args contain the current application instance.</para>
  273. /// </remarks>
  274. /// <seealso cref="AddTimeout"/>
  275. /// <seealso cref="TimedEvents"/>
  276. /// .
  277. public event EventHandler<EventArgs<IApplication?>>? Iteration;
  278. /// <summary>Runs <paramref name="action"/> on the main UI loop thread.</summary>
  279. /// <param name="action">The action to be invoked on the main processing thread.</param>
  280. /// <remarks>
  281. /// <para>
  282. /// If called from the main thread, the action is executed immediately. Otherwise, it is queued via
  283. /// <see cref="AddTimeout"/> with <see cref="TimeSpan.Zero"/> and will be executed on the next main loop
  284. /// iteration.
  285. /// </para>
  286. /// </remarks>
  287. void Invoke (Action<IApplication>? action);
  288. /// <summary>Runs <paramref name="action"/> on the main UI loop thread.</summary>
  289. /// <param name="action">The action to be invoked on the main processing thread.</param>
  290. /// <remarks>
  291. /// <para>
  292. /// If called from the main thread, the action is executed immediately. Otherwise, it is queued via
  293. /// <see cref="AddTimeout"/> with <see cref="TimeSpan.Zero"/> and will be executed on the next main loop
  294. /// iteration.
  295. /// </para>
  296. /// </remarks>
  297. void Invoke (Action action);
  298. #endregion Iteration & Invoke
  299. /// <summary>
  300. /// Set to <see langword="true"/> to cause the session to stop running after first iteration.
  301. /// </summary>
  302. /// <remarks>
  303. /// <para>
  304. /// Used primarily for unit testing. When <see langword="true"/>, <see cref="End(SessionToken)"/> will be
  305. /// called
  306. /// automatically after the first main loop iteration.
  307. /// </para>
  308. /// </remarks>
  309. bool StopAfterFirstIteration { get; set; }
  310. /// <summary>Requests that the currently running Session stop. The Session will stop after the current iteration completes.</summary>
  311. /// <remarks>
  312. /// <para>This will cause <see cref="Run(IRunnable, Func{Exception, bool})"/> to return.</para>
  313. /// <para>
  314. /// This is equivalent to calling <see cref="RequestStop(IRunnable)"/> with <see cref="TopRunnableView"/> as the
  315. /// parameter.
  316. /// </para>
  317. /// </remarks>
  318. void RequestStop ();
  319. /// <summary>
  320. /// Requests that the specified runnable session stop.
  321. /// </summary>
  322. /// <param name="runnable">
  323. /// The runnable to stop. If <see langword="null"/>, stops the current <see cref="TopRunnableView"/>
  324. /// .
  325. /// </param>
  326. /// <remarks>
  327. /// <para>
  328. /// This will cause <see cref="Run(IRunnable, Func{Exception, bool})"/> to return.
  329. /// </para>
  330. /// <para>
  331. /// Raises <see cref="IRunnable.IsRunningChanging"/>, <see cref="IRunnable.IsRunningChanged"/>,
  332. /// and <see cref="IRunnable.IsModalChanged"/> events.
  333. /// </para>
  334. /// </remarks>
  335. void RequestStop (IRunnable? runnable);
  336. /// <summary>
  337. /// Building block API: Ends the session associated with the token and completes the execution of an
  338. /// <see cref="IRunnable"/>.
  339. /// Not usually called directly by applications. <see cref="Run(IRunnable, Func{Exception, bool})"/>
  340. /// will automatically call this method when the session is stopped.
  341. /// </summary>
  342. /// <param name="sessionToken">
  343. /// The <see cref="SessionToken"/> returned by the <see cref="Begin(IRunnable)"/>
  344. /// method.
  345. /// </param>
  346. /// <remarks>
  347. /// <para>
  348. /// This method removes the <see cref="IRunnable"/> from the <see cref="SessionStack"/>,
  349. /// raises the lifecycle events, and disposes the <paramref name="sessionToken"/>.
  350. /// </para>
  351. /// <para>
  352. /// Raises <see cref="IRunnable.IsRunningChanging"/>, <see cref="IRunnable.IsRunningChanged"/>,
  353. /// and <see cref="IRunnable.IsModalChanged"/> events.
  354. /// </para>
  355. /// </remarks>
  356. void End (SessionToken sessionToken);
  357. /// <summary>
  358. /// Raised when <see cref="End(SessionToken)"/> was called and the session is stopping. The event args contain a
  359. /// reference to the <see cref="IRunnable"/>
  360. /// that was active during the session. This can be used to ensure the Runnable is disposed of properly.
  361. /// </summary>
  362. /// <remarks>
  363. /// If <see cref="StopAfterFirstIteration"/> is <see langword="true"/>, callers to <see cref="Begin(IRunnable)"/>
  364. /// must also subscribe to <see cref="SessionEnded"/> and manually dispose of the <see cref="SessionToken"/> token
  365. /// when the application is done.
  366. /// </remarks>
  367. public event EventHandler<SessionTokenEventArgs>? SessionEnded;
  368. #endregion Session Management - Begin->Run->Iteration->Stop->End
  369. #region Result Management
  370. /// <summary>
  371. /// Gets the result from the last <see cref="Run(IRunnable, Func{Exception, bool})"/> or
  372. /// <see cref="Run{TRunnable}(Func{Exception, bool}, string)"/> call.
  373. /// </summary>
  374. /// <returns>
  375. /// The result from the last run session, or <see langword="null"/> if no session has been run or the result was null.
  376. /// </returns>
  377. object? GetResult ();
  378. /// <summary>
  379. /// Gets the result from the last <see cref="Run(IRunnable, Func{Exception, bool})"/> or
  380. /// <see cref="Run{TRunnable}(Func{Exception, bool}, string)"/> call, cast to type <typeparamref name="T"/>.
  381. /// </summary>
  382. /// <typeparam name="T">The expected result type.</typeparam>
  383. /// <returns>
  384. /// The result cast to <typeparamref name="T"/>, or <see langword="null"/> if the result is null or cannot be cast.
  385. /// </returns>
  386. /// <example>
  387. /// <code>
  388. /// using (var app = Application.Create().Init())
  389. /// {
  390. /// app.Run&lt;ColorPickerDialog&gt;();
  391. /// var selectedColor = app.GetResult&lt;Color&gt;();
  392. /// if (selectedColor.HasValue)
  393. /// {
  394. /// // Use the color
  395. /// }
  396. /// }
  397. /// </code>
  398. /// </example>
  399. T? GetResult<T> () where T : class => GetResult () as T;
  400. #endregion Result Management
  401. #region Screen and Driver
  402. /// <summary>Gets or sets the console driver being used.</summary>
  403. /// <remarks>
  404. /// <para>
  405. /// Set by <see cref="Init"/> based on the driver parameter or platform default.
  406. /// </para>
  407. /// </remarks>
  408. IDriver? Driver { get; set; }
  409. /// <summary>
  410. /// Gets the clipboard for this application instance.
  411. /// </summary>
  412. /// <remarks>
  413. /// <para>
  414. /// Provides access to the OS clipboard through the driver. Returns <see langword="null"/> if
  415. /// <see cref="Driver"/> is not initialized.
  416. /// </para>
  417. /// </remarks>
  418. IClipboard? Clipboard { get; }
  419. /// <summary>
  420. /// Gets or sets whether <see cref="Driver"/> will be forced to output only the 16 colors defined in
  421. /// <see cref="ColorName16"/>. The default is <see langword="false"/>, meaning 24-bit (TrueColor) colors will be
  422. /// output as long as the selected <see cref="IDriver"/> supports TrueColor.
  423. /// </summary>
  424. bool Force16Colors { get; set; }
  425. /// <summary>
  426. /// Forces the use of the specified driver (one of "fake", "dotnet", "windows", or "unix"). If not
  427. /// specified, the driver is selected based on the platform.
  428. /// </summary>
  429. string ForceDriver { get; set; }
  430. /// <summary>
  431. /// Gets or sets the size of the screen. By default, this is the size of the screen as reported by the
  432. /// <see cref="IDriver"/>.
  433. /// </summary>
  434. /// <remarks>
  435. /// <para>
  436. /// If the <see cref="IDriver"/> has not been initialized, this will return a default size of 2048x2048; useful
  437. /// for unit tests.
  438. /// </para>
  439. /// </remarks>
  440. Rectangle Screen { get; set; }
  441. /// <summary>Raised when the terminal's size changed. The new size of the terminal is provided.</summary>
  442. /// <remarks>
  443. /// <para>
  444. /// This event is raised when the driver detects a screen size change. The event provides the new screen
  445. /// rectangle.
  446. /// </para>
  447. /// </remarks>
  448. public event EventHandler<EventArgs<Rectangle>>? ScreenChanged;
  449. /// <summary>
  450. /// Gets or sets whether the screen will be cleared, and all Views redrawn, during the next Application iteration.
  451. /// </summary>
  452. /// <remarks>
  453. /// <para>
  454. /// This is typically set to <see langword="true"/> when a View's <see cref="View.Frame"/> changes and that view
  455. /// has no SuperView (e.g. when <see cref="TopRunnableView"/> is moved or resized).
  456. /// </para>
  457. /// <para>
  458. /// Automatically reset to <see langword="false"/> after <see cref="LayoutAndDraw"/> processes it.
  459. /// </para>
  460. /// </remarks>
  461. bool ClearScreenNextIteration { get; set; }
  462. /// <summary>
  463. /// Collection of sixel images to write out to screen when updating.
  464. /// Only add to this collection if you are sure terminal supports sixel format.
  465. /// </summary>
  466. List<SixelToRender> Sixel { get; }
  467. #endregion Screen and Driver
  468. #region Keyboard
  469. /// <summary>
  470. /// Handles keyboard input and key bindings at the Application level.
  471. /// </summary>
  472. /// <remarks>
  473. /// <para>
  474. /// Provides access to keyboard state, key bindings, and keyboard event handling. Set during <see cref="Init"/>.
  475. /// </para>
  476. /// </remarks>
  477. IKeyboard Keyboard { get; set; }
  478. #endregion Keyboard
  479. #region Mouse
  480. /// <summary>
  481. /// Handles mouse event state and processing.
  482. /// </summary>
  483. /// <remarks>
  484. /// <para>
  485. /// Provides access to mouse state, mouse grabbing, and mouse event handling. Set during <see cref="Init"/>.
  486. /// </para>
  487. /// </remarks>
  488. IMouse Mouse { get; set; }
  489. #endregion Mouse
  490. #region Layout and Drawing
  491. /// <summary>
  492. /// Causes any Runnables that need layout to be laid out, then draws any Runnables that need display. Only Views
  493. /// that need to be laid out (see <see cref="View.NeedsLayout"/>) will be laid out. Only Views that need to be drawn
  494. /// (see <see cref="View.NeedsDraw"/>) will be drawn.
  495. /// </summary>
  496. /// <param name="forceRedraw">
  497. /// If <see langword="true"/> the entire View hierarchy will be redrawn. The default is <see langword="false"/> and
  498. /// should only be overridden for testing.
  499. /// </param>
  500. /// <remarks>
  501. /// <para>
  502. /// This method is called automatically each main loop iteration when any views need layout or drawing.
  503. /// </para>
  504. /// <para>
  505. /// If <see cref="ClearScreenNextIteration"/> is <see langword="true"/>, the screen will be cleared before
  506. /// drawing and the flag will be reset to <see langword="false"/>.
  507. /// </para>
  508. /// </remarks>
  509. public void LayoutAndDraw (bool forceRedraw = false);
  510. /// <summary>
  511. /// Calls <see cref="View.PositionCursor"/> on the most focused view.
  512. /// </summary>
  513. /// <remarks>
  514. /// <para>Does nothing if there is no most focused view.</para>
  515. /// <para>
  516. /// If the most focused view is not visible within its superview, the cursor will be hidden.
  517. /// </para>
  518. /// </remarks>
  519. /// <returns><see langword="true"/> if a view positioned the cursor and the position is visible.</returns>
  520. public bool PositionCursor ();
  521. #endregion Layout and Drawing
  522. #region Navigation and Popover
  523. /// <summary>Gets or sets the navigation manager.</summary>
  524. /// <remarks>
  525. /// <para>
  526. /// Manages focus navigation and tracking of the most focused view. Initialized during <see cref="Init"/>.
  527. /// </para>
  528. /// </remarks>
  529. ApplicationNavigation? Navigation { get; set; }
  530. /// <summary>Gets or sets the popover manager.</summary>
  531. /// <remarks>
  532. /// <para>
  533. /// Manages application-level popover views. Initialized during <see cref="Init"/>.
  534. /// </para>
  535. /// </remarks>
  536. ApplicationPopover? Popover { get; set; }
  537. #endregion Navigation and Popover
  538. #region Timeouts
  539. /// <summary>Adds a timeout to the application.</summary>
  540. /// <param name="time">The time span to wait before invoking the callback.</param>
  541. /// <param name="callback">
  542. /// The callback to invoke. If it returns <see langword="true"/>, the timeout will be reset and repeat. If it
  543. /// returns <see langword="false"/>, the timeout will stop and be removed.
  544. /// </param>
  545. /// <returns>
  546. /// Call <see cref="RemoveTimeout(object)"/> with the returned value to stop the timeout.
  547. /// </returns>
  548. /// <remarks>
  549. /// <para>
  550. /// When the time specified passes, the callback will be invoked on the main UI thread.
  551. /// </para>
  552. /// <para>
  553. /// <see cref="IDisposable.Dispose"/> calls StopAll on <see cref="TimedEvents"/> to remove all timeouts.
  554. /// </para>
  555. /// </remarks>
  556. object? AddTimeout (TimeSpan time, Func<bool> callback);
  557. /// <summary>Removes a previously scheduled timeout.</summary>
  558. /// <param name="token">The token returned by <see cref="AddTimeout"/>.</param>
  559. /// <returns>
  560. /// <see langword="true"/> if the timeout is successfully removed; otherwise, <see langword="false"/>.
  561. /// This method also returns <see langword="false"/> if the timeout is not found.
  562. /// </returns>
  563. bool RemoveTimeout (object token);
  564. /// <summary>
  565. /// Handles recurring events. These are invoked on the main UI thread - allowing for
  566. /// safe updates to <see cref="View"/> instances.
  567. /// </summary>
  568. /// <remarks>
  569. /// <para>
  570. /// Provides low-level access to the timeout management system. Most applications should use
  571. /// <see cref="AddTimeout"/> and <see cref="RemoveTimeout"/> instead.
  572. /// </para>
  573. /// </remarks>
  574. ITimedEvents? TimedEvents { get; }
  575. #endregion Timeouts
  576. /// <summary>
  577. /// Gets a string representation of the Application as rendered by <see cref="Driver"/>.
  578. /// </summary>
  579. /// <returns>A string representation of the Application </returns>
  580. public string ToString ();
  581. }