Skip to content

Commit 4d8af83

Browse files
committed
fix: Flyout should return focus to previously focused
1 parent bc8d7fa commit 4d8af83

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/Uno.UI/UI/Xaml/Controls/Popup/PopupBase.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using Uno.UI;
3+
using Uno.UI.Xaml.Core;
34
using Windows.Foundation;
45
using Windows.UI.Xaml.Input;
56
using Windows.UI.Xaml.Media;
@@ -15,6 +16,9 @@ namespace Windows.UI.Xaml.Controls
1516
{
1617
public partial class PopupBase : FrameworkElement, IPopup
1718
{
19+
private WeakReference<UIElement> _lastFocusedElement = null;
20+
private FocusState _lastFocusState = FocusState.Unfocused;
21+
1822
private IDisposable _openPopupRegistration;
1923
private bool _childHasOwnDataContext;
2024

@@ -62,11 +66,32 @@ partial void OnIsOpenChangedPartial(bool oldIsOpen, bool newIsOpen)
6266
if (newIsOpen)
6367
{
6468
_openPopupRegistration = VisualTreeHelper.RegisterOpenPopup(this);
69+
70+
if (IsLightDismissEnabled)
71+
{
72+
// Store last focused element
73+
var focusManager = VisualTree.GetFocusManagerForElement(this);
74+
var focusedElement = focusManager.FocusedElement as UIElement;
75+
var focusState = focusManager.GetRealFocusStateForFocusedElement();
76+
if (focusedElement != null && focusState != FocusState.Unfocused)
77+
{
78+
_lastFocusedElement = new WeakReference<UIElement>(focusedElement);
79+
_lastFocusState = focusState;
80+
}
81+
}
82+
6583
Opened?.Invoke(this, newIsOpen);
6684
}
6785
else
6886
{
6987
_openPopupRegistration?.Dispose();
88+
89+
if (_lastFocusedElement != null && _lastFocusedElement.TryGetTarget(out var target))
90+
{
91+
target.Focus(_lastFocusState);
92+
_lastFocusedElement = null;
93+
}
94+
7095
Closed?.Invoke(this, newIsOpen);
7196
}
7297
}

0 commit comments

Comments
 (0)