Enable touch events on Mac for PinchArea
authorShawn Rutledge <shawn.rutledge@digia.com>
Fri, 3 May 2013 11:47:42 +0000 (13:47 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Fri, 3 May 2013 14:17:41 +0000 (16:17 +0200)
Enabling touch events on a window causes scroll event
lag so we want to avoid avoid it as far as possible.

Enable/disable on scene changes, similar to what we do with
WA_AcceptTouchEvents for widgets, and in change
I2e5b5e2b093cccfc5253f7228f5ec0c588c60371 for MultiPointTouchArea.

Change-Id: I8cd8d172ffd93cfc4ec115917cc8662202f3b069
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@digia.com>
src/quick/items/qquickpincharea.cpp
src/quick/items/qquickpincharea_p.h

index 0e47b61..dc586dc 100644 (file)
@@ -45,6 +45,7 @@
 #include <QtGui/qevent.h>
 #include <QtGui/qguiapplication.h>
 #include <QtGui/qstylehints.h>
+#include <qpa/qplatformnativeinterface.h>
 
 #include <float.h>
 #include <math.h>
@@ -246,11 +247,15 @@ QQuickPinchAreaPrivate::~QQuickPinchAreaPrivate()
 
 QQuickPinchArea::QQuickPinchArea(QQuickItem *parent)
   : QQuickItem(*(new QQuickPinchAreaPrivate), parent)
+  , _currentWindow(0)
 {
     Q_D(QQuickPinchArea);
     d->init();
     setAcceptedMouseButtons(Qt::LeftButton);
     setFiltersChildMouseEvents(true);
+#ifdef Q_OS_MAC
+    connect(this, &QQuickItem::windowChanged, this, &QQuickPinchArea::setTouchEventsEnabledForWindow);
+#endif
 }
 
 QQuickPinchArea::~QQuickPinchArea()
@@ -539,6 +544,27 @@ QQuickPinch *QQuickPinchArea::pinch()
     return d->pinch;
 }
 
+void QQuickPinchArea::setTouchEventsEnabledForWindow(QWindow *window)
+{
+#ifdef Q_OS_MAC
+    // Resolve function for enabling touch events from the (cocoa) platform plugin.
+    typedef void (*RegisterTouchWindowFunction)(QWindow *, bool);
+    RegisterTouchWindowFunction registerTouchWindow = reinterpret_cast<RegisterTouchWindowFunction>(
+        QGuiApplication::platformNativeInterface()->nativeResourceFunctionForIntegration("registertouchwindow"));
+    if (!registerTouchWindow)
+        return; // Not necessarily an error, Qt migh be using a different platform plugin.
+
+    // Disable touch on the old window, enable on the new window.
+    if (_currentWindow)
+        registerTouchWindow(_currentWindow, false);
+    if (window)
+        registerTouchWindow(window, true);
+    // Save the current window, setTouchEventsEnabledForWindow will be called
+    // with a null window on disable.
+    _currentWindow = window;
+#endif
+}
+
 
 QT_END_NAMESPACE
 
index 4fc77d7..60c2dc7 100644 (file)
@@ -283,12 +283,16 @@ protected:
                                  const QRectF &oldGeometry);
     virtual void itemChange(ItemChange change, const ItemChangeData& value);
 
+private slots:
+    void setTouchEventsEnabledForWindow(QWindow *window);
+
 private:
     void updatePinch();
     void handlePress();
     void handleRelease();
 
 private:
+    QWindow *_currentWindow;
     Q_DISABLE_COPY(QQuickPinchArea)
     Q_DECLARE_PRIVATE(QQuickPinchArea)
 };