Browse Source

Fixed TextView and IsDefault

Tig 10 months ago
parent
commit
a5da3cd910
2 changed files with 39 additions and 9 deletions
  1. 15 1
      Terminal.Gui/View/View.Command.cs
  2. 24 8
      UnitTests/Views/TextFieldTests.cs

+ 15 - 1
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;
         }
 

+ 24 - 8
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;