macOS: Modernize worksWhenModal handling

The code in QCocoaEventDispatcher was dead and could be removed.

Change-Id: I0c57e64791045d65033376c096220983059028ba
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Tor Arne Vestbø 2019-02-12 20:33:13 +01:00
parent 025128a7e0
commit c36c5e9b55
5 changed files with 27 additions and 59 deletions

View File

@ -168,7 +168,6 @@ public:
uint processEventsCalled;
NSModalSession currentModalSessionCached;
NSModalSession currentModalSession();
void updateChildrenWorksWhenModal();
void temporarilyStopAllModalSessions();
void beginModalSession(QWindow *widget);
void endModalSession(QWindow *widget);

View File

@ -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<QDialog *> dialogs = window->findChildren<QDialog *>();
for (int i=0; i<dialogs.size(); ++i){
NSWindow *window = qt_mac_window_for(dialogs[i]);
if (window && [window isKindOfClass:[NSPanel class]]) {
[static_cast<NSPanel *>(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;
}

View File

@ -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);
}];
}
}
}

View File

@ -62,6 +62,7 @@ QT_FORWARD_DECLARE_CLASS(QCocoaWindow)
@protocol QNSWindowProtocol
@optional
- (BOOL)canBecomeKeyWindow;
- (BOOL)worksWhenModal;
- (void)sendEvent:(NSEvent*)theEvent;
- (void)closeAndRelease;
- (void)dealloc;

View File

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