IDriver.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 whether the <see cref="IDriver"/> should use 16 colors instead of the default TrueColors.
  71. /// This value is set at driver construction time from the static <c>Terminal.Gui.Drivers.Driver.Force16Colors</c> property.
  72. /// Use <see cref="GetForce16Colors"/> to retrieve this value.
  73. /// </summary>
  74. /// <remarks>
  75. /// <para>
  76. /// Will be forced to <see langword="true"/> if <see cref="IDriver.SupportsTrueColor"/> is
  77. /// <see langword="false"/>, indicating that the <see cref="IDriver"/> cannot support TrueColor.
  78. /// </para>
  79. /// </remarks>
  80. bool Force16Colors { get; set; }
  81. /// <summary>
  82. /// Gets whether the <see cref="IDriver"/> is using 16 colors instead of TrueColors.
  83. /// </summary>
  84. /// <returns>
  85. /// <see langword="true"/> if the driver is using 16 colors; otherwise, <see langword="false"/>.
  86. /// </returns>
  87. bool GetForce16Colors ();
  88. /// <summary>
  89. /// The <see cref="System.Attribute"/> that will be used for the next <see cref="AddRune(Rune)"/> or
  90. /// <see cref="AddStr"/>
  91. /// call.
  92. /// </summary>
  93. Attribute CurrentAttribute { get; set; }
  94. /// <summary>Returns the name of the driver and relevant library version information.</summary>
  95. /// <returns></returns>
  96. string GetVersionInfo ();
  97. /// <summary>
  98. /// Provide proper writing to send escape sequence recognized by the <see cref="IDriver"/>.
  99. /// </summary>
  100. /// <param name="ansi"></param>
  101. void WriteRaw (string ansi);
  102. /// <summary>Tests if the specified rune is supported by the driver.</summary>
  103. /// <param name="rune"></param>
  104. /// <returns>
  105. /// <see langword="true"/> if the rune can be properly presented; <see langword="false"/> if the driver does not
  106. /// support displaying this rune.
  107. /// </returns>
  108. bool IsRuneSupported (Rune rune);
  109. /// <summary>Tests whether the specified coordinate are valid for drawing the specified Text.</summary>
  110. /// <param name="text">Used to determine if one or two columns are required.</param>
  111. /// <param name="col">The column.</param>
  112. /// <param name="row">The row.</param>
  113. /// <returns>
  114. /// <see langword="false"/> if the coordinate is outside the screen bounds or outside of
  115. /// <see cref="IDriver.Clip"/>.
  116. /// <see langword="true"/> otherwise.
  117. /// </returns>
  118. bool IsValidLocation (string text, int col, int row);
  119. /// <summary>
  120. /// Updates <see cref="IDriver.Col"/> and <see cref="IDriver.Row"/> to the specified column and row in
  121. /// <see cref="IDriver.Contents"/>.
  122. /// Used by <see cref="IDriver.AddRune(System.Text.Rune)"/> and <see cref="IDriver.AddStr"/> to determine
  123. /// where to add content.
  124. /// </summary>
  125. /// <remarks>
  126. /// <para>This does not move the cursor on the screen, it only updates the internal state of the driver.</para>
  127. /// <para>
  128. /// If <paramref name="col"/> or <paramref name="row"/> are negative or beyond <see cref="IDriver.Cols"/>
  129. /// and
  130. /// <see cref="IDriver.Rows"/>, the method still sets those properties.
  131. /// </para>
  132. /// </remarks>
  133. /// <param name="col">Column to move to.</param>
  134. /// <param name="row">Row to move to.</param>
  135. void Move (int col, int row);
  136. /// <summary>Adds the specified rune to the display at the current cursor position.</summary>
  137. /// <remarks>
  138. /// <para>
  139. /// When the method returns, <see cref="IDriver.Col"/> will be incremented by the number of columns
  140. /// <paramref name="rune"/> required, even if the new column value is outside of the
  141. /// <see cref="IDriver.Clip"/> or screen
  142. /// dimensions defined by <see cref="IDriver.Cols"/>.
  143. /// </para>
  144. /// <para>
  145. /// If <paramref name="rune"/> requires more than one column, and <see cref="IDriver.Col"/> plus the number
  146. /// of columns
  147. /// needed exceeds the <see cref="IDriver.Clip"/> or screen dimensions, the default Unicode replacement
  148. /// character (U+FFFD)
  149. /// will be added instead.
  150. /// </para>
  151. /// </remarks>
  152. /// <param name="rune">Rune to add.</param>
  153. void AddRune (Rune rune);
  154. /// <summary>
  155. /// Adds the specified <see langword="char"/> to the display at the current cursor position. This method is a
  156. /// convenience method that calls <see cref="IDriver.AddRune(System.Text.Rune)"/> with the <see cref="Rune"/>
  157. /// constructor.
  158. /// </summary>
  159. /// <param name="c">Character to add.</param>
  160. void AddRune (char c);
  161. /// <summary>Adds the <paramref name="str"/> to the display at the cursor position.</summary>
  162. /// <remarks>
  163. /// <para>
  164. /// When the method returns, <see cref="IDriver.Col"/> will be incremented by the number of columns
  165. /// <paramref name="str"/> required, unless the new column value is outside of the <see cref="IDriver.Clip"/>
  166. /// or screen
  167. /// dimensions defined by <see cref="IDriver.Cols"/>.
  168. /// </para>
  169. /// <para>If <paramref name="str"/> requires more columns than are available, the output will be clipped.</para>
  170. /// </remarks>
  171. /// <param name="str">String.</param>
  172. void AddStr (string str);
  173. /// <summary>Clears the <see cref="IDriver.Contents"/> of the driver.</summary>
  174. void ClearContents ();
  175. /// <summary>
  176. /// Fills the specified rectangle with the specified rune, using <see cref="IDriver.CurrentAttribute"/>
  177. /// </summary>
  178. event EventHandler<EventArgs> ClearedContents;
  179. /// <summary>Fills the specified rectangle with the specified rune, using <see cref="IDriver.CurrentAttribute"/></summary>
  180. /// <remarks>
  181. /// The value of <see cref="IDriver.Clip"/> is honored. Any parts of the rectangle not in the clip will not be
  182. /// drawn.
  183. /// </remarks>
  184. /// <param name="rect">The Screen-relative rectangle.</param>
  185. /// <param name="rune">The Rune used to fill the rectangle</param>
  186. void FillRect (Rectangle rect, Rune rune = default);
  187. /// <summary>
  188. /// Fills the specified rectangle with the specified <see langword="char"/>. This method is a convenience method
  189. /// that calls <see cref="IDriver.FillRect(System.Drawing.Rectangle,System.Text.Rune)"/>.
  190. /// </summary>
  191. /// <param name="rect"></param>
  192. /// <param name="c"></param>
  193. void FillRect (Rectangle rect, char c);
  194. /// <summary>Gets the terminal cursor visibility.</summary>
  195. /// <param name="visibility">The current <see cref="CursorVisibility"/></param>
  196. /// <returns><see langword="true"/> upon success</returns>
  197. bool GetCursorVisibility (out CursorVisibility visibility);
  198. /// <summary>
  199. /// INTERNAL: Updates the terminal with the current output buffer. Should not be used by applications. Drawing occurs
  200. /// once each Application main loop iteration.
  201. /// </summary>
  202. void Refresh ();
  203. /// <summary>Sets the terminal cursor visibility.</summary>
  204. /// <param name="visibility">The wished <see cref="CursorVisibility"/></param>
  205. /// <returns><see langword="true"/> upon success</returns>
  206. bool SetCursorVisibility (CursorVisibility visibility);
  207. /// <summary>
  208. /// The event fired when the screen changes (size, position, etc.).
  209. /// <see cref="Screen"/> is the source of truth for screen dimensions.
  210. /// </summary>
  211. event EventHandler<SizeChangedEventArgs>? SizeChanged;
  212. /// <summary>Suspends the application (e.g. on Linux via SIGTSTP) and upon resume, resets the console driver.</summary>
  213. /// <remarks>This is only implemented in UnixDriver.</remarks>
  214. void Suspend ();
  215. /// <summary>
  216. /// Sets the position of the terminal cursor to <see cref="IDriver.Col"/> and
  217. /// <see cref="IDriver.Row"/>.
  218. /// </summary>
  219. void UpdateCursor ();
  220. /// <summary>Initializes the driver</summary>
  221. void Init ();
  222. /// <summary>Ends the execution of the console driver.</summary>
  223. void End ();
  224. /// <summary>Selects the specified attribute as the attribute to use for future calls to AddRune and AddString.</summary>
  225. /// <remarks>Implementations should call <c>base.SetAttribute(c)</c>.</remarks>
  226. /// <param name="c">C.</param>
  227. Attribute SetAttribute (Attribute c);
  228. /// <summary>Gets the current <see cref="Attribute"/>.</summary>
  229. /// <returns>The current attribute.</returns>
  230. Attribute GetAttribute ();
  231. /// <summary>Event fired when a mouse event occurs.</summary>
  232. event EventHandler<MouseEventArgs>? MouseEvent;
  233. /// <summary>Event fired when a key is pressed down. This is a precursor to <see cref="IDriver.KeyUp"/>.</summary>
  234. event EventHandler<Key>? KeyDown;
  235. /// <summary>Event fired when a key is released.</summary>
  236. /// <remarks>
  237. /// Drivers that do not support key release events will fire this event after <see cref="IDriver.KeyDown"/>
  238. /// processing is
  239. /// complete.
  240. /// </remarks>
  241. event EventHandler<Key>? KeyUp;
  242. /// <summary>
  243. /// Enqueues a key input event to the driver. For unit tests.
  244. /// </summary>
  245. /// <param name="key"></param>
  246. void EnqueueKeyEvent (Key key);
  247. /// <summary>
  248. /// Queues the given <paramref name="request"/> for execution
  249. /// </summary>
  250. /// <param name="request"></param>
  251. public void QueueAnsiRequest (AnsiEscapeSequenceRequest request);
  252. /// <summary>
  253. /// Gets the <see cref="AnsiRequestScheduler"/> for the driver
  254. /// </summary>
  255. /// <returns></returns>
  256. public AnsiRequestScheduler GetRequestScheduler ();
  257. /// <summary>
  258. /// Gets a string representation of <see cref="Contents"/>.
  259. /// </summary>
  260. /// <returns></returns>
  261. public string ToString ();
  262. /// <summary>
  263. /// Gets an ANSI escape sequence representation of <see cref="Contents"/>. This is the
  264. /// same output as would be written to the terminal to recreate the current screen contents.
  265. /// </summary>
  266. /// <returns></returns>
  267. public string ToAnsi ();
  268. }