فهرست منبع

Merge branch 'v2_develop' of tig:tig/Terminal.Gui into v2_develop

Tig 4 ماه پیش
والد
کامیت
30fafb4625

+ 20 - 0
Terminal.Gui/ConsoleDrivers/V2/ApplicationV2.cs

@@ -194,6 +194,23 @@ public class ApplicationV2 : ApplicationImpl
     {
     {
         Logging.Logger.LogInformation ($"RequestStop '{top}'");
         Logging.Logger.LogInformation ($"RequestStop '{top}'");
 
 
+        top ??= Application.Top;
+
+        if (top == null)
+        {
+            return;
+        }
+
+        var ev = new ToplevelClosingEventArgs (top);
+        top.OnClosing (ev);
+
+        if (ev.Cancel)
+        {
+            return;
+        }
+
+        top.Running = false;
+
         // TODO: This definition of stop seems sketchy
         // TODO: This definition of stop seems sketchy
         Application.TopLevels.TryPop (out _);
         Application.TopLevels.TryPop (out _);
 
 
@@ -205,6 +222,9 @@ public class ApplicationV2 : ApplicationImpl
         {
         {
             Application.Top = null;
             Application.Top = null;
         }
         }
+
+        // Notify that it is closed
+        top.OnClosed (top);
     }
     }
 
 
     /// <inheritdoc/>
     /// <inheritdoc/>

+ 4 - 0
Terminal.Gui/Views/MessageBox.cs

@@ -374,6 +374,10 @@ public static class MessageBox
                                        {
                                        {
                                            Clicked = (int)btn.Data!;
                                            Clicked = (int)btn.Data!;
                                        }
                                        }
+                                       else
+                                       {
+                                           Clicked = defaultButton;
+                                       }
 
 
                                        e.Cancel = true;
                                        e.Cancel = true;
                                        Application.RequestStop ();
                                        Application.RequestStop ();

+ 47 - 0
Tests/UnitTests/ConsoleDrivers/V2/ApplicationV2Tests.cs

@@ -221,6 +221,53 @@ public class ApplicationV2Tests
 
 
         ApplicationImpl.ChangeInstance (orig);
         ApplicationImpl.ChangeInstance (orig);
     }
     }
+
+    [Fact]
+    public void Test_V2_ClosingRaised ()
+    {
+        var orig = ApplicationImpl.Instance;
+
+        var v2 = NewApplicationV2 ();
+        ApplicationImpl.ChangeInstance (v2);
+
+        v2.Init ();
+
+        int closing=0;
+        int closed = 0;
+        var t=new Toplevel ();
+        t.Closing
+            += (_, a) =>
+               {
+                   // Cancel the first time
+                   if (closing==0)
+                   {
+                       a.Cancel = true;
+                   }
+                   closing++;
+                   Assert.Same(t,a.RequestingTop);
+               };
+
+        t.Closed
+            += (_, a) =>
+               {
+                   closed++;
+                   Assert.Same (t, a.Toplevel);
+               };
+
+        v2.AddIdle (IdleExit);
+
+        // Blocks until the timeout call is hit
+
+        v2.Run (t);
+
+        Assert.Null (Application.Top);
+        v2.Shutdown ();
+
+        ApplicationImpl.ChangeInstance (orig);
+
+        Assert.Equal (2,closing);
+        Assert.Equal (1, closed);
+    }
     private bool IdleExit ()
     private bool IdleExit ()
     {
     {
         if (Application.Top != null)
         if (Application.Top != null)

+ 21 - 1
Tests/UnitTests/Dialogs/MessageBoxTests.cs

@@ -504,5 +504,25 @@ public class MessageBoxTests
         Application.Run (top);
         Application.Run (top);
         top.Dispose ();
         top.Dispose ();
     }
     }
-}
 
 
+    [Theory]
+    [SetupFakeDriver]
+    [MemberData (nameof (AcceptingKeys))]
+    public void Button_IsDefault_True_Return_His_Index_On_Accepting (Key key)
+    {
+        Application.Init ();
+
+        Application.Iteration += (_, _) => Assert.True (Application.RaiseKeyDownEvent (key));
+        int res = MessageBox.Query ("hey", "IsDefault", "Yes", "No");
+
+        Assert.Equal (0, res);
+
+        Application.Shutdown ();
+    }
+
+    public static IEnumerable<object []> AcceptingKeys ()
+    {
+        yield return [Key.Enter];
+        yield return [Key.Space];
+    }
+}