Repository navigation
Description
Current behavior
VisualTreeHelper.FindElementsInHostCoordinates(Point, UIElement) returns elements at points where they are clipped away. IsElementIntersecting only tests the element's transformed layout slot, so UIElement.Clip on an ancestor, the layout clip and a ScrollViewer viewport are all ignored. Pointer hit-testing (SearchDownForTopMostElementAt) does respect clips, so the two disagree: a point that can't be clicked still "hits" the element.
Expected behavior
The element must not be returned at points outside its effective clip, same as WinUI. WinUI's CUIElement::BoundsTestInternalImpl clips the test against the element clip (GetHandOffVisualClipOrElementClip) and the layout clip (LayoutClipGeometry->ClipToFill) before testing the element's bounds (uielement.cpp, BoundsTestInternalImpl). Confirmed by reading the WinUI C++ source; I did not run a native WinUI repro.
How to reproduce it
- Add this runtime test to
src/Uno.UI.RuntimeTests/Microsoft/UI/Xaml/Media/Given_VisualTreeHelper.cs(public API only, so it also compiles on WinUI):
[TestMethod]
public async Task When_FindElementsInHostCoordinates_Clipped()
{
Border visible = new() { Width = 20, Height = 20, Background = new SolidColorBrush(Colors.Green) };
Border clipped = new() { Width = 20, Height = 20, Background = new SolidColorBrush(Colors.Red) };
Canvas.SetLeft(visible, 10);
Canvas.SetTop(visible, 10);
Canvas.SetLeft(clipped, 70);
Canvas.SetTop(clipped, 70);
Canvas sut = new()
{
Width = 100,
Height = 100,
Clip = new RectangleGeometry { Rect = new Rect(0, 0, 50, 50) },
Children = { visible, clipped },
};
await UITestHelper.Load(sut);
var visibleHits = VisualTreeHelper.FindElementsInHostCoordinates(visible.GetAbsoluteBounds().GetCenter(), sut).ToArray();
var clippedHits = VisualTreeHelper.FindElementsInHostCoordinates(clipped.GetAbsoluteBounds().GetCenter(), sut).ToArray();
Assert.IsTrue(visibleHits.Contains(visible), "Control: the child inside the clip should be found");
Assert.IsFalse(clippedHits.Contains(clipped), "A child outside the parent's Clip should not be found");
}- Run it on Skia Desktop (see the
/runtime-testsinstructions in the repo). - The control assertion passes; the last assertion fails (
clippedHits.Contains(clipped)is true), althoughclippedis entirely outside the 50x50 clip.
A ScrollViewer viewport or layout clip goes through the same code, so a child scrolled out of view should be returned too. Only the Clip case is runtime-verified.
Workaround
None through public API. Internally VisualTreeHelper.HitTest is clip-aware.
Works on UWP/WinUI
Yes (per the WinUI source above).
Environment
Skia (reproduced on Skia Desktop)
NuGet package version(s)
Uno master @ 951b7b4, net11.0-desktop
Affected platforms
Skia (Desktop; the code is shared with all heads)
IDE
Not applicable
Relevant plugins
None
Anything else we need to know?
Root cause: IsElementIntersecting takes LayoutInformation.GetLayoutSlot transformed to the root and does Contains(point). Nothing intersects it with the element, layout or transition clips. The recursion in FindElementsInHostCoordinates also doesn't check whether the subtree root itself is hit-test visible or visible.
Related, different problems with the same API: #25267 (duplicates and ordering), #24309 (ancestors only on Skia).
Activity