QCocoaMenuBar: avoid duplication of 'special' entries in the 'Edit' menu

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.

Fixes: QTBUG-104709
Change-Id: I8fee93c43e7cf974f94d173cd53adf8097b263e0
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 6c2387571a6ccace9edb600ce6798f75affd961d)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Timur Pocheptsov 2022-10-20 15:47:22 +02:00 committed by Qt Cherry-pick Bot
parent d1d8a21c53
commit c827b51195

View File

@ -165,18 +165,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
@ -313,6 +301,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()