macOS: Track menu bar clicks for popup closing in QCocoaIntegration

A static member in QCocoaMenuBar relies on the app having at least
one QCocoaMenuBar, and also stays around after the qGuiApp has gone
away, potentially leading to a crash.

As a drive-by, clarify the comment and variable name to point out
that we're closing popups on _any_ native menu opening, not just
menus in the main menu bar (NSApp.mainMenu).

Pick-to: 6.7 6.5
Change-Id: Ib14919e317d70e4c76f3d89c78331edb958bf048
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit daad2b28fdb884e6d796ab024bcb962155cf5534)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2024-06-17 15:33:34 +02:00 committed by Qt Cherry-pick Bot
parent b694c58d5e
commit 9758c1556c
3 changed files with 8 additions and 5 deletions

View File

@ -119,6 +119,8 @@ private:
#endif
QCocoaWindowManager m_windowManager;
QMacNotificationObserver m_menuTrackingObserver;
};
Q_DECLARE_OPERATORS_FOR_FLAGS(QCocoaIntegration::Options)

View File

@ -168,6 +168,12 @@ QCocoaIntegration::QCocoaIntegration(const QStringList &paramList)
connect(qGuiApp, &QGuiApplication::focusWindowChanged,
this, &QCocoaIntegration::focusWindowChanged);
// Opening of a native menu should close all popup windows
m_menuTrackingObserver = QMacNotificationObserver(nil,
NSMenuDidBeginTrackingNotification, ^{
QGuiApplicationPrivate::instance()->closeAllPopups();
});
}
QCocoaIntegration::~QCocoaIntegration()

View File

@ -25,11 +25,6 @@ QCocoaMenuBar::QCocoaMenuBar()
{
static_menubars.append(this);
// clicks into the menu bar should close all popup windows
static QMacNotificationObserver menuBarClickObserver(nil, NSMenuDidBeginTrackingNotification, ^{
QGuiApplicationPrivate::instance()->closeAllPopups();
});
m_nativeMenu = [[NSMenu alloc] init];
qCDebug(lcQpaMenus) << "Constructed" << this << "with" << m_nativeMenu;
}