IDriver.cs 13 KB

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