Skip to content

Commit

Permalink
Fix keyboard opening when scrolling begins within a TextField (#1176)
Browse files Browse the repository at this point in the history
## Proposed Changes
Handle touch up gesture to focus on TextField. The gesture allows the
containing node to cancel the interaction if necessary.

Issue fixed:
JetBrains/compose-multiplatform#4204
  • Loading branch information
ASalavei committed Apr 12, 2024
1 parent 4ae2a44 commit 476d43b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.fastAll
import androidx.compose.ui.util.fastAny
import androidx.compose.ui.util.fastForEach
import kotlinx.coroutines.*
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

/**
* Detects tap, double-tap, and long press gestures and calls [onTap], [onDoubleTap], and
Expand Down Expand Up @@ -212,39 +215,40 @@ internal suspend fun AwaitPointerEventScope.awaitPress(
*
* The order of callback execution is guaranteed.
*
* @param onTap A callback function that will be called when a single tap gesture is detected. It will receive the position (Offset) where the tap occurred.
* @param onDoubleTap A callback function that will be called when a double tap gesture is detected. It will receive the position (Offset) where the tap occurred.
* @param onTripleTap A callback function that will be called when a triple tap gesture is detected. It will receive the position (Offset) where the tap occurred.
* @param onTapRelease A callback function that will be called when a single tap and release gesture is detected. It will receive the position (Offset) where the tap occurred.
* @param onDoubleTapPress A callback function that will be called when a double tap gesture is detected. It will receive the position (Offset) where the tap occurred.
* @param onTripleTapPress A callback function that will be called when a triple tap gesture is detected. It will receive the position (Offset) where the tap occurred.
*/
internal suspend fun PointerInputScope.detectRepeatingTapGestures(
onTap: ((Offset) -> Unit)? = null,
onDoubleTap: ((Offset) -> Unit)? = null,
onTripleTap: ((Offset) -> Unit)? = null,
onTapRelease: ((Offset) -> Unit)? = null,
onDoubleTapPress: ((Offset) -> Unit)? = null,
onTripleTapPress: ((Offset) -> Unit)? = null,
) {
awaitEachGesture {
val touchesCounter = ClicksCounter(viewConfiguration, clicksSlop = 50.dp.toPx())
while (true) {
val down = awaitFirstDown()
touchesCounter.update(down)
val downChange = down
val downChange = awaitFirstDown()
touchesCounter.update(downChange)
when (touchesCounter.clicks) {
1 -> {
if (onTap != null) {
onTap(downChange.position)
downChange.consume()
if (onTapRelease != null) {
waitForUpOrCancellation()?.let {
onTapRelease(it.position)
it.consume()
}
}
}

2 -> {
if (onDoubleTap != null) {
onDoubleTap(downChange.position)
if (onDoubleTapPress != null) {
onDoubleTapPress(downChange.position)
downChange.consume()
}
}

else -> {
if (onTripleTap != null) {
onTripleTap(downChange.position)
if (onTripleTapPress != null) {
onTripleTapPress(downChange.position)
downChange.consume()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private fun getTapHandlerModifier(
*/
return Modifier.pointerInput(interactionSource) {
detectRepeatingTapGestures(
onTap = { touchPointOffset ->
onTapRelease = { touchPointOffset ->
if (currentState.hasFocus) {
// To show keyboard if it was hidden. Even in selection mode (like native)
requestFocusAndShowKeyboardIfNeeded(
Expand Down Expand Up @@ -155,10 +155,10 @@ private fun getTapHandlerModifier(
currentState.handleState = HandleState.Cursor
}
},
onDoubleTap = {
onDoubleTapPress = {
currentManager.doRepeatingTapSelection(it, SelectionAdjustment.Word)
},
onTripleTap = {
onTripleTapPress = {
currentManager.doRepeatingTapSelection(it, SelectionAdjustment.Paragraph)
}
)
Expand Down

0 comments on commit 476d43b

Please sign in to comment.