Cocoa: Keep child NSWindow alive while it grabs the mouse

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 <morten.sorvig@digia.com>
Change-Id: I6fc53292cd96586cfdf401481c5442d759f1fae5
Reviewed-by: Liang Qi <liang.qi@digia.com>
This commit is contained in:
Gabriel de Dietrich 2014-02-26 20:05:00 +01:00 committed by The Qt Project
parent 018d1ca5f3
commit 28c9c2ea50
2 changed files with 24 additions and 2 deletions

View File

@ -69,10 +69,14 @@ typedef NSWindow<QNSWindowProtocol> 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;

View File

@ -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