11package org.wordpress.aztec
22
3+ import android.graphics.Rect
34import android.text.Selection
45import android.text.Spannable
56import android.text.method.ArrowKeyMovementMethod
67import android.text.style.ClickableSpan
8+ import android.util.Log
79import android.view.MotionEvent
810import android.widget.TextView
911import org.wordpress.aztec.spans.AztecMediaClickableSpan
@@ -30,17 +32,29 @@ object EnhancedMovementMethod : ArrowKeyMovementMethod() {
3032 val line = layout.getLineForVertical(y)
3133 val off = layout.getOffsetForHorizontal(line, x.toFloat())
3234
33- val link = text.getSpans(off, off, ClickableSpan ::class .java).firstOrNull()
35+ // get the character's position. This may be the left or the right edge of the character so, find the
36+ // other edge by inspecting nearby characters (if they exist)
37+ val charX = layout.getPrimaryHorizontal(off)
38+ val charPrevX = if (off > 0 ) layout.getPrimaryHorizontal(off - 1 ) else charX
39+ val charNextX = if (off < text.length) layout.getPrimaryHorizontal(off + 1 ) else charX
3440
35- // Only react to AztecMediaClickableSpan and UnknownClickableSpan; not to regular links.
36- if (link != null && (link is AztecMediaClickableSpan || link is UnknownClickableSpan )) {
37- if (action == MotionEvent .ACTION_UP ) {
38- link.onClick(widget)
39- } else {
40- Selection .setSelection(text, text.getSpanStart(link), text.getSpanEnd(link))
41- }
41+ val lineRect = Rect ()
42+ layout.getLineBounds(line, lineRect)
43+
44+ if (((x >= charPrevX && x <= charX) || (x >= charX && x <= charNextX))
45+ && y >= lineRect.top && y <= lineRect.bottom) {
46+ val link = text.getSpans(off, off, ClickableSpan ::class .java).firstOrNull()
4247
43- return true
48+ // Only react to AztecMediaClickableSpan and UnknownClickableSpan; not to regular links.
49+ if (link != null && (link is AztecMediaClickableSpan || link is UnknownClickableSpan )) {
50+ if (action == MotionEvent .ACTION_UP ) {
51+ link.onClick(widget)
52+ } else {
53+ Selection .setSelection(text, text.getSpanStart(link), text.getSpanEnd(link))
54+ }
55+
56+ return true
57+ }
4458 }
4559 }
4660
0 commit comments