Skip to content

Commit c6b10c9

Browse files
committed
Addressed review
1 parent 6efaff7 commit c6b10c9

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/Avalonia.X11/X11WindowModes/XEmbedClientWindowMode.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class XEmbedClientWindowMode : X11WindowMode
1414
EmbeddableControlRoot? Root => Window._inputRoot as EmbeddableControlRoot;
1515
private bool _focusedInEmbedder;
1616
private bool _embedderActivated;
17-
private IInputElement? _savedFocus;
1817
private bool _disabled;
1918
private IntPtr _currentEmbedder;
2019
private bool _suppressConfigureEvents;
@@ -25,6 +24,14 @@ public double Scaling
2524
get => Window._scalingOverride ?? 1;
2625
set => Window._scalingOverride = value;
2726
}
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+
}
2835

2936
public override void OnHandleCreated(IntPtr handle)
3037
{
@@ -76,7 +83,7 @@ static XEmbedClientWindowMode()
7683
&& window._mode is XEmbedClientWindowMode xembedMode
7784
&& xembedMode._currentEmbedder != IntPtr.Zero)
7885
{
79-
xembedMode._savedFocus = KeyboardDevice.Instance.FocusedElement;
86+
xembedMode.SavedFocus = KeyboardDevice.Instance.FocusedElement;
8087
xembedMode.SendXEmbedMessage(XEmbedMessage.RequestFocus);
8188
}
8289
}
@@ -131,13 +138,12 @@ private void UpdateActivation()
131138
if (active)
132139
{
133140
((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;
137143
}
138144
else
139145
{
140-
_savedFocus = Root?.IsKeyboardFocusWithin == true ? Root.FocusManager?.GetFocusedElement() : null;
146+
SavedFocus = Root?.IsKeyboardFocusWithin == true ? Root.FocusManager?.GetFocusedElement() : null;
141147
Window.LostFocus?.Invoke();
142148
}
143149
}

0 commit comments

Comments
 (0)