From d3246b0dbd61ef871363742cff0d1050fe187686 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Tue, 11 Jul 2023 15:30:49 +0200 Subject: [PATCH 1/3] fix: Use LogicalParentOverride for PopupPanel everywhere Previously it was used only on WASM and Skia, but that caused odd issues, because routed events got propagated from Popups to the logical parents (cherry picked from commit 784a58958b01f1f9c1cbdf647e0cfef50cc68110) --- src/Uno.UI/UI/Xaml/DependencyObjectExtensions.cs | 5 ----- src/Uno.UI/UI/Xaml/FrameworkElement.cs | 4 ---- 2 files changed, 9 deletions(-) diff --git a/src/Uno.UI/UI/Xaml/DependencyObjectExtensions.cs b/src/Uno.UI/UI/Xaml/DependencyObjectExtensions.cs index f514834475cf..97f01e8fa673 100644 --- a/src/Uno.UI/UI/Xaml/DependencyObjectExtensions.cs +++ b/src/Uno.UI/UI/Xaml/DependencyObjectExtensions.cs @@ -152,17 +152,12 @@ internal static bool TrySetParent(this object dependencyObject, object parent) internal static void SetLogicalParent(this FrameworkElement element, DependencyObject logicalParent) { -#if UNO_HAS_MANAGED_POINTERS || __WASM__ // WASM has managed-esque pointers - // UWP distinguishes between the 'logical parent' (or inheritance parent) and the 'visual parent' of an element. Uno already // recognises this distinction on some targets, but for targets using CoerceHitTestVisibility() for hit testing, the pointer // implementation depends upon the logical parent (ie DepObjStore.Parent) being identical to the visual parent, because it // piggybacks on the DP inheritance mechanism. Therefore we use LogicalParentOverride as a workaround to modify the publicly-visible // FrameworkElement.Parent without affecting DP propagation. element.LogicalParentOverride = logicalParent; -#else - SetParent(element, logicalParent); -#endif } /// diff --git a/src/Uno.UI/UI/Xaml/FrameworkElement.cs b/src/Uno.UI/UI/Xaml/FrameworkElement.cs index 1e1ec15d6ed1..d34db342ee03 100644 --- a/src/Uno.UI/UI/Xaml/FrameworkElement.cs +++ b/src/Uno.UI/UI/Xaml/FrameworkElement.cs @@ -275,9 +275,7 @@ Microsoft.UI.Xaml.ResourceDictionary Resources new #endif DependencyObject Parent => -#if UNO_HAS_MANAGED_POINTERS || __WASM__ LogicalParentOverride ?? -#endif ((IDependencyObjectStoreProvider)this).Store.Parent as DependencyObject; public global::System.Uri BaseUri @@ -293,12 +291,10 @@ Microsoft.UI.Xaml.ResourceDictionary Resources } } -#if UNO_HAS_MANAGED_POINTERS || __WASM__ /// /// Allows to override the publicly-visible without modifying DP propagation. /// internal DependencyObject LogicalParentOverride { get; set; } -#endif /// /// Provides the managed visual parent of the element. This property can be overriden for specific From 3eb9da86de6150ff3b9f4d60343f925daff430c1 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Tue, 3 Sep 2024 17:00:37 +0200 Subject: [PATCH 2/3] test: Validate parent structure for Popup children (cherry picked from commit 2d8c7673152ec86c99cb403f55401d6df99a12f1) --- .../Given_Popup.cs | 73 +++++++++++++++++-- 1 file changed, 67 insertions(+), 6 deletions(-) diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls_Primitives/Given_Popup.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls_Primitives/Given_Popup.cs index 355fe91c2097..7746bf78c878 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls_Primitives/Given_Popup.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls_Primitives/Given_Popup.cs @@ -1,16 +1,15 @@ using System; using System.Threading.Tasks; -using Private.Infrastructure; -using Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls_Primitives.PopupPages; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls.Primitives; -using Microsoft.UI.Xaml.Media; -using static Private.Infrastructure.TestServices; -using Windows.Media.Capture.Core; -using Windows.System; using Microsoft.UI.Xaml.Input; +using Microsoft.UI.Xaml.Media; +using Private.Infrastructure; using Uno.UI.RuntimeTests.Helpers; +using Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls_Primitives.PopupPages; +using Windows.System; +using static Private.Infrastructure.TestServices; namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls_Primitives { @@ -78,6 +77,68 @@ public void When_Closed_Immediately() popup.IsOpen = false; } + [TestMethod] + public async Task When_Child_Visual_Parents_Include_Popup() + { + var popup = new Popup(); + popup.XamlRoot = TestServices.WindowHelper.XamlRoot; + var button = new Button() + { + Content = "test" + }; + popup.Child = button; + popup.IsOpen = true; + await WindowHelper.WaitForLoaded(button); + bool found = false; + DependencyObject current = popup.Child; + while (current != null) + { + if (current == popup) + { + found = true; + break; + } + + current = VisualTreeHelper.GetParent(current); + } + + Assert.IsFalse(found); + + // Should not throw + popup.IsOpen = false; + } + + [TestMethod] + public async Task When_Child_Logical_Parents_Include_Popup() + { + var popup = new Popup(); + popup.XamlRoot = TestServices.WindowHelper.XamlRoot; + var button = new Button() + { + Content = "test" + }; + popup.Child = button; + popup.IsOpen = true; + await WindowHelper.WaitForLoaded(button); + bool found = false; + DependencyObject current = button; + while (current != null) + { + if (current == popup) + { + found = true; + break; + } + + current = (current as FrameworkElement)?.Parent; + } + + Assert.IsTrue(found); + + // Should not throw + popup.IsOpen = false; + } + [TestMethod] #if __MACOS__ [Ignore("Currently fails on macOS, part of #9282 epic")] From 95329c50d085e595e8cbdfa47ca131db2d5b20e9 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Tue, 3 Sep 2024 18:17:08 +0200 Subject: [PATCH 3/3] chore: Adjust test name (cherry picked from commit 9429e1047c9b5557f3d3ede4c53999ffa74de14e) --- .../Tests/Windows_UI_Xaml_Controls_Primitives/Given_Popup.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls_Primitives/Given_Popup.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls_Primitives/Given_Popup.cs index 7746bf78c878..313c35c5c8cd 100644 --- a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls_Primitives/Given_Popup.cs +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls_Primitives/Given_Popup.cs @@ -78,7 +78,7 @@ public void When_Closed_Immediately() } [TestMethod] - public async Task When_Child_Visual_Parents_Include_Popup() + public async Task When_Child_Visual_Parents_Does_Not_Include_Popup() { var popup = new Popup(); popup.XamlRoot = TestServices.WindowHelper.XamlRoot;