Skip to content

Commit

Permalink
Fix nested scrolling on mouse wheel (#1055)
Browse files Browse the repository at this point in the history
## Proposed Changes

- Use `dispatchScroll` with a new `NestedScrollSource.Wheel` instead of
direct call of `scrollBy`/`dispatchRawDelta`
- Set scrolling priority to `MutatePriority.UserInput` during mouse
wheel scroll animation
- Move all scroll delta dispatching into a single coroutine
- Remove threshold (logic where small delta was applied without
animation), `shouldApplyImmediately` flag based on
`isPreciseWheelScroll` handles it instead.
- Wait `ProgressTimeout` after each mouse wheel event before resetting
`isScrollInProgress` flag
- Enable this logic by default (old "raw" dispatching failed our tests
on iOS)

## Testing

Test: Check "NestedScroll" page in mpp demo + unit tests

## Issues Fixed

Fixes JetBrains/compose-multiplatform#653
Fixes JetBrains/compose-multiplatform#1423
  • Loading branch information
MatkovIvan committed Feb 14, 2024
1 parent 8fd92b6 commit 3d6ece8
Show file tree
Hide file tree
Showing 7 changed files with 305 additions and 128 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ internal interface ScrollConfig {
* Enables animated transition of scroll on mouse wheel events.
*/
val isSmoothScrollingEnabled: Boolean
get() = false
get() = true

fun isPreciseWheelScroll(event: PointerEvent): Boolean = false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ import androidx.compose.ui.util.fastFold
internal actual fun CompositionLocalConsumerModifierNode.platformScrollConfig(): ScrollConfig = JsConfig

private object JsConfig : ScrollConfig {

override val isSmoothScrollingEnabled: Boolean
get() = true

override fun Density.calculateMouseWheelScroll(event: PointerEvent, bounds: IntSize): Offset {
// Note: The returned offset value here is not strictly accurate.
// However, it serves two primary purposes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -768,17 +768,19 @@ class ScrollableTest {

@OptIn(ExperimentalTestApi::class)
@Test
fun scrollable_nestedScroll_disabledForMouseWheel() = runSkikoComposeUiTest {
fun scrollable_nestedScroll_childPartialConsumptionForMouseWheel() = runSkikoComposeUiTest {
var innerDrag = 0f
var outerDrag = 0f
val outerState = ScrollableState(
consumeScrollDelta = {
// Since the child has already consumed half, the parent will consume the rest.
outerDrag += it
it
}
)
val innerState = ScrollableState(
consumeScrollDelta = {
// Child consumes half, leaving the rest for the parent to consume.
innerDrag += it / 2
it / 2
}
Expand Down Expand Up @@ -812,7 +814,10 @@ class ScrollableTest {
}
runOnIdle {
assertThat(innerDrag).isGreaterThan(0f)
assertThat(outerDrag).isEqualTo(0f)
assertThat(outerDrag).isGreaterThan(0f)
// Since child (inner) consumes half of the scroll, the parent (outer) consumes the
// remainder (which is half as well), so they will be equal.
assertThat(innerDrag).isEqualTo(outerDrag)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ val Components = Screen.Selection(
LazyLayouts,
MaterialComponents,
Material3Components,
Screen.Example("NestedScroll") { NestedScrollExample() },
Screen.Example("Selection") { SelectionExample() }
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.mpp.demo.components

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt

// Copy from compose/ui/ui/samples/src/main/java/androidx/compose/ui/samples/NestedScrollSamples.kt
@Composable
fun NestedScrollExample() {
// here we use LazyColumn that has build-in nested scroll, but we want to act like a
// parent for this LazyColumn and participate in its nested scroll.
// Let's make a collapsing toolbar for LazyColumn
val toolbarHeight = 48.dp
val toolbarHeightPx = with(LocalDensity.current) { toolbarHeight.roundToPx().toFloat() }
// our offset to collapse toolbar
val toolbarOffsetHeightPx = remember { mutableStateOf(0f) }
// now, let's create connection to the nested scroll system and listen to the scroll
// happening inside child LazyColumn
val nestedScrollConnection = remember {
object : NestedScrollConnection {
override fun onPreScroll(available: Offset, source: NestedScrollSource): Offset {
// try to consume before LazyColumn to collapse toolbar if needed, hence pre-scroll
val delta = available.y
val newOffset = toolbarOffsetHeightPx.value + delta
toolbarOffsetHeightPx.value = newOffset.coerceIn(-toolbarHeightPx, 0f)
// here's the catch: let's pretend we consumed 0 in any case, since we want
// LazyColumn to scroll anyway for good UX
// We're basically watching scroll without taking it
return Offset.Zero
}
}
}
Box(
Modifier
.fillMaxSize()
// attach as a parent to the nested scroll system
.nestedScroll(nestedScrollConnection)
) {
// our list with build in nested scroll support that will notify us about its scroll
LazyColumn(contentPadding = PaddingValues(top = toolbarHeight)) {
items(100) { index ->
Text("I'm item $index", modifier = Modifier.fillMaxWidth().padding(16.dp))
}
}
TopAppBar(
modifier = Modifier
.height(toolbarHeight)
.offset { IntOffset(x = 0, y = toolbarOffsetHeightPx.value.roundToInt()) },
title = { Text("toolbar offset is ${toolbarOffsetHeightPx.value}") }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ class MouseMoveTest {
val collector2 = EventCollector()

setContent {
LazyColumn(Modifier.size(10.dp)) {
LazyColumn(Modifier.size(12.dp)) {
items(2) {
Box(
modifier = Modifier
Expand All @@ -382,16 +382,16 @@ class MouseMoveTest {
}
}

scene.sendPointerEvent(PointerEventType.Enter, Offset(0f, 0f))
scene.sendPointerEvent(PointerEventType.Enter, Offset(5f, 5f))
collector1.assertCounts(enter = 1, exit = 0)
collector2.assertCounts(enter = 0, exit = 0)

scene.sendPointerEvent(PointerEventType.Scroll, Offset(0f, 0f), scrollDelta = Offset(0f, 10000f))
scene.sendPointerEvent(PointerEventType.Scroll, Offset(5f, 5f), scrollDelta = Offset(0f, 10000f))
waitForIdle()
collector1.assertCounts(enter = 1, exit = 1)
collector2.assertCounts(enter = 1, exit = 0)

scene.sendPointerEvent(PointerEventType.Scroll, Offset(0f, 0f), scrollDelta = Offset(0f, -10000f))
scene.sendPointerEvent(PointerEventType.Scroll, Offset(5f, 5f), scrollDelta = Offset(0f, -10000f))
waitForIdle()
collector1.assertCounts(enter = 2, exit = 1)
collector2.assertCounts(enter = 1, exit = 1)
Expand Down

0 comments on commit 3d6ece8

Please sign in to comment.