Skip to content

Commit 751e825

Browse files
committed
back ported non-thread fakemainloop from v2
1 parent 155e533 commit 751e825

File tree

1 file changed

+10
-73
lines changed

1 file changed

+10
-73
lines changed

Terminal.Gui/ConsoleDrivers/FakeDriver/FakeMainLoop.cs

+10-73
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,34 @@
11
using System;
2-
using System.Threading;
32

43
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 {
195

20-
/// <summary>
21-
/// Invoked when a Key is pressed.
22-
/// </summary>
236
public Action<ConsoleKeyInfo> KeyPressed;
247

25-
/// <summary>
26-
/// Creates an instance of the FakeMainLoop. <paramref name="consoleDriver"/> is not used.
27-
/// </summary>
28-
/// <param name="consoleDriver"></param>
298
public FakeMainLoop (ConsoleDriver consoleDriver = null)
309
{
3110
// consoleDriver is not needed/used in FakeConsole
3211
}
3312

34-
void WindowsKeyReader ()
13+
public void Setup (MainLoop mainLoop)
3514
{
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 ();
4815
}
4916

50-
void IMainLoopDriver.Wakeup ()
17+
public void Wakeup ()
5118
{
19+
// No implementation needed for FakeMainLoop
5220
}
5321

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)
6823
{
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;
8826
}
8927

90-
void IMainLoopDriver.MainIteration ()
28+
public void MainIteration ()
9129
{
92-
if (keyResult.HasValue) {
93-
KeyPressed?.Invoke (keyResult.Value);
94-
keyResult = null;
30+
if (FakeConsole.MockKeyPresses.Count > 0) {
31+
KeyPressed?.Invoke (FakeConsole.MockKeyPresses.Pop ());
9532
}
9633
}
9734
}

0 commit comments

Comments
 (0)