diff --git a/Terminal.Gui/Core/Application.cs b/Terminal.Gui/Core/Application.cs index 608b4c991..ea47e6b51 100644 --- a/Terminal.Gui/Core/Application.cs +++ b/Terminal.Gui/Core/Application.cs @@ -638,18 +638,6 @@ namespace Terminal.Gui { UnGrabbedMouse?.Invoke (view); } - /// - /// - /// Cancellable overload of . - /// - /// - /// Called for new MouseEvent events before any processing is performed or - /// views evaluate. Use for global mouse handling and/or debugging. - /// - /// Return true to suppress the MouseEvent event - /// - public static Func RootMouseEventCancellable; - /// /// Merely a debugging aid to see the raw mouse events /// @@ -683,7 +671,7 @@ namespace Terminal.Gui { } RootMouseEvent?.Invoke (me); - if (RootMouseEventCancellable?.Invoke (me) ?? false) { + if (me.Handled) { return; } diff --git a/Terminal.Gui/Core/Event.cs b/Terminal.Gui/Core/Event.cs index 181724b1c..35212ed62 100644 --- a/Terminal.Gui/Core/Event.cs +++ b/Terminal.Gui/Core/Event.cs @@ -708,7 +708,7 @@ namespace Terminal.Gui { /// /// Describes a mouse event /// - public struct MouseEvent { + public class MouseEvent { /// /// The X (column) location for the mouse event. /// @@ -739,6 +739,12 @@ namespace Terminal.Gui { /// public View View; + /// + /// 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. + /// + public bool Handled { get; set; } + /// /// Returns a that represents the current . /// diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index ea9d80885..0b7591d5a 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -2755,11 +2755,15 @@ namespace Terminal.Gui { /// The for the event. /// public MouseEvent MouseEvent { get; set; } + /// /// 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. /// - public bool Handled { get; set; } + public bool Handled { + get => MouseEvent.Handled; + set => MouseEvent.Handled = value; + } } /// diff --git a/UnitTests/TextFieldTests.cs b/UnitTests/TextFieldTests.cs index bd317447f..c3bf8961e 100644 --- a/UnitTests/TextFieldTests.cs +++ b/UnitTests/TextFieldTests.cs @@ -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]