Browse Source

Fixes #3183 - `OnMouseEvent` calling `OnMouseClick` too often (#3184)

* Removed resharper settings from editorconfig

* Fixed bug; updated bad unit tests

* Fixed API doc issue

* Updated nuget
Tig 1 year ago
parent
commit
a7ea48eb62

+ 2 - 2
ReactiveExample/ReactiveExample.csproj

@@ -11,8 +11,8 @@
     <InformationalVersion>2.0</InformationalVersion>
   </PropertyGroup>
   <ItemGroup>
-    <PackageReference Include="ReactiveUI.Fody" Version="19.5.31" />
-    <PackageReference Include="ReactiveUI" Version="19.5.31" />
+    <PackageReference Include="ReactiveUI.Fody" Version="19.5.39" />
+    <PackageReference Include="ReactiveUI" Version="19.5.39" />
     <PackageReference Include="ReactiveMarbles.ObservableEvents.SourceGenerator" Version="1.3.1" PrivateAssets="all" />
   </ItemGroup>
   <ItemGroup>

+ 1 - 1
Terminal.Gui/Terminal.Gui.csproj

@@ -40,7 +40,7 @@
   <ItemGroup>
     <PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="all" />
     <PackageReference Include="System.IO.Abstractions" Version="20.0.4" />
-    <PackageReference Include="System.Text.Json" Version="8.0.0" />
+    <PackageReference Include="System.Text.Json" Version="8.0.1" />
     <PackageReference Include="System.Management" Version="8.0.0" />
     <PackageReference Include="Wcwidth" Version="2.0.0" />
     <!-- Enable Nuget Source Link for github -->

+ 98 - 92
Terminal.Gui/View/ViewMouse.cs

@@ -1,114 +1,120 @@
 using System;
-using System.Collections.Generic;
-using System.ComponentModel;
-using System.Linq;
-using System.Text;
-
-namespace Terminal.Gui {
-	public partial class View  {
-		/// <summary>
-		/// Event fired when the view receives the mouse event for the first time.
-		/// </summary>
-		public event EventHandler<MouseEventEventArgs> MouseEnter;
-
-		/// <summary>
-		/// Event fired when the view receives a mouse event for the last time.
-		/// </summary>
-		public event EventHandler<MouseEventEventArgs> MouseLeave;
-
-		/// <summary>
-		/// Event fired when a mouse event is generated.
-		/// </summary>
-		public event EventHandler<MouseEventEventArgs> MouseClick;
-
-		/// <inheritdoc/>
-		public override bool OnMouseEnter (MouseEvent mouseEvent)
-		{
-			if (!Enabled) {
-				return true;
-			}
-
-			if (!CanBeVisible (this)) {
-				return false;
-			}
 
-			var args = new MouseEventEventArgs (mouseEvent);
-			MouseEnter?.Invoke (this, args);
-
-			return args.Handled || base.OnMouseEnter (mouseEvent);
+namespace Terminal.Gui; 
+
+public partial class View {
+
+	/// <summary>
+	/// Gets or sets a value indicating whether this <see cref="View"/> wants mouse position reports.
+	/// </summary>
+	/// <value><see langword="true"/> if want mouse position reports; otherwise, <see langword="false"/>.</value>
+	public virtual bool WantMousePositionReports { get; set; }
+
+	/// <summary>
+	/// Gets or sets a value indicating whether this <see cref="View"/> want continuous button pressed event.
+	/// </summary>
+	public virtual bool WantContinuousButtonPressed { get; set; }
+
+	/// <summary>
+	/// Event fired when the view receives the mouse event for the first time.
+	/// </summary>
+	public event EventHandler<MouseEventEventArgs> MouseEnter;
+
+	/// <summary>
+	/// Event fired when the view receives a mouse event for the last time.
+	/// </summary>
+	public event EventHandler<MouseEventEventArgs> MouseLeave;
+
+	/// <summary>
+	/// Event fired when a mouse event is generated.
+	/// </summary>
+	public event EventHandler<MouseEventEventArgs> MouseClick;
+
+	/// <inheritdoc/>
+	public override bool OnMouseEnter (MouseEvent mouseEvent)
+	{
+		if (!Enabled) {
+			return true;
 		}
 
-		/// <inheritdoc/>
-		public override bool OnMouseLeave (MouseEvent mouseEvent)
-		{
-			if (!Enabled) {
-				return true;
-			}
+		if (!CanBeVisible (this)) {
+			return false;
+		}
 
-			if (!CanBeVisible (this)) {
-				return false;
-			}
+		var args = new MouseEventEventArgs (mouseEvent);
+		MouseEnter?.Invoke (this, args);
 
-			var args = new MouseEventEventArgs (mouseEvent);
-			MouseLeave?.Invoke (this, args);
+		return args.Handled || base.OnMouseEnter (mouseEvent);
+	}
 
-			return args.Handled || base.OnMouseLeave (mouseEvent);
+	/// <inheritdoc/>
+	public override bool OnMouseLeave (MouseEvent mouseEvent)
+	{
+		if (!Enabled) {
+			return true;
 		}
 
-		/// <summary>
-		/// Method invoked when a mouse event is generated
-		/// </summary>
-		/// <param name="mouseEvent"></param>
-		/// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
-		public virtual bool OnMouseEvent (MouseEvent mouseEvent)
-		{
-			if (!Enabled) {
-				return true;
-			}
+		if (!CanBeVisible (this)) {
+			return false;
+		}
 
-			if (!CanBeVisible (this)) {
-				return false;
-			}
+		var args = new MouseEventEventArgs (mouseEvent);
+		MouseLeave?.Invoke (this, args);
 
-			var args = new MouseEventEventArgs (mouseEvent);
-			if (OnMouseClick (args))
-				return true;
-			if (MouseEvent (mouseEvent))
-				return true;
+		return args.Handled || base.OnMouseLeave (mouseEvent);
+	}
 
-			if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
-				if (CanFocus && !HasFocus && SuperView != null) {
-					SuperView.SetFocus (this);
-					SetNeedsDisplay ();
-				}
+	/// <summary>
+	/// Method invoked when a mouse event is generated
+	/// </summary>
+	/// <param name="mouseEvent"></param>
+	/// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
+	public virtual bool OnMouseEvent (MouseEvent mouseEvent)
+	{
+		if (!Enabled) {
+			return true;
+		}
 
-				return true;
-			}
+		if (!CanBeVisible (this)) {
 			return false;
 		}
 
-		/// <summary>
-		/// Invokes the MouseClick event.
-		/// </summary>
-		protected bool OnMouseClick (MouseEventEventArgs args)
-		{
-			if (!Enabled) {
-				return true;
+		var args = new MouseEventEventArgs (mouseEvent);
+		if (MouseEvent (mouseEvent)) {
+			return true;
+		}
+
+		if (mouseEvent.Flags == MouseFlags.Button1Clicked) {
+			if (CanFocus && !HasFocus && SuperView != null) {
+				SuperView.SetFocus (this);
+				SetNeedsDisplay ();
 			}
 
-			MouseClick?.Invoke (this, args);
-			return args.Handled;
+			return OnMouseClick (args);
+		}
+		if (mouseEvent.Flags == MouseFlags.Button2Clicked) {
+			return OnMouseClick (args);
+		}
+		if (mouseEvent.Flags == MouseFlags.Button3Clicked) {
+			return OnMouseClick (args);
+		}
+		if (mouseEvent.Flags == MouseFlags.Button4Clicked) {
+			return OnMouseClick (args);
 		}
 
-		/// <summary>
-		/// Gets or sets a value indicating whether this <see cref="View"/> wants mouse position reports.
-		/// </summary>
-		/// <value><see langword="true"/> if want mouse position reports; otherwise, <see langword="false"/>.</value>
-		public virtual bool WantMousePositionReports { get; set; }
+		return false;
+	}
+
+	/// <summary>
+	/// Invokes the MouseClick event.
+	/// </summary>
+	protected bool OnMouseClick (MouseEventEventArgs args)
+	{
+		if (!Enabled) {
+			return true;
+		}
 
-		/// <summary>
-		/// Gets or sets a value indicating whether this <see cref="View"/> want continuous button pressed event.
-		/// </summary>
-		public virtual bool WantContinuousButtonPressed { get; set; }
+		MouseClick?.Invoke (this, args);
+		return args.Handled;
 	}
-}
+}

+ 1 - 1
Terminal.Gui/Views/Dialog.cs

@@ -7,7 +7,7 @@ namespace Terminal.Gui;
 
 /// <summary>
 /// The <see cref="Dialog"/> <see cref="View"/> is a <see cref="Window"/> that by default is centered and contains one 
-/// or more <see cref="Button"/>s. It defaults to the <see cref="Colors.ColorSchemes ["Dialog"]"/> color scheme and has a 1 cell padding around the edges.
+/// or more <see cref="Button"/>s. It defaults to the <c>Colors.ColorSchemes ["Dialog"]</c> color scheme and has a 1 cell padding around the edges.
 /// </summary>
 /// <remarks>
 ///  To run the <see cref="Dialog"/> modally, create the <see cref="Dialog"/>, and pass it to <see cref="Application.Run(Func{Exception, bool})"/>. 

+ 2 - 2
UICatalog/UICatalog.csproj

@@ -28,8 +28,8 @@
   <None Update="./Scenarios/Spinning_globe_dark_small.gif" CopyToOutputDirectory="PreserveNewest" />
   </ItemGroup>
   <ItemGroup>
-    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
-    <PackageReference Include="SixLabors.ImageSharp" Version="3.1.1" />
+    <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
+    <PackageReference Include="SixLabors.ImageSharp" Version="3.1.2" />
     <PackageReference Include="CsvHelper" Version="30.0.1" />
     <PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.6" />
     <PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />

+ 2 - 2
UnitTests/Application/MouseTests.cs

@@ -102,7 +102,7 @@ public class MouseTests {
 		var mouseEvent = new MouseEvent () {
 			X = clickX,
 			Y = clickY,
-			Flags = MouseFlags.Button1Pressed
+			Flags = MouseFlags.Button1Clicked
 		};
 		var mouseEventArgs = new MouseEventEventArgs (mouseEvent);
 
@@ -194,7 +194,7 @@ public class MouseTests {
 		var mouseEvent = new MouseEvent () {
 			X = clickX,
 			Y = clickY,
-			Flags = MouseFlags.Button1Pressed
+			Flags = MouseFlags.Button1Clicked
 		};
 		var mouseEventArgs = new MouseEventEventArgs (mouseEvent);
 

+ 1 - 1
UnitTests/UnitTests.csproj

@@ -25,7 +25,7 @@
     <PackageReference Include="ReportGenerator" Version="5.2.0" />
     <PackageReference Include="System.Collections" Version="4.3.0" />
     <PackageReference Include="TestableIO.System.IO.Abstractions.TestingHelpers" Version="20.0.4" />
-    <PackageReference Include="xunit" Version="2.6.4" />
+    <PackageReference Include="xunit" Version="2.6.6" />
     <PackageReference Include="xunit.runner.visualstudio" Version="2.5.6">
       <PrivateAssets>all</PrivateAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

+ 5 - 1
UnitTests/Views/TextFieldTests.cs

@@ -1307,8 +1307,12 @@ public class TextFieldTests {
 			View = tf
 		};
 
+		// In #3183 OnMouseClicked is no longer called before MouseEvent().
+		// This call causes the context menu to pop, and MouseEvent() returns true.
+		// Thus, the clickCounter is NOT incremented.
+		// Which is correct, because the user did NOT click with the left mouse button.
 		Application.OnMouseEvent (new MouseEventEventArgs (mouseEvent));
-		Assert.Equal (2, clickCounter);
+		Assert.Equal (1, clickCounter);
 	}
 
 	private void SuppressKey (object s, Key arg)