forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Double tap and triple tap gesture handling in textfields on iOS (#831)
## Proposed Changes Added UIKitTapGesturesDetector.kt - repeating tap gestures detector. Moved and separated the logic for handling taps in the textfields. Added handling of double and triple taps in the textfields on iOS. This PR also includes review fixes from [my previous, closed one](#798) ## Testing Test: Open any screen with textfield with mutable text, then try to double or triple tap on text (or if its empty, write something in it). Try to test with focused textfield and unfocused. Double tap selects the word, triple tap selects the paragraph. ## Issues Fixed JetBrains/compose-multiplatform#2682 https://youtrack.jetbrains.com/issue/COMPOSE-333/iOS-TextField-Incorrect-behavior-of-text-selection-by-double-tap (YT double version of the previous issue) https://youtrack.jetbrains.com/issue/COMPOSE-409/iOS-TextField-support-triple-tap-handle ## Google CLA You need to sign the Google Contributor’s License Agreement at https://cla.developers.google.com/. This is needed since we synchronise most of the code with Google’s AOSP repository. Signing this agreement allows us to synchronise code from your Pull Requests as well. --------- Co-authored-by: dima.avdeev <dima.avdeev@jetbrains.com> Co-authored-by: Igor Demin <igordmn@users.noreply.github.com>
- Loading branch information
1 parent
da1be45
commit 3adcf23
Showing
10 changed files
with
601 additions
and
54 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
...c/androidMain/kotlin/androidx/compose/foundation/text/TextFieldPointerModifier.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2023 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.compose.foundation.text | ||
|
||
import androidx.compose.foundation.interaction.MutableInteractionSource | ||
import androidx.compose.foundation.text.selection.TextFieldSelectionManager | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.focus.FocusRequester | ||
import androidx.compose.ui.text.input.OffsetMapping | ||
|
||
@Composable | ||
internal actual fun Modifier.textFieldPointer( | ||
manager: TextFieldSelectionManager, | ||
enabled: Boolean, | ||
interactionSource: MutableInteractionSource?, | ||
state: TextFieldState, | ||
focusRequester: FocusRequester, | ||
readOnly: Boolean, | ||
offsetMapping: OffsetMapping | ||
): Modifier = Modifier.defaultTextFieldPointer( | ||
manager, | ||
enabled, | ||
interactionSource, | ||
state, | ||
focusRequester, | ||
readOnly, | ||
offsetMapping, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...src/commonMain/kotlin/androidx/compose/foundation/text/TextFieldPointerModifier.common.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2023 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.compose.foundation.text | ||
|
||
import androidx.compose.foundation.interaction.MutableInteractionSource | ||
import androidx.compose.foundation.text.selection.TextFieldSelectionManager | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.focus.FocusRequester | ||
import androidx.compose.ui.input.pointer.pointerHoverIcon | ||
import androidx.compose.ui.text.input.OffsetMapping | ||
|
||
@Composable | ||
internal expect fun Modifier.textFieldPointer( | ||
manager: TextFieldSelectionManager, | ||
enabled: Boolean, | ||
interactionSource: MutableInteractionSource?, | ||
state: TextFieldState, | ||
focusRequester: FocusRequester, | ||
readOnly: Boolean, | ||
offsetMapping: OffsetMapping | ||
): Modifier | ||
|
||
@Composable | ||
@OptIn(InternalFoundationTextApi::class) | ||
internal fun Modifier.defaultTextFieldPointer( | ||
manager: TextFieldSelectionManager, | ||
enabled: Boolean, | ||
interactionSource: MutableInteractionSource?, | ||
state: TextFieldState, | ||
focusRequester: FocusRequester, | ||
readOnly: Boolean, | ||
offsetMapping: OffsetMapping | ||
): Modifier = if (isInTouchMode) { | ||
val selectionModifier = | ||
Modifier.longPressDragGestureFilter(manager.touchSelectionObserver, enabled) | ||
this | ||
.tapPressTextFieldModifier(interactionSource, enabled) { offset -> | ||
tapTextFieldToFocus(state, focusRequester, !readOnly) | ||
if (state.hasFocus) { | ||
if (state.handleState != HandleState.Selection) { | ||
state.layoutResult?.let { layoutResult -> | ||
TextFieldDelegate.setCursorOffset( | ||
offset, | ||
layoutResult, | ||
state.processor, | ||
offsetMapping, | ||
state.onValueChange | ||
) | ||
// Won't enter cursor state when text is empty. | ||
if (state.textDelegate.text.isNotEmpty()) { | ||
state.handleState = HandleState.Cursor | ||
} | ||
} | ||
} else { | ||
manager.deselect(offset) | ||
} | ||
} | ||
} | ||
.then(selectionModifier) | ||
.pointerHoverIcon(textPointerIcon) | ||
} else { | ||
this | ||
.mouseDragGestureDetector( | ||
observer = manager.mouseSelectionObserver, | ||
enabled = enabled | ||
) | ||
.pointerHoverIcon(textPointerIcon) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...c/desktopMain/kotlin/androidx/compose/foundation/text/TextFieldPointerModifier.desktop.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2023 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.compose.foundation.text | ||
|
||
import androidx.compose.foundation.interaction.MutableInteractionSource | ||
import androidx.compose.foundation.text.selection.TextFieldSelectionManager | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.focus.FocusRequester | ||
import androidx.compose.ui.text.input.OffsetMapping | ||
|
||
@Composable | ||
internal actual fun Modifier.textFieldPointer( | ||
manager: TextFieldSelectionManager, | ||
enabled: Boolean, | ||
interactionSource: MutableInteractionSource?, | ||
state: TextFieldState, | ||
focusRequester: FocusRequester, | ||
readOnly: Boolean, | ||
offsetMapping: OffsetMapping | ||
): Modifier = Modifier.defaultTextFieldPointer( | ||
manager, | ||
enabled, | ||
interactionSource, | ||
state, | ||
focusRequester, | ||
readOnly, | ||
offsetMapping, | ||
) |
43 changes: 43 additions & 0 deletions
43
...src/jsWasmMain/kotlin/androidx/compose/foundation/text/TextFieldPointerModifier.jsWasm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2023 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.compose.foundation.text | ||
|
||
import androidx.compose.foundation.interaction.MutableInteractionSource | ||
import androidx.compose.foundation.text.selection.TextFieldSelectionManager | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.focus.FocusRequester | ||
import androidx.compose.ui.text.input.OffsetMapping | ||
|
||
@Composable | ||
internal actual fun Modifier.textFieldPointer( | ||
manager: TextFieldSelectionManager, | ||
enabled: Boolean, | ||
interactionSource: MutableInteractionSource?, | ||
state: TextFieldState, | ||
focusRequester: FocusRequester, | ||
readOnly: Boolean, | ||
offsetMapping: OffsetMapping | ||
): Modifier = Modifier.defaultTextFieldPointer( | ||
manager, | ||
enabled, | ||
interactionSource, | ||
state, | ||
focusRequester, | ||
readOnly, | ||
offsetMapping, | ||
) |
Oops, something went wrong.