summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/cocoa/qcocoamenubar.mm
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/platforms/cocoa/qcocoamenubar.mm')
-rw-r--r--src/plugins/platforms/cocoa/qcocoamenubar.mm123
1 files changed, 63 insertions, 60 deletions
diff --git a/src/plugins/platforms/cocoa/qcocoamenubar.mm b/src/plugins/platforms/cocoa/qcocoamenubar.mm
index c0f0f9e702..2493d90724 100644
--- a/src/plugins/platforms/cocoa/qcocoamenubar.mm
+++ b/src/plugins/platforms/cocoa/qcocoamenubar.mm
@@ -36,7 +36,7 @@ QCocoaMenuBar::QCocoaMenuBar()
QCocoaMenuBar::~QCocoaMenuBar()
{
- qCDebug(lcQpaMenus) << "Destructing" << this << "with" << m_nativeMenu;;
+ qCDebug(lcQpaMenus) << "Destructing" << this << "with" << m_nativeMenu;
for (auto menu : std::as_const(m_menus)) {
if (!menu)
continue;
@@ -162,18 +162,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
@@ -186,10 +174,44 @@ void QCocoaMenuBar::syncMenu_helper(QPlatformMenu *menu, bool menubarUpdate)
}
}
- if (NSMenuItem *attachedItem = cocoaMenu->attachedItem()) {
- // Non-nil attached item means the item's submenu is set
- attachedItem.title = cocoaMenu->nsMenu().title;
- attachedItem.hidden = shouldHide;
+ if (NSMenuItem *menuItem = cocoaMenu->attachedItem()) {
+ // Non-nil menu item means the item's sub menu is set
+
+ NSString *menuTitle = cocoaMenu->nsMenu().title;
+
+ // The NSMenu's title is what's visible to the user, and AppKit uses this
+ // for some of its heuristics of when to add special items to the menus,
+ // such as 'Enter Full Screen' in the View menu, the search bare in the
+ // Help menu, and the "Send App feedback to Apple" in the Help menu.
+ // This relies on the title matching AppKit's localized value from the
+ // MenuCommands table, which in turn depends on the preferredLocalizations
+ // of the AppKit bundle. We don't do any automatic translation of menu
+ // titles visible to the user, so this relies on the application developer
+ // having chosen translated titles that match AppKit's, and that the Qt
+ // preferred UI languages match AppKit's preferredLocalizations.
+
+ // In the case of the Edit menu, AppKit uses the NSMenuItem's title
+ // for its heuristics of when to add the dictation and emoji entries,
+ // and this title is not visible to the user. But like above, the
+ // heuristics are based on the localized title of the menu, so we need
+ // to ensure the title matches AppKit's localization.
+
+ // Unfortunately, the title we have at this point may have gone through
+ // Qt's i18n machinery already, via e.g. tr("Edit") in the application,
+ // in which case we don't know the context of the translation, and can't
+ // do a reverse lookup to go back to the untranslated title to pass to
+ // AppKit. As a workaround we translate the title via a our context,
+ // and document that the user needs to ensure their application matches
+ // this translation.
+ if ([menuTitle isEqual:@"Edit"] || [menuTitle isEqual:tr("Edit").toNSString()]) {
+ menuItem.title = qt_mac_AppKitString(@"InputManager", @"Edit");
+ } else {
+ // The Edit menu is the only case we know of so far, but to be on
+ // the safe side we always sync the menu title.
+ menuItem.title = menuTitle;
+ }
+
+ menuItem.hidden = shouldHide;
}
}
@@ -304,7 +326,21 @@ void QCocoaMenuBar::updateMenuBarImmediately()
}
[mergedItems release];
- [NSApp setMainMenu:mb->nsMenu()];
+
+ NSMenu *newMainMenu = mb->nsMenu();
+ if (NSApp.mainMenu == newMainMenu) {
+ // NSApplication triggers _customizeMainMenu when the menu
+ // changes, which takes care of adding text input items to
+ // the edit menu e.g., but this doesn't happen if the menu
+ // is the same. In our case we might be re-using an existing
+ // menu, but the menu might have new sub menus that need to
+ // be customized. To ensure NSApplication does the right
+ // thing we reset the main menu first.
+ qCDebug(lcQpaMenus) << "Clearing main menu temporarily";
+ NSApp.mainMenu = nil;
+ }
+ NSApp.mainMenu = newMainMenu;
+
insertWindowMenu();
[loader qtTranslateApplicationMenu];
}
@@ -325,6 +361,15 @@ void QCocoaMenuBar::insertWindowMenu()
winMenuItem.hidden = YES;
winMenuItem.submenu = [[[NSMenu alloc] initWithTitle:@"QtWindowMenu"] autorelease];
+
+ // AppKit has a bug in [NSApplication setWindowsMenu:] where it will resolve
+ // the last item of the window menu's itemArray, but not account for the array
+ // being empty, resulting in a lookup of itemAtIndex:-1. To work around this,
+ // we insert a hidden dummy item into the menu. See FB13369198.
+ auto *dummyItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
+ dummyItem.hidden = YES;
+ [winMenuItem.submenu addItem:[dummyItem autorelease]];
+
[mainMenu insertItem:winMenuItem atIndex:mainMenu.itemArray.count];
app.windowsMenu = winMenuItem.submenu;
@@ -416,48 +461,6 @@ QCocoaWindow *QCocoaMenuBar::cocoaWindow() const
return m_window.data();
}
-void QCocoaMenuBar::insertDefaultEditItems(QCocoaMenu *menu)
-{
- if (menu->items().isEmpty())
- return;
-
- NSMenu *nsEditMenu = menu->nsMenu();
- if ([nsEditMenu itemAtIndex:nsEditMenu.numberOfItems - 1].action
- == @selector(orderFrontCharacterPalette:)) {
- for (auto defaultEditMenuItem : std::as_const(m_defaultEditMenuItems)) {
- if (menu->items().contains(defaultEditMenuItem))
- menu->removeMenuItem(defaultEditMenuItem);
- }
- qDeleteAll(m_defaultEditMenuItems);
- m_defaultEditMenuItems.clear();
- } else {
- if (m_defaultEditMenuItems.isEmpty()) {
- QCocoaMenuItem *separator = new QCocoaMenuItem;
- separator->setIsSeparator(true);
-
- QCocoaMenuItem *dictationItem = new QCocoaMenuItem;
- dictationItem->setText(QCoreApplication::translate("QCocoaMenuItem", "Start Dictation..."));
- QObject::connect(dictationItem, &QPlatformMenuItem::activated, this, []{
- [NSApplication.sharedApplication performSelector:@selector(startDictation:)];
- });
-
- QCocoaMenuItem *emojiItem = new QCocoaMenuItem;
- emojiItem->setText(QCoreApplication::translate("QCocoaMenuItem", "Emoji && Symbols"));
- emojiItem->setShortcut(QKeyCombination(Qt::MetaModifier|Qt::ControlModifier, Qt::Key_Space));
- QObject::connect(emojiItem, &QPlatformMenuItem::activated, this, []{
- [NSApplication.sharedApplication orderFrontCharacterPalette:nil];
- });
-
- m_defaultEditMenuItems << separator << dictationItem << emojiItem;
- }
- for (auto defaultEditMenuItem : std::as_const(m_defaultEditMenuItems)) {
- if (menu->items().contains(defaultEditMenuItem))
- menu->removeMenuItem(defaultEditMenuItem);
- menu->insertMenuItem(defaultEditMenuItem, nullptr);
- }
- }
-}
-
QT_END_NAMESPACE
#include "moc_qcocoamenubar.cpp"