Ver código fonte

Hide child windows when hiding parent/owner.

Steven Kirk 4 anos atrás
pai
commit
a42334d128

+ 8 - 0
src/Avalonia.Controls/Window.cs

@@ -592,6 +592,14 @@ namespace Avalonia.Controls
                     owner.RemoveChild(this);
                 }
 
+                if (_children.Count > 0)
+                {
+                    foreach (var child in _children.ToArray())
+                    {
+                        child.child.Hide();
+                    }
+                }
+
                 Owner = null;
 
                 PlatformImpl?.Hide();

+ 36 - 0
tests/Avalonia.Controls.UnitTests/WindowTests.cs

@@ -472,6 +472,42 @@ namespace Avalonia.Controls.UnitTests
             }
         }
 
+        [Fact]
+        public void Hiding_Parent_Window_Should_Close_Children()
+        {
+            using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
+            {
+                var parent = new Window();
+                var child = new Window();
+
+                parent.Show();
+                child.Show(parent);
+
+                parent.Hide();
+
+                Assert.False(parent.IsVisible);
+                Assert.False(child.IsVisible);
+            }
+        }
+
+        [Fact]
+        public void Hiding_Parent_Window_Should_Close_Dialog_Children()
+        {
+            using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
+            {
+                var parent = new Window();
+                var child = new Window();
+
+                parent.Show();
+                child.ShowDialog(parent);
+
+                parent.Hide();
+
+                Assert.False(parent.IsVisible);
+                Assert.False(child.IsVisible);
+            }
+        }
+
         [Fact]
         public void Window_Should_Be_Centered_When_WindowStartupLocation_Is_CenterScreen()
         {