Skip to content

Commit fb31571

Browse files
terrakokeymar
authored andcommitted
Account for screen density for mouse wheel scrolling on web (#2724)
Fixes https://youtrack.jetbrains.com/issue/CMP-9701 ## Release Notes ### Fixes - Web - Fix mouse wheel scrolling on web by accounting for screen density
1 parent 0c22c24 commit fb31571

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

compose/foundation/foundation/src/webMain/kotlin/androidx/compose/foundation/gestures/JsScrollable.web.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import androidx.compose.ui.input.pointer.PointerEvent
2323
import androidx.compose.ui.node.CompositionLocalConsumerModifierNode
2424
import androidx.compose.ui.unit.Density
2525
import androidx.compose.ui.unit.IntSize
26+
import androidx.compose.ui.unit.dp
2627
import androidx.compose.ui.util.fastFold
2728

2829
internal actual fun CompositionLocalConsumerModifierNode.platformScrollConfig(): ScrollConfig = JsConfig
@@ -35,7 +36,7 @@ private object JsConfig : ScrollConfig {
3536
// 2. Provides satisfactory UI behavior
3637
// In future iterations, this value could be refined to enhance UI behavior.
3738
// However, keep in mind that any modifications would also necessitate adjustments to the corresponding tests.
38-
return event.totalScrollDelta * -1f
39+
return event.totalScrollDelta * -1.dp.toPx()
3940
}
4041
}
4142

compose/ui/ui/src/webTest/kotlin/androidx/compose/ui/input/ScrollTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class ScrollTests : OnCanvasTests {
100100
}
101101
}
102102

103-
dispatchEvents(createWheelEvent(clientX = 100, clientY = 100, deltaX = 0.0, deltaY = 200.0))
103+
dispatchEvents(createWheelEvent(clientX = 50, clientY = 50, deltaX = 0.0, deltaY = 100.0))
104104

105105
awaitIdle()
106106

compose/ui/ui/src/webTest/kotlin/androidx/compose/ui/window/WheelEventTests.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ class WheelEventTests : OnCanvasTests {
5353
assertEquals(0, verticalScrollState.value)
5454

5555
// do horizontal scroll, and check that scroll state didn't change
56-
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaX = 10.0)))
56+
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaX = 5.0)))
5757
assertEquals(0, verticalScrollState.value, "vertical scroll was not expected to change")
5858

5959
// vertical scroll
60-
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaY = 10.0)))
60+
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaY = 5.0)))
6161
assertEquals(10, verticalScrollState.value, "vertical scroll was expected to change")
6262
}
6363

@@ -78,11 +78,11 @@ class WheelEventTests : OnCanvasTests {
7878
assertEquals(0, horizontalScrollState.value)
7979

8080
// do vertical scroll, and check that scroll state didn't change
81-
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaY = 10.0)))
81+
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaY = 5.0)))
8282
assertEquals(0, horizontalScrollState.value, "horizontal scroll was not expected to change")
8383

8484
// horizontal scroll
85-
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaX = 10.0)))
85+
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaX = 5.0)))
8686
assertEquals(10, horizontalScrollState.value, "horizontal scroll was expected to change")
8787
}
8888

@@ -104,11 +104,11 @@ class WheelEventTests : OnCanvasTests {
104104
assertEquals(0, horizontalScrollState.value)
105105

106106
// do vertical scroll w/o Shift, and check that scroll state didn't change
107-
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaY = 10.0)))
107+
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaY = 5.0)))
108108
assertEquals(0, horizontalScrollState.value, "horizontal scroll was not expected to change")
109109

110110
// do vertical scroll with Shift
111-
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaY = 10.0, shiftKey = true)))
111+
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaY = 5.0, shiftKey = true)))
112112
assertEquals(10, horizontalScrollState.value, "horizontal scroll was expected to change")
113113
}
114114

@@ -129,15 +129,15 @@ class WheelEventTests : OnCanvasTests {
129129
assertEquals(0, verticalScrollState.value)
130130

131131
// press shift and do horizontal scroll (X-axis)
132-
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaX = 10.0, shiftKey = true)))
132+
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaX = 5.0, shiftKey = true)))
133133
assertEquals(0, verticalScrollState.value, "horizontal scroll was not expected to change")
134134

135135
// press shift and do vertical scroll (Y-axis) - verticalScrollState won't change because Shift is pressed
136-
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaY = 10.0, shiftKey = true)))
136+
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaY = 5.0, shiftKey = true)))
137137
assertEquals(0, verticalScrollState.value, "horizontal scroll was not expected to change")
138138

139139
// no Shift, do vertical scroll (Y-axis)
140-
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaY = 10.0, shiftKey = false)))
140+
getCanvas().dispatchEvent(WheelEvent("wheel", WheelEventInit(deltaY = 5.0, shiftKey = false)))
141141
assertEquals(10, verticalScrollState.value, "horizontal scroll expected to change")
142142
}
143143
}

0 commit comments

Comments
 (0)