fix(Input): clear focus on detached element when PresentationSource is torn down#21800
Open
deeferentleeg wants to merge 1 commit into
Open
Conversation
…s torn down When a focused element is detached from the visual tree (e.g. a menu item in a popup that closes), OnDetachedFromVisualTreeCore cleared focus via e.PresentationSource.InputRoot.FocusManager. That chain is not null-safe on the intermediate accesses: during popup teardown the PresentationSource (or its InputRoot) may already be gone, so the code either threw a NullReferenceException or silently skipped focus clearing, leaving keyboard focus on the detached element so shortcuts stopped working until the user clicked elsewhere. Use FocusManager.GetFocusManager(this), which falls back to the locator, and null-guard the PresentationSource chain — mirroring the existing Focus() method which already uses GetFocusManager. Fixes AvaloniaUI#21759
|
You can test this PR using the following package version. |
Collaborator
|
Author
|
@cla-avalonia agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #21759 — after clicking a menu item in a popup menu (e.g. "Delete"), the menu item is detached from the visual tree but keyboard focus stays on it, so shortcuts (Up arrow, Ctrl+Z, …) stop working until the user clicks another element.
Root cause
In
src/Avalonia.Base/Input/InputElement.cs,OnDetachedFromVisualTreeCoreclears focus with:The
?.only guards the finalClearFocusOnElementRemovedcall. When a popup closes,Popup.CloseCore()→popupHost.SetChild(null)detaches the menu items, and at that point the popup host'sPresentationSource(or itsInputRoot) may already be torn down. The direct chaine.PresentationSource.InputRoot.FocusManageris not null-safe on the intermediate accesses, so it either throws aNullReferenceExceptionor silently skipsClearFocusOnElementRemoved— leavingKeyboardDevice.FocusedElementpointing at the detached element, which then has no visual parent to bubble key events to the window.The fix
Use the existing
FocusManager.GetFocusManager(this)helper, which already has a locator fallback (AvaloniaLocator.Current.GetService<IFocusManager>()) and is the pattern theFocus()method in this same file already uses. Null-guard thePresentationSourcechain and fall back toGetFocusManager:Minimal, no public-API change, and mirrors an established in-file pattern.
Notes
AI usage
An AI assistant was used to help locate the relevant code and draft this change. I have reviewed and understand every line of the diff, and I am the sole author of this contribution.
Fixes #21759