From 6c2387571a6ccace9edb600ce6798f75affd961d Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Thu, 20 Oct 2022 15:47:22 +0200 Subject: [PATCH] QCocoaMenuBar: avoid duplication of 'special' entries in the 'Edit' menu MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For this, after the app's main menu is set, we are looking for a special menu (which is essentially translates into 'Edit') and check if AppKit inserted 'Start dictation' magic item. If not - we apply the solution that Volker implemented in d42cfeb84faf154b46f2811b2059946b396fcc12 for 'Edit' menu - we call insertDefaultEditItems. Pick-to: 6.4 Fixes: QTBUG-104709 Change-Id: I8fee93c43e7cf974f94d173cd53adf8097b263e0 Reviewed-by: Tor Arne Vestbø Reviewed-by: Volker Hilsheimer --- src/plugins/platforms/cocoa/qcocoamenubar.mm | 28 +++++++++++--------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/plugins/platforms/cocoa/qcocoamenubar.mm b/src/plugins/platforms/cocoa/qcocoamenubar.mm index 60a25e732f..bd8a08f54a 100644 --- a/src/plugins/platforms/cocoa/qcocoamenubar.mm +++ b/src/plugins/platforms/cocoa/qcocoamenubar.mm @@ -166,18 +166,6 @@ void QCocoaMenuBar::syncMenu_helper(QPlatformMenu *menu, bool menubarUpdate) for (QCocoaMenuItem *item : cocoaMenu->items()) cocoaMenu->syncMenuItem_helper(item, menubarUpdate); - const QString captionNoAmpersand = QString::fromNSString(cocoaMenu->nsMenu().title) - .remove(u'&'); - if (captionNoAmpersand == QCoreApplication::translate("QCocoaMenu", "Edit")) { - // prevent recursion from QCocoaMenu::insertMenuItem - when the menu is visible - // it calls syncMenu again. QCocoaMenu::setVisible just sets the bool, which then - // gets evaluated in the code after this block. - const bool wasVisible = cocoaMenu->isVisible(); - cocoaMenu->setVisible(false); - insertDefaultEditItems(cocoaMenu); - cocoaMenu->setVisible(wasVisible); - } - BOOL shouldHide = YES; if (cocoaMenu->isVisible()) { // If the NSMenu has no visible items, or only separators, we should hide it @@ -314,6 +302,22 @@ void QCocoaMenuBar::updateMenuBarImmediately() [NSApp setMainMenu:mb->nsMenu()]; insertWindowMenu(); [loader qtTranslateApplicationMenu]; + + for (auto menu : std::as_const(mb->m_menus)) { + if (!menu) + continue; + + const QString captionNoAmpersand = QString::fromNSString(menu->nsMenu().title).remove(u'&'); + if (captionNoAmpersand != QCoreApplication::translate("QCocoaMenu", "Edit")) + continue; + + NSMenuItem *item = mb->nativeItemForMenu(menu); + auto *nsMenu = item.submenu; + if ([nsMenu indexOfItemWithTarget:NSApp andAction:@selector(startDictation:)] == -1) { + // AppKit was not able to recognize the special role of this menu item. + mb->insertDefaultEditItems(menu); + } + } } void QCocoaMenuBar::insertWindowMenu()