Skip to content

Commit

Permalink
Android: Map touch position to global coordinates
Browse files Browse the repository at this point in the history
When QtWindow is not at 0,0 of its parent, not mapping the position
leads to offsets in the touched position in Android vs where it is
received in Qt, e.g. needing to touch below a button's actual
position to trigger a click.

Task-number: QTBUG-126178
Pick-to: 6.7 6.8
Change-Id: Icd62ed59f0f323ba3977145c3304e4e874aa4fa2
Reviewed-by: Assam Boudjelthia <[email protected]>
  • Loading branch information
Tinja Paavoseppä committed Jun 13, 2024
1 parent 0a10d23 commit 15674f4
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/plugins/platforms/android/androidjniinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ namespace QtAndroidInput
m_touchPoints.clear();
}

static void touchAdd(JNIEnv */*env*/, jobject /*thiz*/, jint /*winId*/, jint id, jint action, jboolean /*primary*/, jint x, jint y,
static void touchAdd(JNIEnv */*env*/, jobject /*thiz*/, jint winId, jint id, jint action, jboolean /*primary*/, jint x, jint y,
jfloat major, jfloat minor, jfloat rotation, jfloat pressure)
{
QEventPoint::State state = QEventPoint::State::Stationary;
Expand All @@ -287,16 +287,25 @@ namespace QtAndroidInput

const int dw = availableWidthPixels();
const int dh = availableHeightPixels();
QWindow *window = QtAndroid::windowFromId(winId);
if (!window) {
qCWarning(lcQpaInputMethods, "Touch event received for non-existing window %d", winId);
return;
}

QPointF mappedTouchPoint = window->mapToGlobal(QPointF(x, y));
QWindowSystemInterface::TouchPoint touchPoint;
touchPoint.id = id;
touchPoint.pressure = pressure;
touchPoint.rotation = qRadiansToDegrees(rotation);
touchPoint.normalPosition = QPointF(double(x / dw), double(y / dh));
touchPoint.normalPosition = QPointF((mappedTouchPoint.x() / dw),
(mappedTouchPoint.y() / dh));
touchPoint.state = state;
touchPoint.area = QRectF(x - double(minor * 0.5f),
y - double(major * 0.5f),
touchPoint.area = QRectF(mappedTouchPoint.x() - double(minor * 0.5f),
mappedTouchPoint.y() - double(major * 0.5f),
double(minor),
double(major));

m_touchPoints.push_back(touchPoint);
if (state == QEventPoint::State::Pressed) {
QAndroidInputContext *inputContext = QAndroidInputContext::androidInputContext();
Expand Down

0 comments on commit 15674f4

Please sign in to comment.