ConsoleDriver.cs 28 KB

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