From 965bcca6d4e22788721a9af906adfbd97af9af29 Mon Sep 17 00:00:00 2001
From: Andy Shaw <andy.shaw@qt.io>
Date: Thu, 23 Mar 2017 17:49:16 +0100
Subject: [PATCH] Cocoa: Explicitly hide popup windows when the application is
 hidden
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When the application is hidden then Qt will hide the popup
windows associated with it when it has lost the activation
for the application. However, when it gets to this point it
believes the popup windows to be hidden already due to the
fact that the application itself is hidden. As a result,
when the application is restored it causes a problem with
the still visible popup window as it is taking the input
events without responding to them.

Therefore we need to explicitly hide the windows right before the
application is hidden to ensure that they are actually hidden
correctly.

Task-number: QTBUG-58727
Change-Id: I4be1e1c0b1388d0c9ec872e7732185670998b7af
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
---
 .../cocoa/qcocoaapplicationdelegate.mm         | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm b/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm
index 35ac7182af..03148c769b 100644
--- a/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm
+++ b/src/plugins/platforms/cocoa/qcocoaapplicationdelegate.mm
@@ -328,6 +328,24 @@ QT_END_NAMESPACE
     return NO; // Someday qApp->quitOnLastWindowClosed(); when QApp and NSApp work closer together.
 }
 
+- (void)applicationWillHide:(NSNotification *)notification
+{
+    if (reflectionDelegate
+        && [reflectionDelegate respondsToSelector:@selector(applicationWillHide:)]) {
+        [reflectionDelegate applicationWillHide:notification];
+    }
+
+    // When the application is hidden Qt will hide the popup windows associated with
+    // it when it has lost the activation for the application. However, when it gets
+    // to this point it believes the popup windows to be hidden already due to the
+    // fact that the application itself is hidden, which will cause a problem when
+    // the application is made visible again.
+    const QWindowList topLevelWindows = QGuiApplication::topLevelWindows();
+    for (QWindow *topLevelWindow : qAsConst(topLevelWindows)) {
+        if ((topLevelWindow->type() & Qt::Popup) == Qt::Popup && topLevelWindow->isVisible())
+            topLevelWindow->hide();
+    }
+}
 
 - (void)applicationDidBecomeActive:(NSNotification *)notification
 {