FakeDriver.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. //
  2. // FakeDriver.cs: A fake ConsoleDriver for unit tests.
  3. //
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using System.Runtime.InteropServices;
  9. using System.Threading;
  10. using System.Text;
  11. // Alias Console to MockConsole so we don't accidentally use Console
  12. using Console = Terminal.Gui.FakeConsole;
  13. namespace Terminal.Gui {
  14. /// <summary>
  15. /// Implements a mock ConsoleDriver for unit testing
  16. /// </summary>
  17. public class FakeDriver : ConsoleDriver {
  18. #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
  19. public class Behaviors {
  20. public bool UseFakeClipboard { get; internal set; }
  21. public bool FakeClipboardAlwaysThrowsNotSupportedException { get; internal set; }
  22. public bool FakeClipboardIsSupportedAlwaysFalse { get; internal set; }
  23. public Behaviors (bool useFakeClipboard = false, bool fakeClipboardAlwaysThrowsNotSupportedException = false, bool fakeClipboardIsSupportedAlwaysTrue = false)
  24. {
  25. UseFakeClipboard = useFakeClipboard;
  26. FakeClipboardAlwaysThrowsNotSupportedException = fakeClipboardAlwaysThrowsNotSupportedException;
  27. FakeClipboardIsSupportedAlwaysFalse = fakeClipboardIsSupportedAlwaysTrue;
  28. // double check usage is correct
  29. Debug.Assert (useFakeClipboard == false && fakeClipboardAlwaysThrowsNotSupportedException == false);
  30. Debug.Assert (useFakeClipboard == false && fakeClipboardIsSupportedAlwaysTrue == false);
  31. }
  32. }
  33. public static FakeDriver.Behaviors FakeBehaviors = new Behaviors ();
  34. int cols, rows, left, top;
  35. public override int Cols => cols;
  36. public override int Rows => rows;
  37. // Only handling left here because not all terminals has a horizontal scroll bar.
  38. public override int Left => 0;
  39. public override int Top => 0;
  40. public override bool EnableConsoleScrolling { get; set; }
  41. private IClipboard clipboard = null;
  42. public override IClipboard Clipboard => clipboard;
  43. // The format is rows, columns and 3 values on the last column: Rune, Attribute and Dirty Flag
  44. int [,,] contents;
  45. bool [] dirtyLine;
  46. /// <summary>
  47. /// Assists with testing, the format is rows, columns and 3 values on the last column: Rune, Attribute and Dirty Flag
  48. /// </summary>
  49. public override int [,,] Contents => contents;
  50. //void UpdateOffscreen ()
  51. //{
  52. // int cols = Cols;
  53. // int rows = Rows;
  54. // contents = new int [rows, cols, 3];
  55. // for (int r = 0; r < rows; r++) {
  56. // for (int c = 0; c < cols; c++) {
  57. // contents [r, c, 0] = ' ';
  58. // contents [r, c, 1] = MakeColor (ConsoleColor.Gray, ConsoleColor.Black);
  59. // contents [r, c, 2] = 0;
  60. // }
  61. // }
  62. // dirtyLine = new bool [rows];
  63. // for (int row = 0; row < rows; row++)
  64. // dirtyLine [row] = true;
  65. //}
  66. static bool sync = false;
  67. public FakeDriver ()
  68. {
  69. if (FakeBehaviors.UseFakeClipboard) {
  70. clipboard = new FakeClipboard (FakeBehaviors.FakeClipboardAlwaysThrowsNotSupportedException, FakeBehaviors.FakeClipboardIsSupportedAlwaysFalse);
  71. } else {
  72. if (RuntimeInformation.IsOSPlatform (OSPlatform.Windows)) {
  73. clipboard = new WindowsClipboard ();
  74. } else if (RuntimeInformation.IsOSPlatform (OSPlatform.OSX)) {
  75. clipboard = new MacOSXClipboard ();
  76. } else {
  77. if (CursesDriver.Is_WSL_Platform ()) {
  78. clipboard = new WSLClipboard ();
  79. } else {
  80. clipboard = new CursesClipboard ();
  81. }
  82. }
  83. }
  84. }
  85. bool needMove;
  86. // Current row, and current col, tracked by Move/AddCh only
  87. int ccol, crow;
  88. public override void Move (int col, int row)
  89. {
  90. ccol = col;
  91. crow = row;
  92. if (Clip.Contains (col, row)) {
  93. FakeConsole.CursorTop = row;
  94. FakeConsole.CursorLeft = col;
  95. needMove = false;
  96. } else {
  97. FakeConsole.CursorTop = Clip.Y;
  98. FakeConsole.CursorLeft = Clip.X;
  99. needMove = true;
  100. }
  101. }
  102. public override void AddRune (Rune rune)
  103. {
  104. rune = rune.MakePrintable ();
  105. var runeWidth = rune.GetColumns ();
  106. var validClip = IsValidContent (ccol, crow, Clip);
  107. if (validClip) {
  108. if (needMove) {
  109. //MockConsole.CursorLeft = ccol;
  110. //MockConsole.CursorTop = crow;
  111. needMove = false;
  112. }
  113. if (runeWidth == 0 && ccol > 0) {
  114. var r = contents [crow, ccol - 1, 0];
  115. var s = new string (new char [] { (char)r, (char)rune.Value });
  116. string sn;
  117. if (!s.IsNormalized ()) {
  118. sn = s.Normalize ();
  119. } else {
  120. sn = s;
  121. }
  122. var c = sn [0];
  123. contents [crow, ccol - 1, 0] = c;
  124. contents [crow, ccol - 1, 1] = CurrentAttribute;
  125. contents [crow, ccol - 1, 2] = 1;
  126. } else {
  127. if (runeWidth < 2 && ccol > 0
  128. && ((Rune)contents [crow, ccol - 1, 0]).GetColumns () > 1) {
  129. contents [crow, ccol - 1, 0] = (int)(uint)' ';
  130. } else if (runeWidth < 2 && ccol <= Clip.Right - 1
  131. && ((Rune)contents [crow, ccol, 0]).GetColumns () > 1) {
  132. contents [crow, ccol + 1, 0] = (int)(uint)' ';
  133. contents [crow, ccol + 1, 2] = 1;
  134. }
  135. if (runeWidth > 1 && ccol == Clip.Right - 1) {
  136. contents [crow, ccol, 0] = (int)(uint)' ';
  137. } else {
  138. contents [crow, ccol, 0] = (int)(uint)rune.Value;
  139. }
  140. contents [crow, ccol, 1] = CurrentAttribute;
  141. contents [crow, ccol, 2] = 1;
  142. dirtyLine [crow] = true;
  143. }
  144. } else {
  145. needMove = true;
  146. }
  147. if (runeWidth < 0 || runeWidth > 0) {
  148. ccol++;
  149. }
  150. if (runeWidth > 1) {
  151. if (validClip && ccol < Clip.Right) {
  152. contents [crow, ccol, 1] = CurrentAttribute;
  153. contents [crow, ccol, 2] = 0;
  154. }
  155. ccol++;
  156. }
  157. //if (ccol == Cols) {
  158. // ccol = 0;
  159. // if (crow + 1 < Rows)
  160. // crow++;
  161. //}
  162. if (sync) {
  163. UpdateScreen ();
  164. }
  165. }
  166. public override void AddStr (string str)
  167. {
  168. foreach (var rune in str.EnumerateRunes ())
  169. AddRune (rune);
  170. }
  171. public override void End ()
  172. {
  173. FakeConsole.ResetColor ();
  174. FakeConsole.Clear ();
  175. }
  176. public override Attribute MakeColor (Color foreground, Color background)
  177. {
  178. return MakeColor ((ConsoleColor)foreground, (ConsoleColor)background);
  179. }
  180. static Attribute MakeColor (ConsoleColor f, ConsoleColor b)
  181. {
  182. // Encode the colors into the int value.
  183. return new Attribute (
  184. value: ((((int)f) & 0xffff) << 16) | (((int)b) & 0xffff),
  185. foreground: (Color)f,
  186. background: (Color)b
  187. );
  188. }
  189. public override void Init (Action terminalResized)
  190. {
  191. FakeConsole.MockKeyPresses.Clear ();
  192. TerminalResized = terminalResized;
  193. cols = FakeConsole.WindowWidth = FakeConsole.BufferWidth = FakeConsole.WIDTH;
  194. rows = FakeConsole.WindowHeight = FakeConsole.BufferHeight = FakeConsole.HEIGHT;
  195. FakeConsole.Clear ();
  196. ResizeScreen ();
  197. // Call InitalizeColorSchemes before UpdateOffScreen as it references Colors
  198. CurrentAttribute = MakeColor (Color.White, Color.Black);
  199. InitalizeColorSchemes ();
  200. UpdateOffScreen ();
  201. }
  202. public override Attribute MakeAttribute (Color fore, Color back)
  203. {
  204. return MakeColor ((ConsoleColor)fore, (ConsoleColor)back);
  205. }
  206. int redrawColor = -1;
  207. void SetColor (int color)
  208. {
  209. redrawColor = color;
  210. IEnumerable<int> values = Enum.GetValues (typeof (ConsoleColor))
  211. .OfType<ConsoleColor> ()
  212. .Select (s => (int)s);
  213. if (values.Contains (color & 0xffff)) {
  214. FakeConsole.BackgroundColor = (ConsoleColor)(color & 0xffff);
  215. }
  216. if (values.Contains ((color >> 16) & 0xffff)) {
  217. FakeConsole.ForegroundColor = (ConsoleColor)((color >> 16) & 0xffff);
  218. }
  219. }
  220. public override void UpdateScreen ()
  221. {
  222. int top = Top;
  223. int left = Left;
  224. int rows = Math.Min (FakeConsole.WindowHeight + top, Rows);
  225. int cols = Cols;
  226. var savedRow = FakeConsole.CursorTop;
  227. var savedCol = FakeConsole.CursorLeft;
  228. var savedCursorVisible = FakeConsole.CursorVisible;
  229. for (int row = top; row < rows; row++) {
  230. if (!dirtyLine [row])
  231. continue;
  232. dirtyLine [row] = false;
  233. for (int col = left; col < cols; col++) {
  234. FakeConsole.CursorTop = row;
  235. FakeConsole.CursorLeft = col;
  236. for (; col < cols; col++) {
  237. if (contents [row, col, 2] == 0) {
  238. FakeConsole.CursorLeft++;
  239. continue;
  240. }
  241. var color = contents [row, col, 1];
  242. if (color != redrawColor)
  243. SetColor (color);
  244. Rune rune = (Rune)contents [row, col, 0];
  245. if (rune.DecodeSurrogatePair (out char [] spair)) {
  246. FakeConsole.Write (spair);
  247. } else {
  248. FakeConsole.Write ((char)rune.Value);
  249. }
  250. contents [row, col, 2] = 0;
  251. }
  252. }
  253. }
  254. FakeConsole.CursorTop = savedRow;
  255. FakeConsole.CursorLeft = savedCol;
  256. FakeConsole.CursorVisible = savedCursorVisible;
  257. }
  258. public override void Refresh ()
  259. {
  260. UpdateScreen ();
  261. UpdateCursor ();
  262. }
  263. public override void SetAttribute (Attribute c)
  264. {
  265. base.SetAttribute (c);
  266. }
  267. public ConsoleKeyInfo FromVKPacketToKConsoleKeyInfo (ConsoleKeyInfo consoleKeyInfo)
  268. {
  269. if (consoleKeyInfo.Key != ConsoleKey.Packet) {
  270. return consoleKeyInfo;
  271. }
  272. var mod = consoleKeyInfo.Modifiers;
  273. var shift = (mod & ConsoleModifiers.Shift) != 0;
  274. var alt = (mod & ConsoleModifiers.Alt) != 0;
  275. var control = (mod & ConsoleModifiers.Control) != 0;
  276. var keyChar = ConsoleKeyMapping.GetKeyCharFromConsoleKey (consoleKeyInfo.KeyChar, consoleKeyInfo.Modifiers, out uint virtualKey, out _);
  277. return new ConsoleKeyInfo ((char)keyChar, (ConsoleKey)virtualKey, shift, alt, control);
  278. }
  279. Key MapKey (ConsoleKeyInfo keyInfo)
  280. {
  281. switch (keyInfo.Key) {
  282. case ConsoleKey.Escape:
  283. return MapKeyModifiers (keyInfo, Key.Esc);
  284. case ConsoleKey.Tab:
  285. return keyInfo.Modifiers == ConsoleModifiers.Shift ? Key.BackTab : Key.Tab;
  286. case ConsoleKey.Clear:
  287. return MapKeyModifiers (keyInfo, Key.Clear);
  288. case ConsoleKey.Home:
  289. return MapKeyModifiers (keyInfo, Key.Home);
  290. case ConsoleKey.End:
  291. return MapKeyModifiers (keyInfo, Key.End);
  292. case ConsoleKey.LeftArrow:
  293. return MapKeyModifiers (keyInfo, Key.CursorLeft);
  294. case ConsoleKey.RightArrow:
  295. return MapKeyModifiers (keyInfo, Key.CursorRight);
  296. case ConsoleKey.UpArrow:
  297. return MapKeyModifiers (keyInfo, Key.CursorUp);
  298. case ConsoleKey.DownArrow:
  299. return MapKeyModifiers (keyInfo, Key.CursorDown);
  300. case ConsoleKey.PageUp:
  301. return MapKeyModifiers (keyInfo, Key.PageUp);
  302. case ConsoleKey.PageDown:
  303. return MapKeyModifiers (keyInfo, Key.PageDown);
  304. case ConsoleKey.Enter:
  305. return MapKeyModifiers (keyInfo, Key.Enter);
  306. case ConsoleKey.Spacebar:
  307. return MapKeyModifiers (keyInfo, keyInfo.KeyChar == 0 ? Key.Space : (Key)keyInfo.KeyChar);
  308. case ConsoleKey.Backspace:
  309. return MapKeyModifiers (keyInfo, Key.Backspace);
  310. case ConsoleKey.Delete:
  311. return MapKeyModifiers (keyInfo, Key.DeleteChar);
  312. case ConsoleKey.Insert:
  313. return MapKeyModifiers (keyInfo, Key.InsertChar);
  314. case ConsoleKey.PrintScreen:
  315. return MapKeyModifiers (keyInfo, Key.PrintScreen);
  316. case ConsoleKey.Oem1:
  317. case ConsoleKey.Oem2:
  318. case ConsoleKey.Oem3:
  319. case ConsoleKey.Oem4:
  320. case ConsoleKey.Oem5:
  321. case ConsoleKey.Oem6:
  322. case ConsoleKey.Oem7:
  323. case ConsoleKey.Oem8:
  324. case ConsoleKey.Oem102:
  325. case ConsoleKey.OemPeriod:
  326. case ConsoleKey.OemComma:
  327. case ConsoleKey.OemPlus:
  328. case ConsoleKey.OemMinus:
  329. if (keyInfo.KeyChar == 0)
  330. return Key.Unknown;
  331. return (Key)((uint)keyInfo.KeyChar);
  332. }
  333. var key = keyInfo.Key;
  334. if (key >= ConsoleKey.A && key <= ConsoleKey.Z) {
  335. var delta = key - ConsoleKey.A;
  336. if (keyInfo.Modifiers == ConsoleModifiers.Control) {
  337. return (Key)(((uint)Key.CtrlMask) | ((uint)Key.A + delta));
  338. }
  339. if (keyInfo.Modifiers == ConsoleModifiers.Alt) {
  340. return (Key)(((uint)Key.AltMask) | ((uint)Key.A + delta));
  341. }
  342. if (keyInfo.Modifiers == (ConsoleModifiers.Shift | ConsoleModifiers.Alt)) {
  343. return MapKeyModifiers (keyInfo, (Key)((uint)Key.A + delta));
  344. }
  345. if ((keyInfo.Modifiers & (ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  346. if (keyInfo.KeyChar == 0) {
  347. return (Key)(((uint)Key.AltMask | (uint)Key.CtrlMask) | ((uint)Key.A + delta));
  348. } else {
  349. return (Key)((uint)keyInfo.KeyChar);
  350. }
  351. }
  352. return (Key)((uint)keyInfo.KeyChar);
  353. }
  354. if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9) {
  355. var delta = key - ConsoleKey.D0;
  356. if (keyInfo.Modifiers == ConsoleModifiers.Alt) {
  357. return (Key)(((uint)Key.AltMask) | ((uint)Key.D0 + delta));
  358. }
  359. if (keyInfo.Modifiers == ConsoleModifiers.Control) {
  360. return (Key)(((uint)Key.CtrlMask) | ((uint)Key.D0 + delta));
  361. }
  362. if (keyInfo.Modifiers == (ConsoleModifiers.Shift | ConsoleModifiers.Alt)) {
  363. return MapKeyModifiers (keyInfo, (Key)((uint)Key.D0 + delta));
  364. }
  365. if ((keyInfo.Modifiers & (ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  366. if (keyInfo.KeyChar == 0 || keyInfo.KeyChar == 30) {
  367. return MapKeyModifiers (keyInfo, (Key)((uint)Key.D0 + delta));
  368. }
  369. }
  370. return (Key)((uint)keyInfo.KeyChar);
  371. }
  372. if (key >= ConsoleKey.F1 && key <= ConsoleKey.F12) {
  373. var delta = key - ConsoleKey.F1;
  374. if ((keyInfo.Modifiers & (ConsoleModifiers.Shift | ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  375. return MapKeyModifiers (keyInfo, (Key)((uint)Key.F1 + delta));
  376. }
  377. return (Key)((uint)Key.F1 + delta);
  378. }
  379. if (keyInfo.KeyChar != 0) {
  380. return MapKeyModifiers (keyInfo, (Key)((uint)keyInfo.KeyChar));
  381. }
  382. return (Key)(0xffffffff);
  383. }
  384. KeyModifiers keyModifiers;
  385. private Key MapKeyModifiers (ConsoleKeyInfo keyInfo, Key key)
  386. {
  387. Key keyMod = new Key ();
  388. if ((keyInfo.Modifiers & ConsoleModifiers.Shift) != 0)
  389. keyMod = Key.ShiftMask;
  390. if ((keyInfo.Modifiers & ConsoleModifiers.Control) != 0)
  391. keyMod |= Key.CtrlMask;
  392. if ((keyInfo.Modifiers & ConsoleModifiers.Alt) != 0)
  393. keyMod |= Key.AltMask;
  394. return keyMod != Key.Null ? keyMod | key : key;
  395. }
  396. Action<KeyEvent> keyDownHandler;
  397. Action<KeyEvent> keyHandler;
  398. Action<KeyEvent> keyUpHandler;
  399. private CursorVisibility savedCursorVisibility;
  400. public override void PrepareToRun (MainLoop mainLoop, Action<KeyEvent> keyHandler, Action<KeyEvent> keyDownHandler, Action<KeyEvent> keyUpHandler, Action<MouseEvent> mouseHandler)
  401. {
  402. this.keyDownHandler = keyDownHandler;
  403. this.keyHandler = keyHandler;
  404. this.keyUpHandler = keyUpHandler;
  405. // Note: Net doesn't support keydown/up events and thus any passed keyDown/UpHandlers will never be called
  406. (mainLoop.Driver as FakeMainLoop).KeyPressed += (consoleKey) => ProcessInput (consoleKey);
  407. }
  408. void ProcessInput (ConsoleKeyInfo consoleKey)
  409. {
  410. if (consoleKey.Key == ConsoleKey.Packet) {
  411. consoleKey = FromVKPacketToKConsoleKeyInfo (consoleKey);
  412. }
  413. keyModifiers = new KeyModifiers ();
  414. if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Shift)) {
  415. keyModifiers.Shift = true;
  416. }
  417. if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Alt)) {
  418. keyModifiers.Alt = true;
  419. }
  420. if (consoleKey.Modifiers.HasFlag (ConsoleModifiers.Control)) {
  421. keyModifiers.Ctrl = true;
  422. }
  423. var map = MapKey (consoleKey);
  424. if (map == (Key)0xffffffff) {
  425. if ((consoleKey.Modifiers & (ConsoleModifiers.Shift | ConsoleModifiers.Alt | ConsoleModifiers.Control)) != 0) {
  426. keyDownHandler (new KeyEvent (map, keyModifiers));
  427. keyUpHandler (new KeyEvent (map, keyModifiers));
  428. }
  429. return;
  430. }
  431. keyDownHandler (new KeyEvent (map, keyModifiers));
  432. keyHandler (new KeyEvent (map, keyModifiers));
  433. keyUpHandler (new KeyEvent (map, keyModifiers));
  434. }
  435. /// <inheritdoc/>
  436. public override bool GetCursorVisibility (out CursorVisibility visibility)
  437. {
  438. visibility = FakeConsole.CursorVisible
  439. ? CursorVisibility.Default
  440. : CursorVisibility.Invisible;
  441. return FakeConsole.CursorVisible;
  442. }
  443. /// <inheritdoc/>
  444. public override bool SetCursorVisibility (CursorVisibility visibility)
  445. {
  446. savedCursorVisibility = visibility;
  447. return FakeConsole.CursorVisible = visibility == CursorVisibility.Default;
  448. }
  449. /// <inheritdoc/>
  450. public override bool EnsureCursorVisibility ()
  451. {
  452. if (!(ccol >= 0 && crow >= 0 && ccol < Cols && crow < Rows)) {
  453. GetCursorVisibility (out CursorVisibility cursorVisibility);
  454. savedCursorVisibility = cursorVisibility;
  455. SetCursorVisibility (CursorVisibility.Invisible);
  456. return false;
  457. }
  458. SetCursorVisibility (savedCursorVisibility);
  459. return FakeConsole.CursorVisible;
  460. }
  461. public override void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool control)
  462. {
  463. ProcessInput (new ConsoleKeyInfo (keyChar, key, shift, alt, control));
  464. }
  465. public void SetBufferSize (int width, int height)
  466. {
  467. FakeConsole.SetBufferSize (width, height);
  468. cols = width;
  469. rows = height;
  470. if (!EnableConsoleScrolling) {
  471. SetWindowSize (width, height);
  472. }
  473. ProcessResize ();
  474. }
  475. public void SetWindowSize (int width, int height)
  476. {
  477. FakeConsole.SetWindowSize (width, height);
  478. if (!EnableConsoleScrolling) {
  479. if (width != cols || height != rows) {
  480. SetBufferSize (width, height);
  481. cols = width;
  482. rows = height;
  483. }
  484. }
  485. ProcessResize ();
  486. }
  487. public void SetWindowPosition (int left, int top)
  488. {
  489. if (EnableConsoleScrolling) {
  490. this.left = Math.Max (Math.Min (left, Cols - FakeConsole.WindowWidth), 0);
  491. this.top = Math.Max (Math.Min (top, Rows - FakeConsole.WindowHeight), 0);
  492. } else if (this.left > 0 || this.top > 0) {
  493. this.left = 0;
  494. this.top = 0;
  495. }
  496. FakeConsole.SetWindowPosition (this.left, this.top);
  497. }
  498. void ProcessResize ()
  499. {
  500. ResizeScreen ();
  501. UpdateOffScreen ();
  502. TerminalResized?.Invoke ();
  503. }
  504. public override void ResizeScreen ()
  505. {
  506. if (!EnableConsoleScrolling) {
  507. if (FakeConsole.WindowHeight > 0) {
  508. // Can raise an exception while is still resizing.
  509. try {
  510. #pragma warning disable CA1416
  511. FakeConsole.CursorTop = 0;
  512. FakeConsole.CursorLeft = 0;
  513. FakeConsole.WindowTop = 0;
  514. FakeConsole.WindowLeft = 0;
  515. #pragma warning restore CA1416
  516. } catch (System.IO.IOException) {
  517. return;
  518. } catch (ArgumentOutOfRangeException) {
  519. return;
  520. }
  521. }
  522. } else {
  523. try {
  524. #pragma warning disable CA1416
  525. FakeConsole.WindowLeft = Math.Max (Math.Min (left, Cols - FakeConsole.WindowWidth), 0);
  526. FakeConsole.WindowTop = Math.Max (Math.Min (top, Rows - FakeConsole.WindowHeight), 0);
  527. #pragma warning restore CA1416
  528. } catch (Exception) {
  529. return;
  530. }
  531. }
  532. Clip = new Rect (0, 0, Cols, Rows);
  533. }
  534. public override void UpdateOffScreen ()
  535. {
  536. contents = new int [Rows, Cols, 3];
  537. dirtyLine = new bool [Rows];
  538. // Can raise an exception while is still resizing.
  539. try {
  540. for (int row = 0; row < rows; row++) {
  541. for (int c = 0; c < cols; c++) {
  542. contents [row, c, 0] = ' ';
  543. contents [row, c, 1] = (ushort)Colors.TopLevel.Normal;
  544. contents [row, c, 2] = 0;
  545. dirtyLine [row] = true;
  546. }
  547. }
  548. } catch (IndexOutOfRangeException) { }
  549. }
  550. public override bool GetColors (int value, out Color foreground, out Color background)
  551. {
  552. bool hasColor = false;
  553. foreground = default;
  554. background = default;
  555. IEnumerable<int> values = Enum.GetValues (typeof (ConsoleColor))
  556. .OfType<ConsoleColor> ()
  557. .Select (s => (int)s);
  558. if (values.Contains (value & 0xffff)) {
  559. hasColor = true;
  560. background = (Color)(ConsoleColor)(value & 0xffff);
  561. }
  562. if (values.Contains ((value >> 16) & 0xffff)) {
  563. hasColor = true;
  564. foreground = (Color)(ConsoleColor)((value >> 16) & 0xffff);
  565. }
  566. return hasColor;
  567. }
  568. #region Unused
  569. public override void UpdateCursor ()
  570. {
  571. if (!EnsureCursorVisibility ())
  572. return;
  573. // Prevents the exception of size changing during resizing.
  574. try {
  575. if (ccol >= 0 && ccol < FakeConsole.BufferWidth && crow >= 0 && crow < FakeConsole.BufferHeight) {
  576. FakeConsole.SetCursorPosition (ccol, crow);
  577. }
  578. } catch (System.IO.IOException) {
  579. } catch (ArgumentOutOfRangeException) {
  580. }
  581. }
  582. public override void StartReportingMouseMoves ()
  583. {
  584. }
  585. public override void StopReportingMouseMoves ()
  586. {
  587. }
  588. public override void Suspend ()
  589. {
  590. }
  591. public override void SetColors (ConsoleColor foreground, ConsoleColor background)
  592. {
  593. }
  594. public override void SetColors (short foregroundColorId, short backgroundColorId)
  595. {
  596. throw new NotImplementedException ();
  597. }
  598. public override void CookMouse ()
  599. {
  600. }
  601. public override void UncookMouse ()
  602. {
  603. }
  604. #endregion
  605. public class FakeClipboard : ClipboardBase {
  606. public Exception FakeException = null;
  607. string contents = string.Empty;
  608. bool isSupportedAlwaysFalse = false;
  609. public override bool IsSupported => !isSupportedAlwaysFalse;
  610. public FakeClipboard (bool fakeClipboardThrowsNotSupportedException = false, bool isSupportedAlwaysFalse = false)
  611. {
  612. this.isSupportedAlwaysFalse = isSupportedAlwaysFalse;
  613. if (fakeClipboardThrowsNotSupportedException) {
  614. FakeException = new NotSupportedException ("Fake clipboard exception");
  615. }
  616. }
  617. protected override string GetClipboardDataImpl ()
  618. {
  619. if (FakeException != null) {
  620. throw FakeException;
  621. }
  622. return contents;
  623. }
  624. protected override void SetClipboardDataImpl (string text)
  625. {
  626. if (FakeException != null) {
  627. throw FakeException;
  628. }
  629. contents = text;
  630. }
  631. }
  632. #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member
  633. }
  634. }