Search code examples
kotlinandroid-jetpack-composedesktop-applicationkotlin-multiplatformdouble-click

How do you make Text double-clickable?


I've got a Text control in my JetPack Compose desktop application that I'd like to be double-clickable so that users can add its value to a filter. The text is displayed within a LazyColumn as one of several values.

I know you can make it clickable with Modifier.clickable, but how do I make it double-clickable so that it isn't added by accident via a single-click?


Solution

  • You can use detectTapGestures(onDoubleTap= {}) which is available inside PointerInputScope as

    @Preview
    @Composable
    private fun Test() {
        
        val context = LocalContext.current
        
        Text(
            modifier = Modifier.pointerInput(Unit){
              detectTapGestures(
                  onDoubleTap = {
                      Toast.makeText(context, "Double Tapped", Toast.LENGTH_SHORT).show()
                  }
              )  
            },
           text =  "Some Text to Double Tap",
            fontSize = 26.sp
        )
    }