ConsoleDriver.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. #nullable enable
  2. using System.Diagnostics;
  3. namespace Terminal.Gui;
  4. /// <summary>Base class for Terminal.Gui ConsoleDriver implementations.</summary>
  5. /// <remarks>
  6. /// There are currently four implementations: - <see cref="CursesDriver"/> (for Unix and Mac) -
  7. /// <see cref="WindowsDriver"/> - <see cref="NetDriver"/> that uses the .NET Console API - <see cref="FakeConsole"/>
  8. /// for unit testing.
  9. /// </remarks>
  10. public abstract class ConsoleDriver
  11. {
  12. /// <summary>
  13. /// Set this to true in any unit tests that attempt to test drivers other than FakeDriver.
  14. /// <code>
  15. /// public ColorTests ()
  16. /// {
  17. /// ConsoleDriver.RunningUnitTests = true;
  18. /// }
  19. /// </code>
  20. /// </summary>
  21. internal static bool RunningUnitTests { get; set; }
  22. /// <summary>Get the operating system clipboard.</summary>
  23. public IClipboard? Clipboard { get; internal set; }
  24. /// <summary>Returns the name of the driver and relevant library version information.</summary>
  25. /// <returns></returns>
  26. public virtual string GetVersionInfo () { return GetType ().Name; }
  27. /// <summary>Suspends the application (e.g. on Linux via SIGTSTP) and upon resume, resets the console driver.</summary>
  28. /// <remarks>This is only implemented in <see cref="CursesDriver"/>.</remarks>
  29. public abstract void Suspend ();
  30. #region ANSI Esc Sequence Handling
  31. // QUESTION: Should this be virtual with a default implementation that does the common stuff?
  32. // QUESTION: Looking at the implementations of this method, there is TONs of duplicated code.
  33. // QUESTION: We should figure out how to find just the things that are unique to each driver and
  34. // QUESTION: create more fine-grained APIs to handle those.
  35. /// <summary>
  36. /// Provide handling for the terminal write ANSI escape sequence request.
  37. /// </summary>
  38. /// <param name="ansiRequest">The <see cref="AnsiEscapeSequenceRequest"/> object.</param>
  39. /// <returns>The request response.</returns>
  40. public abstract bool TryWriteAnsiRequest (AnsiEscapeSequenceRequest ansiRequest);
  41. // QUESTION: This appears to be an API to help in debugging. It's only implemented in CursesDriver and WindowsDriver.
  42. // QUESTION: Can it be factored such that it does not contaminate the ConsoleDriver API?
  43. /// <summary>
  44. /// Provide proper writing to send escape sequence recognized by the <see cref="ConsoleDriver"/>.
  45. /// </summary>
  46. /// <param name="ansi"></param>
  47. internal abstract void WriteRaw (string ansi);
  48. #endregion ANSI Esc Sequence Handling
  49. #region Screen and Contents
  50. // As performance is a concern, we keep track of the dirty lines and only refresh those.
  51. // This is in addition to the dirty flag on each cell.
  52. internal bool []? _dirtyLines;
  53. // QUESTION: When non-full screen apps are supported, will this represent the app size, or will that be in Application?
  54. /// <summary>Gets the location and size of the terminal screen.</summary>
  55. internal Rectangle Screen => new (0, 0, Cols, Rows);
  56. /// <summary>Redraws the physical screen with the contents that have been queued up via any of the printing commands.</summary>
  57. public abstract void UpdateScreen ();
  58. /// <summary>Called when the terminal size changes. Fires the <see cref="SizeChanged"/> event.</summary>
  59. /// <param name="args"></param>
  60. public void OnSizeChanged (SizeChangedEventArgs args) { SizeChanged?.Invoke (this, args); }
  61. /// <summary>The event fired when the terminal is resized.</summary>
  62. public event EventHandler<SizeChangedEventArgs>? SizeChanged;
  63. /// <summary>Updates the screen to reflect all the changes that have been done to the display buffer</summary>
  64. public abstract void Refresh ();
  65. /// <summary>
  66. /// Gets the column last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  67. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  68. /// </summary>
  69. public int Col { get; internal set; }
  70. /// <summary>The number of columns visible in the terminal.</summary>
  71. public virtual int Cols
  72. {
  73. get => _cols;
  74. internal set
  75. {
  76. _cols = value;
  77. ClearContents ();
  78. }
  79. }
  80. /// <summary>
  81. /// The contents of the application output. The driver outputs this buffer to the terminal when
  82. /// <see cref="UpdateScreen"/> is called.
  83. /// <remarks>The format of the array is rows, columns. The first index is the row, the second index is the column.</remarks>
  84. /// </summary>
  85. public Cell [,]? Contents { get; internal set; }
  86. /// <summary>The leftmost column in the terminal.</summary>
  87. public virtual int Left { get; internal set; } = 0;
  88. /// <summary>Tests if the specified rune is supported by the driver.</summary>
  89. /// <param name="rune"></param>
  90. /// <returns>
  91. /// <see langword="true"/> if the rune can be properly presented; <see langword="false"/> if the driver does not
  92. /// support displaying this rune.
  93. /// </returns>
  94. public virtual bool IsRuneSupported (Rune rune) { return Rune.IsValid (rune.Value); }
  95. /// <summary>Tests whether the specified coordinate are valid for drawing.</summary>
  96. /// <param name="col">The column.</param>
  97. /// <param name="row">The row.</param>
  98. /// <returns>
  99. /// <see langword="false"/> if the coordinate is outside the screen bounds or outside of <see cref="Clip"/>.
  100. /// <see langword="true"/> otherwise.
  101. /// </returns>
  102. public bool IsValidLocation (int col, int row) { return col >= 0 && row >= 0 && col < Cols && row < Rows && Clip.Contains (col, row); }
  103. /// <summary>
  104. /// Updates <see cref="Col"/> and <see cref="Row"/> to the specified column and row in <see cref="Contents"/>.
  105. /// Used by <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  106. /// </summary>
  107. /// <remarks>
  108. /// <para>This does not move the cursor on the screen, it only updates the internal state of the driver.</para>
  109. /// <para>
  110. /// If <paramref name="col"/> or <paramref name="row"/> are negative or beyond <see cref="Cols"/> and
  111. /// <see cref="Rows"/>, the method still sets those properties.
  112. /// </para>
  113. /// </remarks>
  114. /// <param name="col">Column to move to.</param>
  115. /// <param name="row">Row to move to.</param>
  116. public virtual void Move (int col, int row)
  117. {
  118. //Debug.Assert (col >= 0 && row >= 0 && col < Contents.GetLength(1) && row < Contents.GetLength(0));
  119. Col = col;
  120. Row = row;
  121. }
  122. /// <summary>
  123. /// Gets the row last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  124. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  125. /// </summary>
  126. public int Row { get; internal set; }
  127. /// <summary>The number of rows visible in the terminal.</summary>
  128. public virtual int Rows
  129. {
  130. get => _rows;
  131. internal set
  132. {
  133. _rows = value;
  134. ClearContents ();
  135. }
  136. }
  137. /// <summary>The topmost row in the terminal.</summary>
  138. public virtual int Top { get; internal set; } = 0;
  139. private Rectangle _clip;
  140. /// <summary>
  141. /// Gets or sets the clip rectangle that <see cref="AddRune(Rune)"/> and <see cref="AddStr(string)"/> are subject
  142. /// to.
  143. /// </summary>
  144. /// <value>The rectangle describing the of <see cref="Clip"/> region.</value>
  145. public Rectangle Clip
  146. {
  147. get => _clip;
  148. set
  149. {
  150. if (_clip == value)
  151. {
  152. return;
  153. }
  154. // Don't ever let Clip be bigger than Screen
  155. _clip = Rectangle.Intersect (Screen, value);
  156. }
  157. }
  158. /// <summary>Adds the specified rune to the display at the current cursor position.</summary>
  159. /// <remarks>
  160. /// <para>
  161. /// When the method returns, <see cref="Col"/> will be incremented by the number of columns
  162. /// <paramref name="rune"/> required, even if the new column value is outside of the <see cref="Clip"/> or screen
  163. /// dimensions defined by <see cref="Cols"/>.
  164. /// </para>
  165. /// <para>
  166. /// If <paramref name="rune"/> requires more than one column, and <see cref="Col"/> plus the number of columns
  167. /// needed exceeds the <see cref="Clip"/> or screen dimensions, the default Unicode replacement character (U+FFFD)
  168. /// will be added instead.
  169. /// </para>
  170. /// </remarks>
  171. /// <param name="rune">Rune to add.</param>
  172. public void AddRune (Rune rune)
  173. {
  174. int runeWidth = -1;
  175. bool validLocation = IsValidLocation (Col, Row);
  176. if (Contents is null)
  177. {
  178. return;
  179. }
  180. if (validLocation)
  181. {
  182. rune = rune.MakePrintable ();
  183. runeWidth = rune.GetColumns ();
  184. lock (Contents)
  185. {
  186. if (runeWidth == 0 && rune.IsCombiningMark ())
  187. {
  188. // AtlasEngine does not support NON-NORMALIZED combining marks in a way
  189. // compatible with the driver architecture. Any CMs (except in the first col)
  190. // are correctly combined with the base char, but are ALSO treated as 1 column
  191. // width codepoints E.g. `echo "[e`u{0301}`u{0301}]"` will output `[é ]`.
  192. //
  193. // Until this is addressed (see Issue #), we do our best by
  194. // a) Attempting to normalize any CM with the base char to it's left
  195. // b) Ignoring any CMs that don't normalize
  196. if (Col > 0)
  197. {
  198. if (Contents [Row, Col - 1].CombiningMarks.Count > 0)
  199. {
  200. // Just add this mark to the list
  201. Contents [Row, Col - 1].CombiningMarks.Add (rune);
  202. // Ignore. Don't move to next column (let the driver figure out what to do).
  203. }
  204. else
  205. {
  206. // Attempt to normalize the cell to our left combined with this mark
  207. string combined = Contents [Row, Col - 1].Rune + rune.ToString ();
  208. // Normalize to Form C (Canonical Composition)
  209. string normalized = combined.Normalize (NormalizationForm.FormC);
  210. if (normalized.Length == 1)
  211. {
  212. // It normalized! We can just set the Cell to the left with the
  213. // normalized codepoint
  214. Contents [Row, Col - 1].Rune = (Rune)normalized [0];
  215. // Ignore. Don't move to next column because we're already there
  216. }
  217. else
  218. {
  219. // It didn't normalize. Add it to the Cell to left's CM list
  220. Contents [Row, Col - 1].CombiningMarks.Add (rune);
  221. // Ignore. Don't move to next column (let the driver figure out what to do).
  222. }
  223. }
  224. Contents [Row, Col - 1].Attribute = CurrentAttribute;
  225. Contents [Row, Col - 1].IsDirty = true;
  226. }
  227. else
  228. {
  229. // Most drivers will render a combining mark at col 0 as the mark
  230. Contents [Row, Col].Rune = rune;
  231. Contents [Row, Col].Attribute = CurrentAttribute;
  232. Contents [Row, Col].IsDirty = true;
  233. Col++;
  234. }
  235. }
  236. else
  237. {
  238. Contents [Row, Col].Attribute = CurrentAttribute;
  239. Contents [Row, Col].IsDirty = true;
  240. if (Col > 0)
  241. {
  242. // Check if cell to left has a wide glyph
  243. if (Contents [Row, Col - 1].Rune.GetColumns () > 1)
  244. {
  245. // Invalidate cell to left
  246. Contents [Row, Col - 1].Rune = Rune.ReplacementChar;
  247. Contents [Row, Col - 1].IsDirty = true;
  248. }
  249. }
  250. if (runeWidth < 1)
  251. {
  252. Contents [Row, Col].Rune = Rune.ReplacementChar;
  253. }
  254. else if (runeWidth == 1)
  255. {
  256. Contents [Row, Col].Rune = rune;
  257. if (Col < Clip.Right - 1)
  258. {
  259. Contents [Row, Col + 1].IsDirty = true;
  260. }
  261. }
  262. else if (runeWidth == 2)
  263. {
  264. if (Col == Clip.Right - 1)
  265. {
  266. // We're at the right edge of the clip, so we can't display a wide character.
  267. // TODO: Figure out if it is better to show a replacement character or ' '
  268. Contents [Row, Col].Rune = Rune.ReplacementChar;
  269. }
  270. else
  271. {
  272. Contents [Row, Col].Rune = rune;
  273. if (Col < Clip.Right - 1)
  274. {
  275. // Invalidate cell to right so that it doesn't get drawn
  276. // TODO: Figure out if it is better to show a replacement character or ' '
  277. Contents [Row, Col + 1].Rune = Rune.ReplacementChar;
  278. Contents [Row, Col + 1].IsDirty = true;
  279. }
  280. }
  281. }
  282. else
  283. {
  284. // This is a non-spacing character, so we don't need to do anything
  285. Contents [Row, Col].Rune = (Rune)' ';
  286. Contents [Row, Col].IsDirty = false;
  287. }
  288. _dirtyLines! [Row] = true;
  289. }
  290. }
  291. }
  292. if (runeWidth is < 0 or > 0)
  293. {
  294. Col++;
  295. }
  296. if (runeWidth > 1)
  297. {
  298. Debug.Assert (runeWidth <= 2);
  299. if (validLocation && Col < Clip.Right)
  300. {
  301. lock (Contents!)
  302. {
  303. // This is a double-width character, and we are not at the end of the line.
  304. // Col now points to the second column of the character. Ensure it doesn't
  305. // Get rendered.
  306. Contents [Row, Col].IsDirty = false;
  307. Contents [Row, Col].Attribute = CurrentAttribute;
  308. // TODO: Determine if we should wipe this out (for now now)
  309. //Contents [Row, Col].Rune = (Rune)' ';
  310. }
  311. }
  312. Col++;
  313. }
  314. }
  315. /// <summary>
  316. /// Adds the specified <see langword="char"/> to the display at the current cursor position. This method is a
  317. /// convenience method that calls <see cref="AddRune(Rune)"/> with the <see cref="Rune"/> constructor.
  318. /// </summary>
  319. /// <param name="c">Character to add.</param>
  320. public void AddRune (char c) { AddRune (new Rune (c)); }
  321. /// <summary>Adds the <paramref name="str"/> to the display at the cursor position.</summary>
  322. /// <remarks>
  323. /// <para>
  324. /// When the method returns, <see cref="Col"/> will be incremented by the number of columns
  325. /// <paramref name="str"/> required, unless the new column value is outside of the <see cref="Clip"/> or screen
  326. /// dimensions defined by <see cref="Cols"/>.
  327. /// </para>
  328. /// <para>If <paramref name="str"/> requires more columns than are available, the output will be clipped.</para>
  329. /// </remarks>
  330. /// <param name="str">String.</param>
  331. public void AddStr (string str)
  332. {
  333. List<Rune> runes = str.EnumerateRunes ().ToList ();
  334. for (var i = 0; i < runes.Count; i++)
  335. {
  336. AddRune (runes [i]);
  337. }
  338. }
  339. /// <summary>Fills the specified rectangle with the specified rune, using <see cref="CurrentAttribute"/></summary>
  340. /// <remarks>
  341. /// The value of <see cref="Clip"/> is honored. Any parts of the rectangle not in the clip will not be drawn.
  342. /// </remarks>
  343. /// <param name="rect">The Screen-relative rectangle.</param>
  344. /// <param name="rune">The Rune used to fill the rectangle</param>
  345. public void FillRect (Rectangle rect, Rune rune = default)
  346. {
  347. rect = Rectangle.Intersect (rect, Clip);
  348. lock (Contents!)
  349. {
  350. for (int r = rect.Y; r < rect.Y + rect.Height; r++)
  351. {
  352. for (int c = rect.X; c < rect.X + rect.Width; c++)
  353. {
  354. Contents [r, c] = new ()
  355. {
  356. Rune = rune != default (Rune) ? rune : (Rune)' ',
  357. Attribute = CurrentAttribute, IsDirty = true
  358. };
  359. _dirtyLines! [r] = true;
  360. }
  361. }
  362. }
  363. }
  364. /// <summary>Clears the <see cref="Contents"/> of the driver.</summary>
  365. public void ClearContents ()
  366. {
  367. Contents = new Cell [Rows, Cols];
  368. //CONCURRENCY: Unsynchronized access to Clip isn't safe.
  369. // TODO: ClearContents should not clear the clip; it should only clear the contents. Move clearing it elsewhere.
  370. Clip = Screen;
  371. _dirtyLines = new bool [Rows];
  372. lock (Contents)
  373. {
  374. for (var row = 0; row < Rows; row++)
  375. {
  376. for (var c = 0; c < Cols; c++)
  377. {
  378. Contents [row, c] = new ()
  379. {
  380. Rune = (Rune)' ',
  381. Attribute = new Attribute (Color.White, Color.Black),
  382. IsDirty = true
  383. };
  384. }
  385. _dirtyLines [row] = true;
  386. }
  387. }
  388. }
  389. /// <summary>
  390. /// Sets <see cref="Contents"/> as dirty for situations where views
  391. /// don't need layout and redrawing, but just refresh the screen.
  392. /// </summary>
  393. public void SetContentsAsDirty ()
  394. {
  395. lock (Contents!)
  396. {
  397. for (var row = 0; row < Rows; row++)
  398. {
  399. for (var c = 0; c < Cols; c++)
  400. {
  401. Contents [row, c].IsDirty = true;
  402. }
  403. _dirtyLines! [row] = true;
  404. }
  405. }
  406. }
  407. /// <summary>
  408. /// Fills the specified rectangle with the specified <see langword="char"/>. This method is a convenience method
  409. /// that calls <see cref="FillRect(Rectangle, Rune)"/>.
  410. /// </summary>
  411. /// <param name="rect"></param>
  412. /// <param name="c"></param>
  413. public void FillRect (Rectangle rect, char c) { FillRect (rect, new Rune (c)); }
  414. #endregion Screen and Contents
  415. #region Cursor Handling
  416. /// <summary>Determines if the terminal cursor should be visible or not and sets it accordingly.</summary>
  417. /// <returns><see langword="true"/> upon success</returns>
  418. public abstract bool EnsureCursorVisibility ();
  419. /// <summary>Gets the terminal cursor visibility.</summary>
  420. /// <param name="visibility">The current <see cref="CursorVisibility"/></param>
  421. /// <returns><see langword="true"/> upon success</returns>
  422. public abstract bool GetCursorVisibility (out CursorVisibility visibility);
  423. /// <summary>Sets the position of the terminal cursor to <see cref="Col"/> and <see cref="Row"/>.</summary>
  424. public abstract void UpdateCursor ();
  425. /// <summary>Sets the terminal cursor visibility.</summary>
  426. /// <param name="visibility">The wished <see cref="CursorVisibility"/></param>
  427. /// <returns><see langword="true"/> upon success</returns>
  428. public abstract bool SetCursorVisibility (CursorVisibility visibility);
  429. #endregion Cursor Handling
  430. #region Setup & Teardown
  431. /// <summary>Initializes the driver</summary>
  432. /// <returns>Returns an instance of <see cref="MainLoop"/> using the <see cref="IMainLoopDriver"/> for the driver.</returns>
  433. internal abstract MainLoop Init ();
  434. /// <summary>Ends the execution of the console driver.</summary>
  435. internal abstract void End ();
  436. #endregion
  437. #region Color Handling
  438. /// <summary>Gets whether the <see cref="ConsoleDriver"/> supports TrueColor output.</summary>
  439. public virtual bool SupportsTrueColor => true;
  440. // TODO: This makes ConsoleDriver dependent on Application, which is not ideal. This should be moved to Application.
  441. // BUGBUG: Application.Force16Colors should be bool? so if SupportsTrueColor and Application.Force16Colors == false, this doesn't override
  442. /// <summary>
  443. /// Gets or sets whether the <see cref="ConsoleDriver"/> should use 16 colors instead of the default TrueColors.
  444. /// See <see cref="Application.Force16Colors"/> to change this setting via <see cref="ConfigurationManager"/>.
  445. /// </summary>
  446. /// <remarks>
  447. /// <para>
  448. /// Will be forced to <see langword="true"/> if <see cref="ConsoleDriver.SupportsTrueColor"/> is
  449. /// <see langword="false"/>, indicating that the <see cref="ConsoleDriver"/> cannot support TrueColor.
  450. /// </para>
  451. /// </remarks>
  452. internal virtual bool Force16Colors
  453. {
  454. get => Application.Force16Colors || !SupportsTrueColor;
  455. set => Application.Force16Colors = value || !SupportsTrueColor;
  456. }
  457. private Attribute _currentAttribute;
  458. private int _cols;
  459. private int _rows;
  460. /// <summary>
  461. /// The <see cref="Attribute"/> that will be used for the next <see cref="AddRune(Rune)"/> or <see cref="AddStr"/>
  462. /// call.
  463. /// </summary>
  464. public Attribute CurrentAttribute
  465. {
  466. get => _currentAttribute;
  467. set
  468. {
  469. // TODO: This makes ConsoleDriver dependent on Application, which is not ideal. Once Attribute.PlatformColor is removed, this can be fixed.
  470. if (Application.Driver is { })
  471. {
  472. _currentAttribute = new (value.Foreground, value.Background);
  473. return;
  474. }
  475. _currentAttribute = value;
  476. }
  477. }
  478. /// <summary>Selects the specified attribute as the attribute to use for future calls to AddRune and AddString.</summary>
  479. /// <remarks>Implementations should call <c>base.SetAttribute(c)</c>.</remarks>
  480. /// <param name="c">C.</param>
  481. public Attribute SetAttribute (Attribute c)
  482. {
  483. Attribute prevAttribute = CurrentAttribute;
  484. CurrentAttribute = c;
  485. return prevAttribute;
  486. }
  487. /// <summary>Gets the current <see cref="Attribute"/>.</summary>
  488. /// <returns>The current attribute.</returns>
  489. public Attribute GetAttribute () { return CurrentAttribute; }
  490. // TODO: This is only overridden by CursesDriver. Once CursesDriver supports 24-bit color, this virtual method can be
  491. // removed (and Attribute can lose the platformColor property).
  492. /// <summary>Makes an <see cref="Attribute"/>.</summary>
  493. /// <param name="foreground">The foreground color.</param>
  494. /// <param name="background">The background color.</param>
  495. /// <returns>The attribute for the foreground and background colors.</returns>
  496. public virtual Attribute MakeColor (in Color foreground, in Color background)
  497. {
  498. // Encode the colors into the int value.
  499. return new (
  500. -1, // only used by cursesdriver!
  501. foreground,
  502. background
  503. );
  504. }
  505. #endregion Color Handling
  506. #region Mouse Handling
  507. /// <summary>Event fired when a mouse event occurs.</summary>
  508. public event EventHandler<MouseEventArgs>? MouseEvent;
  509. /// <summary>Called when a mouse event occurs. Fires the <see cref="MouseEvent"/> event.</summary>
  510. /// <param name="a"></param>
  511. public void OnMouseEvent (MouseEventArgs a)
  512. {
  513. // Ensure ScreenPosition is set
  514. a.ScreenPosition = a.Position;
  515. MouseEvent?.Invoke (this, a);
  516. }
  517. #endregion Mouse Handling
  518. #region Keyboard Handling
  519. /// <summary>Event fired when a key is pressed down. This is a precursor to <see cref="KeyUp"/>.</summary>
  520. public event EventHandler<Key>? KeyDown;
  521. /// <summary>
  522. /// Called when a key is pressed down. Fires the <see cref="KeyDown"/> event. This is a precursor to
  523. /// <see cref="OnKeyUp"/>.
  524. /// </summary>
  525. /// <param name="a"></param>
  526. public void OnKeyDown (Key a) { KeyDown?.Invoke (this, a); }
  527. /// <summary>Event fired when a key is released.</summary>
  528. /// <remarks>
  529. /// Drivers that do not support key release events will fire this event after <see cref="KeyDown"/> processing is
  530. /// complete.
  531. /// </remarks>
  532. public event EventHandler<Key>? KeyUp;
  533. /// <summary>Called when a key is released. Fires the <see cref="KeyUp"/> event.</summary>
  534. /// <remarks>
  535. /// Drivers that do not support key release events will call this method after <see cref="OnKeyDown"/> processing
  536. /// is complete.
  537. /// </remarks>
  538. /// <param name="a"></param>
  539. public void OnKeyUp (Key a) { KeyUp?.Invoke (this, a); }
  540. // TODO: Remove this API - it was needed when we didn't have a reliable way to simulate key presses.
  541. // TODO: We now do: Applicaiton.RaiseKeyDown and Application.RaiseKeyUp
  542. /// <summary>Simulates a key press.</summary>
  543. /// <param name="keyChar">The key character.</param>
  544. /// <param name="key">The key.</param>
  545. /// <param name="shift">If <see langword="true"/> simulates the Shift key being pressed.</param>
  546. /// <param name="alt">If <see langword="true"/> simulates the Alt key being pressed.</param>
  547. /// <param name="ctrl">If <see langword="true"/> simulates the Ctrl key being pressed.</param>
  548. public abstract void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool ctrl);
  549. #endregion Keyboard Handling
  550. }