From 28c9c2ea50e54236d5975f263ee1d28d831223aa Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Wed, 26 Feb 2014 20:05:00 +0100 Subject: Cocoa: Keep child NSWindow alive while it grabs the mouse MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Otherwise, Cocoa loses sight on which window to send the dragging mouse event. If the window is kept alive (but hidden) Cocoa will send the events to it, and we can forward them to the actual QWindow. This is the expected use-case: 1. Start dragging a QWindow and change its flags. 2. This triggers a call to QCocoaWindow::recreateWindow(), which will get rid of the old NSWindow and create a new one (the QNSView is moved to the new NSWindow). 3. When we stop dragging, the NSWindow is finally destroyed. QNSView Pointer References Remarks: In QNSView, m_window points to the QWindow which remains unchanged until deleted. Similarly m_platformWindow remains valid until deleted, in which case we delete the QNSView, the NSWindow and its helper (see QCocoaWindow destructor). This fixes undocking QToolBars when they are a child NSWindow. Task-number: QTBUG-33082 Reviewed-by: Morten Johan Sørvig Change-Id: I6fc53292cd96586cfdf401481c5442d759f1fae5 Reviewed-by: Liang Qi --- src/plugins/platforms/cocoa/qcocoawindow.h | 4 ++++ src/plugins/platforms/cocoa/qcocoawindow.mm | 22 ++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/plugins/platforms/cocoa/qcocoawindow.h b/src/plugins/platforms/cocoa/qcocoawindow.h index 0f08cd18fb..67cac41383 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.h +++ b/src/plugins/platforms/cocoa/qcocoawindow.h @@ -69,10 +69,14 @@ typedef NSWindow QCocoaNSWindow; { QCocoaNSWindow *_window; QCocoaWindow *_platformWindow; + BOOL _grabbingMouse; + BOOL _releaseOnMouseUp; } @property (nonatomic, readonly) QCocoaNSWindow *window; @property (nonatomic, readonly) QCocoaWindow *platformWindow; +@property (nonatomic) BOOL grabbingMouse; +@property (nonatomic) BOOL releaseOnMouseUp; - (id)initWithNSWindow:(QCocoaNSWindow *)window platformWindow:(QCocoaWindow *)platformWindow; - (void)handleWindowEvent:(NSEvent *)theEvent; diff --git a/src/plugins/platforms/cocoa/qcocoawindow.mm b/src/plugins/platforms/cocoa/qcocoawindow.mm index 65e2a15cec..f1f88a13dd 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.mm +++ b/src/plugins/platforms/cocoa/qcocoawindow.mm @@ -103,6 +103,8 @@ static bool isMouseEvent(NSEvent *ev) @synthesize window = _window; @synthesize platformWindow = _platformWindow; +@synthesize grabbingMouse = _grabbingMouse; +@synthesize releaseOnMouseUp = _releaseOnMouseUp; - (id)initWithNSWindow:(QCocoaNSWindow *)window platformWindow:(QCocoaWindow *)platformWindow { @@ -142,6 +144,17 @@ static bool isMouseEvent(NSEvent *ev) } } + if (theEvent.type == NSLeftMouseDown) { + self.grabbingMouse = YES; + } else if (theEvent.type == NSLeftMouseUp) { + self.grabbingMouse = NO; + if (self.releaseOnMouseUp) { + [self detachFromPlatformWindow]; + [self.window release]; + return; + } + } + [self.window superSendEvent:theEvent]; if (!self.window.delegate) @@ -234,9 +247,14 @@ static bool isMouseEvent(NSEvent *ev) - (void)closeAndRelease { - [self.helper detachFromPlatformWindow]; [self close]; - [self release]; + + if (self.helper.grabbingMouse) { + self.helper.releaseOnMouseUp = YES; + } else { + [self.helper detachFromPlatformWindow]; + [self release]; + } } - (void)dealloc -- cgit v1.2.3