فهرست منبع

Replace to ForceRead and to WaitForInput.

BDisp 8 ماه پیش
والد
کامیت
53ea0a83f3

+ 10 - 7
Terminal.Gui/Application/MainLoop.cs

@@ -30,9 +30,15 @@ public interface IMainLoopDriver
     /// <summary>Wakes up the <see cref="MainLoop"/> that might be waiting on input, must be thread safe.</summary>
     void Wakeup ();
 
-    bool _forceRead { get; set; }
+    /// <summary>
+    ///     Flag <see cref="WaitForInput"/> to force reading input instead of call <see cref="ManualResetEventSlim.Wait ()"/>.
+    /// </summary>
+    bool ForceRead { get; set; }
 
-    ManualResetEventSlim _waitForInput { get; set; }
+    /// <summary>
+    ///     Switch for waiting for input or signaled to resume.
+    /// </summary>
+    ManualResetEventSlim WaitForInput { get; set; }
 }
 
 /// <summary>The MainLoop monitors timers and idle handlers.</summary>
@@ -134,10 +140,7 @@ public class MainLoop : IDisposable
     /// </remarks>
     internal object AddTimeout (TimeSpan time, Func<bool> callback)
     {
-        if (callback is null)
-        {
-            throw new ArgumentNullException (nameof (callback));
-        }
+        ArgumentNullException.ThrowIfNull (callback);
 
         var timeout = new Timeout { Span = time, Callback = callback };
         AddTimeout (time, timeout);
@@ -276,7 +279,7 @@ public class MainLoop : IDisposable
 
         MainLoopDriver.Iteration ();
 
-        var runIdle = false;
+        bool runIdle;
 
         lock (_idleHandlersLock)
         {

+ 3 - 3
Terminal.Gui/ConsoleDrivers/ConsoleDriver.cs

@@ -62,12 +62,12 @@ public abstract class ConsoleDriver
 
                             WriteRaw (ansiRequest.Request);
 
-                            _mainLoopDriver!._forceRead = true;
+                            _mainLoopDriver!.ForceRead = true;
                         }
 
                         if (!_ansiRequestTokenSource.IsCancellationRequested)
                         {
-                            _mainLoopDriver._waitForInput.Set ();
+                            _mainLoopDriver.WaitForInput.Set ();
 
                             _waitAnsiRequest.Wait (_ansiRequestTokenSource.Token);
                         }
@@ -135,7 +135,7 @@ public abstract class ConsoleDriver
 
             lock (ansiRequest._responseLock)
             {
-                _mainLoopDriver._forceRead = false;
+                _mainLoopDriver.ForceRead = false;
 
                 if (AnsiEscapeSequenceRequests.Statuses.TryPeek (out AnsiEscapeSequenceRequestStatus? request))
                 {

+ 8 - 8
Terminal.Gui/ConsoleDrivers/CursesDriver/UnixMainLoop.cs

@@ -43,7 +43,7 @@ internal class UnixMainLoop (ConsoleDriver consoleDriver) : IMainLoopDriver
     private Pollfd []? _pollMap;
     private readonly ConcurrentQueue<PollData> _pollDataQueue = new ();
     private readonly ManualResetEventSlim _eventReady = new (false);
-    ManualResetEventSlim IMainLoopDriver._waitForInput { get; set; } = new (false);
+    ManualResetEventSlim IMainLoopDriver.WaitForInput { get; set; } = new (false);
     private readonly ManualResetEventSlim _windowSizeChange = new (false);
     private readonly CancellationTokenSource _eventReadyTokenSource = new ();
     private readonly CancellationTokenSource _inputHandlerTokenSource = new ();
@@ -152,7 +152,7 @@ internal class UnixMainLoop (ConsoleDriver consoleDriver) : IMainLoopDriver
         }
     }
 
-    bool IMainLoopDriver._forceRead { get; set; }
+    bool IMainLoopDriver.ForceRead { get; set; }
     private int _retries;
 
     private void CursesInputHandler ()
@@ -161,11 +161,11 @@ internal class UnixMainLoop (ConsoleDriver consoleDriver) : IMainLoopDriver
         {
             try
             {
-                if (!_inputHandlerTokenSource.IsCancellationRequested && !((IMainLoopDriver)this)._forceRead)
+                if (!_inputHandlerTokenSource.IsCancellationRequested && !((IMainLoopDriver)this).ForceRead)
                 {
                     try
                     {
-                        ((IMainLoopDriver)this)._waitForInput.Wait (_inputHandlerTokenSource.Token);
+                        ((IMainLoopDriver)this).WaitForInput.Wait (_inputHandlerTokenSource.Token);
                     }
                     catch (Exception ex)
                     {
@@ -177,7 +177,7 @@ internal class UnixMainLoop (ConsoleDriver consoleDriver) : IMainLoopDriver
                         throw;
                     }
 
-                    ((IMainLoopDriver)this)._waitForInput.Reset ();
+                    ((IMainLoopDriver)this).WaitForInput.Reset ();
                 }
 
                 ProcessInputQueue ();
@@ -191,7 +191,7 @@ internal class UnixMainLoop (ConsoleDriver consoleDriver) : IMainLoopDriver
 
     private void ProcessInputQueue ()
     {
-        if (_pollDataQueue.Count == 0 || ((IMainLoopDriver)this)._forceRead)
+        if (_pollDataQueue.Count == 0 || ((IMainLoopDriver)this).ForceRead)
         {
             while (!_inputHandlerTokenSource.IsCancellationRequested)
             {
@@ -355,7 +355,7 @@ internal class UnixMainLoop (ConsoleDriver consoleDriver) : IMainLoopDriver
 
     bool IMainLoopDriver.EventsPending ()
     {
-        ((IMainLoopDriver)this)._waitForInput.Set ();
+        ((IMainLoopDriver)this).WaitForInput.Set ();
         _windowSizeChange.Set ();
 
         if (_mainLoop!.CheckTimersAndIdleHandlers (out int waitTimeout))
@@ -402,7 +402,7 @@ internal class UnixMainLoop (ConsoleDriver consoleDriver) : IMainLoopDriver
 
         _inputHandlerTokenSource.Cancel ();
         _inputHandlerTokenSource.Dispose ();
-        ((IMainLoopDriver)this)._waitForInput?.Dispose ();
+        ((IMainLoopDriver)this).WaitForInput?.Dispose ();
 
         _windowSizeChange.Dispose();
 

+ 2 - 2
Terminal.Gui/ConsoleDrivers/FakeDriver/FakeMainLoop.cs

@@ -3,8 +3,8 @@
 internal class FakeMainLoop : IMainLoopDriver
 {
     public Action<ConsoleKeyInfo> MockKeyPressed;
-    public bool _forceRead { get; set; }
-    public ManualResetEventSlim _waitForInput { get; set; } = new ();
+    public bool ForceRead { get; set; }
+    public ManualResetEventSlim WaitForInput { get; set; } = new ();
 
     public FakeMainLoop (ConsoleDriver consoleDriver = null)
     {

+ 8 - 8
Terminal.Gui/ConsoleDrivers/NetDriver/NetMainLoop.cs

@@ -21,8 +21,8 @@ internal class NetMainLoop : IMainLoopDriver
     private readonly CancellationTokenSource _inputHandlerTokenSource = new ();
     private readonly ConcurrentQueue<NetEvents.InputResult> _resultQueue = new ();
     private MainLoop? _mainLoop;
-    bool IMainLoopDriver._forceRead { get; set; }
-    ManualResetEventSlim IMainLoopDriver._waitForInput { get; set; } = new (false);
+    bool IMainLoopDriver.ForceRead { get; set; }
+    ManualResetEventSlim IMainLoopDriver.WaitForInput { get; set; } = new (false);
 
     /// <summary>Initializes the class with the console driver.</summary>
     /// <remarks>Passing a consoleDriver is provided to capture windows resizing.</remarks>
@@ -52,7 +52,7 @@ internal class NetMainLoop : IMainLoopDriver
 
     bool IMainLoopDriver.EventsPending ()
     {
-        ((IMainLoopDriver)this)._waitForInput.Set ();
+        ((IMainLoopDriver)this).WaitForInput.Set ();
 
         if (_resultQueue.Count > 0 || _mainLoop!.CheckTimersAndIdleHandlers (out int waitTimeout))
         {
@@ -104,7 +104,7 @@ internal class NetMainLoop : IMainLoopDriver
         _eventReadyTokenSource.Dispose ();
 
         _eventReady.Dispose ();
-        ((IMainLoopDriver)this)._waitForInput.Dispose ();
+        ((IMainLoopDriver)this).WaitForInput.Dispose ();
 
         _resultQueue.Clear ();
         _netEvents?.Dispose ();
@@ -119,11 +119,11 @@ internal class NetMainLoop : IMainLoopDriver
         {
             try
             {
-                if (!_inputHandlerTokenSource.IsCancellationRequested && !((IMainLoopDriver)this)._forceRead)
+                if (!_inputHandlerTokenSource.IsCancellationRequested && !((IMainLoopDriver)this).ForceRead)
                 {
                     try
                     {
-                        ((IMainLoopDriver)this)._waitForInput.Wait (_inputHandlerTokenSource.Token);
+                        ((IMainLoopDriver)this).WaitForInput.Wait (_inputHandlerTokenSource.Token);
                     }
                     catch (Exception ex)
                     {
@@ -135,7 +135,7 @@ internal class NetMainLoop : IMainLoopDriver
                         throw;
                     }
 
-                    ((IMainLoopDriver)this)._waitForInput.Reset ();
+                    ((IMainLoopDriver)this).WaitForInput.Reset ();
                 }
 
                 ProcessInputQueue ();
@@ -149,7 +149,7 @@ internal class NetMainLoop : IMainLoopDriver
 
     private void ProcessInputQueue ()
     {
-        if (_resultQueue?.Count == 0 || ((IMainLoopDriver)this)._forceRead)
+        if (_resultQueue?.Count == 0 || ((IMainLoopDriver)this).ForceRead)
         {
             NetEvents.InputResult? result = _netEvents!.DequeueInput ();
 

+ 8 - 8
Terminal.Gui/ConsoleDrivers/WindowsDriver/WindowsMainLoop.cs

@@ -23,7 +23,7 @@ internal class WindowsMainLoop : IMainLoopDriver
 
     // The records that we keep fetching
     private readonly ConcurrentQueue<WindowsConsole.InputRecord> _resultQueue = new ();
-    ManualResetEventSlim IMainLoopDriver._waitForInput { get; set; } = new (false);
+    ManualResetEventSlim IMainLoopDriver.WaitForInput { get; set; } = new (false);
     private readonly WindowsConsole? _winConsole;
     private CancellationTokenSource _eventReadyTokenSource = new ();
     private readonly CancellationTokenSource _inputHandlerTokenSource = new ();
@@ -59,7 +59,7 @@ internal class WindowsMainLoop : IMainLoopDriver
 
     bool IMainLoopDriver.EventsPending ()
     {
-        ((IMainLoopDriver)this)._waitForInput.Set ();
+        ((IMainLoopDriver)this).WaitForInput.Set ();
 #if HACK_CHECK_WINCHANGED
         _winChange.Set ();
 #endif
@@ -133,7 +133,7 @@ internal class WindowsMainLoop : IMainLoopDriver
             }
         }
 
-        ((IMainLoopDriver)this)._waitForInput?.Dispose ();
+        ((IMainLoopDriver)this).WaitForInput?.Dispose ();
 
         _resultQueue.Clear ();
 
@@ -148,7 +148,7 @@ internal class WindowsMainLoop : IMainLoopDriver
         _mainLoop = null;
     }
 
-    public bool _forceRead { get; set; }
+    public bool ForceRead { get; set; }
 
     private void WindowsInputHandler ()
     {
@@ -156,11 +156,11 @@ internal class WindowsMainLoop : IMainLoopDriver
         {
             try
             {
-                if (_inputHandlerTokenSource.IsCancellationRequested && !_forceRead)
+                if (_inputHandlerTokenSource.IsCancellationRequested && !ForceRead)
                 {
                     try
                     {
-                        ((IMainLoopDriver)this)._waitForInput.Wait (_inputHandlerTokenSource.Token);
+                        ((IMainLoopDriver)this).WaitForInput.Wait (_inputHandlerTokenSource.Token);
                     }
                     catch (Exception ex)
                     {
@@ -172,7 +172,7 @@ internal class WindowsMainLoop : IMainLoopDriver
                         throw;
                     }
 
-                    ((IMainLoopDriver)this)._waitForInput.Reset ();
+                    ((IMainLoopDriver)this).WaitForInput.Reset ();
                 }
 
                 ProcessInputQueue ();
@@ -187,7 +187,7 @@ internal class WindowsMainLoop : IMainLoopDriver
 
     private void ProcessInputQueue ()
     {
-        if (_resultQueue?.Count == 0 || _forceRead)
+        if (_resultQueue?.Count == 0 || ForceRead)
         {
             WindowsConsole.InputRecord? result = _winConsole!.DequeueInput ();
 

+ 2 - 2
UnitTests/Application/MainLoopTests.cs

@@ -953,7 +953,7 @@ public class MainLoopTests
         public void TearDown () { throw new NotImplementedException (); }
         public void Setup (MainLoop mainLoop) { this.mainLoop = mainLoop; }
         public void Wakeup () { throw new NotImplementedException (); }
-        public bool _forceRead { get; set; }
-        public ManualResetEventSlim _waitForInput { get; set; }
+        public bool ForceRead { get; set; }
+        public ManualResetEventSlim WaitForInput { get; set; }
     }
 }