1 /*
<lambda>null2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.compose.foundation.text
18 
19 import androidx.compose.foundation.gestures.detectTapAndPress
20 import androidx.compose.foundation.interaction.MutableInteractionSource
21 import androidx.compose.foundation.interaction.PressInteraction
22 import androidx.compose.runtime.DisposableEffect
23 import androidx.compose.runtime.mutableStateOf
24 import androidx.compose.runtime.remember
25 import androidx.compose.runtime.rememberCoroutineScope
26 import androidx.compose.runtime.rememberUpdatedState
27 import androidx.compose.ui.Modifier
28 import androidx.compose.ui.composed
29 import androidx.compose.ui.geometry.Offset
30 import androidx.compose.ui.input.pointer.pointerInput
31 import kotlinx.coroutines.launch
32 
33 /** Required for the press and tap [MutableInteractionSource] consistency for TextField. */
34 internal fun Modifier.tapPressTextFieldModifier(
35     interactionSource: MutableInteractionSource?,
36     enabled: Boolean = true,
37     onTap: (Offset) -> Unit
38 ): Modifier =
39     if (enabled)
40         composed {
41             val scope = rememberCoroutineScope()
42             val pressedInteraction = remember { mutableStateOf<PressInteraction.Press?>(null) }
43             val onTapState = rememberUpdatedState(onTap)
44             DisposableEffect(interactionSource) {
45                 onDispose {
46                     pressedInteraction.value?.let { oldValue ->
47                         val interaction = PressInteraction.Cancel(oldValue)
48                         interactionSource?.tryEmit(interaction)
49                         pressedInteraction.value = null
50                     }
51                 }
52             }
53             Modifier.pointerInput(interactionSource) {
54                 detectTapAndPress(
55                     onPress = {
56                         scope.launch {
57                             // Remove any old interactions if we didn't fire stop / cancel properly
58                             pressedInteraction.value?.let { oldValue ->
59                                 val interaction = PressInteraction.Cancel(oldValue)
60                                 interactionSource?.emit(interaction)
61                                 pressedInteraction.value = null
62                             }
63                             val interaction = PressInteraction.Press(it)
64                             interactionSource?.emit(interaction)
65                             pressedInteraction.value = interaction
66                         }
67                         val success = tryAwaitRelease()
68                         scope.launch {
69                             pressedInteraction.value?.let { oldValue ->
70                                 val interaction =
71                                     if (success) {
72                                         PressInteraction.Release(oldValue)
73                                     } else {
74                                         PressInteraction.Cancel(oldValue)
75                                     }
76                                 interactionSource?.emit(interaction)
77                                 pressedInteraction.value = null
78                             }
79                         }
80                     },
81                     onTap = { onTapState.value.invoke(it) }
82                 )
83             }
84         }
85     else this
86