1
1
using System ;
2
2
using Uno . UI ;
3
+ using Uno . UI . Xaml . Core ;
3
4
using Windows . Foundation ;
4
5
using Windows . UI . Xaml . Input ;
5
6
using Windows . UI . Xaml . Media ;
@@ -15,6 +16,9 @@ namespace Windows.UI.Xaml.Controls
15
16
{
16
17
public partial class PopupBase : FrameworkElement , IPopup
17
18
{
19
+ private WeakReference < UIElement > _lastFocusedElement = null ;
20
+ private FocusState _lastFocusState = FocusState . Unfocused ;
21
+
18
22
private IDisposable _openPopupRegistration ;
19
23
private bool _childHasOwnDataContext ;
20
24
@@ -62,11 +66,32 @@ partial void OnIsOpenChangedPartial(bool oldIsOpen, bool newIsOpen)
62
66
if ( newIsOpen )
63
67
{
64
68
_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
+
65
83
Opened ? . Invoke ( this , newIsOpen ) ;
66
84
}
67
85
else
68
86
{
69
87
_openPopupRegistration ? . Dispose ( ) ;
88
+
89
+ if ( _lastFocusedElement != null && _lastFocusedElement . TryGetTarget ( out var target ) )
90
+ {
91
+ target . Focus ( _lastFocusState ) ;
92
+ _lastFocusedElement = null ;
93
+ }
94
+
70
95
Closed ? . Invoke ( this , newIsOpen ) ;
71
96
}
72
97
}
0 commit comments