|
1 | 1 | using System;
|
2 |
| -using System.Threading; |
3 | 2 |
|
4 | 3 | namespace Terminal.Gui {
|
5 |
| - /// <summary> |
6 |
| - /// Mainloop intended to be used with the .NET System.Console API, and can |
7 |
| - /// be used on Windows and Unix, it is cross platform but lacks things like |
8 |
| - /// file descriptor monitoring. |
9 |
| - /// </summary> |
10 |
| - /// <remarks> |
11 |
| - /// This implementation is used for FakeDriver. |
12 |
| - /// </remarks> |
13 |
| - public class FakeMainLoop : IMainLoopDriver { |
14 |
| - AutoResetEvent keyReady = new AutoResetEvent (false); |
15 |
| - AutoResetEvent waitForProbe = new AutoResetEvent (false); |
16 |
| - ConsoleKeyInfo? keyResult = null; |
17 |
| - MainLoop mainLoop; |
18 |
| - Func<ConsoleKeyInfo> consoleKeyReaderFn = () => FakeConsole.ReadKey (true); |
| 4 | + internal class FakeMainLoop : IMainLoopDriver { |
19 | 5 |
|
20 |
| - /// <summary> |
21 |
| - /// Invoked when a Key is pressed. |
22 |
| - /// </summary> |
23 | 6 | public Action<ConsoleKeyInfo> KeyPressed;
|
24 | 7 |
|
25 |
| - /// <summary> |
26 |
| - /// Creates an instance of the FakeMainLoop. <paramref name="consoleDriver"/> is not used. |
27 |
| - /// </summary> |
28 |
| - /// <param name="consoleDriver"></param> |
29 | 8 | public FakeMainLoop (ConsoleDriver consoleDriver = null)
|
30 | 9 | {
|
31 | 10 | // consoleDriver is not needed/used in FakeConsole
|
32 | 11 | }
|
33 | 12 |
|
34 |
| - void WindowsKeyReader () |
| 13 | + public void Setup (MainLoop mainLoop) |
35 | 14 | {
|
36 |
| - while (true) { |
37 |
| - waitForProbe.WaitOne (); |
38 |
| - keyResult = consoleKeyReaderFn (); |
39 |
| - keyReady.Set (); |
40 |
| - } |
41 |
| - } |
42 |
| - |
43 |
| - void IMainLoopDriver.Setup (MainLoop mainLoop) |
44 |
| - { |
45 |
| - this.mainLoop = mainLoop; |
46 |
| - Thread readThread = new Thread (WindowsKeyReader); |
47 |
| - readThread.Start (); |
48 | 15 | }
|
49 | 16 |
|
50 |
| - void IMainLoopDriver.Wakeup () |
| 17 | + public void Wakeup () |
51 | 18 | {
|
| 19 | + // No implementation needed for FakeMainLoop |
52 | 20 | }
|
53 | 21 |
|
54 |
| - bool IMainLoopDriver.EventsPending (bool wait) |
55 |
| - { |
56 |
| - keyResult = null; |
57 |
| - waitForProbe.Set (); |
58 |
| - |
59 |
| - if (CheckTimers (wait, out var waitTimeout)) { |
60 |
| - return true; |
61 |
| - } |
62 |
| - |
63 |
| - keyReady.WaitOne (waitTimeout); |
64 |
| - return keyResult.HasValue; |
65 |
| - } |
66 |
| - |
67 |
| - bool CheckTimers (bool wait, out int waitTimeout) |
| 22 | + public bool EventsPending (bool wait) |
68 | 23 | {
|
69 |
| - long now = DateTime.UtcNow.Ticks; |
70 |
| - |
71 |
| - if (mainLoop.timeouts.Count > 0) { |
72 |
| - waitTimeout = (int)((mainLoop.timeouts.Keys [0] - now) / TimeSpan.TicksPerMillisecond); |
73 |
| - if (waitTimeout < 0) |
74 |
| - return true; |
75 |
| - } else { |
76 |
| - waitTimeout = -1; |
77 |
| - } |
78 |
| - |
79 |
| - if (!wait) |
80 |
| - waitTimeout = 0; |
81 |
| - |
82 |
| - int ic; |
83 |
| - lock (mainLoop.idleHandlers) { |
84 |
| - ic = mainLoop.idleHandlers.Count; |
85 |
| - } |
86 |
| - |
87 |
| - return ic > 0; |
| 24 | + // Always return true for FakeMainLoop |
| 25 | + return true; |
88 | 26 | }
|
89 | 27 |
|
90 |
| - void IMainLoopDriver.MainIteration () |
| 28 | + public void MainIteration () |
91 | 29 | {
|
92 |
| - if (keyResult.HasValue) { |
93 |
| - KeyPressed?.Invoke (keyResult.Value); |
94 |
| - keyResult = null; |
| 30 | + if (FakeConsole.MockKeyPresses.Count > 0) { |
| 31 | + KeyPressed?.Invoke (FakeConsole.MockKeyPresses.Pop ()); |
95 | 32 | }
|
96 | 33 | }
|
97 | 34 | }
|
|
0 commit comments