compose-multiplatform icon indicating copy to clipboard operation
compose-multiplatform copied to clipboard

Scrolling by wheel did not produce scroll offset to NestedScrollConnection

Open cy745 opened this issue 1 year ago • 1 comments

Describe the bug When a scrollable scroll arrives to the bound by wheel scrolling, it stops the scroll event. It makes the nested scroll can not work correctly.

Affected platforms

  • Desktop (Windows, Linux, macOS)
  • Web (K/Wasm) - Canvas based API
  • Web (K/JS) - Canvas based API
  • Web (K/JS) - HTML library

In summary, it affeted all the platforms that use wheel to scroll.

Versions

  • Libraries:
    • Compose Multiplatform version: 1.6.10
  • Kotlin version: 2.0.0

To Reproduce Copy and try this demo.

 @Composable
 fun TestScreen() {
     val spacerHeight = remember { mutableStateOf(0f) }
 
     val nestedScrollConnection = remember {
         object : NestedScrollConnection {
             override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
                 if (available.y < 0) {
                     val old = spacerHeight.value
                     spacerHeight.value = (spacerHeight.value + available.y).coerceAtLeast(0f)
                     val conumed = spacerHeight.value - old
 
                     println("[onPreScroll]: spacerHeight: ${spacerHeight.value} $source")
                     return available.copy(y = conumed)
                 }
                 return super.onPreScroll(available, source)
             }
 
             override fun onPostScroll(consumed: Offset, available: Offset, source: NestedScrollSource): Offset {
                 if (available.y > 0) {
                     spacerHeight.value += available.y
                     println("[onPostScroll]: spacerHeight: ${spacerHeight.value} $source")
 
                     return available
                 }
                 return super.onPostScroll(consumed, available, source)
             }
 
             override suspend fun onPreFling(available: Velocity): Velocity {
                 println("[onPreFling]: ${available.y}")
                 return super.onPreFling(available)
             }
 
             override suspend fun onPostFling(consumed: Velocity, available: Velocity): Velocity {
                 println("[onPostFling]: ${available.y}")
                 return super.onPostFling(consumed, available)
             }
         }
     }
 
     LazyColumn(
         modifier = Modifier.nestedScroll(nestedScrollConnection)
     ) {
         item {
             Spacer(Modifier
                 .fillMaxWidth()
                 .background(color = Color.Black)
                 .layout { measurable, constraints ->
                     val placeable =
                         measurable.measure(
                             constraints.copy(
                                 minHeight = spacerHeight.value.toInt(),
                                 maxHeight = spacerHeight.value.toInt()
                             )
                         )
                     layout(constraints.maxWidth, spacerHeight.value.toInt()) {
                         placeable.place(0, 0)
                     }
                 })
         }
 
         items(50) {
             Text(
                 modifier = Modifier
                     .fillMaxWidth()
                     .padding(32.dp),
                 text = "Card: $it"
             )
         }
     }
 }

Expected behavior It should be the same as the mobile user touching behavior, just like the video below.

Screenshots

https://github.com/JetBrains/compose-multiplatform/assets/35896157/4191c09a-198e-4639-b6f4-799178e28034

Additional context nope

cy745 avatar Jun 15 '24 16:06 cy745

I can confirm this when scrolling using a trackpad on a Macbook. No onPreScroll event received by NestedScrollConnection. Compose Multiplatform 1.6.10. My code is similar to @cy745's one so I'm not attaching it. The same code works on Android.

Him188 avatar Jun 28 '24 22:06 Him188

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

okushnikov avatar Aug 26 '24 13:08 okushnikov