@@ -57,6 +57,7 @@ import com.nativephp.mobile.ui.nativerender.NodeView
5757import com.nativephp.mobile.ui.nativerender.SharedValueStore
5858import kotlinx.coroutines.CoroutineScope
5959import kotlinx.coroutines.Job
60+ import kotlinx.coroutines.coroutineScope
6061import kotlinx.coroutines.launch
6162import kotlin.math.abs
6263import kotlin.math.exp
@@ -482,11 +483,11 @@ object GestureAreaRenderer {
482483 // axes so `on_drag_end` can report an unbound one too.
483484 // A snapping axis follows the finger from where it was
484485 // when the drag began (rawX / rawY), with resistance
485- // past its outer points, and the tracker's fling picks
486- // the point it springs to on release.
486+ // past its outer points, and the fling it lets go with
487+ // picks the point it springs to on release.
487488 var rawX = 0f
488489 var rawY = 0f
489- val velocity = VelocityTracker ()
490+ val velocity = ReleaseVelocity ()
490491 // A one-axis pan's trigger, if it has one, and whether the
491492 // pull is past its point.
492493 val pullsX = panXId != 0 && panYId == 0 && ! triggerX.isEmpty
@@ -496,7 +497,7 @@ object GestureAreaRenderer {
496497 snapping.stop()
497498 rawX = if (panXId != 0 ) SharedValueStore .valueOf(panXId) else 0f
498499 rawY = if (panYId != 0 ) SharedValueStore .valueOf(panYId) else 0f
499- velocity.resetTracking ()
500+ velocity.reset ()
500501 }
501502 val onDrag: (Offset , Offset ) -> Unit = { bound, raw ->
502503 gestureX + = raw.x.toDp().value
@@ -529,7 +530,7 @@ object GestureAreaRenderer {
529530 val onEnd: (Boolean ) -> Unit = { lifted ->
530531 // px per second, into dp like the pan itself. A pull
531532 // always goes back to rest.
532- val fling = velocity.calculateVelocity ()
533+ val fling = velocity.atRelease ()
533534 val x = when {
534535 panXId == 0 -> gestureX
535536 pullsX -> restX.also { snapping.to(panXId, it, scope) }
@@ -550,44 +551,50 @@ object GestureAreaRenderer {
550551 gestureX = 0f
551552 gestureY = 0f
552553 }
553- when {
554- // A pull: the same as below, but only a drag its own way.
555- pullsX || pullsY -> detectPull(
556- horizontal = pullsX,
557- way = if (pullsX) triggerX.distance else triggerY.distance,
558- onStart = onStart,
559- onEnd = onEnd,
560- ) { change, dragAmount ->
561- velocity.addPosition(change.uptimeMillis, change.position)
562- val raw = change.position - change.previousPosition
563- onDrag(if (pullsX) Offset (dragAmount, 0f ) else Offset (0f , dragAmount), raw)
564- }
565- // One axis bound: detect on that axis only, so the
566- // other direction stays free for an enclosing
567- // scroll container.
568- panXId != 0 && panYId == 0 -> detectHorizontalDragGestures(
569- onDragStart = onStart,
570- onDragEnd = { onEnd(true ) },
571- onDragCancel = { onEnd(false ) },
572- ) { change, dragAmount ->
573- velocity.addPosition(change.uptimeMillis, change.position)
574- onDrag(Offset (dragAmount, 0f ), change.position - change.previousPosition)
575- }
576- panXId == 0 && panYId != 0 -> detectVerticalDragGestures(
577- onDragStart = onStart,
578- onDragEnd = { onEnd(true ) },
579- onDragCancel = { onEnd(false ) },
580- ) { change, dragAmount ->
581- velocity.addPosition(change.uptimeMillis, change.position)
582- onDrag(Offset (0f , dragAmount), change.position - change.previousPosition)
583- }
584- else -> detectDragGestures(
585- onDragStart = onStart,
586- onDragEnd = { onEnd(true ) },
587- onDragCancel = { onEnd(false ) },
588- ) { change, dragAmount ->
589- velocity.addPosition(change.uptimeMillis, change.position)
590- onDrag(dragAmount, dragAmount)
554+ coroutineScope {
555+ // The detectors below end a drag without saying
556+ // when the finger lifted, so hear that here first,
557+ // for the fling a snap settles by.
558+ if (! snapX.isEmpty || ! snapY.isEmpty) launch { velocity.watchLifts(this @pointerInput) }
559+ when {
560+ // A pull: the same as below, but only a drag its own way.
561+ pullsX || pullsY -> detectPull(
562+ horizontal = pullsX,
563+ way = if (pullsX) triggerX.distance else triggerY.distance,
564+ onStart = onStart,
565+ onEnd = onEnd,
566+ ) { change, dragAmount ->
567+ velocity.add(change)
568+ val raw = change.position - change.previousPosition
569+ onDrag(if (pullsX) Offset (dragAmount, 0f ) else Offset (0f , dragAmount), raw)
570+ }
571+ // One axis bound: detect on that axis only, so the
572+ // other direction stays free for an enclosing
573+ // scroll container.
574+ panXId != 0 && panYId == 0 -> detectHorizontalDragGestures(
575+ onDragStart = onStart,
576+ onDragEnd = { onEnd(true ) },
577+ onDragCancel = { onEnd(false ) },
578+ ) { change, dragAmount ->
579+ velocity.add(change)
580+ onDrag(Offset (dragAmount, 0f ), change.position - change.previousPosition)
581+ }
582+ panXId == 0 && panYId != 0 -> detectVerticalDragGestures(
583+ onDragStart = onStart,
584+ onDragEnd = { onEnd(true ) },
585+ onDragCancel = { onEnd(false ) },
586+ ) { change, dragAmount ->
587+ velocity.add(change)
588+ onDrag(Offset (0f , dragAmount), change.position - change.previousPosition)
589+ }
590+ else -> detectDragGestures(
591+ onDragStart = onStart,
592+ onDragEnd = { onEnd(true ) },
593+ onDragCancel = { onEnd(false ) },
594+ ) { change, dragAmount ->
595+ velocity.add(change)
596+ onDrag(dragAmount, dragAmount)
597+ }
591598 }
592599 }
593600 }
@@ -1188,6 +1195,52 @@ object GestureAreaRenderer {
11881195 ! isEmpty && if (distance < 0f ) offset <= distance else offset >= distance
11891196 }
11901197
1198+ /* *
1199+ * The speed a drag lets go with, in px per second. A tracker fed only
1200+ * the moves the drag detectors pass on keeps the last move's speed
1201+ * however long the finger then rests, so a drag held still and let go
1202+ * short of halfway would fling on to the next snap point. Told when the
1203+ * finger lifted, a finger that hadn't moved for more than 40ms before
1204+ * lets go with none, as Android's own VelocityTracker judges it from
1205+ * the up event.
1206+ */
1207+ private class ReleaseVelocity {
1208+ private val tracker = VelocityTracker ()
1209+ private var lastMove = 0L
1210+ private var liftedAt = 0L
1211+
1212+ fun reset () {
1213+ tracker.resetTracking()
1214+ lastMove = 0L
1215+ liftedAt = 0L
1216+ }
1217+
1218+ fun add (change : PointerInputChange ) {
1219+ tracker.addPosition(change.uptimeMillis, change.position)
1220+ lastMove = change.uptimeMillis
1221+ }
1222+
1223+ /* *
1224+ * Notes each finger that lifts. The Initial pass reaches here before
1225+ * a drag detector's Main pass ends the drag on the same event.
1226+ */
1227+ suspend fun watchLifts (scope : PointerInputScope ) = scope.awaitPointerEventScope {
1228+ while (true ) {
1229+ awaitPointerEvent(PointerEventPass .Initial ).changes.forEach {
1230+ if (it.changedToUpIgnoreConsumed()) liftedAt = it.uptimeMillis
1231+ }
1232+ }
1233+ }
1234+
1235+ fun atRelease (): Velocity =
1236+ if (liftedAt - lastMove > StoppedMillis ) Velocity .Zero else tracker.calculateVelocity()
1237+
1238+ private companion object {
1239+ /* * Android's own: no move for this long before the finger lifts means it had stopped. */
1240+ const val StoppedMillis = 40L
1241+ }
1242+ }
1243+
11911244 /* * Springs a snapping or trigger axis to where it rests, on release or a PHP write-back. */
11921245 private class SnapPan {
11931246 private var job: Job ? = null
0 commit comments