Avoid exceptions when getting a position from a PointerEventArgs#21301
Merged
MrJul merged 1 commit intoMay 4, 2026
Merged
Conversation
Return null when an IPresentationSource can't convert from/to screen coordinates Document exceptions in VisualExtensions PointToClient and PointToScreen Add tests for cross-root pointer event positions
|
You can test this PR using the following package version. |
MrJul
pushed a commit
to MrJul/Avalonia
that referenced
this pull request
May 28, 2026
…niaUI#21301) Return null when an IPresentationSource can't convert from/to screen coordinates Document exceptions in VisualExtensions PointToClient and PointToScreen Add tests for cross-root pointer event positions
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.
#21294 identifies a case in which
PointerEventArgs.GetPositioncan throw. The bug report blamesItemSelectionEventTriggers.IsPointerEventWithinBounds, which is where the exception was observed. But the problem is more general than that.What is the current behaviour?
Inspecting the code of
GetPosition, it's clear that the intent is to return (0,0) if a position can't be calculated. However, it will in fact throw if_rootVisual.VisualRootis null when the method is called.What is the updated/expected behavior with this PR?
GetPositionis no longer dependent on the value of_rootVisual.VisualRoot, and so no longer throws if it's null.VisualExtensions.PointToClientandPointToScreenare now documented.How was the solution implemented (if it's not obvious)?
I refactored
PointerEventArgs.GetPositionto use the newIPresentationSource.PointToClientandPointToScreenmethods when transforming a point between two presentation sources. We can bypass theVisualExtensionsequivalents because we already know that the position is in the coordinate space of the presentation source which raised the event.This refactoring allows us to store
rootVisual.PresentationSourcein the constructor instead ofrootVisualitself. This protects us from the property changing later. The onlyVisualinvolved is now the one passed toGetPosition.IPresentationSource.PointToClientandPointToScreennow returnnullif they failed to transform the point, so that callers can respond appropriately.Breaking changes
None.
Obsoletions / Deprecations
None.
Fixed issues
Fixes #21294