JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
15.88k stars 1.15k forks source link

interactionSource.collectIsPressedAsState() does not work #3417

Open anakiK1to opened 1 year ago

anakiK1to commented 1 year ago

Describe the bug Trying to handle isPressed interaction with composable slider i encountered a problem thats its never returns true, it seems to me, thats it's because isDragged fireing first and my click recognaized like very fast drag -> release i'm not sure if this behavior is intended, but for it's more like a bug

Affected platforms

Tested on Desktop

### Versions

To Reproduce

  1. create slider with default implementation
  2. init interaction source with interactionSource = remember { MutableInteractionSource() }
  3. init isPressed with val isPressed by interactionSource.collectIsPressedAsState()
  4. init isDragged with val isDragged by interactionSource.collectIsDraggedAsState()
  5. create text to debug changing isPressed and isDraggeed values
  6. bind interactionSource to slider

Expected behavior I expecteed isPressed to return true when tap on it's line/ point, and expected isDragged to return true when slider's thumb is dragged and false when dragged ends

dima-avdeev-jb commented 1 year ago

Can you please provide a reproducible sample project o GitHub? Or just a block of code here in comments

anakiK1to commented 1 year ago

Sorry to keep you waiting for an answer, simple version of code will look like that

@Composable 
fun slider() {
 val interactionSource = remember { MutableInteractionSource() } 
 val isPressed by interactionSource.collectIsPressedAsState()
 val isDragged by interactionSource.collectIsDraggedAsState()
 val sliderValue by remeber { mutableStateOf(0f) }

Column{
  Slider(
      enabled = true,
      value = sliderValue,
      onValueChange = { newValue ->
      sliderValueRaw = newValue},
      valueRange = 0F..100f,
      interactionSource = interactionSource
   )
  Text(
      text = Text("isPressed: ${isPressed} || isDragged: ${isDragged}"
   )
 }
}

in this example isPressed never becomes true, but isDragged works correctly I'm not sure if isPressed is supposed to work on Slider, however for me it was the expected behavior

sdzshn3 commented 11 months ago

I guess I have similar issue but slightly different use case.

I have a OutlinedTextField acting as DropDown but instead opens a ModalBottomSheet when clicked. So, I'm listening for interactions from MutableInteractionSource trying to invoke onClick when the ineraction is PressInteraction.Release.

This code is working well with Android and iOS but not on Desktop (tested in Mac).

Use the following composable as a reproducer: https://gist.github.com/sdzshn3/4f14132222df30151b4bec6ab84d4e79

okushnikov commented 1 month ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.