From ae5f3df59b37e0ce8aaef27dc1e02f40def340ae Mon Sep 17 00:00:00 2001
From: Timur Pocheptsov <Timur.Pocheptsov@digia.com>
Date: Thu, 16 Oct 2014 12:53:04 +0200
Subject: [PATCH] OS X - unified toolbar and AA_NativeWindows
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

In some cases QToolBar creates a child window (a child borderless NSWindow)
placed on top of the toolbar's  main window. As a result it's not possible
to drag this main window using its toolbar - the window "jumps" on the screen.
The reason is quite subtle - QNSView -handleMouseEvent: uses
[NSEvent mouseLocation] and this location is invalid:

- we have an NSWindow (parent)
- we have a child NSWindow ([parent addChildWindow:child ....]
- we handle drag event in a child window
- we move a parent window as a result of drag event
- Cocoa also moves a child window for us
- when handling the  next drag event, location [NSEvent mouseLocation]
  differs from the real event's location.

Task-number: QTBUG-40106
Change-Id: Ic68cb92ab4233a1e0746b478820c1e33fd37a462
Reviewed-by: Morten Johan Sørvig <morten.sorvig@digia.com>
---
 src/plugins/platforms/cocoa/qnsview.mm | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/plugins/platforms/cocoa/qnsview.mm b/src/plugins/platforms/cocoa/qnsview.mm
index 06680228bc2..6234c0dcbe7 100644
--- a/src/plugins/platforms/cocoa/qnsview.mm
+++ b/src/plugins/platforms/cocoa/qnsview.mm
@@ -686,7 +686,23 @@ static NSString *_q_NSWindowDidChangeOcclusionStateNotification = nil;
             m_platformWindow->m_forwardWindow = 0;
     }
 
-    [targetView convertFromScreen:[NSEvent mouseLocation] toWindowPoint:&qtWindowPoint andScreenPoint:&qtScreenPoint];
+    NSPoint globalPos = [NSEvent mouseLocation];
+
+    if ([self.window parentWindow]
+        && (theEvent.type == NSLeftMouseDragged || theEvent.type == NSLeftMouseUp)) {
+        // QToolBar can be implemented as a child window on top of its main window
+        // (with a borderless NSWindow). If an option "unified toolbar" set on the main window,
+        // it's possible to drag such a window using this toolbar.
+        // While handling mouse drag events, QToolBar moves the window (QWidget::move).
+        // In such a combination [NSEvent mouseLocation] is very different from the
+        // real event location and as a result a window will move chaotically.
+        NSPoint winPoint = [theEvent locationInWindow];
+        NSRect tmpRect = NSMakeRect(winPoint.x, winPoint.y, 1., 1.);
+        tmpRect = [[theEvent window] convertRectToScreen:tmpRect];
+        globalPos = tmpRect.origin;
+    }
+
+    [targetView convertFromScreen:globalPos toWindowPoint:&qtWindowPoint andScreenPoint:&qtScreenPoint];
     ulong timestamp = [theEvent timestamp] * 1000;
 
     QCocoaDrag* nativeDrag = QCocoaIntegration::instance()->drag();
-- 
GitLab