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 d42cfeb84f
for 'Edit' menu - we call insertDefaultEditItems.

Pick-to: 6.4
Fixes: QTBUG-104709
Change-Id: I8fee93c43e7cf974f94d173cd53adf8097b263e0
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Timur Pocheptsov 2022-10-20 15:47:22 +02:00
parent a7bd656cf4
commit 6c2387571a

View File

@ -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()