Kaynağa Gözat

Merge branch 'v2_develop' into v2_3370_continuous_button

Tig 1 yıl önce
ebeveyn
işleme
d71554d1ef
1 değiştirilmiş dosya ile 48 ekleme ve 26 silme
  1. 48 26
      Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

+ 48 - 26
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -838,6 +838,29 @@ internal class WindowsConsole
     [DllImport ("kernel32.dll", SetLastError = true)]
     [DllImport ("kernel32.dll", SetLastError = true)]
     private static extern bool GetNumberOfConsoleInputEvents (nint handle, out uint lpcNumberOfEvents);
     private static extern bool GetNumberOfConsoleInputEvents (nint handle, out uint lpcNumberOfEvents);
 
 
+    internal uint GetNumberOfConsoleInputEvents ()
+    {
+        if (!GetNumberOfConsoleInputEvents (_inputHandle, out uint numOfEvents))
+        {
+            Console.WriteLine ($"Error: {Marshal.GetLastWin32Error ()}");
+
+            return 0;
+        }
+
+        return numOfEvents;
+    }
+
+    [DllImport ("kernel32.dll", SetLastError = true)]
+    private static extern bool FlushConsoleInputBuffer (nint handle);
+
+    internal void FlushConsoleInputBuffer ()
+    {
+        if (!FlushConsoleInputBuffer (_inputHandle))
+        {
+            Console.WriteLine ($"Error: {Marshal.GetLastWin32Error ()}");
+        }
+    }
+
     public InputRecord [] ReadConsoleInput ()
     public InputRecord [] ReadConsoleInput ()
     {
     {
         const int bufferSize = 1;
         const int bufferSize = 1;
@@ -1281,7 +1304,7 @@ internal class WindowsDriver : ConsoleDriver
         {
         {
 #if HACK_CHECK_WINCHANGED
 #if HACK_CHECK_WINCHANGED
 
 
-            //_mainLoop.WinChanged -= ChangeWin;
+            _mainLoopDriver.WinChanged -= ChangeWin;
 #endif
 #endif
         }
         }
 
 
@@ -1696,13 +1719,7 @@ internal class WindowsDriver : ConsoleDriver
 
 
     private async Task ProcessButtonDoubleClickedAsync ()
     private async Task ProcessButtonDoubleClickedAsync ()
     {
     {
-        // Only delay if there's not a view wanting continuous button presses
-        if (Application.WantContinuousButtonPressedView is null)
-        {
-            // QUESTION: Why 300ms?
-            await Task.Delay (300);
-        }
-
+        await Task.Delay (200);
         _isButtonDoubleClicked = false;
         _isButtonDoubleClicked = false;
         _isOneFingerDoubleClicked = false;
         _isOneFingerDoubleClicked = false;
 
 
@@ -2147,34 +2164,32 @@ internal class WindowsMainLoop : IMainLoopDriver
 
 
     void IMainLoopDriver.TearDown ()
     void IMainLoopDriver.TearDown ()
     {
     {
-        // Eat any outstanding events. See #
-        //var records = 
-            _winConsole.ReadConsoleInput ();
-
-        //if (records != null)
-        //{
-        //    foreach (var rec in records)
-        //    {
-        //        Debug.WriteLine ($"Teardown: {rec.ToString ()}");
-        //        //Debug.Assert (rec is not { EventType: WindowsConsole.EventType.Mouse, MouseEvent.ButtonState: WindowsConsole.ButtonState.Button1Pressed });
-        //    }
-        //}
-
         _inputHandlerTokenSource?.Cancel ();
         _inputHandlerTokenSource?.Cancel ();
         _inputHandlerTokenSource?.Dispose ();
         _inputHandlerTokenSource?.Dispose ();
 
 
+        if (_winConsole is { })
+        {
+            var numOfEvents = _winConsole.GetNumberOfConsoleInputEvents ();
+
+            if (numOfEvents > 0)
+            {
+                _winConsole.FlushConsoleInputBuffer ();
+                //Debug.WriteLine ($"Flushed {numOfEvents} events.");
+            }
+        }
+
+        _waitForProbe?.Dispose ();
+
+        _resultQueue?.Clear ();
+
         _eventReadyTokenSource?.Cancel ();
         _eventReadyTokenSource?.Cancel ();
         _eventReadyTokenSource?.Dispose ();
         _eventReadyTokenSource?.Dispose ();
         _eventReady?.Dispose ();
         _eventReady?.Dispose ();
 
 
-        _resultQueue?.Clear ();
-
 #if HACK_CHECK_WINCHANGED
 #if HACK_CHECK_WINCHANGED
         _winChange?.Dispose ();
         _winChange?.Dispose ();
 #endif
 #endif
 
 
-        //_waitForProbe?.Dispose ();
-
         _mainLoop = null;
         _mainLoop = null;
     }
     }
 
 
@@ -2191,11 +2206,18 @@ internal class WindowsMainLoop : IMainLoopDriver
             }
             }
             catch (OperationCanceledException)
             catch (OperationCanceledException)
             {
             {
+                // Wakes the _waitForProbe if it's waiting
+                _waitForProbe.Set ();
                 return;
                 return;
             }
             }
             finally
             finally
             {
             {
-                _waitForProbe.Reset ();
+                // If IsCancellationRequested is true the code after
+                // the `finally` block will not be executed.
+                if (!_inputHandlerTokenSource.IsCancellationRequested)
+                {
+                    _waitForProbe.Reset ();
+                }
             }
             }
 
 
             if (_resultQueue?.Count == 0)
             if (_resultQueue?.Count == 0)