Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Avalonia.Base/Input/InputElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,19 @@ protected override void OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventAr
if (IsFocused)
{
var root = e.AttachmentPoint ?? e.RootVisual;
((FocusManager?)e.PresentationSource.InputRoot.FocusManager)
?.ClearFocusOnElementRemoved(this, root);
// The PresentationSource (and its InputRoot) may already be torn
// down by the time a focused element is detached — for example
// when a popup that hosts a focused menu item closes. The
// previous direct chain (e.PresentationSource.InputRoot.FocusManager)
// was not null-safe on the intermediate accesses and would either
// throw a NullReferenceException or silently skip focus clearing,
// leaving keyboard focus on a detached element so that shortcuts
// stopped working until the user clicked elsewhere (see #21759).
// Use GetFocusManager, which falls back to the locator, mirroring
// the Focus() method below.
Comment on lines +571 to +580

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reduce the AI comment or drop it entirely. 10 lines to explain ?? is just noise.

var focusManager = (FocusManager?)e.PresentationSource?.InputRoot?.FocusManager
?? FocusManager.GetFocusManager(this);
focusManager?.ClearFocusOnElementRemoved(this, root);
}

IsKeyboardFocusWithin = false;
Expand Down