Skip to content

Commit d114576

Browse files
committed
fix(mobile): Android chip taps open the chip from either half
A chip is one replacement span, so a tap on its right half reported the offset at its end and moved the cursor instead of opening the chip. The tap now also checks the chip's horizontal bounds.
1 parent e6d4256 commit d114576

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

‎apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
132132
tokens.firstOrNull {
133133
(it.type == "context" || it.type == "mention" || it.type == "skill") &&
134134
offset >= it.start &&
135-
offset < it.end
135+
(offset < it.end || (offset == it.end && chipContainsX(it, event.x)))
136136
}
137137
?: return false
138138
if (token.end <= editor.length() &&
@@ -450,6 +450,21 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
450450
)
451451
}
452452

453+
// A chip is one replacement span, so a tap on its right half reports the offset at its end.
454+
private fun chipContainsX(token: ComposerToken, viewX: Float): Boolean {
455+
val layout = editor.layout ?: return false
456+
val x = viewX - editor.totalPaddingLeft + editor.scrollX
457+
val line = layout.getLineForOffset(token.start)
458+
val left = layout.getPrimaryHorizontal(token.start)
459+
val right =
460+
if (layout.getLineForOffset(token.end) == line) {
461+
layout.getPrimaryHorizontal(token.end)
462+
} else {
463+
layout.getLineRight(line)
464+
}
465+
return x >= minOf(left, right) && x <= maxOf(left, right)
466+
}
467+
453468
private fun applyTokenSpans() {
454469
val editable = editor.text ?: return
455470
editable.getSpans(

0 commit comments

Comments
 (0)