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

Add mouseButton implementation on Android #2680

Merged
merged 49 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
cb67ac2
Remove context menu
m-bert Nov 17, 2023
bd3b6a9
Add prop
m-bert Nov 17, 2023
441c02c
Change enum order to match web
m-bert Nov 17, 2023
f82e08a
Change enum
m-bert Nov 17, 2023
6e1c651
Fix field in TouchEventManager
m-bert Nov 17, 2023
9731777
Implement logic on tap
m-bert Nov 17, 2023
edd5c2c
Add example
m-bert Nov 17, 2023
75f94b2
Merge branch 'main' into @mbert/web-right-click
m-bert Nov 20, 2023
eeb5ada
Change type
m-bert Nov 20, 2023
f414760
Another type change
m-bert Nov 20, 2023
fd7e0bb
Yet another type change
m-bert Nov 20, 2023
5373c13
change condition
m-bert Nov 20, 2023
0e2aaf8
Add check to other gestures
m-bert Nov 20, 2023
ecfcee2
Update example
m-bert Nov 20, 2023
6284ab3
Change View to ScrollView
m-bert Nov 20, 2023
64ff5d4
remove console.log
m-bert Nov 21, 2023
193e240
Change button names
m-bert Nov 21, 2023
0cbaebd
Change MouseButtons name in App
m-bert Nov 21, 2023
f77f453
Add MouseButton.ALL
m-bert Nov 21, 2023
09e11fd
Rename function
m-bert Nov 21, 2023
577724b
Merge branch 'main' into @mbert/web-right-click
m-bert Nov 21, 2023
7501737
Merge branch 'main' into @mbert/web-right-click
m-bert Nov 21, 2023
8e1270f
Android implementation
m-bert Nov 23, 2023
d88ca89
Fix linter errors
m-bert Nov 23, 2023
a81b115
Remove unused imports
m-bert Nov 23, 2023
2d39d22
Merge branch 'main' into @mbert/web-right-click
m-bert Dec 6, 2023
3604140
Merge branch 'main' into @mbert/android-mouse-tests
m-bert Dec 6, 2023
870f218
Merge branch 'main' into @mbert/android-mouse-tests
m-bert Dec 14, 2023
ac10025
Merge branch 'main' into @mbert/web-right-click
m-bert Dec 14, 2023
7b660b4
Merge branch 'main' into @mbert/web-right-click
m-bert Dec 18, 2023
fbe82e2
Add enableContextMenu to builder
m-bert Dec 18, 2023
8e3d149
Merge branch 'main' into @mbert/android-mouse-tests
m-bert Dec 18, 2023
3183dc1
Merge branch '@mbert/web-right-click' into @mbert/android-mouse-tests
m-bert Dec 19, 2023
8f518b0
Merge branch 'main' into @mbert/web-right-click
m-bert Dec 19, 2023
c93f359
Merge branch 'main' into @mbert/android-mouse-tests
m-bert Dec 19, 2023
0a1a18d
False now turns off context menu
m-bert Dec 19, 2023
1107e38
Move enableContextMenu to GestureDetector
m-bert Dec 19, 2023
6bd1c79
Add context menu example
m-bert Dec 20, 2023
19aa15b
Remove listeners on handler drop
m-bert Dec 20, 2023
063889e
Update example
m-bert Dec 20, 2023
5313243
Merge main
m-bert Jan 12, 2024
b75319b
Merge main
m-bert Jan 12, 2024
d239d62
Merge web-right-click
m-bert Jan 12, 2024
71b7e4e
Merge main
m-bert Feb 20, 2024
9936d77
Remove context menu alias
m-bert Feb 20, 2024
9568562
Change enum values
m-bert Feb 20, 2024
c95bff3
Fix format in iOS format
m-bert Feb 20, 2024
d7aedb7
Remove space
m-bert Feb 20, 2024
7e24388
Merge branch '@mbert/android-mouse-tests' of github.com:software-mans…
m-bert Feb 20, 2024
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 @@ -67,6 +67,10 @@ class FlingGestureHandler : GestureHandler<FlingGestureHandler>() {
}

override fun onHandle(event: MotionEvent, sourceEvent: MotionEvent) {
if (!shouldActivateWithMouse(sourceEvent)) {
return
}
j-piasecki marked this conversation as resolved.
Show resolved Hide resolved

val state = state
if (state == STATE_UNDETERMINED) {
startFling(sourceEvent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Context
import android.content.ContextWrapper
import android.graphics.PointF
import android.graphics.Rect
import android.os.Build
import android.view.MotionEvent
import android.view.MotionEvent.PointerCoords
import android.view.MotionEvent.PointerProperties
Expand Down Expand Up @@ -68,6 +69,8 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
private var onTouchEventListener: OnTouchEventListener? = null
private var interactionController: GestureHandlerInteractionController? = null

protected var mouseButton = 0

@Suppress("UNCHECKED_CAST")
protected fun self(): ConcreteGestureHandlerT = this as ConcreteGestureHandlerT

Expand Down Expand Up @@ -159,6 +162,10 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
fun setInteractionController(controller: GestureHandlerInteractionController?): ConcreteGestureHandlerT =
applySelf { interactionController = controller }

fun setMouseButton(mouseButton: Int) = apply {
this.mouseButton = mouseButton
}

fun prepare(view: View?, orchestrator: GestureHandlerOrchestrator?) {
check(!(this.view != null || this.orchestrator != null)) { "Already prepared or hasn't been reset" }
Arrays.fill(trackedPointerIDs, -1)
Expand Down Expand Up @@ -688,6 +695,46 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
protected open fun onReset() {}
protected open fun onCancel() {}

private fun isButtonInConfig(clickedButton: Int): Boolean {
if (mouseButton == 0) {
return clickedButton == MotionEvent.BUTTON_PRIMARY
}

return clickedButton and mouseButton != 0
}

protected fun shouldActivateWithMouse(sourceEvent: MotionEvent): Boolean {
// While using mouse, we get both sets of events, for example ACTION_DOWN and ACTION_BUTTON_PRESS. That's why we want to take actions to only one of them.
// On API >= 23, we will use events with infix BUTTON, otherwise we use standard action events (like ACTION_DOWN).

with(sourceEvent) {
// To use actionButton, we need API >= 23.
if (getToolType(0) == MotionEvent.TOOL_TYPE_MOUSE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// While using mouse, we want to ignore default events for touch.
if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP || action == MotionEvent.ACTION_POINTER_DOWN) {
return@shouldActivateWithMouse false
}

// We don't want to do anything if wrong button was clicked. If we received event for BUTTON, we have to use actionButton to get which one was clicked.
if (action != MotionEvent.ACTION_MOVE && !isButtonInConfig(actionButton)) {
return@shouldActivateWithMouse false
}

// When we receive ACTION_MOVE, we have to check buttonState field.
if (action == MotionEvent.ACTION_MOVE && !isButtonInConfig(buttonState)) {
return@shouldActivateWithMouse false
}
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
// We do not fully support mouse below API 23, so we will ignore BUTTON events.
if (action == MotionEvent.ACTION_BUTTON_PRESS || action == MotionEvent.ACTION_BUTTON_RELEASE) {
return@shouldActivateWithMouse false
}
}
j-piasecki marked this conversation as resolved.
Show resolved Hide resolved
}

return true
}

/**
* Transforms a point in the coordinate space of the wrapperView (GestureHandlerRootView) to
* coordinate space of the view the gesture is attached to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class LongPressGestureHandler(context: Context) : GestureHandler<LongPressGestur
}

override fun onHandle(event: MotionEvent, sourceEvent: MotionEvent) {
if (!shouldActivateWithMouse(sourceEvent)) {
return
}

if (state == STATE_UNDETERMINED) {
previousTime = SystemClock.uptimeMillis()
startTime = previousTime
Expand All @@ -51,7 +55,7 @@ class LongPressGestureHandler(context: Context) : GestureHandler<LongPressGestur
activate()
}
}
if (sourceEvent.actionMasked == MotionEvent.ACTION_UP) {
if (sourceEvent.actionMasked == MotionEvent.ACTION_UP || sourceEvent.actionMasked == MotionEvent.ACTION_BUTTON_RELEASE) {
handler?.let {
it.removeCallbacksAndMessages(null)
handler = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ class PanGestureHandler(context: Context?) : GestureHandler<PanGestureHandler>()
}

override fun onHandle(event: MotionEvent, sourceEvent: MotionEvent) {
if (!shouldActivateWithMouse(sourceEvent)) {
return
}

val state = state
val action = sourceEvent.actionMasked
if (action == MotionEvent.ACTION_POINTER_UP || action == MotionEvent.ACTION_POINTER_DOWN) {
Expand Down Expand Up @@ -246,7 +250,7 @@ class PanGestureHandler(context: Context?) : GestureHandler<PanGestureHandler>()
velocityX = velocityTracker!!.xVelocity
velocityY = velocityTracker!!.yVelocity
}
if (action == MotionEvent.ACTION_UP) {
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_BUTTON_RELEASE) {
if (state == STATE_ACTIVE) {
end()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ class TapGestureHandler : GestureHandler<TapGestureHandler>() {
}

override fun onHandle(event: MotionEvent, sourceEvent: MotionEvent) {
if (!shouldActivateWithMouse(sourceEvent)) {
return
}

val state = state
val action = sourceEvent.actionMasked
if (state == STATE_UNDETERMINED) {
Expand All @@ -130,14 +134,14 @@ class TapGestureHandler : GestureHandler<TapGestureHandler>() {
if (shouldFail()) {
fail()
} else if (state == STATE_UNDETERMINED) {
if (action == MotionEvent.ACTION_DOWN) {
if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_BUTTON_PRESS) {
begin()
}
startTap()
} else if (state == STATE_BEGAN) {
if (action == MotionEvent.ACTION_UP) {
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_BUTTON_RELEASE) {
endTap()
} else if (action == MotionEvent.ACTION_DOWN) {
} else if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_BUTTON_PRESS) {
startTap()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class RNGestureHandlerModule(reactContext: ReactApplicationContext?) :
if (config.hasKey(KEY_MANUAL_ACTIVATION)) {
handler.setManualActivation(config.getBoolean(KEY_MANUAL_ACTIVATION))
}
if (config.hasKey("mouseButton")) {
handler.setMouseButton(config.getInt("mouseButton"))
}
}

abstract fun createEventBuilder(handler: T): GestureHandlerEventDataBuilder<T>
Expand Down
5 changes: 2 additions & 3 deletions apple/Handlers/RNFlingHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@ - (RNGestureHandlerEventExtraData *)eventExtraData:(id)_recognizer

RNBetterSwipeGestureRecognizer *recognizer = (RNBetterSwipeGestureRecognizer *)_recognizer;

CGPoint viewAbsolutePosition =
[recognizer.view convertPoint:recognizer.view.bounds.origin
toView:RCTKeyWindow().rootViewController.view];
CGPoint viewAbsolutePosition = [recognizer.view convertPoint:recognizer.view.bounds.origin
toView:RCTKeyWindow().rootViewController.view];
CGPoint locationInView = [recognizer getLastLocation];

return [RNGestureHandlerEventExtraData
Expand Down
2 changes: 2 additions & 0 deletions src/handlers/gestureHandlerCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export type ActiveCursor =
| 'zoom-in'
| 'zoom-out';

export type EnableContextMenu = boolean;

m-bert marked this conversation as resolved.
Show resolved Hide resolved
//TODO(TS) events in handlers

export interface GestureEvent<ExtraEventPayloadT = Record<string, unknown>> {
Expand Down
Loading