Skip to content

Commit

Permalink
fix(popup): not working after alt-tabbing
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoy312 committed Jan 7, 2022
1 parent 6d3d96c commit db6ada0
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\MenuFlyoutTests\MenuFlyout_DroidAltTab.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\MenuFlyoutTests\MenuFlyout_IosNative.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -5457,6 +5461,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\MenuFlyoutTests\MenuFlyoutItem_Hierarchy.xaml.cs">
<DependentUpon>MenuFlyoutItem_Hierarchy.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\MenuFlyoutTests\MenuFlyout_DroidAltTab.xaml.cs">
<DependentUpon>MenuFlyout_DroidAltTab.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\MenuFlyoutTests\MenuFlyout_IosNative.xaml.cs">
<DependentUpon>MenuFlyout_IosNative.xaml</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<UserControl x:Class="UITests.Windows_UI_Xaml_Controls.MenuFlyoutTests.MenuFlyout_DroidAltTab"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UITests.Windows_UI_Xaml_Controls.MenuFlyoutTests"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:android="http://uno.ui/android"
mc:Ignorable="d android"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<CommandBar Content="asd" Style="{StaticResource NativeDefaultCommandBar}">
<CommandBar.PrimaryCommands>
<AppBarButton Content="Here">
<AppBarButton.Flyout>
<MenuFlyout android:UseNativePopup="True">
<MenuFlyoutItem Text="asd1" />
<MenuFlyoutItem Text="asd2" />
<MenuFlyoutItem Text="asd3" />
</MenuFlyout>
</AppBarButton.Flyout>
</AppBarButton>
</CommandBar.PrimaryCommands>
</CommandBar>
</Grid>
</UserControl>
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 User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

namespace UITests.Windows_UI_Xaml_Controls.MenuFlyoutTests
{
[Uno.UI.Samples.Controls.SampleControlInfo("MenuFlyout", nameof(MenuFlyout_DroidAltTab), description: Description, IsManualTest = true, IgnoreInSnapshotTests = true)]
public sealed partial class MenuFlyout_DroidAltTab : UserControl
{
private const string Description = "[ManualTest]: tap 'Here' to open popup, switch app/go to home and come back, tap `Here` and popup should still work.";

public MenuFlyout_DroidAltTab()
{
this.InitializeComponent();
}
}
}
22 changes: 21 additions & 1 deletion src/Uno.UI/UI/Xaml/Controls/Popup/Popup.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;
using Uno.UI;
using Android.Content;
using Uno.Disposables;

namespace Windows.UI.Xaml.Controls.Primitives
{
public partial class Popup
{
private readonly SerialDisposable _disposable = new SerialDisposable();
private PopupWindow _popupWindow;
private WeakReference<Context> _popupContextReference;

internal FlyoutPlacementMode Placement { get; set; }

Expand All @@ -26,7 +30,8 @@ internal Popup(bool useNativePopup) : this()

partial void InitializeNativePartial()
{
_popupWindow = new PopupWindow(this, WindowManagerLayoutParams.MatchParent, WindowManagerLayoutParams.MatchParent, true);
_popupWindow = new PopupWindow(ApplicationActivity.Instance);
_popupContextReference = new WeakReference<Context>(ApplicationActivity.Instance);

_popupWindow.Width = WindowManagerLayoutParams.MatchParent;
_popupWindow.Height = WindowManagerLayoutParams.MatchParent;
Expand All @@ -36,6 +41,19 @@ internal Popup(bool useNativePopup) : this()
OnIsLightDismissEnabledChanged(false, true);

_popupWindow.DismissEvent += OnPopupDismissed;
_disposable.Disposable = Disposable.Create(() => _popupWindow.DismissEvent -= OnPopupDismissed);
}

private void ReinitializePopupWindowIfContextChanged()
{
if (_popupWindow != null &&
_popupContextReference?.TryGetTarget(out var popupContextReference) == true &&
popupContextReference != ApplicationActivity.Instance)
{
_disposable.Dispose();
InitializeNativePartial();
OnPopupPanelChangedPartialNative(PopupPanel, PopupPanel);
}
}

partial void OnPopupPanelChangedPartialNative(PopupPanel previousPanel, PopupPanel newPanel)
Expand Down Expand Up @@ -63,6 +81,8 @@ private void OnPopupDismissed(object sender, EventArgs e)

partial void OnIsOpenChangedNative(bool oldIsOpen, bool newIsOpen)
{
ReinitializePopupWindowIfContextChanged();

if (newIsOpen)
{
PopupPanel.Visibility = Visibility.Visible;
Expand Down

0 comments on commit db6ada0

Please sign in to comment.