Skip to content

Commit 42e1484

Browse files
committed
WIP better click detection.
1 parent 072979d commit 42e1484

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

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

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
1616
override fun onTouchEvent(widget: TextView, text: Spannable, event: MotionEvent): Boolean {
1717
val action = event.action
1818

19-
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) {
19+
if (action == MotionEvent.ACTION_UP) {
2020
var x = event.x.toInt()
2121
var y = event.y.toInt()
2222

@@ -30,9 +30,15 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
3030
val line = layout.getLineForVertical(y)
3131
var off = layout.getOffsetForHorizontal(line, x.toFloat())
3232

33-
if (text.length > off) {
34-
off++
35-
}
33+
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+
// }
3642

3743
// get the character's position. This may be the left or the right edge of the character so, find the
3844
// other edge by inspecting nearby characters (if they exist)
@@ -43,17 +49,42 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
4349
val lineRect = Rect()
4450
layout.getLineBounds(line, lineRect)
4551

46-
if (((x in charPrevX..charX) || (x in charX..charNextX))
47-
&& y >= lineRect.top && y <= lineRect.bottom) {
48-
val link = text.getSpans(off, off, ClickableSpan::class.java).firstOrNull()
52+
val clickedWithinLineHeight = y >= lineRect.top && y <= lineRect.bottom
53+
val clickedToSpanToTheLeftOfCursor = x in charPrevX..charX
54+
val clickedToSpanToTheRightOfCursor = x in charX..charNextX
55+
56+
val clickedOnSpan = clickedWithinLineHeight && (clickedToSpanToTheLeftOfCursor || clickedToSpanToTheRightOfCursor)
57+
58+
val ignoreBounds = isClickedSpanAmbiguous && !clickedToSpanToTheLeftOfCursor && !clickedToSpanToTheRightOfCursor
59+
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
70+
71+
if (isClickedSpanAmbiguous) {
72+
if (clickedToSpanToTheLeftOfCursor) {
73+
link = text.getSpans(off, off, ClickableSpan::class.java)[0]
74+
} else if (clickedToSpanToTheRightOfCursor) {
75+
link = text.getSpans(off, off, ClickableSpan::class.java)[1]
76+
}
77+
} else {
78+
link = text.getSpans(off, off, ClickableSpan::class.java).firstOrNull()
79+
}
4980

50-
// Only react to AztecMediaClickableSpan and UnknownClickableSpan; not to regular links.
5181
if (link != null && (link is AztecMediaClickableSpan || link is UnknownClickableSpan)) {
5282
if (action == MotionEvent.ACTION_UP) {
5383
link.onClick(widget)
5484
}
5585
return true
5686
}
87+
5788
}
5889
}
5990

0 commit comments

Comments
 (0)