Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nested taps activation #2759

Merged
merged 21 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
}

fun cancel() {
if (state == STATE_ACTIVE || state == STATE_UNDETERMINED || state == STATE_BEGAN) {
if (state == STATE_ACTIVE || state == STATE_UNDETERMINED || state == STATE_BEGAN || this.isAwaiting) {
onCancel()
moveToState(STATE_CANCELLED)
}
Expand Down Expand Up @@ -762,7 +762,6 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
orchestrator = null
Arrays.fill(trackedPointerIDs, -1)
trackedPointersIDsCount = 0

trackedPointersCount = 0
trackedPointers.fill(null)
touchEventType = RNGestureHandlerTouchEvent.EVENT_UNDETERMINED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import java.util.*
import kotlin.collections.HashSet

class GestureHandlerOrchestrator(
private val wrapperView: ViewGroup,
Expand All @@ -22,6 +23,14 @@ class GestureHandlerOrchestrator(
private val gestureHandlers = arrayListOf<GestureHandler<*>>()
private val awaitingHandlers = arrayListOf<GestureHandler<*>>()
private val preparedHandlers = arrayListOf<GestureHandler<*>>()

// In `onHandlerStateChange` method we iterate through `awaitingHandlers`, but calling `tryActivate` may modify this list.
// To avoid `ConcurrentModificationException` we iterate through copy. There is one more problem though - if handler was
// removed from `awaitingHandlers`, it was still present in copy of original list. This hashset helps us identify which handlers
// are really inside `awaitingHandlers`.
// `contains` method on HashSet has O(1) complexity, so calling it inside for loop won't result in O(n^2) (contrary to ArrayList)
private val awaitingHandlersTags = HashSet<Int>()

private var isHandlingTouch = false
private var handlingChangeSemaphore = 0
private var finishedHandlersCleanupScheduled = false
Expand Down Expand Up @@ -73,16 +82,18 @@ class GestureHandlerOrchestrator(
finishedHandlersCleanupScheduled = false
}

private fun hasOtherHandlerToWaitFor(handler: GestureHandler<*>): Boolean {
for (otherHandler in gestureHandlers) {
if (!isFinished(otherHandler.state) && shouldHandlerWaitForOther(handler, otherHandler)) {
return true
}
}
return false
}
private fun hasOtherHandlerToWaitFor(handler: GestureHandler<*>) =
gestureHandlers.any { !isFinished(it.state) && shouldHandlerWaitForOther(handler, it) }

private fun shouldBeCancelledByFinishedHandler(handler: GestureHandler<*>) = gestureHandlers.any { shouldHandlerWaitForOther(handler, it) && it.state == GestureHandler.STATE_END }

private fun tryActivate(handler: GestureHandler<*>) {
// If we are waiting for a gesture that has successfully finished, we should cancel handler
if (shouldBeCancelledByFinishedHandler(handler)) {
handler.cancel()
return
}

// see if there is anyone else who we need to wait for
if (hasOtherHandlerToWaitFor(handler)) {
addAwaitingHandler(handler)
Expand All @@ -94,7 +105,14 @@ class GestureHandlerOrchestrator(
}

private fun cleanupAwaitingHandlers() {
awaitingHandlers.removeAll { !it.isAwaiting }
val awaitingHandlersCopy = awaitingHandlers.toList()

for (handler in awaitingHandlersCopy) {
if (!handler.isAwaiting) {
awaitingHandlers.remove(handler)
awaitingHandlersTags.remove(handler.tag)
}
}
}

/*package*/
Expand All @@ -107,7 +125,7 @@ class GestureHandlerOrchestrator(

// if there were handlers awaiting completion of this handler, we can trigger active state
for (otherHandler in currentlyAwaitingHandlers) {
if (!shouldHandlerWaitForOther(otherHandler, handler)) {
if (!shouldHandlerWaitForOther(otherHandler, handler) || !awaitingHandlersTags.contains(otherHandler.tag)) {
continue
}

Expand Down Expand Up @@ -174,7 +192,6 @@ class GestureHandlerOrchestrator(
// Clear all awaiting handlers waiting for the current handler to fail
for (otherHandler in awaitingHandlers.reversed()) {
if (shouldHandlerBeCancelledBy(otherHandler, handler)) {
otherHandler.cancel()
j-piasecki marked this conversation as resolved.
Show resolved Hide resolved
otherHandler.isAwaiting = false
}
}
Expand Down Expand Up @@ -389,6 +406,8 @@ class GestureHandlerOrchestrator(
}

awaitingHandlers.add(handler)
awaitingHandlersTags.add(handler.tag)

with(handler) {
isAwaiting = true
activationIndex = [email protected]++
Expand Down
13 changes: 13 additions & 0 deletions src/web/tools/GestureHandlerOrchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,20 @@ export default class GestureHandlerOrchestrator {
return hasToWait;
}

private shouldBeCancelledByFinishedHandler(handler: GestureHandler): boolean {
return this.gestureHandlers.some(
(otherHandler) =>
this.shouldHandlerWaitForOther(handler, otherHandler) &&
otherHandler.getState() === State.END
);
}

private tryActivate(handler: GestureHandler): void {
if (this.shouldBeCancelledByFinishedHandler(handler)) {
m-bert marked this conversation as resolved.
Show resolved Hide resolved
handler.cancel();
return;
}

if (this.hasOtherHandlerToWaitFor(handler)) {
this.addAwaitingHandler(handler);
} else if (
Expand Down
Loading