From 15674f4ce9ea455b47f68d8871d5676d7a731630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tinja=20Paavosepp=C3=A4?= Date: Thu, 13 Jun 2024 09:21:32 +0300 Subject: [PATCH] Android: Map touch position to global coordinates 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 --- .../platforms/android/androidjniinput.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/plugins/platforms/android/androidjniinput.cpp b/src/plugins/platforms/android/androidjniinput.cpp index 07f2786cc63..2692488ec62 100644 --- a/src/plugins/platforms/android/androidjniinput.cpp +++ b/src/plugins/platforms/android/androidjniinput.cpp @@ -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; @@ -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();