Skip to content

Commit f7c9190

Browse files
committed
Merge branch 'main' into @jpiasecki/fix-event-order-android-2
2 parents 4d94d15 + 49709ad commit f7c9190

36 files changed

+148
-147
lines changed

android/build.gradle

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,8 @@ android {
172172
exclude "**/libreact_render*.so"
173173
}
174174

175-
// Include "lib/" as sources, unfortunately react-native link can't handle
176-
// setting up alternative gradle modules. We still have "lib" defined as a
177-
// standalone gradle module just to be used in AndroidNativeExample
178175
sourceSets.main {
179176
java {
180-
srcDirs += 'lib/src/main/java'
181-
182177
// Include "common/" only when it's not provided by Reanimated to mitigate
183178
// multiple definitions of the same class preventing build
184179
if (shouldUseCommonInterfaceFromReanimated()) {

android/lib/src/main/AndroidManifest.xml

Lines changed: 0 additions & 3 deletions
This file was deleted.

android/lib/src/test/java/com/swmansion/gesturehandler/ExampleUnitTest.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

android/noreanimated/src/main/java/com/swmansion/gesturehandler/ReanimatedEventDispatcher.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.facebook.react.bridge.ReactContext
44
import com.facebook.react.uimanager.events.Event
55

66
class ReanimatedEventDispatcher {
7-
fun <T : Event<T>>sendEvent(event: T, reactApplicationContext: ReactContext) {
8-
// no-op
9-
}
7+
fun <T : Event<T>>sendEvent(event: T, reactApplicationContext: ReactContext) {
8+
// no-op
9+
}
1010
}

android/reanimated/src/main/java/com/swmansion/gesturehandler/ReanimatedEventDispatcher.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import com.facebook.react.uimanager.events.Event
55
import com.swmansion.reanimated.ReanimatedModule
66

77
class ReanimatedEventDispatcher {
8-
private var reanimatedModule: ReanimatedModule? = null
8+
private var reanimatedModule: ReanimatedModule? = null
99

10-
fun <T : Event<T>>sendEvent(event: T, reactApplicationContext: ReactContext) {
11-
if (reanimatedModule == null) {
12-
reanimatedModule = reactApplicationContext.getNativeModule(ReanimatedModule::class.java)
13-
}
14-
15-
reanimatedModule?.nodesManager?.onEventDispatch(event)
10+
fun <T : Event<T>>sendEvent(event: T, reactApplicationContext: ReactContext) {
11+
if (reanimatedModule == null) {
12+
reanimatedModule = reactApplicationContext.getNativeModule(ReanimatedModule::class.java)
1613
}
14+
15+
reanimatedModule?.nodesManager?.onEventDispatch(event)
16+
}
1717
}

android/spotless.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: "com.diffplug.spotless"
22

33
spotless {
44
kotlin {
5-
target "src/**/*.kt"
5+
target "src/**/*.kt", "reanimated/**/*.kt", "noreanimated/**/*.kt", "common/**/*.kt"
66
ktlint().editorConfigOverride([indent_size: 2])
77
trimTrailingWhitespace()
88
indentWithSpaces()

android/lib/src/main/java/com/swmansion/gesturehandler/FlingGestureHandler.kt renamed to android/src/main/java/com/swmansion/gesturehandler/core/FlingGestureHandler.kt

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.swmansion.gesturehandler
1+
package com.swmansion.gesturehandler.core
22

33
import android.os.Handler
44
import android.os.Looper
@@ -37,14 +37,17 @@ class FlingGestureHandler : GestureHandler<FlingGestureHandler>() {
3737

3838
private fun tryEndFling(event: MotionEvent) = if (
3939
maxNumberOfPointersSimultaneously == numberOfPointersRequired &&
40-
(direction and DIRECTION_RIGHT != 0 &&
41-
event.rawX - startX > minAcceptableDelta ||
42-
direction and DIRECTION_LEFT != 0 &&
43-
startX - event.rawX > minAcceptableDelta ||
44-
direction and DIRECTION_UP != 0 &&
45-
startY - event.rawY > minAcceptableDelta ||
46-
direction and DIRECTION_DOWN != 0 &&
47-
event.rawY - startY > minAcceptableDelta)) {
40+
(
41+
direction and DIRECTION_RIGHT != 0 &&
42+
event.rawX - startX > minAcceptableDelta ||
43+
direction and DIRECTION_LEFT != 0 &&
44+
startX - event.rawX > minAcceptableDelta ||
45+
direction and DIRECTION_UP != 0 &&
46+
startY - event.rawY > minAcceptableDelta ||
47+
direction and DIRECTION_DOWN != 0 &&
48+
event.rawY - startY > minAcceptableDelta
49+
)
50+
) {
4851
handler!!.removeCallbacksAndMessages(null)
4952
activate()
5053
true

android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandler.kt renamed to android/src/main/java/com/swmansion/gesturehandler/core/GestureHandler.kt

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.swmansion.gesturehandler
1+
package com.swmansion.gesturehandler.core
22

33
import android.app.Activity
44
import android.content.Context
@@ -14,6 +14,7 @@ import com.facebook.react.bridge.Arguments
1414
import com.facebook.react.bridge.UiThreadUtil
1515
import com.facebook.react.bridge.WritableArray
1616
import com.facebook.react.uimanager.PixelUtil
17+
import com.swmansion.gesturehandler.BuildConfig
1718
import com.swmansion.gesturehandler.react.RNGestureHandlerTouchEvent
1819
import java.lang.IllegalStateException
1920
import java.util.*
@@ -48,7 +49,6 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
4849
private val trackedPointers: Array<PointerData?> = Array(MAX_POINTERS_COUNT) { null }
4950
var needsPointerData = false
5051

51-
5252
private var hitSlop: FloatArray? = null
5353
var eventCoalescingKey: Short = 0
5454
private set
@@ -130,11 +130,15 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
130130
}
131131

132132
fun setManualActivation(manualActivation: Boolean): ConcreteGestureHandlerT =
133-
applySelf { this.manualActivation = manualActivation }
133+
applySelf { this.manualActivation = manualActivation }
134134

135135
fun setHitSlop(
136-
leftPad: Float, topPad: Float, rightPad: Float, bottomPad: Float,
137-
width: Float, height: Float,
136+
leftPad: Float,
137+
topPad: Float,
138+
rightPad: Float,
139+
bottomPad: Float,
140+
width: Float,
141+
height: Float,
138142
): ConcreteGestureHandlerT = applySelf {
139143
if (hitSlop == null) {
140144
hitSlop = FloatArray(6)
@@ -281,7 +285,7 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
281285
}
282286

283287
// introduced in 1.11.0, remove if crashes are not reported
284-
if(pointerProps.isEmpty()|| pointerCoords.isEmpty()){
288+
if (pointerProps.isEmpty() || pointerCoords.isEmpty()) {
285289
throw IllegalStateException("pointerCoords.size=${pointerCoords.size}, pointerProps.size=${pointerProps.size}")
286290
}
287291

@@ -292,8 +296,8 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
292296
event.eventTime,
293297
action,
294298
count,
295-
pointerProps, /* props are copied and hence it is safe to use static array here */
296-
pointerCoords, /* same applies to coords */
299+
pointerProps, /* props are copied and hence it is safe to use static array here */
300+
pointerCoords, /* same applies to coords */
297301
event.metaState,
298302
event.buttonState,
299303
event.xPrecision,
@@ -316,7 +320,8 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
316320
handler: GestureHandler<*>,
317321
event: MotionEvent,
318322
e: IllegalArgumentException
319-
) : Exception("""
323+
) : Exception(
324+
"""
320325
handler: ${handler::class.simpleName}
321326
state: ${handler.state}
322327
view: ${handler.view}
@@ -327,14 +332,17 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
327332
trackedPointersCount: ${handler.trackedPointersIDsCount}
328333
trackedPointers: ${handler.trackedPointerIDs.joinToString(separator = ", ")}
329334
while handling event: $event
330-
""".trimIndent(), e) {}
335+
""".trimIndent(),
336+
e
337+
)
331338

332339
fun handle(transformedEvent: MotionEvent, sourceEvent: MotionEvent) {
333-
if (!isEnabled
334-
|| state == STATE_CANCELLED
335-
|| state == STATE_FAILED
336-
|| state == STATE_END
337-
|| trackedPointersIDsCount < 1) {
340+
if (!isEnabled ||
341+
state == STATE_CANCELLED ||
342+
state == STATE_FAILED ||
343+
state == STATE_END ||
344+
trackedPointersIDsCount < 1
345+
) {
338346
return
339347
}
340348

@@ -383,11 +391,11 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
383391
val offsetY = event.rawY - event.y
384392

385393
trackedPointers[pointerId] = PointerData(
386-
pointerId,
387-
event.getX(event.actionIndex),
388-
event.getY(event.actionIndex),
389-
event.getX(event.actionIndex) + offsetX - windowOffset[0],
390-
event.getY(event.actionIndex) + offsetY - windowOffset[1],
394+
pointerId,
395+
event.getX(event.actionIndex),
396+
event.getY(event.actionIndex),
397+
event.getX(event.actionIndex) + offsetX - windowOffset[0],
398+
event.getY(event.actionIndex) + offsetY - windowOffset[1],
391399
)
392400
trackedPointersCount++
393401
addChangedPointer(trackedPointers[pointerId]!!)
@@ -405,11 +413,11 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
405413
val offsetY = event.rawY - event.y
406414

407415
trackedPointers[pointerId] = PointerData(
408-
pointerId,
409-
event.getX(event.actionIndex),
410-
event.getY(event.actionIndex),
411-
event.getX(event.actionIndex) + offsetX - windowOffset[0],
412-
event.getY(event.actionIndex) + offsetY - windowOffset[1],
416+
pointerId,
417+
event.getX(event.actionIndex),
418+
event.getY(event.actionIndex),
419+
event.getX(event.actionIndex) + offsetX - windowOffset[0],
420+
event.getY(event.actionIndex) + offsetY - windowOffset[1],
413421
)
414422
addChangedPointer(trackedPointers[pointerId]!!)
415423
trackedPointers[pointerId] = null
@@ -556,11 +564,11 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
556564
}
557565

558566
fun wantEvents(): Boolean {
559-
return isEnabled
560-
&& state != STATE_FAILED
561-
&& state != STATE_CANCELLED
562-
&& state != STATE_END
563-
&& trackedPointersIDsCount > 0
567+
return isEnabled &&
568+
state != STATE_FAILED &&
569+
state != STATE_CANCELLED &&
570+
state != STATE_END &&
571+
trackedPointersIDsCount > 0
564572
}
565573

566574
open fun shouldRequireToWaitForFailure(handler: GestureHandler<*>): Boolean {
@@ -768,7 +776,7 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
768776
private lateinit var pointerCoords: Array<PointerCoords?>
769777
private fun initPointerProps(size: Int) {
770778
var size = size
771-
if (!::pointerProps.isInitialized) {
779+
if (!Companion::pointerProps.isInitialized) {
772780
pointerProps = arrayOfNulls(MAX_POINTERS_COUNT)
773781
pointerCoords = arrayOfNulls(MAX_POINTERS_COUNT)
774782
}

android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandlerInteractionController.kt renamed to android/src/main/java/com/swmansion/gesturehandler/core/GestureHandlerInteractionController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.swmansion.gesturehandler
1+
package com.swmansion.gesturehandler.core
22

33
interface GestureHandlerInteractionController {
44
fun shouldWaitForHandlerFailure(handler: GestureHandler<*>, otherHandler: GestureHandler<*>): Boolean

android/lib/src/main/java/com/swmansion/gesturehandler/GestureHandlerOrchestrator.kt renamed to android/src/main/java/com/swmansion/gesturehandler/core/GestureHandlerOrchestrator.kt

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.swmansion.gesturehandler
1+
package com.swmansion.gesturehandler.core
22

33
import android.graphics.Matrix
44
import android.graphics.PointF
@@ -134,7 +134,10 @@ class GestureHandlerOrchestrator(
134134
// their state is set to END and when the gesture they are waiting for activates they
135135
// should be cancelled, however `cancel` was never sent as gestures were already in the END state.
136136
// Send synthetic BEGAN -> CANCELLED to properly handle JS logic
137-
otherHandler.dispatchStateChange(GestureHandler.STATE_CANCELLED, GestureHandler.STATE_BEGAN)
137+
otherHandler.dispatchStateChange(
138+
GestureHandler.STATE_CANCELLED,
139+
GestureHandler.STATE_BEGAN
140+
)
138141
}
139142
otherHandler.isAwaiting = false
140143
} else {
@@ -460,9 +463,10 @@ class GestureHandlerOrchestrator(
460463

461464
// if the pointer is inside the view but it overflows its parent, handlers attached to the parent
462465
// might not have been extracted (pointer might be in a child, but may be outside parent)
463-
if (coords[0] in 0f..view.width.toFloat() && coords[1] in 0f..view.height.toFloat()
464-
&& isViewOverflowingParent(view) && extractAncestorHandlers(view, coords, pointerId)) {
465-
found = true
466+
if (coords[0] in 0f..view.width.toFloat() && coords[1] in 0f..view.height.toFloat() &&
467+
isViewOverflowingParent(view) && extractAncestorHandlers(view, coords, pointerId)
468+
) {
469+
found = true
466470
}
467471

468472
return found
@@ -512,8 +516,10 @@ class GestureHandlerOrchestrator(
512516
}
513517
PointerEventsConfig.BOX_ONLY -> {
514518
// This view is the target, its children don't matter
515-
(recordViewHandlersForPointer(view, coords, pointerId)
516-
|| shouldHandlerlessViewBecomeTouchTarget(view, coords))
519+
(
520+
recordViewHandlersForPointer(view, coords, pointerId) ||
521+
shouldHandlerlessViewBecomeTouchTarget(view, coords)
522+
)
517523
}
518524
PointerEventsConfig.BOX_NONE -> {
519525
// This view can't be the target, but its children might
@@ -542,8 +548,10 @@ class GestureHandlerOrchestrator(
542548
extractGestureHandlers(view, coords, pointerId)
543549
} else false
544550

545-
(recordViewHandlersForPointer(view, coords, pointerId)
546-
|| found || shouldHandlerlessViewBecomeTouchTarget(view, coords))
551+
(
552+
recordViewHandlersForPointer(view, coords, pointerId) ||
553+
found || shouldHandlerlessViewBecomeTouchTarget(view, coords)
554+
)
547555
}
548556
}
549557

@@ -555,7 +563,6 @@ class GestureHandlerOrchestrator(
555563
private fun isClipping(view: View) =
556564
view !is ViewGroup || viewConfigHelper.isViewClippingChildren(view)
557565

558-
559566
companion object {
560567
// The limit doesn't necessarily need to exists, it was just simpler to implement it that way
561568
// it is also more allocation-wise efficient to have a fixed limit
@@ -622,14 +629,15 @@ class GestureHandlerOrchestrator(
622629
x in 0f..child.width.toFloat() && y in 0f..child.height.toFloat()
623630

624631
private fun shouldHandlerWaitForOther(handler: GestureHandler<*>, other: GestureHandler<*>): Boolean {
625-
return handler !== other && (handler.shouldWaitForHandlerFailure(other)
626-
|| other.shouldRequireToWaitForFailure(handler))
632+
return handler !== other && (
633+
handler.shouldWaitForHandlerFailure(other) ||
634+
other.shouldRequireToWaitForFailure(handler)
635+
)
627636
}
628637

629638
private fun canRunSimultaneously(a: GestureHandler<*>, b: GestureHandler<*>) =
630639
a === b || a.shouldRecognizeSimultaneously(b) || b.shouldRecognizeSimultaneously(a)
631640

632-
633641
private fun shouldHandlerBeCancelledBy(handler: GestureHandler<*>, other: GestureHandler<*>): Boolean {
634642
if (!handler.hasCommonPointers(other)) {
635643
// if two handlers share no common pointer one can never trigger cancel for the other
@@ -641,16 +649,17 @@ class GestureHandlerOrchestrator(
641649
return false
642650
}
643651
return if (handler !== other &&
644-
(handler.isAwaiting || handler.state == GestureHandler.STATE_ACTIVE)) {
652+
(handler.isAwaiting || handler.state == GestureHandler.STATE_ACTIVE)
653+
) {
645654
// in every other case as long as the handler is about to be activated or already in active
646655
// state, we delegate the decision to the implementation of GestureHandler#shouldBeCancelledBy
647656
handler.shouldBeCancelledBy(other)
648657
} else true
649658
}
650659

651660
private fun isFinished(state: Int) =
652-
state == GestureHandler.STATE_CANCELLED
653-
|| state == GestureHandler.STATE_FAILED
654-
|| state == GestureHandler.STATE_END
661+
state == GestureHandler.STATE_CANCELLED ||
662+
state == GestureHandler.STATE_FAILED ||
663+
state == GestureHandler.STATE_END
655664
}
656665
}

0 commit comments

Comments
 (0)