Skip to content

Commit 9dabf26

Browse files
authored
Prevent child gestures from activating when a parent gesture is already active (#3296)
## Description Restores #3095 with a small change, I guess supersedes #3264. ## Test plan Same as in #3095 See also #3264 (comment)
1 parent 41defa7 commit 9dabf26

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,22 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
684684
}
685685
}
686686

687+
/*
688+
* Returns true if the view this handler is attached to is a descendant of the view the other handler
689+
* is attached to and false otherwise.
690+
*/
691+
fun isDescendantOf(of: GestureHandler<*>): Boolean {
692+
var view = this.view?.parent as? View
693+
while (view != null) {
694+
if (view == of.view) {
695+
return true
696+
}
697+
698+
view = view.parent as? View
699+
}
700+
return false
701+
}
702+
687703
// responsible for resetting the state of handler upon activation (may be called more than once
688704
// if the handler is waiting for failure of other one)
689705
open fun resetProgress() {}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,15 @@ class GestureHandlerOrchestrator(
8585
private fun hasOtherHandlerToWaitFor(handler: GestureHandler<*>) =
8686
gestureHandlers.any { !isFinished(it.state) && shouldHandlerWaitForOther(handler, it) }
8787

88-
private fun shouldBeCancelledByFinishedHandler(handler: GestureHandler<*>) = gestureHandlers.any { shouldHandlerWaitForOther(handler, it) && it.state == GestureHandler.STATE_END }
88+
private fun shouldBeCancelledByFinishedHandler(handler: GestureHandler<*>) =
89+
gestureHandlers.any { shouldHandlerWaitForOther(handler, it) && it.state == GestureHandler.STATE_END }
90+
91+
private fun shouldBeCancelledByActiveHandler(handler: GestureHandler<*>) =
92+
gestureHandlers.any { handler.hasCommonPointers(it) && it.state == GestureHandler.STATE_ACTIVE && !canRunSimultaneously(handler, it) && handler.isDescendantOf(it) }
8993

9094
private fun tryActivate(handler: GestureHandler<*>) {
9195
// If we are waiting for a gesture that has successfully finished, we should cancel handler
92-
if (shouldBeCancelledByFinishedHandler(handler)) {
96+
if (shouldBeCancelledByFinishedHandler(handler) || shouldBeCancelledByActiveHandler(handler)) {
9397
handler.cancel()
9498
return
9599
}

0 commit comments

Comments
 (0)