mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2026-01-02 01:03:29 +01:00
Non breaking implementation of cancellable root mouse events
This commit is contained in:
@@ -1203,10 +1203,52 @@ namespace Terminal.Gui.Views {
|
||||
Application.Driver.SendKeys ('j', ConsoleKey.A, false, false, false);
|
||||
Assert.Equal ("aj", tf.Text.ToString ());
|
||||
}
|
||||
[Fact]
|
||||
[AutoInitShutdown]
|
||||
public void Test_RootMouseKeyEvent_Cancel ()
|
||||
{
|
||||
Application.RootMouseEventCancellable += SuppressRightClick;
|
||||
|
||||
var tf = new TextField () { Width = 10 };
|
||||
int clickCounter = 0;
|
||||
tf.MouseClick += (m) => { clickCounter++; };
|
||||
|
||||
Application.Top.Add (tf);
|
||||
Application.Begin (Application.Top);
|
||||
|
||||
var processMouseEventMethod = typeof (Application).GetMethod ("ProcessMouseEvent", BindingFlags.Static | BindingFlags.NonPublic)
|
||||
?? throw new Exception ("Expected private method not found 'ProcessMouseEvent', this method was used for testing mouse behaviours");
|
||||
|
||||
var mouseEvent = new MouseEvent {
|
||||
Flags = MouseFlags.Button1Clicked,
|
||||
View = tf
|
||||
};
|
||||
|
||||
processMouseEventMethod.Invoke (null, new object [] { mouseEvent });
|
||||
Assert.Equal (1, clickCounter);
|
||||
|
||||
mouseEvent.Flags = MouseFlags.Button3Clicked;
|
||||
|
||||
// should be ignored because of SuppressRightClick callback
|
||||
processMouseEventMethod.Invoke (null, new object [] { mouseEvent });
|
||||
Assert.Equal (1, clickCounter);
|
||||
|
||||
Application.RootMouseEventCancellable -= SuppressRightClick;
|
||||
|
||||
// should no longer be ignored as the callback was removed
|
||||
processMouseEventMethod.Invoke (null, new object [] { mouseEvent });
|
||||
Assert.Equal (2, clickCounter);
|
||||
}
|
||||
|
||||
|
||||
private bool SuppressKey (KeyEvent arg)
|
||||
{
|
||||
if (arg.KeyValue == 'j')
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool SuppressRightClick (MouseEvent arg)
|
||||
{
|
||||
if (arg.Flags.HasFlag (MouseFlags.Button3Clicked))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user