From ce7ecb51d8626c38cce96367724b05c7b1289fb4 Mon Sep 17 00:00:00 2001 From: BDisp Date: Sat, 20 Jan 2024 16:16:59 +0000 Subject: [PATCH] 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 --- Terminal.Gui/Views/Button.cs | 6 +++++- UnitTests/Views/ButtonTests.cs | 16 ++++++++++++++++ testenvironments.json | 5 +++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Terminal.Gui/Views/Button.cs b/Terminal.Gui/Views/Button.cs index c5995f838d..0c90c13b8e 100644 --- a/Terminal.Gui/Views/Button.cs +++ b/Terminal.Gui/Views/Button.cs @@ -129,6 +129,10 @@ void Initialize (ustring text, bool is_default) /// Gets or sets whether the is the default action to activate in a dialog. /// /// true if is default; otherwise, false. + /// + /// If is the current focused view + /// will remain focused if the window is not closed. + /// public bool IsDefault { get => is_default; set { @@ -219,7 +223,7 @@ bool ExecuteColdKey (KeyEvent ke) bool AcceptKey () { - if (!HasFocus) { + if (!IsDefault && !HasFocus) { SetFocus (); } OnClicked (); diff --git a/UnitTests/Views/ButtonTests.cs b/UnitTests/Views/ButtonTests.cs index 0202db0e3b..5ef99cfa80 100644 --- a/UnitTests/Views/ButtonTests.cs +++ b/UnitTests/Views/ButtonTests.cs @@ -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); + } } } diff --git a/testenvironments.json b/testenvironments.json index 898ac827de..25324739d6 100644 --- a/testenvironments.json +++ b/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",