From c36c5e9b552f826c96bf066252b49de03921c03f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Tue, 12 Feb 2019 20:33:13 +0100 Subject: macOS: Modernize worksWhenModal handling The code in QCocoaEventDispatcher was dead and could be removed. Change-Id: I0c57e64791045d65033376c096220983059028ba Reviewed-by: Timur Pocheptsov --- .../platforms/cocoa/qcocoaeventdispatcher.h | 1 - .../platforms/cocoa/qcocoaeventdispatcher.mm | 41 ---------------------- src/plugins/platforms/cocoa/qcocoawindow.mm | 30 +++++++--------- src/plugins/platforms/cocoa/qnswindow.h | 1 + src/plugins/platforms/cocoa/qnswindow.mm | 13 +++++++ 5 files changed, 27 insertions(+), 59 deletions(-) (limited to 'src/plugins/platforms') diff --git a/src/plugins/platforms/cocoa/qcocoaeventdispatcher.h b/src/plugins/platforms/cocoa/qcocoaeventdispatcher.h index ebf33cf4e2..44a9c52b7b 100644 --- a/src/plugins/platforms/cocoa/qcocoaeventdispatcher.h +++ b/src/plugins/platforms/cocoa/qcocoaeventdispatcher.h @@ -168,7 +168,6 @@ public: uint processEventsCalled; NSModalSession currentModalSessionCached; NSModalSession currentModalSession(); - void updateChildrenWorksWhenModal(); void temporarilyStopAllModalSessions(); void beginModalSession(QWindow *widget); void endModalSession(QWindow *widget); diff --git a/src/plugins/platforms/cocoa/qcocoaeventdispatcher.mm b/src/plugins/platforms/cocoa/qcocoaeventdispatcher.mm index b0f2b6d940..b944c3d28c 100644 --- a/src/plugins/platforms/cocoa/qcocoaeventdispatcher.mm +++ b/src/plugins/platforms/cocoa/qcocoaeventdispatcher.mm @@ -672,45 +672,6 @@ NSModalSession QCocoaEventDispatcherPrivate::currentModalSession() return currentModalSessionCached; } -static void setChildrenWorksWhenModal(QWindow *window, bool worksWhenModal) -{ - Q_UNUSED(window) - Q_UNUSED(worksWhenModal) - - // For NSPanels (but not NSWindows, sadly), we can set the flag - // worksWhenModal, so that they are active even when they are not modal. -/* - ### not ported - QList dialogs = window->findChildren(); - for (int i=0; i(window) setWorksWhenModal:worksWhenModal]; - if (worksWhenModal && [window isVisible]){ - [window orderFront:window]; - } - } - } -*/ -} - -void QCocoaEventDispatcherPrivate::updateChildrenWorksWhenModal() -{ - // Make the dialog children of the window - // active. And make the dialog children of - // the previous modal dialog unactive again: - QMacAutoReleasePool pool; - int size = cocoaModalSessionStack.size(); - if (size > 0){ - if (QWindow *prevModal = cocoaModalSessionStack[size-1].window) - setChildrenWorksWhenModal(prevModal, true); - if (size > 1){ - if (QWindow *prevModal = cocoaModalSessionStack[size-2].window) - setChildrenWorksWhenModal(prevModal, false); - } - } -} - void QCocoaEventDispatcherPrivate::cleanupModalSessions() { // Go through the list of modal sessions, and end those @@ -743,7 +704,6 @@ void QCocoaEventDispatcherPrivate::cleanupModalSessions() cocoaModalSessionStack.remove(i); } - updateChildrenWorksWhenModal(); cleanupModalSessionsNeeded = false; } @@ -764,7 +724,6 @@ void QCocoaEventDispatcherPrivate::beginModalSession(QWindow *window) // stopped in cleanupModalSessions()). QCocoaModalSessionInfo info = {window, nullptr, nullptr}; cocoaModalSessionStack.push(info); - updateChildrenWorksWhenModal(); currentModalSessionCached = nullptr; } diff --git a/src/plugins/platforms/cocoa/qcocoawindow.mm b/src/plugins/platforms/cocoa/qcocoawindow.mm index ec7ad38be5..5a26f57c8c 100644 --- a/src/plugins/platforms/cocoa/qcocoawindow.mm +++ b/src/plugins/platforms/cocoa/qcocoawindow.mm @@ -367,23 +367,19 @@ void QCocoaWindow::setVisible(bool visible) [m_view.window orderFront:nil]; } - // We want the events to properly reach the popup, dialog, and tool - if ((window()->type() == Qt::Popup || window()->type() == Qt::Dialog || window()->type() == Qt::Tool) - && [m_view.window isKindOfClass:[NSPanel class]]) { - ((NSPanel *)m_view.window).worksWhenModal = YES; - if (!(parentCocoaWindow && window()->transientParent()->isActive()) && window()->type() == Qt::Popup) { - removeMonitor(); - NSEventMask eventMask = NSEventMaskLeftMouseDown | NSEventMaskRightMouseDown - | NSEventMaskOtherMouseDown | NSEventMaskMouseMoved; - monitor = [NSEvent addGlobalMonitorForEventsMatchingMask:eventMask handler:^(NSEvent *e) { - const auto button = cocoaButton2QtButton(e); - const auto buttons = currentlyPressedMouseButtons(); - const auto eventType = cocoaEvent2QtMouseEvent(e); - const auto globalPoint = QCocoaScreen::mapFromNative(NSEvent.mouseLocation); - const auto localPoint = window()->mapFromGlobal(globalPoint.toPoint()); - QWindowSystemInterface::handleMouseEvent(window(), localPoint, globalPoint, buttons, button, eventType); - }]; - } + // Close popup when clicking outside it + if (window()->type() == Qt::Popup && !(parentCocoaWindow && window()->transientParent()->isActive())) { + removeMonitor(); + NSEventMask eventMask = NSEventMaskLeftMouseDown | NSEventMaskRightMouseDown + | NSEventMaskOtherMouseDown | NSEventMaskMouseMoved; + monitor = [NSEvent addGlobalMonitorForEventsMatchingMask:eventMask handler:^(NSEvent *e) { + const auto button = cocoaButton2QtButton(e); + const auto buttons = currentlyPressedMouseButtons(); + const auto eventType = cocoaEvent2QtMouseEvent(e); + const auto globalPoint = QCocoaScreen::mapFromNative(NSEvent.mouseLocation); + const auto localPoint = window()->mapFromGlobal(globalPoint.toPoint()); + QWindowSystemInterface::handleMouseEvent(window(), localPoint, globalPoint, buttons, button, eventType); + }]; } } } diff --git a/src/plugins/platforms/cocoa/qnswindow.h b/src/plugins/platforms/cocoa/qnswindow.h index 64f1ed0802..dcbcd58901 100644 --- a/src/plugins/platforms/cocoa/qnswindow.h +++ b/src/plugins/platforms/cocoa/qnswindow.h @@ -62,6 +62,7 @@ QT_FORWARD_DECLARE_CLASS(QCocoaWindow) @protocol QNSWindowProtocol @optional - (BOOL)canBecomeKeyWindow; +- (BOOL)worksWhenModal; - (void)sendEvent:(NSEvent*)theEvent; - (void)closeAndRelease; - (void)dealloc; diff --git a/src/plugins/platforms/cocoa/qnswindow.mm b/src/plugins/platforms/cocoa/qnswindow.mm index c17ad47aba..28a9fa8607 100644 --- a/src/plugins/platforms/cocoa/qnswindow.mm +++ b/src/plugins/platforms/cocoa/qnswindow.mm @@ -177,6 +177,19 @@ static bool isMouseEvent(NSEvent *ev) return canBecomeMain; } +- (BOOL)worksWhenModal +{ + if ([self isKindOfClass:[QNSPanel class]]) { + if (QCocoaWindow *pw = self.platformWindow) { + Qt::WindowType type = pw->window()->type(); + if (type == Qt::Popup || type == Qt::Dialog || type == Qt::Tool) + return YES; + } + } + + return qt_objcDynamicSuper(); +} + - (BOOL)isOpaque { return self.platformWindow ? -- cgit v1.2.3