Android: Do not modify Touch Event position in JAVA code

In QtWindow class, the Touch Event coordinates were increased by Layout
position. That allows to move to global coordinates.
In the same time, in 15674f4ce9ea455b47f68d8871d5676d7a731630 commit, we
started mapping coordinates of Touch Event to Global position. That
cause the issue with incorrectly handled touches for example with
QDialogs.

To avoid this issue, we will stop modify Touch Event position in JAVA
code. We will send only localPosition to not make transformation twice.

With this change, all events received from JAVA have local position of
their window.

Pick-to: 6.8
Fixes: QTBUG-130576
Fixes: QTBUG-127925
Change-Id: I6b58e0369959840f869e31a3d96425c64507b39d
Reviewed-by: Tinja Paavoseppä <tinja.paavoseppa@qt.io>
(cherry picked from commit 23fd2cfb015226d6894b9cbe5fa50b43f0134e99)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Bartlomiej Moskal 2025-03-21 11:15:25 +01:00 committed by Qt Cherry-pick Bot
parent 1bf44cc71f
commit 141f061130
2 changed files with 24 additions and 22 deletions

View File

@ -141,7 +141,6 @@ class QtWindow extends QtLayout implements QtSurfaceInterface {
if (m_editText != null && m_inputConnectionListener != null)
m_inputConnectionListener.onEditTextChanged(m_editText);
event.setLocation(event.getX() + getX(), event.getY() + getY());
QtInputDelegate.sendTouchEvent(event, getId());
m_gestureDetector.onTouchEvent(event);
return true;

View File

@ -168,23 +168,23 @@ namespace QtAndroidInput
if (m_ignoreMouseEvents)
return;
const QPoint globalPos(x,y);
const QPoint localPos(x,y);
QWindow *window = windowFromId(winId);
m_mouseGrabber = window;
const QPoint localPos = window && window->handle() ?
window->handle()->mapFromGlobal(globalPos) : globalPos;
const QPoint globalPos = window && window->handle() ?
window->handle()->mapToGlobal(localPos) : localPos;
sendMouseButtonEvents(window, localPos, globalPos, mouseButtonState, QEvent::MouseButtonPress);
}
static void mouseUp(JNIEnv */*env*/, jobject /*thiz*/, jint winId, jint x, jint y, jint mouseButtonState)
{
const QPoint globalPos(x,y);
const QPoint localPos(x,y);
QWindow *window = m_mouseGrabber.data();
if (!window)
window = windowFromId(winId);
const QPoint localPos = window && window->handle() ?
window->handle()->mapFromGlobal(globalPos) : globalPos;
const QPoint globalPos = window && window->handle() ?
window->handle()->mapToGlobal(localPos) : localPos;
sendMouseButtonEvents(window, localPos, globalPos, mouseButtonState, QEvent::MouseButtonRelease);
m_ignoreMouseEvents = false;
@ -196,12 +196,12 @@ namespace QtAndroidInput
if (m_ignoreMouseEvents)
return;
const QPoint globalPos(x,y);
const QPoint localPos(x,y);
QWindow *window = m_mouseGrabber.data();
if (!window)
window = windowFromId(winId);
const QPoint localPos = window && window->handle() ?
window->handle()->mapFromGlobal(globalPos) : globalPos;
const QPoint globalPos = window && window->handle() ?
window->handle()->mapToGlobal(localPos) : localPos;
sendMouseButtonEvents(window, localPos, globalPos, mouseButtonState, QEvent::MouseMove);
}
@ -210,12 +210,12 @@ namespace QtAndroidInput
if (m_ignoreMouseEvents)
return;
const QPoint globalPos(x,y);
const QPoint localPos(x,y);
QWindow *window = m_mouseGrabber.data();
if (!window)
window = windowFromId(winId);
const QPoint localPos = window && window->handle() ?
window->handle()->mapFromGlobal(globalPos) : globalPos;
const QPoint globalPos = window && window->handle() ?
window->handle()->mapToGlobal(localPos) : localPos;
const QPoint angleDelta(hdelta * 120, vdelta * 120);
QWindowSystemInterface::handleWheelEvent(window,
@ -228,18 +228,21 @@ namespace QtAndroidInput
static void longPress(JNIEnv */*env*/, jobject /*thiz*/, jint winId, jint x, jint y)
{
QAndroidInputContext *inputContext = QAndroidInputContext::androidInputContext();
const QPoint globalPos(x,y);
QWindow *window = windowFromId(winId);
const QPoint localPos = window && window->handle() ?
window->handle()->mapFromGlobal(globalPos) : globalPos;
if (inputContext && qGuiApp)
QMetaObject::invokeMethod(inputContext, "longPress", Q_ARG(int, x), Q_ARG(int, y));
QMetaObject::invokeMethod(inputContext, "longPress", Q_ARG(int, globalPos.x()), Q_ARG(int, globalPos.y()));
//### TODO: add proper API for Qt 5.2
static bool rightMouseFromLongPress = qEnvironmentVariableIntValue("QT_ANDROID_ENABLE_RIGHT_MOUSE_FROM_LONG_PRESS");
if (!rightMouseFromLongPress)
return;
m_ignoreMouseEvents = true;
const QPoint globalPos(x,y);
QWindow *window = windowFromId(winId);
const QPoint localPos = window && window->handle() ?
window->handle()->mapFromGlobal(globalPos) : globalPos;
// Click right button if no other button is already pressed.
if (!m_mouseGrabber) {
@ -307,7 +310,7 @@ namespace QtAndroidInput
if (state == QEventPoint::State::Pressed) {
QAndroidInputContext *inputContext = QAndroidInputContext::androidInputContext();
if (inputContext && qGuiApp)
QMetaObject::invokeMethod(inputContext, "touchDown", Q_ARG(int, x), Q_ARG(int, y));
QMetaObject::invokeMethod(inputContext, "touchDown", Q_ARG(int, mappedTouchPoint.x()), Q_ARG(int, mappedTouchPoint.y()));
}
}
@ -379,10 +382,10 @@ namespace QtAndroidInput
jint pointerType, jint buttonState, jfloat x, jfloat y, jfloat pressure)
{
#if QT_CONFIG(tabletevent)
const QPointF globalPosF(x, y);
const QPointF localPos(x, y);
QWindow *window = windowFromId(winId);
const QPointF localPos = window && window->handle() ?
window->handle()->mapFromGlobalF(globalPosF) : globalPosF;
const QPointF globalPosF = window && window->handle() ?
window->handle()->mapFromGlobalF(localPos) : localPos;
// Galaxy Note with plain Android:
// 0 1 0 stylus press