Skip to content

Commit

Permalink
Fixes #2252. Pressing the ENTER key in a TextField should not move th…
Browse files Browse the repository at this point in the history
…e 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
  • Loading branch information
BDisp authored Jan 20, 2024
1 parent c8e54bb commit ce7ecb5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Terminal.Gui/Views/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ void Initialize (ustring text, bool is_default)
/// 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 {
Expand Down Expand Up @@ -219,7 +223,7 @@ bool ExecuteColdKey (KeyEvent ke)

bool AcceptKey ()
{
if (!HasFocus) {
if (!IsDefault && !HasFocus) {
SetFocus ();
}
OnClicked ();
Expand Down
16 changes: 16 additions & 0 deletions UnitTests/Views/ButtonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -585,5 +585,21 @@ public void Pos_Center_Layout_AutoSize_False ()

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 changes: 5 additions & 0 deletions testenvironments.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
"type": "wsl",
"wslDistribution": "Ubuntu"
},
{
"name": "WSL-Ubuntu-20.04",
"type": "wsl",
"wslDistribution": "Ubuntu-20.04"
},
{
"name": "WSL-Debian",
"type": "wsl",
Expand Down

0 comments on commit ce7ecb5

Please sign in to comment.