From 3205ed46c53cf6bd6fcf4ef0962da148917e573f Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Tue, 18 Oct 2022 16:28:52 +0200 Subject: [PATCH 1/3] Add gesture initialization --- .../gesturehandler/GestureHandler.kt | 61 +++++++++++++------ .../GestureHandlerOrchestrator.kt | 19 +----- .../gesturehandler/LongPressGestureHandler.kt | 12 ++-- .../gesturehandler/PanGestureHandler.kt | 16 +++-- .../gesturehandler/PinchGestureHandler.kt | 14 +++-- .../gesturehandler/RotationGestureHandler.kt | 8 ++- .../gesturehandler/TapGestureHandler.kt | 12 ++-- .../react/RNGestureHandlerModule.kt | 5 +- 8 files changed, 89 insertions(+), 58 deletions(-) diff --git a/android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandler.kt b/android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandler.kt index f690e42efb..2719aff954 100644 --- a/android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandler.kt +++ b/android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandler.kt @@ -57,6 +57,7 @@ open class GestureHandler 0) { extractAllPointersData() - dispatchTouchEvent() + tryDispatchingTouchEvent() } } - fun updatePointerData(event: MotionEvent) { - if (event.actionMasked == MotionEvent.ACTION_DOWN || event.actionMasked == MotionEvent.ACTION_POINTER_DOWN) { - dispatchTouchDownEvent(event) - dispatchTouchMoveEvent(event) - } else if (event.actionMasked == MotionEvent.ACTION_UP || event.actionMasked == MotionEvent.ACTION_POINTER_UP) { - dispatchTouchMoveEvent(event) - dispatchTouchUpEvent(event) - } else if (event.actionMasked == MotionEvent.ACTION_MOVE) { - dispatchTouchMoveEvent(event) + fun updatePointerData(event: MotionEvent, sourceEvent: MotionEvent) { + eventTriggeringStateChangeInTouchEventHandler = sourceEvent + + when (event.actionMasked) { + MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> { + handleTouchDownEvent(event) + handleTouchMoveEvent(event) + } + MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP -> { + handleTouchMoveEvent(event) + handleTouchUpEvent(event) + } + MotionEvent.ACTION_MOVE -> { + handleTouchMoveEvent(event) + } } + + eventTriggeringStateChangeInTouchEventHandler = null } private fun extractAllPointersData() { @@ -482,7 +493,7 @@ open class GestureHandler 0) { handler!!.postDelayed({ activate() }, minDurationMs) @@ -83,6 +80,13 @@ class LongPressGestureHandler(context: Context) : GestureHandler() lastY = getLastPointerY(sourceEvent, averageTouches) } if (state == STATE_UNDETERMINED && sourceEvent.pointerCount >= minPointers) { - resetProgress() - offsetX = 0f - offsetY = 0f - velocityX = 0f - velocityY = 0f - velocityTracker = VelocityTracker.obtain() + initialize(sourceEvent) addVelocityMovement(velocityTracker, sourceEvent) begin() @@ -283,6 +278,15 @@ class PanGestureHandler(context: Context?) : GestureHandler() handler?.removeCallbacksAndMessages(null) } + override fun onInitialize(event: MotionEvent) { + resetProgress() + offsetX = 0f + offsetY = 0f + velocityX = 0f + velocityY = 0f + velocityTracker = VelocityTracker.obtain() + } + override fun onReset() { handler?.removeCallbacksAndMessages(null) velocityTracker?.let { diff --git a/android/lib/src/main/java/com/swmansion/gesturehandler/PinchGestureHandler.kt b/android/lib/src/main/java/com/swmansion/gesturehandler/PinchGestureHandler.kt index 6f6afd7255..633bf3c468 100644 --- a/android/lib/src/main/java/com/swmansion/gesturehandler/PinchGestureHandler.kt +++ b/android/lib/src/main/java/com/swmansion/gesturehandler/PinchGestureHandler.kt @@ -50,11 +50,7 @@ class PinchGestureHandler : GestureHandler() { override fun onHandle(event: MotionEvent, sourceEvent: MotionEvent) { if (state == STATE_UNDETERMINED) { - val context = view!!.context - resetProgress() - scaleGestureDetector = ScaleGestureDetector(context, gestureListener) - val configuration = ViewConfiguration.get(context) - spanSlop = configuration.scaledTouchSlop.toFloat() + initialize(sourceEvent) begin() } scaleGestureDetector?.onTouchEvent(sourceEvent) @@ -89,6 +85,14 @@ class PinchGestureHandler : GestureHandler() { resetProgress() } + override fun onInitialize(event: MotionEvent) { + resetProgress() + val context = view!!.context + val configuration = ViewConfiguration.get(context) + scaleGestureDetector = ScaleGestureDetector(context, gestureListener) + spanSlop = configuration.scaledTouchSlop.toFloat() + } + override fun resetProgress() { velocity = 0.0 scale = 1.0 diff --git a/android/lib/src/main/java/com/swmansion/gesturehandler/RotationGestureHandler.kt b/android/lib/src/main/java/com/swmansion/gesturehandler/RotationGestureHandler.kt index e86c7447c1..6c67614020 100644 --- a/android/lib/src/main/java/com/swmansion/gesturehandler/RotationGestureHandler.kt +++ b/android/lib/src/main/java/com/swmansion/gesturehandler/RotationGestureHandler.kt @@ -43,8 +43,7 @@ class RotationGestureHandler : GestureHandler() { override fun onHandle(event: MotionEvent, sourceEvent: MotionEvent) { if (state == STATE_UNDETERMINED) { - resetProgress() - rotationGestureDetector = RotationGestureDetector(gestureListener) + initialize(sourceEvent) begin() } rotationGestureDetector?.onTouchEvent(sourceEvent) @@ -70,6 +69,11 @@ class RotationGestureHandler : GestureHandler() { super.activate(force) } + override fun onInitialize(event: MotionEvent) { + resetProgress() + rotationGestureDetector = RotationGestureDetector(gestureListener) + } + override fun onReset() { rotationGestureDetector = null anchorX = Float.NaN diff --git a/android/lib/src/main/java/com/swmansion/gesturehandler/TapGestureHandler.kt b/android/lib/src/main/java/com/swmansion/gesturehandler/TapGestureHandler.kt index 048abd7b11..73b6cc25d6 100644 --- a/android/lib/src/main/java/com/swmansion/gesturehandler/TapGestureHandler.kt +++ b/android/lib/src/main/java/com/swmansion/gesturehandler/TapGestureHandler.kt @@ -108,10 +108,7 @@ class TapGestureHandler : GestureHandler() { val state = state val action = sourceEvent.actionMasked if (state == STATE_UNDETERMINED) { - offsetX = 0f - offsetY = 0f - startX = getLastPointerX(sourceEvent, true) - startY = getLastPointerY(sourceEvent, true) + initialize(sourceEvent) } if (action == MotionEvent.ACTION_POINTER_UP || action == MotionEvent.ACTION_POINTER_DOWN) { offsetX += lastX - startX @@ -148,6 +145,13 @@ class TapGestureHandler : GestureHandler() { end() } + override fun onInitialize(event: MotionEvent) { + offsetX = 0f + offsetY = 0f + startX = getLastPointerX(event, true) + startY = getLastPointerY(event, true) + } + override fun onCancel() { handler?.removeCallbacksAndMessages(null) } diff --git a/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerModule.kt b/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerModule.kt index 86b0fa7c4a..544e9d53bf 100644 --- a/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerModule.kt +++ b/android/src/main/java/com/swmansion/gesturehandler/react/RNGestureHandlerModule.kt @@ -433,9 +433,12 @@ class RNGestureHandlerModule(reactContext: ReactApplicationContext?) : override fun setGestureHandlerState(handlerTag: Int, newState: Int) { registry.getHandler(handlerTag)?.let { handler -> + handler.initialize() + handler.begin() + when (newState) { GestureHandler.STATE_ACTIVE -> handler.activate(force = true) - GestureHandler.STATE_BEGAN -> handler.begin() + GestureHandler.STATE_BEGAN -> {} GestureHandler.STATE_END -> handler.end() GestureHandler.STATE_FAILED -> handler.fail() GestureHandler.STATE_CANCELLED -> handler.cancel() From 4d94d159537fea346a37c1ec9294e7d2a2d99cd1 Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Wed, 19 Oct 2022 16:11:31 +0200 Subject: [PATCH 2/3] Make `PointerData` private again Add comments --- .../main/java/com/swmansion/gesturehandler/GestureHandler.kt | 2 +- .../swmansion/gesturehandler/react/RNGestureHandlerModule.kt | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandler.kt b/android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandler.kt index 2719aff954..dd2dd50363 100644 --- a/android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandler.kt +++ b/android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandler.kt @@ -797,7 +797,7 @@ open class GestureHandler + // Try to initialize the handler in case it was not yet initialized handler.initialize() + // Try to transition to the BEGIN state (possible only from UNDETERMINED) + // to force the correct event flow handler.begin() when (newState) { From 785664676e7533e9d0ed06ca4d97c53302e1e198 Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Wed, 19 Oct 2022 16:23:31 +0200 Subject: [PATCH 3/3] Add comment --- .../gesturehandler/core/GestureHandler.kt | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt b/android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt index bc020c1ebe..973dd23cb2 100644 --- a/android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt +++ b/android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt @@ -69,6 +69,8 @@ open class GestureHandler { - handleTouchDownEvent(event) - handleTouchMoveEvent(event) - } - MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP -> { - handleTouchMoveEvent(event) - handleTouchUpEvent(event) - } - MotionEvent.ACTION_MOVE -> { - handleTouchMoveEvent(event) - } + MotionEvent.ACTION_DOWN, MotionEvent.ACTION_POINTER_DOWN -> { + handleTouchDownEvent(event) + handleTouchMoveEvent(event) + } + MotionEvent.ACTION_UP, MotionEvent.ACTION_POINTER_UP -> { + handleTouchMoveEvent(event) + handleTouchUpEvent(event) + } + MotionEvent.ACTION_MOVE -> { + handleTouchMoveEvent(event) + } } eventTriggeringStateChangeInTouchEventHandler = null