Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clicking TextField should consume pointer press event #5029

Open
mgroth0 opened this issue Jun 26, 2024 · 1 comment
Open

Clicking TextField should consume pointer press event #5029

mgroth0 opened this issue Jun 26, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@mgroth0
Copy link

mgroth0 commented Jun 26, 2024

Describe the bug

Clicking a TextField should consume the press event (in the Main pass). But instead, it is not consumed and propagates to the parent element.

Affected platforms

  • Desktop (macOS)

Versions

  • Libraries:

    • Compose Multiplatform version: 1.7.0-dev1698
  • Kotlin version:

  • OS version(s) (required for Desktop and iOS issues): Mac 14.5 (23F79)

  • OS architecture (x86 or arm64): arm64

  • JDK (for desktop issues): 20

To Reproduce
Steps to reproduce the behavior:

  1. Run this code snippet:
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.material3.TextField
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.awtEventOrNull
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.PointerEventType
import androidx.compose.ui.input.pointer.onPointerEvent
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
    val text = mutableStateOf("some text")
    var nextPointerEvent = 0
    application {
        Window(
            onCloseRequest = ::exitApplication
        ) {
            Box(
                Modifier
                    .size(400.dp)
                    .background(Color.Yellow)
                    .onPointerEvent(PointerEventType.Press) {
                        val consumed = it.awtEventOrNull!!.isConsumed
                        text.value = "Got pointer event ${nextPointerEvent++} (consumed=$consumed)"
                    }
            ) {
                TextField(
                    value = text.value,
                    onValueChange = {
                        text.value = it
                    }
                )
            }
        }
    }
}
  1. Click on the text field
  2. See that the pointer event progated to the parent

Expected behavior

The event handler registered to the container should not receive the click event.

If the pass was PointerEventPass.Initial, then the click event should be received. However, the default value for pass is PointerEventPass.Main. Since the TextField behaves as expected, it seems that the TextField does handle the mouse press event. However, it then fails to consume it and prevent it from propagating further up the tree.

Additional context

I have a mouse click handler on the TextField container in my app. When it receives a click, it uses focusRequester.requestFocus() on its own component. But when the click was actually in the TextField (inside the container), this unexpectedly messes with the focus in the TextField.

@mgroth0 mgroth0 added bug Something isn't working submitted labels Jun 26, 2024
@m-sasha
Copy link
Contributor

m-sasha commented Jun 26, 2024

Does Android consume taps on TextField?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants
@kropp @m-sasha @mgroth0 and others