From e8a41ed8668437eb6ad406ddabf4141fd66a85d2 Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Wed, 2 Nov 2016 17:37:43 -0700 Subject: QCocoaMenu: Force NSMenuValidation when syncing items When a menu item's enabled state changes after -[QCocoaMenuDelegate menuWillOpen:] is invoked, i.e., during or after QMenu::aboutToShow() is emitted, that state change may not be taken into account. This is because the automatic menu validation, upon which Qt relies, is not made aware of any such change. By calling -[NSMenu update] when syncing the QPA menu item, we induce Cocoa to invoke -[QCocoaMenuDelegate validateMenuItem:] and ensure that previously synced items, whose state may have changed, will be properly updated. This, however, has a small side effect, namely that menu-holding items will also go through the automatic menu enabling path and may appear disabled since, until now, they were not properly configured. In order to solve this, we set the action on those items as well, and make sure that both of QCocoaMenuDelegate's relevant methods, validateMenuItem: and itemFired:, properly process menu-holding items. Menurama manual test updated accordingly. Change-Id: I62f955538b8be09b8494ea0ce87fca7910148d38 Task-number: QTBUG-56850 Reviewed-by: Timur Pocheptsov --- tests/manual/cocoa/menurama/mainwindow.cpp | 8 +- tests/manual/cocoa/menurama/mainwindow.ui | 98 ++++++++++++---------- .../manual/cocoa/menurama/menuramaapplication.cpp | 16 ++-- tests/manual/cocoa/menurama/menuramaapplication.h | 1 + 4 files changed, 72 insertions(+), 51 deletions(-) (limited to 'tests/manual') diff --git a/tests/manual/cocoa/menurama/mainwindow.cpp b/tests/manual/cocoa/menurama/mainwindow.cpp index db8fdafc21..f7762f57f5 100644 --- a/tests/manual/cocoa/menurama/mainwindow.cpp +++ b/tests/manual/cocoa/menurama/mainwindow.cpp @@ -56,7 +56,13 @@ MainWindow::MainWindow(QWidget *parent) : }); connect(ui->menuDynamic_Stuff, &QMenu::aboutToShow, [=] { - menuApp->addDynMenu(QLatin1String("Added After aboutToShow()"), ui->menuDynamic_Stuff); + menuApp->addDynMenu(QLatin1String("Menu Added After aboutToShow()"), ui->menuDynamic_Stuff); + + const QLatin1String itemTitle = QLatin1String("Disabled Item Added After aboutToShow()"); + if (QAction *a = menuApp->findAction(itemTitle, ui->menuDynamic_Stuff)) + ui->menuDynamic_Stuff->removeAction(a); + QAction *a = ui->menuDynamic_Stuff->addAction(itemTitle); + a->setEnabled(false); }); connect(ui->pushButton, &QPushButton::clicked, [=] { diff --git a/tests/manual/cocoa/menurama/mainwindow.ui b/tests/manual/cocoa/menurama/mainwindow.ui index f73b41b861..d3caa6c608 100644 --- a/tests/manual/cocoa/menurama/mainwindow.ui +++ b/tests/manual/cocoa/menurama/mainwindow.ui @@ -6,63 +6,71 @@ 0 0 - 566 - 300 + 429 + 251 MainWindow - - - - 10 - 40 - 151 - 20 - - - - Enable "Stuff" Menu - - - true - - - - - - 10 - 10 - 321 - 16 - - - - The "Help" menu should NOT be visible. - - - - - - 10 - 80 - 211 - 32 - - - - Populate Dynamic Submenu - - + + + + + 24 + + + + + The "Help" menu should NOT be visible. + +Click on "Dynamic Stuff" then move left and right to other menus. Disabled items should remain that way. + + + false + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + true + + + + + + + Enable "Stuff" Menu + + + true + + + + + + + + 0 + 0 + + + + Populate Dynamic Submenu + + + + + + 0 0 - 566 + 429 22 diff --git a/tests/manual/cocoa/menurama/menuramaapplication.cpp b/tests/manual/cocoa/menurama/menuramaapplication.cpp index 534d5fa371..13e457d7dd 100644 --- a/tests/manual/cocoa/menurama/menuramaapplication.cpp +++ b/tests/manual/cocoa/menurama/menuramaapplication.cpp @@ -69,13 +69,19 @@ void MenuramaApplication::populateMenu(QMenu *menu, bool clear) void MenuramaApplication::addDynMenu(QLatin1String title, QMenu *parentMenu) { - foreach (QAction *a, parentMenu->actions()) - if (a->text() == title) { - parentMenu->removeAction(a); - break; - } + if (QAction *a = findAction(title, parentMenu)) + parentMenu->removeAction(a); QMenu *subMenu = new QMenu(title, parentMenu); populateMenu(subMenu, false /*clear*/); parentMenu->addMenu(subMenu); } + +QAction *MenuramaApplication::findAction(QLatin1String title, QMenu *parentMenu) +{ + foreach (QAction *a, parentMenu->actions()) + if (a->text() == title) + return a; + + return Q_NULLPTR; +} diff --git a/tests/manual/cocoa/menurama/menuramaapplication.h b/tests/manual/cocoa/menurama/menuramaapplication.h index 07c8da27a1..b0670cc53b 100644 --- a/tests/manual/cocoa/menurama/menuramaapplication.h +++ b/tests/manual/cocoa/menurama/menuramaapplication.h @@ -50,6 +50,7 @@ class MenuramaApplication : public QApplication public: MenuramaApplication(int argc, char **argv); void addDynMenu(QLatin1String title, QMenu *parentMenu); + QAction *findAction(QLatin1String title, QMenu *parentMenu); public slots: void populateMenu(QMenu *menu, bool clear); -- cgit v1.2.3