2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2017 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2016-08-11 00:24:04 +00:00
|
|
|
|
|
|
|
#include "menuramaapplication.h"
|
|
|
|
|
2017-11-14 16:23:49 +00:00
|
|
|
MenuramaApplication::MenuramaApplication(int &argc, char **argv)
|
2016-08-11 00:24:04 +00:00
|
|
|
: QApplication (argc, argv)
|
|
|
|
{
|
|
|
|
#if 0
|
|
|
|
QMenuBar *mb = new QMenuBar();
|
|
|
|
QMenu *menu = mb->addMenu("App Dynamic");
|
|
|
|
QMenu *dynMenu = menu->addMenu("After aboutToShow()");
|
|
|
|
connect(dynMenu, &QMenu::aboutToShow, [=] {
|
|
|
|
qDebug() << "aboutToShow(), populating" << dynMenu;
|
|
|
|
menuApp->populateMenu(dynMenu, true /*clear*/);
|
|
|
|
});
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void MenuramaApplication::populateMenu(QMenu *menu, bool clear)
|
|
|
|
{
|
|
|
|
if (clear)
|
|
|
|
menu->clear();
|
|
|
|
|
|
|
|
static const char *sym[] = { "Foo", "Bar", "Baz", "Huux" };
|
|
|
|
static int id = 0;
|
|
|
|
for (unsigned i = 0; i < sizeof(sym) / sizeof(sym[0]); i++)
|
|
|
|
menu->addAction(QStringLiteral("%1 — %2 %3 ")
|
|
|
|
.arg(menu->title()).arg(sym[i]).arg(id));
|
|
|
|
++id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MenuramaApplication::addDynMenu(QLatin1String title, QMenu *parentMenu)
|
|
|
|
{
|
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 <timur.pocheptsov@qt.io>
2016-11-03 00:37:43 +00:00
|
|
|
if (QAction *a = findAction(title, parentMenu))
|
|
|
|
parentMenu->removeAction(a);
|
2016-08-11 00:24:04 +00:00
|
|
|
|
|
|
|
QMenu *subMenu = new QMenu(title, parentMenu);
|
|
|
|
populateMenu(subMenu, false /*clear*/);
|
|
|
|
parentMenu->addMenu(subMenu);
|
|
|
|
}
|
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 <timur.pocheptsov@qt.io>
2016-11-03 00:37:43 +00:00
|
|
|
|
|
|
|
QAction *MenuramaApplication::findAction(QLatin1String title, QMenu *parentMenu)
|
|
|
|
{
|
|
|
|
foreach (QAction *a, parentMenu->actions())
|
|
|
|
if (a->text() == title)
|
|
|
|
return a;
|
|
|
|
|
2017-09-18 09:49:52 +00:00
|
|
|
return nullptr;
|
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 <timur.pocheptsov@qt.io>
2016-11-03 00:37:43 +00:00
|
|
|
}
|