Skip to content

Commit 4fbe02a

Browse files
committed
Fixed hit detection.
1 parent 42e1484 commit 4fbe02a

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed

aztec/src/main/kotlin/org/wordpress/aztec/EnhancedMovementMethod.kt

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,12 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
2828

2929
val layout = widget.layout
3030
val line = layout.getLineForVertical(y)
31-
var off = layout.getOffsetForHorizontal(line, x.toFloat())
31+
val off = layout.getOffsetForHorizontal(line, x.toFloat())
3232

33+
val clickedSpanBordersAnotherOne = (text.getSpans(off, off, AztecMediaClickableSpan::class.java).size == 1 &&
34+
text.getSpans(off + 1, off + 1, AztecMediaClickableSpan::class.java).isNotEmpty())
3335

34-
val isClickedSpanAmbiguous = text.getSpans(off, off, AztecMediaClickableSpan::class.java).size > 1 ||
35-
(text.getSpans(off, off, AztecMediaClickableSpan::class.java).size == 1 &&
36-
text.getSpans(off + 1, off + 1, AztecMediaClickableSpan::class.java).isNotEmpty())
37-
// val ignoreBoundingBox =
38-
39-
// if (text.length > off && ignoreBoundingBox) {
40-
// off++
41-
// }
36+
val isClickedSpanAmbiguous = text.getSpans(off, off, AztecMediaClickableSpan::class.java).size > 1
4237

4338
// get the character's position. This may be the left or the right edge of the character so, find the
4439
// other edge by inspecting nearby characters (if they exist)
@@ -50,41 +45,36 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
5045
layout.getLineBounds(line, lineRect)
5146

5247
val clickedWithinLineHeight = y >= lineRect.top && y <= lineRect.bottom
53-
val clickedToSpanToTheLeftOfCursor = x in charPrevX..charX
54-
val clickedToSpanToTheRightOfCursor = x in charX..charNextX
48+
val clickedOnSpanToTheLeftOfCursor = x in charPrevX..charX
49+
val clickedOnSpanToTheRightOfCursor = x in charX..charNextX
5550

56-
val clickedOnSpan = clickedWithinLineHeight && (clickedToSpanToTheLeftOfCursor || clickedToSpanToTheRightOfCursor)
51+
val clickedOnSpan = clickedWithinLineHeight && (clickedOnSpanToTheLeftOfCursor || clickedOnSpanToTheRightOfCursor)
5752

58-
val ignoreBounds = isClickedSpanAmbiguous && !clickedToSpanToTheLeftOfCursor && !clickedToSpanToTheRightOfCursor
53+
val failedToPinpointClickedSpan = (isClickedSpanAmbiguous || clickedSpanBordersAnotherOne)
54+
&& !clickedOnSpanToTheLeftOfCursor && !clickedOnSpanToTheRightOfCursor
5955

60-
if (ignoreBounds) {
61-
val link = text.getSpans(off+1, off+1, ClickableSpan::class.java).firstOrNull()
62-
if (link != null && (link is AztecMediaClickableSpan || link is UnknownClickableSpan)) {
63-
if (action == MotionEvent.ACTION_UP) {
64-
link.onClick(widget)
65-
}
66-
return true
67-
}
68-
} else if (clickedOnSpan) {
69-
var link: ClickableSpan? = null
7056

57+
var link: ClickableSpan? = null
58+
59+
if (clickedOnSpan) {
7160
if (isClickedSpanAmbiguous) {
72-
if (clickedToSpanToTheLeftOfCursor) {
61+
if (clickedOnSpanToTheLeftOfCursor) {
7362
link = text.getSpans(off, off, ClickableSpan::class.java)[0]
74-
} else if (clickedToSpanToTheRightOfCursor) {
63+
} else if (clickedOnSpanToTheRightOfCursor) {
7564
link = text.getSpans(off, off, ClickableSpan::class.java)[1]
7665
}
7766
} else {
7867
link = text.getSpans(off, off, ClickableSpan::class.java).firstOrNull()
7968
}
69+
} else if (failedToPinpointClickedSpan) {
70+
link = text.getSpans(off, off, ClickableSpan::class.java).firstOrNull { text.getSpanStart(it) == off }
71+
}
8072

81-
if (link != null && (link is AztecMediaClickableSpan || link is UnknownClickableSpan)) {
82-
if (action == MotionEvent.ACTION_UP) {
83-
link.onClick(widget)
84-
}
85-
return true
73+
if (link != null && (link is AztecMediaClickableSpan || link is UnknownClickableSpan)) {
74+
if (action == MotionEvent.ACTION_UP) {
75+
link.onClick(widget)
8676
}
87-
77+
return true
8878
}
8979
}
9080

0 commit comments

Comments
 (0)