diff --git a/Terminal.Gui/Application/Application.Mouse.cs b/Terminal.Gui/Application/Application.Mouse.cs index 3db58fc23..5bbef9dde 100644 --- a/Terminal.Gui/Application/Application.Mouse.cs +++ b/Terminal.Gui/Application/Application.Mouse.cs @@ -320,7 +320,7 @@ public static partial class Application // Mouse handling Point frameLoc = adornmentView.ScreenToFrame (screenPosition); if (adornmentView.Parent is { } && !adornmentView.Contains (frameLoc)) { - view.NewMouseLeaveEvent (me); + view.NewMouseLeaveEvent (); } } else @@ -328,7 +328,7 @@ public static partial class Application // Mouse handling Point superViewLoc = view.SuperView?.ScreenToViewport (screenPosition) ?? screenPosition; if (!view.Contains (superViewLoc)) { - view.NewMouseLeaveEvent (me); + view.NewMouseLeaveEvent (); } } } diff --git a/Terminal.Gui/View/View.Mouse.cs b/Terminal.Gui/View/View.Mouse.cs index fdfd17286..040185876 100644 --- a/Terminal.Gui/View/View.Mouse.cs +++ b/Terminal.Gui/View/View.Mouse.cs @@ -39,9 +39,6 @@ public partial class View // Mouse APIs /// public event EventHandler? MouseEvent; - /// Event fired when the mouse leaves the View's . - public event EventHandler? MouseLeave; - /// /// Processes a . This method is called by when a mouse /// event occurs. @@ -180,7 +177,7 @@ public partial class View // Mouse APIs /// /// Called when the mouse moves over the View's and no other non-Subview occludes it. will - /// be raised when the mouse is no longer over the . + /// be raised when the mouse is no longer over the . /// /// /// @@ -230,6 +227,60 @@ public partial class View // Mouse APIs /// public event EventHandler? MouseEnter; + /// + /// INTERNAL Called by when the mouse leaves , or is occluded by another non-SubView. + /// + /// + /// + /// This method calls and raises the event. + /// + /// + /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's . + /// + /// + /// See for more information. + /// + /// + internal void NewMouseLeaveEvent () + { + OnMouseLeave (); + + MouseLeave?.Invoke (this, EventArgs.Empty); + +#if HOVER + if (HighlightStyle.HasFlag (HighlightStyle.Hover)) + { + SetHighlight (HighlightStyle.None); + } +#endif + } + + /// + /// Called when the mouse moves outside View's , or is occluded by another non-SubView. + /// + /// + /// + /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's . + /// + /// + /// See for more information. + /// + /// + protected virtual void OnMouseLeave () { } + + /// + /// Raised when the mouse moves outside View's , or is occluded by another non-SubView. + /// + /// + /// + /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's . + /// + /// + /// See for more information. + /// + /// + public event EventHandler? MouseLeave; + #endregion MouseEnterLeave /// Called when a mouse event occurs within the view's . @@ -249,22 +300,6 @@ public partial class View // Mouse APIs return args.Handled; } - /// - /// Called by when a mouse leaves . The view will - /// no longer receive mouse events. - /// - /// - /// - /// Override this method or subscribe to to change the default leave behavior. - /// - /// - /// The coordinates are relative to . - /// - /// - /// - /// , if the event was handled, otherwise. - protected internal virtual bool OnMouseLeave (MouseEvent mouseEvent) { return false; } - /// /// Called when the view is to be highlighted. /// @@ -385,45 +420,6 @@ public partial class View // Mouse APIs return false; } - /// - /// INTERNAL Called by when the mouse leaves . - /// - /// - /// - /// A view must be visible to receive Enter events (Leave events are always received). - /// - /// - /// This method calls to raise the event. - /// - /// - /// Adornments receive MouseEnter/Leave events when the mouse is over the Adornment's . - /// - /// - /// See for more information. - /// - /// - /// - /// if the event was handled, otherwise. - internal bool? NewMouseLeaveEvent (MouseEvent mouseEvent) - { - if (OnMouseLeave (mouseEvent)) - { - return true; - } - - var args = new MouseEventEventArgs (mouseEvent); - MouseLeave?.Invoke (this, args); - -#if HOVER - if (HighlightStyle.HasFlag (HighlightStyle.Hover)) - { - SetHighlight (HighlightStyle.None); - } -#endif - - return args.Handled; - } - /// /// Enables the highlight for the view when the mouse is pressed. Called from OnMouseEvent. /// diff --git a/Terminal.Gui/Views/ScrollView.cs b/Terminal.Gui/Views/ScrollView.cs index 2b5ca2855..11e929ceb 100644 --- a/Terminal.Gui/Views/ScrollView.cs +++ b/Terminal.Gui/Views/ScrollView.cs @@ -747,7 +747,7 @@ public class ScrollView : View private void View_MouseEnter (object sender, CancelEventArgs e) { Application.GrabMouse (this); } - private void View_MouseLeave (object sender, MouseEventEventArgs e) + private void View_MouseLeave (object sender, EventArgs e) { if (Application.MouseGrabView is { } && Application.MouseGrabView != this && Application.MouseGrabView != _vertical && Application.MouseGrabView != _horizontal) { diff --git a/UICatalog/Scenarios/Mouse.cs b/UICatalog/Scenarios/Mouse.cs index ea9eab6d5..fa7254613 100644 --- a/UICatalog/Scenarios/Mouse.cs +++ b/UICatalog/Scenarios/Mouse.cs @@ -251,7 +251,7 @@ public class Mouse : Scenario Padding.ColorScheme = Colors.ColorSchemes ["Error"]; } - void PaddingOnMouseLeave (object o, MouseEventEventArgs mouseEventEventArgs) + void PaddingOnMouseLeave (object o, EventArgs e) { Padding.ColorScheme = Colors.ColorSchemes ["Dialog"]; } diff --git a/UnitTests/Application/Mouse/ApplicationMouseEnterLeaveTests.cs b/UnitTests/Application/Mouse/ApplicationMouseEnterLeaveTests.cs index 5b8cb1fb8..3f48a3512 100644 --- a/UnitTests/Application/Mouse/ApplicationMouseEnterLeaveTests.cs +++ b/UnitTests/Application/Mouse/ApplicationMouseEnterLeaveTests.cs @@ -29,13 +29,11 @@ public class ApplicationMouseEnterLeaveTests return eventArgs.Cancel; } - protected internal override bool OnMouseLeave (MouseEvent mouseEvent) + protected override void OnMouseLeave () { OnMouseLeaveCalled++; - base.OnMouseLeave (mouseEvent); - - return mouseEvent.Handled; + base.OnMouseLeave (); } } diff --git a/UnitTests/View/Mouse/MouseEnterLeaveTests.cs b/UnitTests/View/Mouse/MouseEnterLeaveTests.cs index ea28c291d..28c002eba 100644 --- a/UnitTests/View/Mouse/MouseEnterLeaveTests.cs +++ b/UnitTests/View/Mouse/MouseEnterLeaveTests.cs @@ -30,13 +30,11 @@ public class MouseEnterLeaveTests return eventArgs.Cancel; } - protected internal override bool OnMouseLeave (MouseEvent mouseEvent) + protected override void OnMouseLeave () { OnMouseLeaveCalled = true; - base.OnMouseLeave (mouseEvent); - - return mouseEvent.Handled; + base.OnMouseLeave (); } public bool MouseEnterRaised { get; private set; } @@ -52,7 +50,7 @@ public class MouseEnterLeaveTests } } - private void OnMouseLeaveHandler (object s, MouseEventEventArgs e) { MouseLeaveRaised = true; } + private void OnMouseLeaveHandler (object s, EventArgs e) { MouseLeaveRaised = true; } } [Fact] @@ -141,11 +139,10 @@ public class MouseEnterLeaveTests var mouseEvent = new MouseEvent (); // Act - bool? cancelled = view.NewMouseLeaveEvent (mouseEvent); + view.NewMouseLeaveEvent (); // Assert Assert.True (view.OnMouseLeaveCalled); - Assert.False (cancelled); Assert.False (mouseEvent.Handled); // Cleanup @@ -165,11 +162,10 @@ public class MouseEnterLeaveTests var mouseEvent = new MouseEvent (); // Act - bool? cancelled = view.NewMouseLeaveEvent (mouseEvent); + view.NewMouseLeaveEvent (); // Assert Assert.True (view.OnMouseLeaveCalled); - Assert.False (cancelled); Assert.False (mouseEvent.Handled); // Cleanup @@ -263,11 +259,10 @@ public class MouseEnterLeaveTests var mouseEvent = new MouseEvent (); // Act - bool? cancelled = view.NewMouseLeaveEvent (mouseEvent); + view.NewMouseLeaveEvent (); // Assert Assert.True (view.MouseLeaveRaised); - Assert.False (cancelled); Assert.False (mouseEvent.Handled); // Cleanup @@ -287,11 +282,10 @@ public class MouseEnterLeaveTests var mouseEvent = new MouseEvent (); // Act - bool? cancelled = view.NewMouseLeaveEvent (mouseEvent); + view.NewMouseLeaveEvent (); // Assert Assert.True (view.MouseLeaveRaised); - Assert.False (cancelled); Assert.False (mouseEvent.Handled); // Cleanup diff --git a/docfx/docs/mouse.md b/docfx/docs/mouse.md index 4a7d9f6e8..63ef13696 100644 --- a/docfx/docs/mouse.md +++ b/docfx/docs/mouse.md @@ -25,3 +25,8 @@ When the user does something with the mouse, the `ConsoleDriver` maps the platfo ## **Global Mouse Handling** The @Terminal.Gui.Application.MouseEvent event can be used if an application wishes to receive all mouse events. + +## Mouse Enter/Leave Events + +The @Terminal.Gui.View.MouseEnter and @Terminal.Gui.View.MouseLeave events enable a View to take action when the mouse is over the view. Internally, this is used to enable @Terminal.Gui.View.Highlight. +