IDriver.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #nullable enable
  2. namespace Terminal.Gui.Drivers;
  3. /// <summary>Base interface for Terminal.Gui Driver implementations.</summary>
  4. /// <remarks>
  5. /// There are currently four implementations: UnixDriver, WindowsDriver, DotNetDriver, and FakeDriver
  6. /// </remarks>
  7. public interface IDriver
  8. {
  9. /// <summary>
  10. /// Gets the name of the driver implementation.
  11. /// </summary>
  12. string? GetName ();
  13. /// <summary>
  14. /// Class responsible for processing native driver input objects
  15. /// e.g. <see cref="ConsoleKeyInfo"/> into <see cref="Key"/> events
  16. /// and detecting and processing ansi escape sequences.
  17. /// </summary>
  18. IInputProcessor InputProcessor { get; }
  19. /// <summary>
  20. /// Describes the desired screen state. Data source for <see cref="IOutput"/>.
  21. /// </summary>
  22. IOutputBuffer OutputBuffer { get; }
  23. /// <summary>
  24. /// Interface for classes responsible for reporting the current
  25. /// size of the terminal window.
  26. /// </summary>
  27. ISizeMonitor SizeMonitor { get; }
  28. /// <summary>Get the operating system clipboard.</summary>
  29. ///
  30. IClipboard? Clipboard { get; }
  31. /// <summary>Gets the location and size of the terminal screen.</summary>
  32. Rectangle Screen { get; }
  33. /// <summary>
  34. /// Sets the screen size for testing purposes. Only supported by FakeDriver.
  35. /// <see cref="Screen"/> is the source of truth for screen dimensions.
  36. /// </summary>
  37. /// <param name="width">The new width in columns.</param>
  38. /// <param name="height">The new height in rows.</param>
  39. /// <exception cref="NotSupportedException">Thrown when called on non-FakeDriver instances.</exception>
  40. void SetScreenSize (int width, int height);
  41. /// <summary>
  42. /// Gets or sets the clip rectangle that <see cref="AddRune(Rune)"/> and <see cref="AddStr(string)"/> are subject
  43. /// to.
  44. /// </summary>
  45. /// <value>The rectangle describing the of <see cref="Clip"/> region.</value>
  46. Region? Clip { get; set; }
  47. /// <summary>
  48. /// Gets the column last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  49. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  50. /// </summary>
  51. int Col { get; }
  52. /// <summary>The number of columns visible in the terminal.</summary>
  53. int Cols { get; set; }
  54. // BUGBUG: This should not be publicly settable.
  55. /// <summary>
  56. /// Gets or sets the contents of the application output. The driver outputs this buffer to the terminal.
  57. /// <remarks>The format of the array is rows, columns. The first index is the row, the second index is the column.</remarks>
  58. /// </summary>
  59. Cell [,]? Contents { get; set; }
  60. /// <summary>The leftmost column in the terminal.</summary>
  61. int Left { get; set; }
  62. /// <summary>
  63. /// Gets the row last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  64. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  65. /// </summary>
  66. int Row { get; }
  67. /// <summary>The number of rows visible in the terminal.</summary>
  68. int Rows { get; set; }
  69. /// <summary>The topmost row in the terminal.</summary>
  70. int Top { get; set; }
  71. /// <summary>Gets whether the <see cref="IDriver"/> supports TrueColor output.</summary>
  72. bool SupportsTrueColor { get; }
  73. /// <summary>
  74. /// Gets or sets whether the <see cref="IDriver"/> should use 16 colors instead of the default TrueColors.
  75. /// See <see cref="Application.Force16Colors"/> to change this setting via <see cref="ConfigurationManager"/>.
  76. /// </summary>
  77. /// <remarks>
  78. /// <para>
  79. /// Will be forced to <see langword="true"/> if <see cref="IDriver.SupportsTrueColor"/> is
  80. /// <see langword="false"/>, indicating that the <see cref="IDriver"/> cannot support TrueColor.
  81. /// </para>
  82. /// </remarks>
  83. bool Force16Colors { get; set; }
  84. /// <summary>
  85. /// The <see cref="System.Attribute"/> that will be used for the next <see cref="AddRune(Rune)"/> or <see cref="AddStr"/>
  86. /// call.
  87. /// </summary>
  88. Attribute CurrentAttribute { get; set; }
  89. /// <summary>Returns the name of the driver and relevant library version information.</summary>
  90. /// <returns></returns>
  91. string GetVersionInfo ();
  92. /// <summary>
  93. /// Provide proper writing to send escape sequence recognized by the <see cref="IDriver"/>.
  94. /// </summary>
  95. /// <param name="ansi"></param>
  96. void WriteRaw (string ansi);
  97. /// <summary>Tests if the specified rune is supported by the driver.</summary>
  98. /// <param name="rune"></param>
  99. /// <returns>
  100. /// <see langword="true"/> if the rune can be properly presented; <see langword="false"/> if the driver does not
  101. /// support displaying this rune.
  102. /// </returns>
  103. bool IsRuneSupported (Rune rune);
  104. /// <summary>Tests whether the specified coordinate are valid for drawing the specified Rune.</summary>
  105. /// <param name="rune">Used to determine if one or two columns are required.</param>
  106. /// <param name="col">The column.</param>
  107. /// <param name="row">The row.</param>
  108. /// <returns>
  109. /// <see langword="false"/> if the coordinate is outside the screen bounds or outside of
  110. /// <see cref="IDriver.Clip"/>.
  111. /// <see langword="true"/> otherwise.
  112. /// </returns>
  113. bool IsValidLocation (Rune rune, int col, int row);
  114. /// <summary>
  115. /// Updates <see cref="IDriver.Col"/> and <see cref="IDriver.Row"/> to the specified column and row in
  116. /// <see cref="IDriver.Contents"/>.
  117. /// Used by <see cref="IDriver.AddRune(System.Text.Rune)"/> and <see cref="IDriver.AddStr"/> to determine
  118. /// where to add content.
  119. /// </summary>
  120. /// <remarks>
  121. /// <para>This does not move the cursor on the screen, it only updates the internal state of the driver.</para>
  122. /// <para>
  123. /// If <paramref name="col"/> or <paramref name="row"/> are negative or beyond <see cref="IDriver.Cols"/>
  124. /// and
  125. /// <see cref="IDriver.Rows"/>, the method still sets those properties.
  126. /// </para>
  127. /// </remarks>
  128. /// <param name="col">Column to move to.</param>
  129. /// <param name="row">Row to move to.</param>
  130. void Move (int col, int row);
  131. /// <summary>Adds the specified rune to the display at the current cursor position.</summary>
  132. /// <remarks>
  133. /// <para>
  134. /// When the method returns, <see cref="IDriver.Col"/> will be incremented by the number of columns
  135. /// <paramref name="rune"/> required, even if the new column value is outside of the
  136. /// <see cref="IDriver.Clip"/> or screen
  137. /// dimensions defined by <see cref="IDriver.Cols"/>.
  138. /// </para>
  139. /// <para>
  140. /// If <paramref name="rune"/> requires more than one column, and <see cref="IDriver.Col"/> plus the number
  141. /// of columns
  142. /// needed exceeds the <see cref="IDriver.Clip"/> or screen dimensions, the default Unicode replacement
  143. /// character (U+FFFD)
  144. /// will be added instead.
  145. /// </para>
  146. /// </remarks>
  147. /// <param name="rune">Rune to add.</param>
  148. void AddRune (Rune rune);
  149. /// <summary>
  150. /// Adds the specified <see langword="char"/> to the display at the current cursor position. This method is a
  151. /// convenience method that calls <see cref="IDriver.AddRune(System.Text.Rune)"/> with the <see cref="Rune"/>
  152. /// constructor.
  153. /// </summary>
  154. /// <param name="c">Character to add.</param>
  155. void AddRune (char c);
  156. /// <summary>Adds the <paramref name="str"/> to the display at the cursor position.</summary>
  157. /// <remarks>
  158. /// <para>
  159. /// When the method returns, <see cref="IDriver.Col"/> will be incremented by the number of columns
  160. /// <paramref name="str"/> required, unless the new column value is outside of the <see cref="IDriver.Clip"/>
  161. /// or screen
  162. /// dimensions defined by <see cref="IDriver.Cols"/>.
  163. /// </para>
  164. /// <para>If <paramref name="str"/> requires more columns than are available, the output will be clipped.</para>
  165. /// </remarks>
  166. /// <param name="str">String.</param>
  167. void AddStr (string str);
  168. /// <summary>Clears the <see cref="IDriver.Contents"/> of the driver.</summary>
  169. void ClearContents ();
  170. /// <summary>
  171. /// Fills the specified rectangle with the specified rune, using <see cref="IDriver.CurrentAttribute"/>
  172. /// </summary>
  173. event EventHandler<EventArgs> ClearedContents;
  174. /// <summary>Fills the specified rectangle with the specified rune, using <see cref="IDriver.CurrentAttribute"/></summary>
  175. /// <remarks>
  176. /// The value of <see cref="IDriver.Clip"/> is honored. Any parts of the rectangle not in the clip will not be
  177. /// drawn.
  178. /// </remarks>
  179. /// <param name="rect">The Screen-relative rectangle.</param>
  180. /// <param name="rune">The Rune used to fill the rectangle</param>
  181. void FillRect (Rectangle rect, Rune rune = default);
  182. /// <summary>
  183. /// Fills the specified rectangle with the specified <see langword="char"/>. This method is a convenience method
  184. /// that calls <see cref="IDriver.FillRect(System.Drawing.Rectangle,System.Text.Rune)"/>.
  185. /// </summary>
  186. /// <param name="rect"></param>
  187. /// <param name="c"></param>
  188. void FillRect (Rectangle rect, char c);
  189. /// <summary>Gets the terminal cursor visibility.</summary>
  190. /// <param name="visibility">The current <see cref="CursorVisibility"/></param>
  191. /// <returns><see langword="true"/> upon success</returns>
  192. bool GetCursorVisibility (out CursorVisibility visibility);
  193. /// <summary>Updates the screen to reflect all the changes that have been done to the display buffer</summary>
  194. void Refresh ();
  195. /// <summary>Sets the terminal cursor visibility.</summary>
  196. /// <param name="visibility">The wished <see cref="CursorVisibility"/></param>
  197. /// <returns><see langword="true"/> upon success</returns>
  198. bool SetCursorVisibility (CursorVisibility visibility);
  199. /// <summary>
  200. /// The event fired when the screen changes (size, position, etc.).
  201. /// <see cref="Screen"/> is the source of truth for screen dimensions.
  202. /// </summary>
  203. event EventHandler<SizeChangedEventArgs>? SizeChanged;
  204. /// <summary>Suspends the application (e.g. on Linux via SIGTSTP) and upon resume, resets the console driver.</summary>
  205. /// <remarks>This is only implemented in UnixDriver.</remarks>
  206. void Suspend ();
  207. /// <summary>
  208. /// Sets the position of the terminal cursor to <see cref="IDriver.Col"/> and
  209. /// <see cref="IDriver.Row"/>.
  210. /// </summary>
  211. void UpdateCursor ();
  212. /// <summary>Initializes the driver</summary>
  213. void Init ();
  214. /// <summary>Ends the execution of the console driver.</summary>
  215. void End ();
  216. /// <summary>Selects the specified attribute as the attribute to use for future calls to AddRune and AddString.</summary>
  217. /// <remarks>Implementations should call <c>base.SetAttribute(c)</c>.</remarks>
  218. /// <param name="c">C.</param>
  219. Attribute SetAttribute (Attribute c);
  220. /// <summary>Gets the current <see cref="Attribute"/>.</summary>
  221. /// <returns>The current attribute.</returns>
  222. Attribute GetAttribute ();
  223. /// <summary>Event fired when a mouse event occurs.</summary>
  224. event EventHandler<MouseEventArgs>? MouseEvent;
  225. /// <summary>Event fired when a key is pressed down. This is a precursor to <see cref="IDriver.KeyUp"/>.</summary>
  226. event EventHandler<Key>? KeyDown;
  227. /// <summary>Event fired when a key is released.</summary>
  228. /// <remarks>
  229. /// Drivers that do not support key release events will fire this event after <see cref="IDriver.KeyDown"/>
  230. /// processing is
  231. /// complete.
  232. /// </remarks>
  233. event EventHandler<Key>? KeyUp;
  234. /// <summary>
  235. /// Enqueues a key input event to the driver. For unit tests.
  236. /// </summary>
  237. /// <param name="key"></param>
  238. void EnqueueKeyEvent (Key key);
  239. /// <summary>
  240. /// Queues the given <paramref name="request"/> for execution
  241. /// </summary>
  242. /// <param name="request"></param>
  243. public void QueueAnsiRequest (AnsiEscapeSequenceRequest request);
  244. /// <summary>
  245. /// Gets the <see cref="AnsiRequestScheduler"/> for the driver
  246. /// </summary>
  247. /// <returns></returns>
  248. public AnsiRequestScheduler GetRequestScheduler ();
  249. }