ConsoleDriverFacade.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. using System.Runtime.InteropServices;
  2. namespace Terminal.Gui;
  3. internal class ConsoleDriverFacade<T> : IConsoleDriver, IConsoleDriverFacade
  4. {
  5. private readonly IConsoleOutput _output;
  6. private readonly IOutputBuffer _outputBuffer;
  7. private readonly AnsiRequestScheduler _ansiRequestScheduler;
  8. private CursorVisibility _lastCursor = CursorVisibility.Default;
  9. /// <summary>The event fired when the terminal is resized.</summary>
  10. public event EventHandler<SizeChangedEventArgs> SizeChanged;
  11. public IInputProcessor InputProcessor { get; }
  12. public ConsoleDriverFacade (
  13. IInputProcessor inputProcessor,
  14. IOutputBuffer outputBuffer,
  15. IConsoleOutput output,
  16. AnsiRequestScheduler ansiRequestScheduler,
  17. IWindowSizeMonitor windowSizeMonitor
  18. )
  19. {
  20. InputProcessor = inputProcessor;
  21. _output = output;
  22. _outputBuffer = outputBuffer;
  23. _ansiRequestScheduler = ansiRequestScheduler;
  24. InputProcessor.KeyDown += (s, e) => KeyDown?.Invoke (s, e);
  25. InputProcessor.KeyUp += (s, e) => KeyUp?.Invoke (s, e);
  26. InputProcessor.MouseEvent += (s, e) => MouseEvent?.Invoke (s, e);
  27. windowSizeMonitor.SizeChanging += (_, e) => SizeChanged?.Invoke (this, e);
  28. CreateClipboard ();
  29. }
  30. private void CreateClipboard ()
  31. {
  32. PlatformID p = Environment.OSVersion.Platform;
  33. if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
  34. {
  35. Clipboard = new WindowsClipboard ();
  36. }
  37. else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX))
  38. {
  39. Clipboard = new MacOSXClipboard ();
  40. }
  41. else if (CursesDriver.Is_WSL_Platform ())
  42. {
  43. Clipboard = new WSLClipboard ();
  44. }
  45. else
  46. {
  47. Clipboard = new FakeDriver.FakeClipboard ();
  48. }
  49. }
  50. /// <summary>Gets the location and size of the terminal screen.</summary>
  51. public Rectangle Screen => new (new (0, 0), _output.GetWindowSize ());
  52. /// <summary>
  53. /// Gets or sets the clip rectangle that <see cref="AddRune(Rune)"/> and <see cref="AddStr(string)"/> are subject
  54. /// to.
  55. /// </summary>
  56. /// <value>The rectangle describing the of <see cref="Clip"/> region.</value>
  57. public Region Clip
  58. {
  59. get => _outputBuffer.Clip;
  60. set => _outputBuffer.Clip = value;
  61. }
  62. /// <summary>Get the operating system clipboard.</summary>
  63. public IClipboard Clipboard { get; private set; } = new FakeDriver.FakeClipboard ();
  64. /// <summary>
  65. /// Gets the column last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  66. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  67. /// </summary>
  68. public int Col => _outputBuffer.Col;
  69. /// <summary>The number of columns visible in the terminal.</summary>
  70. public int Cols
  71. {
  72. get => _outputBuffer.Cols;
  73. set => _outputBuffer.Cols = value;
  74. }
  75. /// <summary>
  76. /// The contents of the application output. The driver outputs this buffer to the terminal.
  77. /// <remarks>The format of the array is rows, columns. The first index is the row, the second index is the column.</remarks>
  78. /// </summary>
  79. public Cell [,] Contents
  80. {
  81. get => _outputBuffer.Contents;
  82. set => _outputBuffer.Contents = value;
  83. }
  84. /// <summary>The leftmost column in the terminal.</summary>
  85. public int Left
  86. {
  87. get => _outputBuffer.Left;
  88. set => _outputBuffer.Left = value;
  89. }
  90. /// <summary>
  91. /// Gets the row last set by <see cref="Move"/>. <see cref="Col"/> and <see cref="Row"/> are used by
  92. /// <see cref="AddRune(Rune)"/> and <see cref="AddStr"/> to determine where to add content.
  93. /// </summary>
  94. public int Row => _outputBuffer.Row;
  95. /// <summary>The number of rows visible in the terminal.</summary>
  96. public int Rows
  97. {
  98. get => _outputBuffer.Rows;
  99. set => _outputBuffer.Rows = value;
  100. }
  101. /// <summary>The topmost row in the terminal.</summary>
  102. public int Top
  103. {
  104. get => _outputBuffer.Top;
  105. set => _outputBuffer.Top = value;
  106. }
  107. // TODO: Probably not everyone right?
  108. /// <summary>Gets whether the <see cref="ConsoleDriver"/> supports TrueColor output.</summary>
  109. public bool SupportsTrueColor => true;
  110. // TODO: Currently ignored
  111. /// <summary>
  112. /// Gets or sets whether the <see cref="ConsoleDriver"/> should use 16 colors instead of the default TrueColors.
  113. /// See <see cref="Application.Force16Colors"/> to change this setting via <see cref="ConfigurationManager"/>.
  114. /// </summary>
  115. /// <remarks>
  116. /// <para>
  117. /// Will be forced to <see langword="true"/> if <see cref="ConsoleDriver.SupportsTrueColor"/> is
  118. /// <see langword="false"/>, indicating that the <see cref="ConsoleDriver"/> cannot support TrueColor.
  119. /// </para>
  120. /// </remarks>
  121. public bool Force16Colors { get; set; }
  122. /// <summary>
  123. /// The <see cref="Attribute"/> that will be used for the next <see cref="AddRune(Rune)"/> or <see cref="AddStr"/>
  124. /// call.
  125. /// </summary>
  126. public Attribute CurrentAttribute
  127. {
  128. get => _outputBuffer.CurrentAttribute;
  129. set => _outputBuffer.CurrentAttribute = value;
  130. }
  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="ConsoleDriver.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="ConsoleDriver.Clip"/> or screen
  137. /// dimensions defined by <see cref="ConsoleDriver.Cols"/>.
  138. /// </para>
  139. /// <para>
  140. /// If <paramref name="rune"/> requires more than one column, and <see cref="ConsoleDriver.Col"/> plus the number
  141. /// of columns
  142. /// needed exceeds the <see cref="ConsoleDriver.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. public void AddRune (Rune rune) { _outputBuffer.AddRune (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="ConsoleDriver.AddRune(System.Text.Rune)"/> with the <see cref="Rune"/>
  152. /// constructor.
  153. /// </summary>
  154. /// <param name="c">Character to add.</param>
  155. public void AddRune (char c) { _outputBuffer.AddRune (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="ConsoleDriver.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="ConsoleDriver.Clip"/>
  161. /// or screen
  162. /// dimensions defined by <see cref="ConsoleDriver.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. public void AddStr (string str) { _outputBuffer.AddStr (str); }
  168. /// <summary>Clears the <see cref="ConsoleDriver.Contents"/> of the driver.</summary>
  169. public void ClearContents ()
  170. {
  171. _outputBuffer.ClearContents ();
  172. ClearedContents?.Invoke (this, new MouseEventArgs ());
  173. }
  174. /// <summary>
  175. /// Raised each time <see cref="ConsoleDriver.ClearContents"/> is called. For benchmarking.
  176. /// </summary>
  177. public event EventHandler<EventArgs> ClearedContents;
  178. /// <summary>
  179. /// Fills the specified rectangle with the specified rune, using <see cref="ConsoleDriver.CurrentAttribute"/>
  180. /// </summary>
  181. /// <remarks>
  182. /// The value of <see cref="ConsoleDriver.Clip"/> is honored. Any parts of the rectangle not in the clip will not be
  183. /// drawn.
  184. /// </remarks>
  185. /// <param name="rect">The Screen-relative rectangle.</param>
  186. /// <param name="rune">The Rune used to fill the rectangle</param>
  187. public void FillRect (Rectangle rect, Rune rune = default) { _outputBuffer.FillRect (rect, rune); }
  188. /// <summary>
  189. /// Fills the specified rectangle with the specified <see langword="char"/>. This method is a convenience method
  190. /// that calls <see cref="ConsoleDriver.FillRect(System.Drawing.Rectangle,System.Text.Rune)"/>.
  191. /// </summary>
  192. /// <param name="rect"></param>
  193. /// <param name="c"></param>
  194. public void FillRect (Rectangle rect, char c) { _outputBuffer.FillRect (rect, c); }
  195. /// <inheritdoc/>
  196. public virtual string GetVersionInfo ()
  197. {
  198. var type = "";
  199. if (InputProcessor is WindowsInputProcessor)
  200. {
  201. type = "win";
  202. }
  203. else if (InputProcessor is NetInputProcessor)
  204. {
  205. type = "net";
  206. }
  207. return "v2" + type;
  208. }
  209. /// <summary>Tests if the specified rune is supported by the driver.</summary>
  210. /// <param name="rune"></param>
  211. /// <returns>
  212. /// <see langword="true"/> if the rune can be properly presented; <see langword="false"/> if the driver does not
  213. /// support displaying this rune.
  214. /// </returns>
  215. public bool IsRuneSupported (Rune rune) { return Rune.IsValid (rune.Value); }
  216. /// <summary>Tests whether the specified coordinate are valid for drawing the specified Rune.</summary>
  217. /// <param name="rune">Used to determine if one or two columns are required.</param>
  218. /// <param name="col">The column.</param>
  219. /// <param name="row">The row.</param>
  220. /// <returns>
  221. /// <see langword="false"/> if the coordinate is outside the screen bounds or outside of
  222. /// <see cref="ConsoleDriver.Clip"/>.
  223. /// <see langword="true"/> otherwise.
  224. /// </returns>
  225. public bool IsValidLocation (Rune rune, int col, int row) { return _outputBuffer.IsValidLocation (rune, col, row); }
  226. /// <summary>
  227. /// Updates <see cref="ConsoleDriver.Col"/> and <see cref="ConsoleDriver.Row"/> to the specified column and row in
  228. /// <see cref="ConsoleDriver.Contents"/>.
  229. /// Used by <see cref="ConsoleDriver.AddRune(System.Text.Rune)"/> and <see cref="ConsoleDriver.AddStr"/> to determine
  230. /// where to add content.
  231. /// </summary>
  232. /// <remarks>
  233. /// <para>This does not move the cursor on the screen, it only updates the internal state of the driver.</para>
  234. /// <para>
  235. /// If <paramref name="col"/> or <paramref name="row"/> are negative or beyond <see cref="ConsoleDriver.Cols"/>
  236. /// and
  237. /// <see cref="ConsoleDriver.Rows"/>, the method still sets those properties.
  238. /// </para>
  239. /// </remarks>
  240. /// <param name="col">Column to move to.</param>
  241. /// <param name="row">Row to move to.</param>
  242. public void Move (int col, int row) { _outputBuffer.Move (col, row); }
  243. // TODO: Probably part of output
  244. /// <summary>Sets the terminal cursor visibility.</summary>
  245. /// <param name="visibility">The wished <see cref="CursorVisibility"/></param>
  246. /// <returns><see langword="true"/> upon success</returns>
  247. public bool SetCursorVisibility (CursorVisibility visibility)
  248. {
  249. _lastCursor = visibility;
  250. _output.SetCursorVisibility (visibility);
  251. return true;
  252. }
  253. /// <inheritdoc/>
  254. public bool GetCursorVisibility (out CursorVisibility current)
  255. {
  256. current = _lastCursor;
  257. return true;
  258. }
  259. /// <inheritdoc/>
  260. public void Suspend () { }
  261. /// <summary>
  262. /// Sets the position of the terminal cursor to <see cref="ConsoleDriver.Col"/> and
  263. /// <see cref="ConsoleDriver.Row"/>.
  264. /// </summary>
  265. public void UpdateCursor () { _output.SetCursorPosition (Col, Row); }
  266. /// <summary>Initializes the driver</summary>
  267. /// <returns>Returns an instance of <see cref="MainLoop"/> using the <see cref="IMainLoopDriver"/> for the driver.</returns>
  268. public MainLoop Init () { throw new NotSupportedException (); }
  269. /// <summary>Ends the execution of the console driver.</summary>
  270. public void End ()
  271. {
  272. // TODO: Nope
  273. }
  274. /// <summary>Selects the specified attribute as the attribute to use for future calls to AddRune and AddString.</summary>
  275. /// <remarks>Implementations should call <c>base.SetAttribute(c)</c>.</remarks>
  276. /// <param name="c">C.</param>
  277. public Attribute SetAttribute (Attribute c) { return _outputBuffer.CurrentAttribute = c; }
  278. /// <summary>Gets the current <see cref="Attribute"/>.</summary>
  279. /// <returns>The current attribute.</returns>
  280. public Attribute GetAttribute () { return _outputBuffer.CurrentAttribute; }
  281. /// <summary>Makes an <see cref="Attribute"/>.</summary>
  282. /// <param name="foreground">The foreground color.</param>
  283. /// <param name="background">The background color.</param>
  284. /// <returns>The attribute for the foreground and background colors.</returns>
  285. public Attribute MakeColor (in Color foreground, in Color background)
  286. {
  287. // TODO: what even is this? why Attribute constructor wants to call Driver method which must return an instance of Attribute? ?!?!?!
  288. return new (
  289. -1, // only used by cursesdriver!
  290. foreground,
  291. background
  292. );
  293. }
  294. /// <summary>Event fired when a key is pressed down. This is a precursor to <see cref="ConsoleDriver.KeyUp"/>.</summary>
  295. public event EventHandler<Key> KeyDown;
  296. /// <summary>Event fired when a key is released.</summary>
  297. /// <remarks>
  298. /// Drivers that do not support key release events will fire this event after <see cref="ConsoleDriver.KeyDown"/>
  299. /// processing is
  300. /// complete.
  301. /// </remarks>
  302. public event EventHandler<Key> KeyUp;
  303. /// <summary>Event fired when a mouse event occurs.</summary>
  304. public event EventHandler<MouseEventArgs> MouseEvent;
  305. /// <summary>Simulates a key press.</summary>
  306. /// <param name="keyChar">The key character.</param>
  307. /// <param name="key">The key.</param>
  308. /// <param name="shift">If <see langword="true"/> simulates the Shift key being pressed.</param>
  309. /// <param name="alt">If <see langword="true"/> simulates the Alt key being pressed.</param>
  310. /// <param name="ctrl">If <see langword="true"/> simulates the Ctrl key being pressed.</param>
  311. public void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool ctrl)
  312. {
  313. // TODO: implement
  314. }
  315. /// <summary>
  316. /// Provide proper writing to send escape sequence recognized by the <see cref="ConsoleDriver"/>.
  317. /// </summary>
  318. /// <param name="ansi"></param>
  319. public void WriteRaw (string ansi) { _output.Write (ansi); }
  320. /// <summary>
  321. /// Queues the given <paramref name="request"/> for execution
  322. /// </summary>
  323. /// <param name="request"></param>
  324. public void QueueAnsiRequest (AnsiEscapeSequenceRequest request) { _ansiRequestScheduler.SendOrSchedule (request); }
  325. public AnsiRequestScheduler GetRequestScheduler () { return _ansiRequestScheduler; }
  326. /// <inheritdoc/>
  327. public void Refresh ()
  328. {
  329. // No need we will always draw when dirty
  330. }
  331. }