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

Change absolute position to be relative to the window instead of the screen #1812

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
@@ -1,9 +1,14 @@
package com.swmansion.gesturehandler

import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.graphics.Rect
import android.view.MotionEvent
import android.view.MotionEvent.PointerCoords
import android.view.MotionEvent.PointerProperties
import android.view.View
import android.view.Window
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.UiThreadUtil
import com.facebook.react.bridge.WritableArray
Expand All @@ -15,6 +20,7 @@ import java.util.*
open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestureHandlerT>> {
private val trackedPointerIDs = IntArray(MAX_POINTERS_COUNT)
private var trackedPointersIDsCount = 0
private val windowOffset = IntArray(2) { 0 }
var tag = 0
var view: View? = null
private set
Expand Down Expand Up @@ -158,6 +164,25 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
state = STATE_UNDETERMINED
this.view = view
this.orchestrator = orchestrator

val decorView = getWindow(view?.context)?.decorView
if (decorView != null) {
val frame = Rect()
decorView.getWindowVisibleDisplayFrame(frame)
windowOffset[0] = frame.left
windowOffset[1] = frame.top
} else {
windowOffset[0] = 0
windowOffset[1] = 0
}
}

private fun getWindow(context: Context?): Window? {
if (context == null) return null
if (context is Activity) return context.window
if (context is ContextWrapper) return getWindow(context.baseContext)

return null
}

private fun findNextLocalPointerId(): Int {
Expand Down Expand Up @@ -341,8 +366,8 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
pointerId,
event.getX(event.actionIndex),
event.getY(event.actionIndex),
event.getX(event.actionIndex) + offsetX,
event.getY(event.actionIndex) + offsetY,
event.getX(event.actionIndex) + offsetX - windowOffset[0],
event.getY(event.actionIndex) + offsetY - windowOffset[1],
)
trackedPointersCount++
addChangedPointer(trackedPointers[pointerId]!!)
Expand All @@ -363,8 +388,8 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
pointerId,
event.getX(event.actionIndex),
event.getY(event.actionIndex),
event.getX(event.actionIndex) + offsetX,
event.getY(event.actionIndex) + offsetY,
event.getX(event.actionIndex) + offsetX - windowOffset[0],
event.getY(event.actionIndex) + offsetY - windowOffset[1],
)
addChangedPointer(trackedPointers[pointerId]!!)
trackedPointers[pointerId] = null
Expand All @@ -387,8 +412,8 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
if (pointer.x != event.getX(i) || pointer.y != event.getY(i)) {
pointer.x = event.getX(i)
pointer.y = event.getY(i)
pointer.absoluteX = event.getX(i) + offsetX
pointer.absoluteY = event.getY(i) + offsetY
pointer.absoluteX = event.getX(i) + offsetX - windowOffset[0]
pointer.absoluteY = event.getY(i) + offsetY - windowOffset[1]

addChangedPointer(pointer)
pointersAdded++
Expand Down Expand Up @@ -651,6 +676,11 @@ open class GestureHandler<ConcreteGestureHandlerT : GestureHandler<ConcreteGestu
val lastRelativePositionY: Float
get() = lastAbsolutePositionY - lastEventOffsetY

val lastPositionInWindowX: Float
get() = lastAbsolutePositionX - windowOffset[0]
val lastPositionInWindowY: Float
get() = lastAbsolutePositionY - windowOffset[1]

companion object {
const val STATE_UNDETERMINED = 0
const val STATE_FAILED = 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ class RNGestureHandlerModule(reactContext: ReactApplicationContext?)
with(eventData) {
putDouble("x", PixelUtil.toDIPFromPixel(handler.lastRelativePositionX).toDouble())
putDouble("y", PixelUtil.toDIPFromPixel(handler.lastRelativePositionY).toDouble())
putDouble("absoluteX", PixelUtil.toDIPFromPixel(handler.lastAbsolutePositionX).toDouble())
putDouble("absoluteY", PixelUtil.toDIPFromPixel(handler.lastAbsolutePositionY).toDouble())
putDouble("absoluteX", PixelUtil.toDIPFromPixel(handler.lastPositionInWindowX).toDouble())
putDouble("absoluteY", PixelUtil.toDIPFromPixel(handler.lastPositionInWindowY).toDouble())
}
}
}
Expand Down Expand Up @@ -135,8 +135,8 @@ class RNGestureHandlerModule(reactContext: ReactApplicationContext?)
with(eventData) {
putDouble("x", PixelUtil.toDIPFromPixel(handler.lastRelativePositionX).toDouble())
putDouble("y", PixelUtil.toDIPFromPixel(handler.lastRelativePositionY).toDouble())
putDouble("absoluteX", PixelUtil.toDIPFromPixel(handler.lastAbsolutePositionX).toDouble())
putDouble("absoluteY", PixelUtil.toDIPFromPixel(handler.lastAbsolutePositionY).toDouble())
putDouble("absoluteX", PixelUtil.toDIPFromPixel(handler.lastPositionInWindowX).toDouble())
putDouble("absoluteY", PixelUtil.toDIPFromPixel(handler.lastPositionInWindowY).toDouble())
putInt("duration", handler.duration)
}
}
Expand Down Expand Up @@ -223,8 +223,8 @@ class RNGestureHandlerModule(reactContext: ReactApplicationContext?)
with(eventData) {
putDouble("x", PixelUtil.toDIPFromPixel(handler.lastRelativePositionX).toDouble())
putDouble("y", PixelUtil.toDIPFromPixel(handler.lastRelativePositionY).toDouble())
putDouble("absoluteX", PixelUtil.toDIPFromPixel(handler.lastAbsolutePositionX).toDouble())
putDouble("absoluteY", PixelUtil.toDIPFromPixel(handler.lastAbsolutePositionY).toDouble())
putDouble("absoluteX", PixelUtil.toDIPFromPixel(handler.lastPositionInWindowX).toDouble())
putDouble("absoluteY", PixelUtil.toDIPFromPixel(handler.lastPositionInWindowY).toDouble())
putDouble("translationX", PixelUtil.toDIPFromPixel(handler.translationX).toDouble())
putDouble("translationY", PixelUtil.toDIPFromPixel(handler.translationY).toDouble())
putDouble("velocityX", PixelUtil.toDIPFromPixel(handler.velocityX).toDouble())
Expand Down Expand Up @@ -275,8 +275,8 @@ class RNGestureHandlerModule(reactContext: ReactApplicationContext?)
with(eventData) {
putDouble("x", PixelUtil.toDIPFromPixel(handler.lastRelativePositionX).toDouble())
putDouble("y", PixelUtil.toDIPFromPixel(handler.lastRelativePositionY).toDouble())
putDouble("absoluteX", PixelUtil.toDIPFromPixel(handler.lastAbsolutePositionX).toDouble())
putDouble("absoluteY", PixelUtil.toDIPFromPixel(handler.lastAbsolutePositionY).toDouble())
putDouble("absoluteX", PixelUtil.toDIPFromPixel(handler.lastPositionInWindowX).toDouble())
putDouble("absoluteY", PixelUtil.toDIPFromPixel(handler.lastPositionInWindowY).toDouble())
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/api/gestures/fling-gesture.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ Y coordinate of the current position of the pointer (finger or a leading pointer

### `absoluteX`

X coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. The value is expressed in point units. It is recommended to use it instead of [`x`](#x) in cases when the original view can be transformed as an effect of the gesture.
X coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. The value is expressed in point units. It is recommended to use it instead of [`x`](#x) in cases when the original view can be transformed as an effect of the gesture.

### `absoluteY`

Y coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. The value is expressed in point units. It is recommended to use it instead of [`y`](#y) in cases when the original view can be transformed as an effect of the gesture.
Y coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. The value is expressed in point units. It is recommended to use it instead of [`y`](#y) in cases when the original view can be transformed as an effect of the gesture.

<BaseEventData />

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/api/gestures/long-press-gesture.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ Y coordinate, expressed in points, of the current position of the pointer (finge

### `absoluteX`

X coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. It is recommended to use `absoluteX` instead of [`x`](#x) in cases when the view attached to the [`GestureDetector`](./gesture-detector.md) can be transformed as an effect of the gesture.
X coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. It is recommended to use `absoluteX` instead of [`x`](#x) in cases when the view attached to the [`GestureDetector`](./gesture-detector.md) can be transformed as an effect of the gesture.

### `absoluteY`

Y coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. It is recommended to use `absoluteY` instead of [`y`](#y) in cases when the view attached to the [`GestureDetector`](./gesture-detector.md) can be transformed as an effect of the gesture.
Y coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. It is recommended to use `absoluteY` instead of [`y`](#y) in cases when the view attached to the [`GestureDetector`](./gesture-detector.md) can be transformed as an effect of the gesture.

### `duration`

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/api/gestures/pan-gesture.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ Y coordinate of the current position of the pointer (finger or a leading pointer

### `absoluteX`

X coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. The value is expressed in point units. It is recommended to use it instead of [`x`](#x) in cases when the original view can be transformed as an effect of the gesture.
X coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. The value is expressed in point units. It is recommended to use it instead of [`x`](#x) in cases when the original view can be transformed as an effect of the gesture.

### `absoluteY`

Y coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. The value is expressed in point units. It is recommended to use it instead of [`y`](#y) in cases when the original view can be transformed as an effect of the gesture.
Y coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. The value is expressed in point units. It is recommended to use it instead of [`y`](#y) in cases when the original view can be transformed as an effect of the gesture.

<BaseEventData />

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/api/gestures/tap-gesture.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ Y coordinate, expressed in points, of the current position of the pointer (finge

### `absoluteX`

X coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. It is recommended to use `absoluteX` instead of [`x`](#x) in cases when the view attached to the [`GestureDetector`](./gesture-detector.md) can be transformed as an effect of the gesture.
X coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. It is recommended to use `absoluteX` instead of [`x`](#x) in cases when the view attached to the [`GestureDetector`](./gesture-detector.md) can be transformed as an effect of the gesture.

### `absoluteY`

Y coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. It is recommended to use `absoluteY` instead of [`y`](#y) in cases when the view attached to the [`GestureDetector`](./gesture-detector.md) can be transformed as an effect of the gesture.
Y coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. It is recommended to use `absoluteY` instead of [`y`](#y) in cases when the view attached to the [`GestureDetector`](./gesture-detector.md) can be transformed as an effect of the gesture.

<BaseEventData />

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/api/gestures/touch-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ Y coordinate of the current position of the touch relative to the view attached

### `absoluteX`

X coordinate of the current position of the touch relative to the root view. The value is expressed in point units. It is recommended to use it instead of [`x`](#x) in cases when the original view can be transformed as an effect of the gesture.
X coordinate of the current position of the touch relative to the window. The value is expressed in point units. It is recommended to use it instead of [`x`](#x) in cases when the original view can be transformed as an effect of the gesture.

### `absoluteY`

Y coordinate of the current position of the touch relative to the root view. The value is expressed in point units. It is recommended to use it instead of [`y`](#y) in cases when the original view can be transformed as an effect of the gesture.
Y coordinate of the current position of the touch relative to the window. The value is expressed in point units. It is recommended to use it instead of [`y`](#y) in cases when the original view can be transformed as an effect of the gesture.
4 changes: 2 additions & 2 deletions docs/docs/gesture-handlers/api/fling-gh.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ Y coordinate of the current position of the pointer (finger or a leading pointer

### `absoluteX`

X coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. The value is expressed in point units. It is recommended to use it instead of [`x`](#x) in cases when the original view can be transformed as an effect of the gesture.
X coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. The value is expressed in point units. It is recommended to use it instead of [`x`](#x) in cases when the original view can be transformed as an effect of the gesture.

### `absoluteY`

Y coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. The value is expressed in point units. It is recommended to use it instead of [`y`](#y) in cases when the original view can be transformed as an effect of the gesture.
Y coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. The value is expressed in point units. It is recommended to use it instead of [`y`](#y) in cases when the original view can be transformed as an effect of the gesture.

## Example

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/gesture-handlers/api/longpress-gh.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ Y coordinate, expressed in points, of the current position of the pointer (finge

### `absoluteX`

X coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. It is recommended to use `absoluteX` instead of [`x`](#x) in cases when the view attached to the handler can be transformed as an effect of the gesture.
X coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. It is recommended to use `absoluteX` instead of [`x`](#x) in cases when the view attached to the handler can be transformed as an effect of the gesture.

### `absoluteY`

Y coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. It is recommended to use `absoluteY` instead of [`y`](#y) in cases when the view attached to the handler can be transformed as an effect of the gesture.
Y coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. It is recommended to use `absoluteY` instead of [`y`](#y) in cases when the view attached to the handler can be transformed as an effect of the gesture.

### `duration`

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/gesture-handlers/api/pan-gh.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ Y coordinate of the current position of the pointer (finger or a leading pointer

### `absoluteX`

X coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. The value is expressed in point units. It is recommended to use it instead of [`x`](#x) in cases when the original view can be transformed as an effect of the gesture.
X coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. The value is expressed in point units. It is recommended to use it instead of [`x`](#x) in cases when the original view can be transformed as an effect of the gesture.

### `absoluteY`

Y coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. The value is expressed in point units. It is recommended to use it instead of [`y`](#y) in cases when the original view can be transformed as an effect of the gesture.
Y coordinate of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. The value is expressed in point units. It is recommended to use it instead of [`y`](#y) in cases when the original view can be transformed as an effect of the gesture.

## Example

Expand Down
4 changes: 2 additions & 2 deletions docs/docs/gesture-handlers/api/tap-gh.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ Y coordinate, expressed in points, of the current position of the pointer (finge

### `absoluteX`

X coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. It is recommended to use `absoluteX` instead of [`x`](#x) in cases when the view attached to the handler can be transformed as an effect of the gesture.
X coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. It is recommended to use `absoluteX` instead of [`x`](#x) in cases when the view attached to the handler can be transformed as an effect of the gesture.

### `absoluteY`

Y coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the root view. It is recommended to use `absoluteY` instead of [`y`](#y) in cases when the view attached to the handler can be transformed as an effect of the gesture.
Y coordinate, expressed in points, of the current position of the pointer (finger or a leading pointer when there are multiple fingers placed) relative to the window. It is recommended to use `absoluteY` instead of [`y`](#y) in cases when the view attached to the handler can be transformed as an effect of the gesture.

## Example

Expand Down
Loading