Skip to content

Commit

Permalink
Merge branch 'v2_develop' into v2_3370_continuous_button
Browse files Browse the repository at this point in the history
  • Loading branch information
tig authored Apr 8, 2024
2 parents 102269e + ef6e22c commit d71554d
Showing 1 changed file with 48 additions and 26 deletions.
74 changes: 48 additions & 26 deletions Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,29 @@ nint screenBufferData
[DllImport ("kernel32.dll", SetLastError = true)]
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 ()
{
const int bufferSize = 1;
Expand Down Expand Up @@ -1281,7 +1304,7 @@ internal override void End ()
{
#if HACK_CHECK_WINCHANGED

//_mainLoop.WinChanged -= ChangeWin;
_mainLoopDriver.WinChanged -= ChangeWin;
#endif
}

Expand Down Expand Up @@ -1696,13 +1719,7 @@ private MouseFlags ProcessButtonClick (WindowsConsole.MouseEventRecord mouseEven

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;
_isOneFingerDoubleClicked = false;

Expand Down Expand Up @@ -2147,34 +2164,32 @@ void IMainLoopDriver.Iteration ()

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?.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?.Dispose ();
_eventReady?.Dispose ();

_resultQueue?.Clear ();

#if HACK_CHECK_WINCHANGED
_winChange?.Dispose ();
#endif

//_waitForProbe?.Dispose ();

_mainLoop = null;
}

Expand All @@ -2191,11 +2206,18 @@ private void WindowsInputHandler ()
}
catch (OperationCanceledException)
{
// Wakes the _waitForProbe if it's waiting
_waitForProbe.Set ();
return;
}
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)
Expand Down

0 comments on commit d71554d

Please sign in to comment.