Browse Source

Merge pull request #3213 from AvaloniaUI/fixes/3212-formatted-text-hit-testing

fix hit testing skia formatted text.
Benedikt Stebner 6 năm trước cách đây
mục cha
commit
cb03771290
1 tập tin đã thay đổi với 27 bổ sung7 xóa
  1. 27 7
      src/Skia/Avalonia.Skia/FormattedTextImpl.cs

+ 27 - 7
src/Skia/Avalonia.Skia/FormattedTextImpl.cs

@@ -99,9 +99,26 @@ namespace Avalonia.Skia
         public TextHitTestResult HitTestPoint(Point point)
         {
             float y = (float)point.Y;
-            var line = _skiaLines.Find(l => l.Top <= y && (l.Top + l.Height) > y);
 
-            if (!line.Equals(default(AvaloniaFormattedTextLine)))
+            AvaloniaFormattedTextLine line = default;
+
+            float nextTop = 0;
+
+            foreach(var currentLine in _skiaLines)
+            {
+                if(currentLine.Top <= y)
+                {
+                    line = currentLine;
+                    nextTop = currentLine.Top + currentLine.Height;
+                }
+                else
+                {
+                    nextTop = currentLine.Top;
+                    break;
+                }
+            }
+
+            if (!line.Equals(default))
             {
                 var rects = GetRects();
 
@@ -127,12 +144,15 @@ namespace Avalonia.Skia
                                     line.Length : (line.Length - 1);
                 }
 
-                return new TextHitTestResult
+                if (y < nextTop)
                 {
-                    IsInside = false,
-                    TextPosition = line.Start + offset,
-                    IsTrailing = Text.Length == (line.Start + offset + 1)
-                };
+                    return new TextHitTestResult
+                    {
+                        IsInside = false,
+                        TextPosition = line.Start + offset,
+                        IsTrailing = Text.Length == (line.Start + offset + 1)
+                    };
+                }
             }
 
             bool end = point.X > _bounds.Width || point.Y > _lines.Sum(l => l.Height);