From c30a7ecce1a236514599825cc818323d8affd9dc Mon Sep 17 00:00:00 2001
From: Richard Moe Gustavsen <richard.gustavsen@digia.com>
Date: Wed, 15 May 2013 09:35:50 +0200
Subject: [PATCH] iOS: fix bug when reporting the screen position of touch
 events
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The way we reported screen position (and normalized position)
for touch events was just wrong. The old implementation did
not take into account that a view could be anything else than
a direct child of the window, which fails for many cases (e.g
when using QGLWidgets). Nor did it take into account the status
bar, which made it hard to push small buttons since the touch
would always be slightly offset.

This patch calculates the screen pos by converting the touch pos
to window pos, and then subtract the application frame (that
contains the size of the status bar).

Change-Id: Ib7f5f6dcea3a611e1ed75d57fb4a4718564752f0
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@digia.com>
---
 src/plugins/platforms/ios/qioswindow.mm | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/src/plugins/platforms/ios/qioswindow.mm b/src/plugins/platforms/ios/qioswindow.mm
index cd2e5f9cb9e..02ac413b3b1 100644
--- a/src/plugins/platforms/ios/qioswindow.mm
+++ b/src/plugins/platforms/ios/qioswindow.mm
@@ -150,6 +150,8 @@
 
 - (void)updateTouchList:(NSSet *)touches withState:(Qt::TouchPointState)state
 {
+    QRect applicationRect = fromCGRect(self.window.screen.applicationFrame);
+
     foreach (UITouch *uiTouch, m_activeTouches.keys()) {
         QWindowSystemInterface::TouchPoint &touchPoint = m_activeTouches[uiTouch];
         if (![touches containsObject:uiTouch]) {
@@ -158,14 +160,12 @@
             touchPoint.state = state;
             touchPoint.pressure = (state == Qt::TouchPointReleased) ? 0.0 : 1.0;
 
-            // Set position
-            QRect viewGeometry = fromCGRect(self.frame);
-            QPoint touchViewLocation = fromCGPoint([uiTouch locationInView:self]);
-            QPoint touchScreenLocation = touchViewLocation + viewGeometry.topLeft();
-            touchPoint.area = QRectF(touchScreenLocation , QSize(0, 0));
-
-            CGSize fullscreenSize = self.window.rootViewController.view.bounds.size;
-            touchPoint.normalPosition = QPointF(touchScreenLocation.x() / fullscreenSize.width, touchScreenLocation.y() / fullscreenSize.height);
+            // Find the touch position relative to the window. Then calculate the screen
+            // position by subtracting the position of the applicationRect (since UIWindow
+            // does not take that into account when reporting its own frame):
+            QPoint touchPos = fromCGPoint([uiTouch locationInView:nil]);
+            touchPoint.area = QRectF(touchPos - applicationRect.topLeft(), QSize(0, 0));
+            touchPoint.normalPosition = QPointF(touchPos.x() / applicationRect.width(), touchPos.y() / applicationRect.height());
         }
     }
 }
-- 
GitLab