IApplication.cs 28 KB

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