From a5da3cd910aee76754b28dead6fca6659412b612 Mon Sep 17 00:00:00 2001 From: Tig Date: Wed, 2 Oct 2024 13:04:50 -0600 Subject: [PATCH] Fixed TextView and IsDefault --- Terminal.Gui/View/View.Command.cs | 16 +++++++++++++++- UnitTests/Views/TextFieldTests.cs | 32 +++++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/Terminal.Gui/View/View.Command.cs b/Terminal.Gui/View/View.Command.cs index 56e72093f..8d10a7536 100644 --- a/Terminal.Gui/View/View.Command.cs +++ b/Terminal.Gui/View/View.Command.cs @@ -48,9 +48,23 @@ public partial class View // Command APIs Accept?.Invoke (this, args); } - // Accept is a special case where if the event is not canceled, the event is bubbled up the SuperView hierarchy. + // Accept is a special case where if the event is not canceled, the event is + // - Invoked on any peer-View with IsDefault == true + // - bubbled up the SuperView hierarchy. if (!args.Handled) { + // If there's an IsDefault peer view in Subviews, try it + var isDefaultView = SuperView?.Subviews.FirstOrDefault (v => v is Button { IsDefault: true }); + + if (isDefaultView != this && isDefaultView is Button { IsDefault: true } button) + { + bool? handled = isDefaultView.InvokeCommand (Command.Accept); + if (handled == true) + { + return true; + } + } + return SuperView?.InvokeCommand (Command.Accept) == true; } diff --git a/UnitTests/Views/TextFieldTests.cs b/UnitTests/Views/TextFieldTests.cs index a5c6fe4e1..6af276a6b 100644 --- a/UnitTests/Views/TextFieldTests.cs +++ b/UnitTests/Views/TextFieldTests.cs @@ -792,14 +792,22 @@ public class TextFieldTests (ITestOutputHelper output) } [Theory] - [InlineData (false, 0)] - [InlineData (true, 1)] + [InlineData (false, 1)] + [InlineData (true, 0)] public void Accept_Handler_Handled_Prevents_Default_Button_Accept (bool handleAccept, int expectedButtonAccepts) { - var superView = new Window (); - var tf = new TextField (); + var superView = new Window () + { + Id = "superView" + }; + + var tf = new TextField () + { + Id = "tf" + }; var button = new Button () { + Id = "button", IsDefault = true, }; @@ -840,10 +848,18 @@ public class TextFieldTests (ITestOutputHelper output) [Fact] public void Accept_No_Handler_Enables_Default_Button_Accept () { - var superView = new Window (); - var tf = new TextField (); + var superView = new Window () + { + Id = "superView" + }; + + var tf = new TextField () + { + Id = "tf" + }; var button = new Button () { + Id="button", IsDefault = true, }; @@ -883,13 +899,13 @@ public class TextFieldTests (ITestOutputHelper output) var tfAcceptedInvoked = false; var handle = false; view.Accept += TextViewAccept; - Assert.True (view.InvokeCommand (Command.Accept)); + Assert.False (view.InvokeCommand (Command.Accept)); Assert.True (tfAcceptedInvoked); tfAcceptedInvoked = false; handle = true; view.Accept += TextViewAccept; - Assert.False (view.InvokeCommand (Command.Accept)); + Assert.True (view.InvokeCommand (Command.Accept)); Assert.True (tfAcceptedInvoked); return;