Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading