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
This commit is contained in:
BDisp
2024-01-20 16:16:59 +00:00
committed by GitHub
parent c8e54bba09
commit ce7ecb51d8
3 changed files with 26 additions and 1 deletions

View File

@@ -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 ();

View File

@@ -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);
}
}
}

View File

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