Skip to content

Commit 9c975b6

Browse files
committed
optionally scroll lazy list by drag (enabled by default)
1 parent f03ee40 commit 9c975b6

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

  • kool-core/src/commonMain/kotlin/de/fabmax/kool/modules/ui2

kool-core/src/commonMain/kotlin/de/fabmax/kool/modules/ui2/LazyList.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package de.fabmax.kool.modules.ui2
22

33
import de.fabmax.kool.KoolContext
44
import de.fabmax.kool.math.MutableVec2f
5+
import de.fabmax.kool.math.Vec2d
56
import de.fabmax.kool.math.clamp
67
import de.fabmax.kool.util.Color
78
import de.fabmax.kool.util.logE
@@ -75,6 +76,7 @@ fun UiScope.LazyList(
7576
scrollPaneModifier: ((ScrollPaneModifier) -> Unit)? = null,
7677
vScrollbarModifier: ((ScrollbarModifier) -> Unit)? = null,
7778
hScrollbarModifier: ((ScrollbarModifier) -> Unit)? = null,
79+
isScrollByDrag: Boolean = true,
7880
state: LazyListState = rememberListState(),
7981
scopeName: String? = null,
8082
block: LazyListScope.() -> Unit
@@ -99,6 +101,26 @@ fun UiScope.LazyList(
99101
}
100102
}
101103

104+
if (isScrollByDrag) {
105+
var lastTouchDrag by remember(Vec2d.ZERO)
106+
modifier
107+
.onDragStart {
108+
lastTouchDrag = Vec2d(it.pointer.dragDeltaX,it.pointer.dragDeltaY)
109+
}
110+
.onDrag {
111+
val drag = Vec2d(it.pointer.dragDeltaX, it.pointer.dragDeltaY)
112+
val delta = lastTouchDrag - drag
113+
lastTouchDrag = drag
114+
115+
if (isScrollableHorizontal && delta.x != 0.0) {
116+
state.scrollDpX(Dp.fromPx(delta.x.toFloat()).value)
117+
}
118+
if (isScrollableVertical && delta.y != 0.0) {
119+
state.scrollDpY(Dp.fromPx(delta.y.toFloat()).value)
120+
}
121+
}
122+
}
123+
102124
containerModifier?.invoke(modifier)
103125

104126
ScrollPane(state) {

0 commit comments

Comments
 (0)