Răsfoiți Sursa

Fixes #2252. Pressing the ENTER key in a TextField should not move the focus (#3195)

* Fixes #2252. Pressing the ENTER key in a TextField should not move the focus

* Update xml doc.

* Tested on WSL-Ubuntu-20.04
BDisp 1 an în urmă
părinte
comite
ce7ecb51d8
3 a modificat fișierele cu 26 adăugiri și 1 ștergeri
  1. 5 1
      Terminal.Gui/Views/Button.cs
  2. 16 0
      UnitTests/Views/ButtonTests.cs
  3. 5 0
      testenvironments.json

+ 5 - 1
Terminal.Gui/Views/Button.cs

@@ -129,6 +129,10 @@ namespace Terminal.Gui {
 		/// Gets or sets whether the <see cref="Button"/> is the default action to activate in a dialog.
 		/// </summary>
 		/// <value><c>true</c> if is default; otherwise, <c>false</c>.</value>
+		/// <remarks>
+		/// If is <see langword="true"/> the current focused view
+		/// will remain focused if the window is not closed.
+		/// </remarks>
 		public bool IsDefault {
 			get => is_default;
 			set {
@@ -219,7 +223,7 @@ namespace Terminal.Gui {
 
 		bool AcceptKey ()
 		{
-			if (!HasFocus) {
+			if (!IsDefault && !HasFocus) {
 				SetFocus ();
 			}
 			OnClicked ();

+ 16 - 0
UnitTests/Views/ButtonTests.cs

@@ -585,5 +585,21 @@ namespace Terminal.Gui.ViewTests {
 
 			TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
 		}
+
+		[Fact, AutoInitShutdown]
+		public void IsDefault_True_Does_Not_Get_The_Focus_On_Enter_Key ()
+		{
+			var wasClicked = false;
+			var view = new View { CanFocus = true };
+			var btn = new Button { Text = "Ok", IsDefault = true };
+			btn.Clicked += () => wasClicked = true;
+			Application.Top.Add (view, btn);
+			Application.Begin (Application.Top);
+			Assert.True (view.HasFocus);
+
+			Application.Top.ProcessColdKey (new KeyEvent (Key.Enter, new KeyModifiers ()));
+			Assert.True (view.HasFocus);
+			Assert.True (wasClicked);
+		}
 	}
 }

+ 5 - 0
testenvironments.json

@@ -10,6 +10,11 @@
 			"type": "wsl",
 			"wslDistribution": "Ubuntu"
 		},
+		{
+			"name": "WSL-Ubuntu-20.04",
+			"type": "wsl",
+			"wslDistribution": "Ubuntu-20.04"
+		},
 		{
 			"name": "WSL-Debian",
 			"type": "wsl",