Skip to content

Commit

Permalink
Merge pull request #18242 from unoplatform/mergify/bp/release/stable/…
Browse files Browse the repository at this point in the history
…5.4/pr-12860

fix: Use LogicalParentOverride for PopupPanel everywhere (backport #12860)
  • Loading branch information
jeromelaban authored Sep 19, 2024
2 parents 4864019 + 95329c5 commit 45e3f89
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -78,6 +77,68 @@ public void When_Closed_Immediately()
popup.IsOpen = false;
}

[TestMethod]
public async Task When_Child_Visual_Parents_Does_Not_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")]
Expand Down
5 changes: 0 additions & 5 deletions src/Uno.UI/UI/Xaml/DependencyObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/// <summary>
Expand Down
4 changes: 0 additions & 4 deletions src/Uno.UI/UI/Xaml/FrameworkElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -293,12 +291,10 @@ Microsoft.UI.Xaml.ResourceDictionary Resources
}
}

#if UNO_HAS_MANAGED_POINTERS || __WASM__
/// <summary>
/// Allows to override the publicly-visible <see cref="Parent"/> without modifying DP propagation.
/// </summary>
internal DependencyObject LogicalParentOverride { get; set; }
#endif

/// <summary>
/// Provides the managed visual parent of the element. This property can be overriden for specific
Expand Down

0 comments on commit 45e3f89

Please sign in to comment.