Change MouseEvent to a mutable class (previously struct) and added Handled

This commit is contained in:
tznind
2022-10-19 13:57:53 +01:00
parent 67839d108a
commit b20483cc68
4 changed files with 30 additions and 25 deletions

View File

@@ -638,18 +638,6 @@ namespace Terminal.Gui {
UnGrabbedMouse?.Invoke (view);
}
/// <summary>
/// <para>
/// Cancellable overload of <see cref="RootMouseEvent"/>.
/// </para>
/// <para>
/// Called for new MouseEvent events before any processing is performed or
/// views evaluate. Use for global mouse handling and/or debugging.
/// </para>
/// <para>Return true to suppress the MouseEvent event</para>
/// </summary>
public static Func<MouseEvent, bool> RootMouseEventCancellable;
/// <summary>
/// Merely a debugging aid to see the raw mouse events
/// </summary>
@@ -683,7 +671,7 @@ namespace Terminal.Gui {
}
RootMouseEvent?.Invoke (me);
if (RootMouseEventCancellable?.Invoke (me) ?? false) {
if (me.Handled) {
return;
}

View File

@@ -708,7 +708,7 @@ namespace Terminal.Gui {
/// <summary>
/// Describes a mouse event
/// </summary>
public struct MouseEvent {
public class MouseEvent {
/// <summary>
/// The X (column) location for the mouse event.
/// </summary>
@@ -739,6 +739,12 @@ namespace Terminal.Gui {
/// </summary>
public View View;
/// <summary>
/// Indicates if the current mouse event has already been processed and the driver should stop notifying any other event subscriber.
/// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
/// </summary>
public bool Handled { get; set; }
/// <summary>
/// Returns a <see cref="T:System.String"/> that represents the current <see cref="MouseEvent"/>.
/// </summary>

View File

@@ -2755,11 +2755,15 @@ namespace Terminal.Gui {
/// The <see cref="MouseEvent"/> for the event.
/// </summary>
public MouseEvent MouseEvent { get; set; }
/// <summary>
/// Indicates if the current mouse event has already been processed and the driver should stop notifying any other event subscriber.
/// Its important to set this value to true specially when updating any View's layout from inside the subscriber method.
/// </summary>
public bool Handled { get; set; }
public bool Handled {
get => MouseEvent.Handled;
set => MouseEvent.Handled = value;
}
}
/// <inheritdoc/>

View File

@@ -1207,7 +1207,7 @@ namespace Terminal.Gui.Views {
[AutoInitShutdown]
public void Test_RootMouseKeyEvent_Cancel ()
{
Application.RootMouseEventCancellable += SuppressRightClick;
Application.RootMouseEvent += SuppressRightClick;
var tf = new TextField () { Width = 10 };
int clickCounter = 0;
@@ -1227,15 +1227,24 @@ namespace Terminal.Gui.Views {
processMouseEventMethod.Invoke (null, new object [] { mouseEvent });
Assert.Equal (1, clickCounter);
mouseEvent.Flags = MouseFlags.Button3Clicked;
// should be ignored because of SuppressRightClick callback
// Get a fresh instance that represents a right click.
// Should be ignored because of SuppressRightClick callback
mouseEvent = new MouseEvent {
Flags = MouseFlags.Button3Clicked,
View = tf
};
processMouseEventMethod.Invoke (null, new object [] { mouseEvent });
Assert.Equal (1, clickCounter);
Application.RootMouseEventCancellable -= SuppressRightClick;
Application.RootMouseEvent -= SuppressRightClick;
// Get a fresh instance that represents a right click.
// Should no longer be ignored as the callback was removed
mouseEvent = new MouseEvent {
Flags = MouseFlags.Button3Clicked,
View = tf
};
// should no longer be ignored as the callback was removed
processMouseEventMethod.Invoke (null, new object [] { mouseEvent });
Assert.Equal (2, clickCounter);
}
@@ -1249,12 +1258,10 @@ namespace Terminal.Gui.Views {
return false;
}
private bool SuppressRightClick (MouseEvent arg)
private void SuppressRightClick (MouseEvent arg)
{
if (arg.Flags.HasFlag (MouseFlags.Button3Clicked))
return true;
return false;
arg.Handled = true;
}
[Fact, AutoInitShutdown]