@@ -14,7 +14,6 @@ public class XEmbedClientWindowMode : X11WindowMode
14
14
EmbeddableControlRoot ? Root => Window . _inputRoot as EmbeddableControlRoot ;
15
15
private bool _focusedInEmbedder ;
16
16
private bool _embedderActivated ;
17
- private IInputElement ? _savedFocus ;
18
17
private bool _disabled ;
19
18
private IntPtr _currentEmbedder ;
20
19
private bool _suppressConfigureEvents ;
@@ -25,6 +24,14 @@ public double Scaling
25
24
get => Window . _scalingOverride ?? 1 ;
26
25
set => Window . _scalingOverride = value ;
27
26
}
27
+
28
+ private WeakReference < IInputElement > ? _savedFocus ;
29
+
30
+ private IInputElement ? SavedFocus
31
+ {
32
+ get => _savedFocus ? . TryGetTarget ( out var target ) == true ? target : null ;
33
+ set => _savedFocus = value == null ? null : new WeakReference < IInputElement > ( value ) ;
34
+ }
28
35
29
36
public override void OnHandleCreated ( IntPtr handle )
30
37
{
@@ -76,7 +83,7 @@ static XEmbedClientWindowMode()
76
83
&& window . _mode is XEmbedClientWindowMode xembedMode
77
84
&& xembedMode . _currentEmbedder != IntPtr . Zero )
78
85
{
79
- xembedMode . _savedFocus = KeyboardDevice . Instance . FocusedElement ;
86
+ xembedMode . SavedFocus = KeyboardDevice . Instance . FocusedElement ;
80
87
xembedMode . SendXEmbedMessage ( XEmbedMessage . RequestFocus ) ;
81
88
}
82
89
}
@@ -131,13 +138,12 @@ private void UpdateActivation()
131
138
if ( active )
132
139
{
133
140
( ( FocusManager ? ) Root ? . FocusManager ) ? . SetFocusScope ( Root ) ;
134
- if ( _savedFocus != null )
135
- KeyboardDevice . Instance ? . SetFocusedElement ( _savedFocus , NavigationMethod . Unspecified , KeyModifiers . None ) ;
136
- _savedFocus = null ;
141
+ SavedFocus ? . Focus ( ) ;
142
+ SavedFocus = null ;
137
143
}
138
144
else
139
145
{
140
- _savedFocus = Root ? . IsKeyboardFocusWithin == true ? Root . FocusManager ? . GetFocusedElement ( ) : null ;
146
+ SavedFocus = Root ? . IsKeyboardFocusWithin == true ? Root . FocusManager ? . GetFocusedElement ( ) : null ;
141
147
Window . LostFocus ? . Invoke ( ) ;
142
148
}
143
149
}
0 commit comments