Skip to content
Open
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 @@ -52,7 +52,8 @@ class GestureHandlerOrchestrator(
// `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
var isHandlingTouch = false
private set
private var handlingChangeSemaphore = 0
private var finishedHandlersCleanupScheduled = false
private var activationIndex = 0
Expand Down Expand Up @@ -358,6 +359,30 @@ class GestureHandlerOrchestrator(
event.recycle()
}

/**
* Cancels all handlers created using API v1 and v2
*/
fun cancelAllLegacyHandlers() {
val handlersToProcess = obtainHandlerList()
handlersToProcess.addAll(gestureHandlers)

try {
handlersToProcess.forEach {
if (it.actionType == GestureHandler.ACTION_TYPE_JS_FUNCTION_OLD_API ||
it.actionType == GestureHandler.ACTION_TYPE_JS_FUNCTION_NEW_API ||
it.actionType == GestureHandler.ACTION_TYPE_REANIMATED_WORKLET ||
it.actionType == GestureHandler.ACTION_TYPE_NATIVE_ANIMATED_EVENT
) {
it.cancel()
}
}

scheduleFinishedHandlersCleanup()
} finally {
recycleHandlerList(handlersToProcess)
}
}

/**
* isViewAttachedUnderWrapper checks whether all of parents for view related to handler
* view are attached. Since there might be an issue rarely observed when view
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,24 @@ class RNGestureHandlerButtonViewManager :
}
}

override fun requestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
super.requestDisallowInterceptTouchEvent(disallowIntercept)
if (!disallowIntercept) {
return
}

val moduleId = moduleId ?: return
val managedHandlerTag = managedHandlerTag ?: return

val orchestrator =
RNGestureHandlerRootView.findGestureHandlerRootView(this)?.orchestrator ?: return
val handler = RNGestureHandlerModule.registries[moduleId]?.getHandler(managedHandlerTag) ?: return

if (!orchestrator.isHandlingTouch && handler.view != null) {
handler.cancel()
}
}
Comment thread
j-piasecki marked this conversation as resolved.

override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
if (super.onInterceptTouchEvent(event)) {
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,28 @@ class RNGestureHandlerDetectorView(context: Context) : ReactViewGroup(context) {
super.onDetachedFromWindow()
}

override fun requestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
super.requestDisallowInterceptTouchEvent(disallowIntercept)
if (!disallowIntercept) {
return
}

val orchestrator = RNGestureHandlerRootView.findGestureHandlerRootView(this)?.orchestrator ?: return

if (!orchestrator.isHandlingTouch) {
val currentHandlers = attachedHandlers.mapNotNull { tag ->
RNGestureHandlerModule.registries[this.moduleId]?.getHandler(tag)
}.filter {
// The handler has been recorded
it.view != null
}

currentHandlers.forEach {
it.cancel()
}
Comment on lines +58 to +67

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we do it simpler?

Suggested change
val currentHandlers = attachedHandlers.mapNotNull { tag ->
RNGestureHandlerModule.registries[this.moduleId]?.getHandler(tag)
}.filter {
// The handler has been recorded
it.view != null
}
currentHandlers.forEach {
it.cancel()
}
val registry = RNGestureHandlerModule.registries[moduleId] ?: return
for (tag in attachedHandlers) {
registry.getHandler(tag)?.takeIf { it.view != null }?.cancel()
}

}
}

fun setModuleId(id: Int) {
if (this.moduleId == id) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.swmansion.gesturehandler.core.GestureHandlerOrchestrator
import com.swmansion.gesturehandler.core.OnJSResponderCancelListener

class RNGestureHandlerRootHelper(private val context: ReactContext, wrappedView: ViewGroup, private val moduleId: Int) {
private val orchestrator: GestureHandlerOrchestrator?
val orchestrator: GestureHandlerOrchestrator?
private val jsGestureHandler: GestureHandler?
val rootView: ViewGroup
private var shouldIntercept = false
Expand Down Expand Up @@ -57,7 +57,7 @@ class RNGestureHandlerRootHelper(private val context: ReactContext, wrappedView:
}
jsGestureHandler = RootViewGestureHandler(handlerTag = -wrappedViewTag)
registry.registerHandler(jsGestureHandler)
registry.attachHandlerToView(jsGestureHandler.tag, wrappedViewTag, GestureHandler.ACTION_TYPE_JS_FUNCTION_OLD_API)
registry.attachHandlerToView(jsGestureHandler.tag, wrappedViewTag, GestureHandler.ACTION_TYPE_NONE)
module.registerRootHelper(this)
}

Expand Down Expand Up @@ -120,7 +120,7 @@ class RNGestureHandlerRootHelper(private val context: ReactContext, wrappedView:
if (orchestrator != null && !passingTouch) {
// if we are in the process of delivering touch events via GH orchestrator, we don't want to
// treat it as a native gesture capturing the lock
tryCancelAllHandlers()
orchestrator.cancelAllLegacyHandlers()
}
}

Expand All @@ -142,17 +142,6 @@ class RNGestureHandlerRootHelper(private val context: ReactContext, wrappedView:
return shouldIntercept
}

private fun tryCancelAllHandlers() {
// In order to cancel handlers we activate handler that is hooked to the root view
jsGestureHandler?.apply {
if (state == GestureHandler.STATE_BEGAN) {
// Try activate main JS handler
activate()
end()
}
}
}

fun activateNativeHandlers(view: View) {
orchestrator?.activateNativeHandlersForView(view)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@ import com.facebook.react.common.ReactConstants
import com.facebook.react.uimanager.RootView
import com.facebook.react.views.view.ReactViewGroup
import com.swmansion.gesturehandler.core.GestureHandler
import com.swmansion.gesturehandler.core.GestureHandlerOrchestrator

class RNGestureHandlerRootView(context: Context?) : ReactViewGroup(context) {
private var moduleId: Int = -1
private var rootViewEnabled = false
private var unstableForceActive = false
private var rootHelper: RNGestureHandlerRootHelper? = null // TODO: resettable lateinit

val orchestrator: GestureHandlerOrchestrator?
get() = rootHelper?.orchestrator

override fun onAttachedToWindow() {
super.onAttachedToWindow()
rootViewEnabled = unstableForceActive || !hasGestureHandlerEnabledRootView(this)
Expand Down Expand Up @@ -72,7 +76,7 @@ class RNGestureHandlerRootView(context: Context?) : ReactViewGroup(context) {
}

override fun requestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
if (rootViewEnabled) {
if (rootViewEnabled && disallowIntercept) {
rootHelper!!.requestDisallowInterceptTouchEvent()
}
super.requestDisallowInterceptTouchEvent(disallowIntercept)
Expand Down Expand Up @@ -114,6 +118,10 @@ class RNGestureHandlerRootView(context: Context?) : ReactViewGroup(context) {
while (parent != null) {
if (parent is RNGestureHandlerRootView) {
gestureHandlerRootView = parent

if (parent.rootViewEnabled) {
return parent
}
}
parent = parent.parent
}
Expand Down
Loading