avalonia-docs icon indicating copy to clipboard operation
avalonia-docs copied to clipboard

Add docs for ICustomHitTest

Open maxkatz6 opened this issue 6 months ago • 3 comments

Possible use case and a demo is extending hit test area:

public class BiggerButton : Button, ICustomHitTest
{
    public BiggerButton()
    {
        ClipToBounds = false; // By default, Avalonia optimizes hit testing by clipping to bounds.
    }

    protected override Type StyleKeyOverride => typeof(Button); // unrelated to hit test, we just want to reuse Button styles

    bool ICustomHitTest.HitTest(Point point) => Bounds
        .WithX(0).WithY(0) // reset position relative to the parent, as ICustomHitTest expects the point to be in the local coordinate system
        .Inflate(100) // inflate by how much we want hit test to be extended
        .Contains(point);
}

maxkatz6 avatar Jun 14 '25 05:06 maxkatz6

Oh that works? Extending the grabber area of a GridSplitter (or other resizing controls) would be another good use case.

stevemonaco avatar Jun 14 '25 05:06 stevemonaco

Oh, thinking about that before would save a ton of time with GridSplitter indeed

maxkatz6 avatar Jun 14 '25 09:06 maxkatz6

But it works only until there is another hit-testable element overlapping this area, that was earlier in the tree (in reverse order)

maxkatz6 avatar Jun 14 '25 09:06 maxkatz6