QCocoaSystemTrayIcon: fix updateMenu() logic

Amends da754d5b65
We should avoid registering an observer multiple times for the same
NSMenu object, because it results in multiple consecutive
emitActivated() calls.

So,
- bail out early if menu is the same, i.e. we have nothing to do
- unregister an observer we added previously for the old menu

Fixes: QTBUG-119053
Pick-to: 6.6 6.5
Change-Id: I14fce693dba351b1d12f2140fa8e672227cc7a9e
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
bb10
Vladimir Belyavsky 2023-11-10 18:54:40 +03:00
parent 37048d0417
commit 5b451efe81
1 changed files with 12 additions and 1 deletions

View File

@ -184,7 +184,18 @@ void QCocoaSystemTrayIcon::updateIcon(const QIcon &icon)
void QCocoaSystemTrayIcon::updateMenu(QPlatformMenu *menu)
{
m_statusItem.menu = menu ? static_cast<QCocoaMenu *>(menu)->nsMenu() : nil;
auto *nsMenu = menu ? static_cast<QCocoaMenu *>(menu)->nsMenu() : nil;
if (m_statusItem.menu == nsMenu)
return;
if (m_statusItem.menu) {
[NSNotificationCenter.defaultCenter removeObserver:m_delegate
name:NSMenuDidBeginTrackingNotification
object:m_statusItem.menu
];
}
m_statusItem.menu = nsMenu;
if (m_statusItem.menu) {
// When a menu is assigned, NSStatusBarButtonCell will intercept the mouse