From 306071e50eac8290d234caab90985ddf705a5fc6 Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Thu, 10 Aug 2017 10:30:56 +0700 Subject: [PATCH] QCocoaMenu: Sync menubar menu when adding items MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Empty menus on a menubar are hidden by default. If the menu gets added to the menubar before it contains any item, we need to get the menubar to sync the menu, which will update its native menu item hidden property. Menurama manual test's 'Add Many Items' button should now work. Change-Id: I8ce1df21031c171789318fdf28ae495819458d71 Task-number: QTBUG-62260 Reviewed-by: Tor Arne Vestbø --- src/plugins/platforms/cocoa/qcocoamenu.mm | 7 +++++++ src/plugins/platforms/cocoa/qcocoamenubar.mm | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/plugins/platforms/cocoa/qcocoamenu.mm b/src/plugins/platforms/cocoa/qcocoamenu.mm index e5681c0894..d7f0efe86c 100644 --- a/src/plugins/platforms/cocoa/qcocoamenu.mm +++ b/src/plugins/platforms/cocoa/qcocoamenu.mm @@ -328,6 +328,13 @@ void QCocoaMenu::insertMenuItem(QPlatformMenuItem *menuItem, QPlatformMenuItem * } insertNative(cocoaItem, beforeItem); + + // Empty menus on a menubar are hidden by default. If the menu gets + // added to the menubar before it contains any item, we need to sync. + if (isVisible() && attachedItem().hidden) { + if (auto *mb = qobject_cast(menuParent())) + mb->syncMenu(this); + } } void QCocoaMenu::insertNative(QCocoaMenuItem *item, QCocoaMenuItem *beforeItem) diff --git a/src/plugins/platforms/cocoa/qcocoamenubar.mm b/src/plugins/platforms/cocoa/qcocoamenubar.mm index 4cdf004dd1..70fcb40774 100644 --- a/src/plugins/platforms/cocoa/qcocoamenubar.mm +++ b/src/plugins/platforms/cocoa/qcocoamenubar.mm @@ -370,7 +370,7 @@ void QCocoaMenuBar::updateMenuBarImmediately() QCocoaMenuLoader *loader = [QCocoaMenuLoader sharedMenuLoader]; [loader ensureAppMenuInMenu:mb->nsMenu()]; - NSMutableSet *mergedItems = [[NSMutableSet setWithCapacity:0] retain]; + NSMutableSet *mergedItems = [[NSMutableSet setWithCapacity:mb->merged().count()] retain]; foreach (QCocoaMenuItem *m, mb->merged()) { [mergedItems addObject:m->nsItem()]; m->syncMerged();