IDriver.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. namespace Terminal.Gui.Drivers;
  2. /// <summary>Base interface for Terminal.Gui Driver implementations.</summary>
  3. /// <remarks>
  4. /// There are currently four implementations: UnixDriver, WindowsDriver, DotNetDriver, and FakeDriver
  5. /// </remarks>
  6. public interface IDriver
  7. {
  8. /// <summary>
  9. /// Gets the name of the driver implementation.
  10. /// </summary>
  11. string? GetName ();
  12. /// <summary>
  13. /// Class responsible for processing native driver input objects
  14. /// e.g. <see cref="ConsoleKeyInfo"/> into <see cref="Key"/> events
  15. /// and detecting and processing ansi escape sequences.
  16. /// </summary>
  17. IInputProcessor InputProcessor { get; }
  18. /// <summary>
  19. /// Describes the desired screen state. Data source for <see cref="IOutput"/>.
  20. /// </summary>
  21. IOutputBuffer OutputBuffer { get; }
  22. /// <summary>
  23. /// Interface for classes responsible for reporting the current
  24. /// size of the terminal window.
  25. /// </summary>
  26. ISizeMonitor SizeMonitor { get; }
  27. /// <summary>Get the operating system clipboard.</summary>
  28. IClipboard? Clipboard { get; }
  29. /// <summary>Gets the location and size of the terminal screen.</summary>
  30. Rectangle Screen { get; }
  31. /// <summary>
  32. /// Sets the screen size. <see cref="Screen"/> is the source of truth for screen dimensions.
  33. /// </summary>
  34. /// <param name="width">The new width in columns.</param>
  35. /// <param name="height">The new height in rows.</param>
  36. void SetScreenSize (int width, int height);
  37. /// <summary>
  38. /// Gets or sets the clip rectangle that <see cref="AddRune(Rune)"/> and <see cref="AddStr(string)"/> are subject
  39. /// to.
  40. /// </summary>
  41. /// <value>The rectangle describing the of <see cref="Clip"/> region.</value>
  42. Region? Clip { get; set; }
  43. /// <summary>
  44. /// Gets the column last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  45. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  46. /// </summary>
  47. int Col { get; }
  48. /// <summary>The number of columns visible in the terminal.</summary>
  49. int Cols { get; set; }
  50. // BUGBUG: This should not be publicly settable.
  51. /// <summary>
  52. /// Gets or sets the contents of the application output. The driver outputs this buffer to the terminal.
  53. /// <remarks>The format of the array is rows, columns. The first index is the row, the second index is the column.</remarks>
  54. /// </summary>
  55. Cell [,]? Contents { get; set; }
  56. /// <summary>The leftmost column in the terminal.</summary>
  57. int Left { get; set; }
  58. /// <summary>
  59. /// Gets the row last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  60. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  61. /// </summary>
  62. int Row { get; }
  63. /// <summary>The number of rows visible in the terminal.</summary>
  64. int Rows { get; set; }
  65. /// <summary>The topmost row in the terminal.</summary>
  66. int Top { get; set; }
  67. /// <summary>Gets whether the <see cref="IDriver"/> supports TrueColor output.</summary>
  68. bool SupportsTrueColor { get; }
  69. /// <summary>
  70. /// Gets or sets whether the <see cref="IDriver"/> should use 16 colors instead of the default TrueColors.
  71. /// See <see cref="Application.Force16Colors"/> to change this setting via <see cref="ConfigurationManager"/>.
  72. /// </summary>
  73. /// <remarks>
  74. /// <para>
  75. /// Will be forced to <see langword="true"/> if <see cref="IDriver.SupportsTrueColor"/> is
  76. /// <see langword="false"/>, indicating that the <see cref="IDriver"/> cannot support TrueColor.
  77. /// </para>
  78. /// </remarks>
  79. bool Force16Colors { get; set; }
  80. /// <summary>
  81. /// The <see cref="System.Attribute"/> that will be used for the next <see cref="AddRune(Rune)"/> or
  82. /// <see cref="AddStr"/>
  83. /// call.
  84. /// </summary>
  85. Attribute CurrentAttribute { get; set; }
  86. /// <summary>Returns the name of the driver and relevant library version information.</summary>
  87. /// <returns></returns>
  88. string GetVersionInfo ();
  89. /// <summary>
  90. /// Provide proper writing to send escape sequence recognized by the <see cref="IDriver"/>.
  91. /// </summary>
  92. /// <param name="ansi"></param>
  93. void WriteRaw (string ansi);
  94. /// <summary>Tests if the specified rune is supported by the driver.</summary>
  95. /// <param name="rune"></param>
  96. /// <returns>
  97. /// <see langword="true"/> if the rune can be properly presented; <see langword="false"/> if the driver does not
  98. /// support displaying this rune.
  99. /// </returns>
  100. bool IsRuneSupported (Rune rune);
  101. /// <summary>Tests whether the specified coordinate are valid for drawing the specified Text.</summary>
  102. /// <param name="text">Used to determine if one or two columns are required.</param>
  103. /// <param name="col">The column.</param>
  104. /// <param name="row">The row.</param>
  105. /// <returns>
  106. /// <see langword="false"/> if the coordinate is outside the screen bounds or outside of
  107. /// <see cref="IDriver.Clip"/>.
  108. /// <see langword="true"/> otherwise.
  109. /// </returns>
  110. bool IsValidLocation (string text, int col, int row);
  111. /// <summary>
  112. /// Updates <see cref="IDriver.Col"/> and <see cref="IDriver.Row"/> to the specified column and row in
  113. /// <see cref="IDriver.Contents"/>.
  114. /// Used by <see cref="IDriver.AddRune(System.Text.Rune)"/> and <see cref="IDriver.AddStr"/> to determine
  115. /// where to add content.
  116. /// </summary>
  117. /// <remarks>
  118. /// <para>This does not move the cursor on the screen, it only updates the internal state of the driver.</para>
  119. /// <para>
  120. /// If <paramref name="col"/> or <paramref name="row"/> are negative or beyond <see cref="IDriver.Cols"/>
  121. /// and
  122. /// <see cref="IDriver.Rows"/>, the method still sets those properties.
  123. /// </para>
  124. /// </remarks>
  125. /// <param name="col">Column to move to.</param>
  126. /// <param name="row">Row to move to.</param>
  127. void Move (int col, int row);
  128. /// <summary>Adds the specified rune to the display at the current cursor position.</summary>
  129. /// <remarks>
  130. /// <para>
  131. /// When the method returns, <see cref="IDriver.Col"/> will be incremented by the number of columns
  132. /// <paramref name="rune"/> required, even if the new column value is outside of the
  133. /// <see cref="IDriver.Clip"/> or screen
  134. /// dimensions defined by <see cref="IDriver.Cols"/>.
  135. /// </para>
  136. /// <para>
  137. /// If <paramref name="rune"/> requires more than one column, and <see cref="IDriver.Col"/> plus the number
  138. /// of columns
  139. /// needed exceeds the <see cref="IDriver.Clip"/> or screen dimensions, the default Unicode replacement
  140. /// character (U+FFFD)
  141. /// will be added instead.
  142. /// </para>
  143. /// </remarks>
  144. /// <param name="rune">Rune to add.</param>
  145. void AddRune (Rune rune);
  146. /// <summary>
  147. /// Adds the specified <see langword="char"/> to the display at the current cursor position. This method is a
  148. /// convenience method that calls <see cref="IDriver.AddRune(System.Text.Rune)"/> with the <see cref="Rune"/>
  149. /// constructor.
  150. /// </summary>
  151. /// <param name="c">Character to add.</param>
  152. void AddRune (char c);
  153. /// <summary>Adds the <paramref name="str"/> to the display at the cursor position.</summary>
  154. /// <remarks>
  155. /// <para>
  156. /// When the method returns, <see cref="IDriver.Col"/> will be incremented by the number of columns
  157. /// <paramref name="str"/> required, unless the new column value is outside of the <see cref="IDriver.Clip"/>
  158. /// or screen
  159. /// dimensions defined by <see cref="IDriver.Cols"/>.
  160. /// </para>
  161. /// <para>If <paramref name="str"/> requires more columns than are available, the output will be clipped.</para>
  162. /// </remarks>
  163. /// <param name="str">String.</param>
  164. void AddStr (string str);
  165. /// <summary>Clears the <see cref="IDriver.Contents"/> of the driver.</summary>
  166. void ClearContents ();
  167. /// <summary>
  168. /// Fills the specified rectangle with the specified rune, using <see cref="IDriver.CurrentAttribute"/>
  169. /// </summary>
  170. event EventHandler<EventArgs> ClearedContents;
  171. /// <summary>Fills the specified rectangle with the specified rune, using <see cref="IDriver.CurrentAttribute"/></summary>
  172. /// <remarks>
  173. /// The value of <see cref="IDriver.Clip"/> is honored. Any parts of the rectangle not in the clip will not be
  174. /// drawn.
  175. /// </remarks>
  176. /// <param name="rect">The Screen-relative rectangle.</param>
  177. /// <param name="rune">The Rune used to fill the rectangle</param>
  178. void FillRect (Rectangle rect, Rune rune = default);
  179. /// <summary>
  180. /// Fills the specified rectangle with the specified <see langword="char"/>. This method is a convenience method
  181. /// that calls <see cref="IDriver.FillRect(System.Drawing.Rectangle,System.Text.Rune)"/>.
  182. /// </summary>
  183. /// <param name="rect"></param>
  184. /// <param name="c"></param>
  185. void FillRect (Rectangle rect, char c);
  186. /// <summary>Gets the terminal cursor visibility.</summary>
  187. /// <param name="visibility">The current <see cref="CursorVisibility"/></param>
  188. /// <returns><see langword="true"/> upon success</returns>
  189. bool GetCursorVisibility (out CursorVisibility visibility);
  190. /// <summary>
  191. /// INTERNAL: Updates the terminal with the current output buffer. Should not be used by applications. Drawing occurs
  192. /// once each Application main loop iteration.
  193. /// </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. /// <summary>
  250. /// Gets a string representation of <see cref="Contents"/>.
  251. /// </summary>
  252. /// <returns></returns>
  253. public string ToString ();
  254. /// <summary>
  255. /// Gets an ANSI escape sequence representation of <see cref="Contents"/>. This is the
  256. /// same output as would be written to the terminal to recreate the current screen contents.
  257. /// </summary>
  258. /// <returns></returns>
  259. public string ToAnsi ();
  260. }