Skip to content

Commit

Permalink
fix(popup): Put Popup in Parent hierarchy of its Child content.
Browse files Browse the repository at this point in the history
Set Popup as Parent of PopupPanel. This emulates UWP behavior that the Popup is a Parent of one of the parents of the Child content, which is relied upon in some cases (eg WCT DataGrid relies upon this for focus management).
  • Loading branch information
davidjohnoliver committed May 6, 2021
1 parent afbfd2e commit 765a2b6
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls_Primitives.PopupPages;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using static Private.Infrastructure.TestServices;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls_Primitives
{
[TestClass]
[RunsOnUIThread]
public class Given_Popup
{
[TestMethod]
public async Task Check_Can_Reach_Main_Visual_Tree()
{
var page = new ReachMainTreePage();
WindowHelper.WindowContent = page;

await WindowHelper.WaitForLoaded(page);

Assert.IsTrue(CanReach(page.DummyTextBlock, page));

try
{
page.TargetPopup.IsOpen = true;
await WindowHelper.WaitForLoaded(page.PopupButton);

Assert.IsTrue(CanReach(page.PopupButton, page));
}
finally
{
page.TargetPopup.IsOpen = false;
}
}

#if __ANDROID__
[TestMethod]
public async Task Check_Can_Reach_Main_Visual_Tree_Alternate_Mode()
{
var originalConfig = FeatureConfiguration.Popup.UseNativePopup;
FeatureConfiguration.Popup.UseNativePopup = !originalConfig;
try
{
await Check_Can_Reach_Main_Visual_Tree();
}
finally
{
FeatureConfiguration.Popup.UseNativePopup = originalConfig;
}
}
#endif

private static bool CanReach(DependencyObject startingElement, DependencyObject targetElement)
{
var currentElement = startingElement;
while (currentElement != null)
{
if (currentElement == targetElement)
{
return true;
}

// Quoting WCT DataGrid:
// // Walk up the visual tree. Try using the framework element's
// // parent. We do this because Popups behave differently with respect to the visual tree,
// // and it could have a parent even if the VisualTreeHelper doesn't find it.
DependencyObject parent = null;
if (currentElement is FrameworkElement fe)
{
parent = fe.Parent;
}
if (parent == null)
{
parent = VisualTreeHelper.GetParent(currentElement);
}

currentElement = parent;
}

// Did not hit targetElement
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Page x:Class="Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls_Primitives.PopupPages.ReachMainTreePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls_Primitives.PopupPages"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<Grid>
<TextBlock x:Name="DummyTextBlock"
x:FieldModifier="public"
Text="Page with Popup" />
<Popup x:Name="TargetPopup"
x:FieldModifier="public">
<StackPanel>
<TextBlock Text="Popup content" />
<Grid>
<Button x:Name="PopupButton"
x:FieldModifier="public"
Content="Button in Popup" />
</Grid>
</StackPanel>
</Popup>
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls_Primitives.PopupPages
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class ReachMainTreePage : Page
{
public ReachMainTreePage()
{
this.InitializeComponent();
}
}
}
17 changes: 9 additions & 8 deletions src/Uno.UI/UI/Xaml/Controls/Popup/PopupPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,18 @@ protected override Size ArrangeOverride(Size finalSize)
return finalSize;
}

#if __ANDROID__
private protected override void OnLoaded()
{
base.OnLoaded();
// Set Parent to the Popup, to obtain the same behavior as UWP that the Popup (and therefore the rest of the main visual tree)
// is reachable by scaling the combined Parent/GetVisualParent() hierarchy.
this.SetParent(Popup);
}

private protected override void OnUnloaded()
{
base.OnUnloaded();

if (FeatureConfiguration.Popup.UseNativePopup)
{
// Unset parent here, because it doesn't get unset in the usual way because the parent is a native view.
this.SetParent(null);
}
this.SetParent(null);
}
#endif
}
}

0 comments on commit 765a2b6

Please sign in to comment.