From 6eb27afcdfb6df2e620f1ec66196600f0f9ed022 Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Tue, 19 Apr 2016 13:45:12 -0700 Subject: Cocoa: Add QCocoaWindowPointer This patch extends 5b54c352edbc597ec5283bc9cfdd77906350161f by abstracting the watcher pattern. The class is specialized for QCocoaWindow since it's aware of the QObject sentinel there. We update the usage in QNSWindowHelper and extend it to the forward window (this one is used for mouse coordinate conversion when docking windows). Change-Id: I628415527593daec835bbad1b6e83d13fe7b6703 Reviewed-by: Timur Pocheptsov --- src/plugins/platforms/cocoa/qcocoawindow.mm | 31 +++++++++++++++++++---------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'src/plugins/platforms/cocoa/qcocoawindow.mm') diff --git a/src/plugins/platforms/cocoa/qcocoawindow.mm b/src/plugins/platforms/cocoa/qcocoawindow.mm index 12e85c5205..dd67946a27 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.mm +++ b/src/plugins/platforms/cocoa/qcocoawindow.mm @@ -85,7 +85,7 @@ static bool isMouseEvent(NSEvent *ev) self = [super init]; if (self) { _window = window; - _platformWindow = platformWindow; + _platformWindow.assign(platformWindow); _window.delegate = [[QNSWindowDelegate alloc] initWithQCocoaWindow:_platformWindow]; @@ -94,7 +94,6 @@ static bool isMouseEvent(NSEvent *ev) // make sure that m_nsWindow stays valid until the // QCocoaWindow is deleted by Qt. [_window setReleasedWhenClosed:NO]; - _watcher = &_platformWindow->sentinel; } return self; @@ -103,19 +102,19 @@ static bool isMouseEvent(NSEvent *ev) - (void)handleWindowEvent:(NSEvent *)theEvent { QCocoaWindow *pw = self.platformWindow; - if (_watcher && pw && pw->m_forwardWindow) { + if (pw && pw->m_forwardWindow) { if (theEvent.type == NSLeftMouseUp || theEvent.type == NSLeftMouseDragged) { QNSView *forwardView = pw->m_qtView; if (theEvent.type == NSLeftMouseUp) { [forwardView mouseUp:theEvent]; - pw->m_forwardWindow = 0; + pw->m_forwardWindow.clear(); } else { [forwardView mouseDragged:theEvent]; } } if (!pw->m_isNSWindowChild && theEvent.type == NSLeftMouseDown) { - pw->m_forwardWindow = 0; + pw->m_forwardWindow.clear(); } } @@ -142,7 +141,7 @@ static bool isMouseEvent(NSEvent *ev) if (!self.window.delegate) return; // Already detached, pending NSAppKitDefined event - if (_watcher && pw && pw->frameStrutEventsEnabled() && isMouseEvent(theEvent)) { + if (pw && pw->frameStrutEventsEnabled() && isMouseEvent(theEvent)) { NSPoint loc = [theEvent locationInWindow]; NSRect windowFrame = [self.window convertRectFromScreen:[self.window frame]]; NSRect contentFrame = [[self.window contentView] frame]; @@ -157,8 +156,7 @@ static bool isMouseEvent(NSEvent *ev) - (void)detachFromPlatformWindow { - _platformWindow = 0; - _watcher.clear(); + self.platformWindow.clear(); [self.window.delegate release]; self.window.delegate = nil; } @@ -179,7 +177,7 @@ static bool isMouseEvent(NSEvent *ev) - (void)dealloc { _window = nil; - _platformWindow = 0; + self.platformWindow.clear(); [super dealloc]; } @@ -331,6 +329,18 @@ static bool isMouseEvent(NSEvent *ev) @end +void QCocoaWindowPointer::assign(QCocoaWindow *w) +{ + window = w; + watcher = &w->sentinel; +} + +void QCocoaWindowPointer::clear() +{ + window = Q_NULLPTR; + watcher.clear(); +} + const int QCocoaWindow::NoAlertRequest = -1; QCocoaWindow::QCocoaWindow(QWindow *tlw) @@ -338,7 +348,6 @@ QCocoaWindow::QCocoaWindow(QWindow *tlw) , m_contentView(nil) , m_qtView(nil) , m_nsWindow(0) - , m_forwardWindow(0) , m_contentViewIsEmbedded(false) , m_contentViewIsToBeEmbedded(false) , m_parentCocoaWindow(0) @@ -1320,7 +1329,7 @@ void QCocoaWindow::recreateWindow(const QPlatformWindow *parentWindow) if (oldParentCocoaWindow) { if (!m_isNSWindowChild || oldParentCocoaWindow != m_parentCocoaWindow) oldParentCocoaWindow->removeChildWindow(this); - m_forwardWindow = oldParentCocoaWindow; + m_forwardWindow.assign(oldParentCocoaWindow); } setNSWindow(m_nsWindow); -- cgit v1.2.3 From ab3a8443faa36799ab8142e48d25d8ff10f9d0bf Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Fri, 29 Apr 2016 18:07:36 +0200 Subject: QCocoaWindow - do not call resignKeyWindow MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QCocoaWindow::setMouseGrabEnabled/setKeyboardGrabEnabled calls resignKeyWindow. According to Apple's docs, we should _never_ call this function (it's done by Cocoa automatically). Calling this function leaves a window in weird state where it can not become key again until you explicitly make another window key first. Task-number: QTBUG-53050 Change-Id: I7a887659df8df11880328ffa2adc07c4a3af63e6 Reviewed-by: Gabriel de Dietrich Reviewed-by: Morten Johan Sørvig --- src/plugins/platforms/cocoa/qcocoawindow.mm | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/plugins/platforms/cocoa/qcocoawindow.mm') diff --git a/src/plugins/platforms/cocoa/qcocoawindow.mm b/src/plugins/platforms/cocoa/qcocoawindow.mm index dd67946a27..c5519995b6 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.mm +++ b/src/plugins/platforms/cocoa/qcocoawindow.mm @@ -1135,8 +1135,7 @@ bool QCocoaWindow::setKeyboardGrabEnabled(bool grab) if (grab && ![m_nsWindow isKeyWindow]) [m_nsWindow makeKeyWindow]; - else if (!grab && [m_nsWindow isKeyWindow]) - [m_nsWindow resignKeyWindow]; + return true; } @@ -1147,8 +1146,7 @@ bool QCocoaWindow::setMouseGrabEnabled(bool grab) if (grab && ![m_nsWindow isKeyWindow]) [m_nsWindow makeKeyWindow]; - else if (!grab && [m_nsWindow isKeyWindow]) - [m_nsWindow resignKeyWindow]; + return true; } -- cgit v1.2.3