From 3eada7eb202b3df24a8bc75f650c0af65268669a Mon Sep 17 00:00:00 2001 From: donandren Date: Fri, 27 May 2016 16:18:56 +0300 Subject: [PATCH] Fixes memory leak in hit testing simplifies hit testing not to use geometry, but simple transforms --- src/Avalonia.Input/InputExtensions.cs | 6 ++--- src/Avalonia.SceneGraph/Rect.cs | 4 ++-- .../VisualTree/TransformedBounds.cs | 22 +++++++++---------- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/Avalonia.Input/InputExtensions.cs b/src/Avalonia.Input/InputExtensions.cs index a2321f3493..5d80653322 100644 --- a/src/Avalonia.Input/InputExtensions.cs +++ b/src/Avalonia.Input/InputExtensions.cs @@ -25,7 +25,6 @@ namespace Avalonia.Input { Contract.Requires(element != null); var transformedBounds = BoundsTracker.GetTransformedBounds((Visual)element); - var geometry = transformedBounds.GetTransformedBoundsGeometry(); if (element.IsVisible && element.IsHitTestVisible && @@ -42,7 +41,7 @@ namespace Avalonia.Input } } - if (geometry.FillContains(p)) + if (transformedBounds.Contains(p)) { yield return element; } @@ -71,7 +70,6 @@ namespace Avalonia.Input }) .OrderBy(x => x, null) .Select(x => x.Element); - } private class ZOrderElement : IComparable @@ -95,4 +93,4 @@ namespace Avalonia.Input } } } -} +} \ No newline at end of file diff --git a/src/Avalonia.SceneGraph/Rect.cs b/src/Avalonia.SceneGraph/Rect.cs index de4d13890b..806ad39b53 100644 --- a/src/Avalonia.SceneGraph/Rect.cs +++ b/src/Avalonia.SceneGraph/Rect.cs @@ -230,8 +230,8 @@ namespace Avalonia /// true if the point is in the bounds of the rectangle; otherwise false. public bool Contains(Point p) { - return p.X >= _x && p.X < _x + _width && - p.Y >= _y && p.Y < _y + _height; + return p.X >= _x && p.X <= _x + _width && + p.Y >= _y && p.Y <= _y + _height; } /// diff --git a/src/Avalonia.SceneGraph/VisualTree/TransformedBounds.cs b/src/Avalonia.SceneGraph/VisualTree/TransformedBounds.cs index da8ed01ccc..4c548669bd 100644 --- a/src/Avalonia.SceneGraph/VisualTree/TransformedBounds.cs +++ b/src/Avalonia.SceneGraph/VisualTree/TransformedBounds.cs @@ -38,20 +38,18 @@ namespace Avalonia.VisualTree /// public Matrix Transform { get; } - public Geometry GetTransformedBoundsGeometry() + public bool Contains(Point point) { - StreamGeometry geometry = new StreamGeometry(); - using (var context = geometry.Open()) + if (Transform.HasInverse) { - context.SetFillRule(FillRule.EvenOdd); - context.BeginFigure(Bounds.TopLeft * Transform, true); - context.LineTo(Bounds.TopRight * Transform); - context.LineTo(Bounds.BottomRight * Transform); - context.LineTo(Bounds.BottomLeft * Transform); - context.LineTo(Bounds.TopLeft * Transform); - context.EndFigure(true); + Point trPoint = point * Transform.Invert(); + + return Bounds.Contains(trPoint); + } + else + { + return Bounds.Contains(point); } - return geometry; } } -} +} \ No newline at end of file