Skip to content

Commit

Permalink
Merge pull request #3382 from tig/v2_3381-WindowsDriver-Spurious-Mous…
Browse files Browse the repository at this point in the history
…eEvents

Fixes #3381. `WindowsDriver` spurious ButtonPressed mouse events
  • Loading branch information
tig committed Apr 4, 2024
2 parents 9937771 + 36401a6 commit dec65ff
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 39 deletions.
25 changes: 20 additions & 5 deletions Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ public readonly override string ToString ()
[Flags]
public enum ButtonState
{
NoButtonPressed = 0,
Button1Pressed = 1,
Button2Pressed = 4,
Button3Pressed = 8,
Expand All @@ -482,6 +483,7 @@ public enum ButtonState
[Flags]
public enum ControlKeyState
{
NoControlKeyPressed = 0,
RightAltPressed = 1,
LeftAltPressed = 2,
RightControlPressed = 4,
Expand All @@ -496,6 +498,7 @@ public enum ControlKeyState
[Flags]
public enum EventFlags
{
NoEvent = 0,
MouseMoved = 1,
DoubleClick = 2,
MouseWheeled = 4,
Expand All @@ -517,7 +520,7 @@ public struct MouseEventRecord
[FieldOffset (12)]
public EventFlags EventFlags;

public readonly override string ToString () { return $"[Mouse({MousePosition},{ButtonState},{ControlKeyState},{EventFlags}"; }
public readonly override string ToString () { return $"[Mouse{MousePosition},{ButtonState},{ControlKeyState},{EventFlags}]"; }
}

public struct WindowBufferSizeRecord
Expand Down Expand Up @@ -1388,7 +1391,7 @@ internal void ProcessInput (WindowsConsole.InputRecord inputEvent)
case WindowsConsole.EventType.Mouse:
MouseEvent me = ToDriverMouse (inputEvent.MouseEvent);

if (me is null)
if (me is null || me.Flags == MouseFlags.None)
{
break;
}
Expand Down Expand Up @@ -1774,8 +1777,7 @@ private MouseEvent ToDriverMouse (WindowsConsole.MouseEventRecord mouseEvent)
{
var mouseFlag = MouseFlags.AllEvents;

//System.Diagnostics.Debug.WriteLine (
// $"X:{mouseEvent.MousePosition.X};Y:{mouseEvent.MousePosition.Y};ButtonState:{mouseEvent.ButtonState};EventFlags:{mouseEvent.EventFlags}");
//Debug.WriteLine ($"ToDriverMouse: {mouseEvent}");

if (_isButtonDoubleClicked || _isOneFingerDoubleClicked)
{
Expand Down Expand Up @@ -2003,7 +2005,7 @@ private MouseEvent ToDriverMouse (WindowsConsole.MouseEventRecord mouseEvent)
else if (mouseEvent is { ButtonState: 0, EventFlags: 0 })
{
// This happens on a double or triple click event.
mouseFlag = 0;
mouseFlag = MouseFlags.None;
}

mouseFlag = SetControlKeyStates (mouseEvent, mouseFlag);
Expand Down Expand Up @@ -2128,6 +2130,19 @@ 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 ();

Expand Down
13 changes: 1 addition & 12 deletions UICatalog/Scenarios/CharacterMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -883,20 +883,9 @@ private void ShowDetails ()
}
catch (HttpRequestException e)
{
(s as Dialog).Text = e.Message;
Application.Invoke (
() =>
{
spinner.Visible = false;
errorLabel.Text = e.Message;
errorLabel.ColorScheme = Colors.ColorSchemes ["Error"];
errorLabel.Visible = true;
}
);
Application.Invoke (() => waitIndicator.RequestStop ());
}
(s as Dialog)?.RequestStop ();
};
Application.Run (waitIndicator);
waitIndicator.Dispose ();
Expand Down
101 changes: 79 additions & 22 deletions UICatalog/Scenarios/Mouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,102 @@ namespace UICatalog.Scenarios;
[ScenarioCategory ("Mouse and Keyboard")]
public class Mouse : Scenario
{
public override void Setup ()
public override void Main ()
{
Application.Init ();
Window win = new ()
{
Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
};

Label ml;
var count = 0;
ml = new Label { X = 1, Y = 1, Text = "Mouse: " };
List<string> rme = new ();

Win.Add (ml);
win.Add (ml);

CheckBox cbWantContinuousPresses = new CheckBox ()
{
X = 0,
Y = Pos.Bottom(ml) + 1,
Title = "_Want Continuous Button Presses",
};
cbWantContinuousPresses.Toggled += (s,e) =>
{
win.WantContinuousButtonPressed = !win.WantContinuousButtonPressed;
};

win.Add (cbWantContinuousPresses);

var demo = new MouseDemo ()
{
X = 0,
Y = Pos.Bottom (cbWantContinuousPresses) + 1,
Width = 20,
Height = 5,
Text = "Enter/Leave Demo",
TextAlignment = TextAlignment.Centered,
VerticalTextAlignment = VerticalTextAlignment.Middle,
ColorScheme = Colors.ColorSchemes ["Dialog"],
};
win.Add (demo);

var logList = new ListView
var label = new Label ()
{
X = Pos.AnchorEnd (41),
Y = 0,
Width = 41,
Text = "_App Events:",
X = 0,
Y = Pos.Bottom (demo),
};
List<string> appLogList = new ();
var appLog = new ListView
{
X = Pos.Left (label),
Y = Pos.Bottom (label),
Width = Dim.Percent(49),
Height = Dim.Fill (),
ColorScheme = Colors.ColorSchemes ["TopLevel"],
Source = new ListWrapper (rme)
Source = new ListWrapper (appLogList)
};
Win.Add (logList);
win.Add (label, appLog);

Application.MouseEvent += (sender, a) =>
{
ml.Text = $"Mouse: ({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags} {count}";
rme.Add ($"({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags} {count++}");
logList.MoveDown ();
ml.Text = $"MouseEvent: ({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags} {count}";
appLogList.Add ($"({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags} {count++}");
appLog.MoveDown ();
};

Win.Add (new MouseDemo ()

label = new Label ()
{
X = 0,
Y = 3,
Width = 15,
Height = 10,
Text = "Mouse Demo",
TextAlignment = TextAlignment.Centered,
VerticalTextAlignment = VerticalTextAlignment.Middle,
ColorScheme = Colors.ColorSchemes ["Dialog"],
});
Text = "_Window Events:",
X = Pos.Percent(50),
Y = Pos.Bottom (demo),
};
List<string> winLogList = new ();
var winLog = new ListView
{
X = Pos.Left(label),
Y = Pos.Bottom (label),
Width = Dim.Percent (50),
Height = Dim.Fill (),
ColorScheme = Colors.ColorSchemes ["TopLevel"],
Source = new ListWrapper (winLogList)
};
win.Add (label, winLog);
win.MouseEvent += (sender, a) =>
{
winLogList.Add ($"MouseEvent: ({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags} {count++}");
winLog.MoveDown ();
};
win.MouseClick += (sender, a) =>
{
winLogList.Add ($"MouseClick: ({a.MouseEvent.X},{a.MouseEvent.Y}) - {a.MouseEvent.Flags} {count++}");
winLog.MoveDown ();
};

Application.Run (win);
win.Dispose ();
}

public class MouseDemo : View
Expand Down

0 comments on commit dec65ff

Please sign in to comment.