summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/cocoa/qcocoamenu.mm
diff options
context:
space:
mode:
authorGabriel de Dietrich <gabriel.dedietrich@qt.io>2018-03-28 18:07:30 -0700
committerGabriel de Dietrich <gabriel.dedietrich@qt.io>2018-04-19 22:19:46 +0000
commitb7be91b5f28cd42e6f9898f480fa12a0cd359a84 (patch)
tree78a8d28dd227d12afbd95677e8a4932ed1ca319a /src/plugins/platforms/cocoa/qcocoamenu.mm
parent0b9a301e89c6473091a9b80552d6e4058d35bbe6 (diff)
Cocoa Menus: Use the responder chain for menu items target/action
We start by setting the menu item target to nil. Then, -[qt_itemFired:] action is now in QNSView, which itself is naturally inserted in the responder chain. This removes the need to track and change the menu item's target/action when we're displaying a native dialog. Part of this is possible because we now derive our own QCocoaNSMenuItem class from NSMenuItem. We use -[respondsToSelector:] to decide whether the QNSView in the responder chain should respond to cut:, copy:, etc. And we only return YES when the view is first responder. The invocation to these action is forwarded to the same views' -[qt_itemFired:]. Message forwarding is done via forwardInvocation:, but experiments have shown that it can be done by the sole means of respondsToSelector: and direct invocation from cut:, copy:, etc. See the usage of the macro QT_COCOA_DYNAMIC_MENU_ITEM_ACTION. Menu validation also happens in QNSView and looks for modal windows. Therefore, -[worksWhenModal] is no longer necessary. Also, since the target is no longer set, the logic as documented in NSMenuItem.target won't work anymore. Most items from QCocoaMenuLoader also become QCocoaNSMenuItem and get the same target/action, which removes a bit of duplicated (and outdated) code. A particular case is the Quit item, which gets the terminate: action set until an actual menu item is added. Tested with texedit and standard dialogs examples together with menus, menurama and bigmenucreator manual tests. We also renamed some functions and variables to reflect common naming practices. Change-Id: I9b51d3be3467a666d8c3dcf8585edbc821e0282e Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Diffstat (limited to 'src/plugins/platforms/cocoa/qcocoamenu.mm')
-rw-r--r--src/plugins/platforms/cocoa/qcocoamenu.mm15
1 files changed, 4 insertions, 11 deletions
diff --git a/src/plugins/platforms/cocoa/qcocoamenu.mm b/src/plugins/platforms/cocoa/qcocoamenu.mm
index 49b3e76606..75d5ea2dd8 100644
--- a/src/plugins/platforms/cocoa/qcocoamenu.mm
+++ b/src/plugins/platforms/cocoa/qcocoamenu.mm
@@ -62,7 +62,7 @@ QCocoaMenu::QCocoaMenu() :
{
QMacAutoReleasePool pool;
- m_nativeMenu = [[QCocoaNSMenu alloc] initWithQPAMenu:this];
+ m_nativeMenu = [[QCocoaNSMenu alloc] initWithPlatformMenu:this];
}
QCocoaMenu::~QCocoaMenu()
@@ -132,7 +132,7 @@ void QCocoaMenu::insertMenuItem(QPlatformMenuItem *menuItem, QPlatformMenuItem *
void QCocoaMenu::insertNative(QCocoaMenuItem *item, QCocoaMenuItem *beforeItem)
{
- setItemTargetAction(item);
+ item->resolveTargetAction();
NSMenuItem *nativeItem = item->nsItem();
// Someone's adding new items after aboutToShow() was emitted
if (isOpen() && nativeItem && item->menu())
@@ -423,7 +423,7 @@ QPlatformMenuItem *QCocoaMenu::menuItemAt(int position) const
if (0 <= position && position < m_menuItems.count())
return m_menuItems.at(position);
- return 0;
+ return nullptr;
}
QPlatformMenuItem *QCocoaMenu::menuItemForTag(quintptr tag) const
@@ -433,7 +433,7 @@ QPlatformMenuItem *QCocoaMenu::menuItemForTag(quintptr tag) const
return item;
}
- return 0;
+ return nullptr;
}
QList<QCocoaMenuItem *> QCocoaMenu::items() const
@@ -493,11 +493,4 @@ NSMenuItem *QCocoaMenu::attachedItem() const
return m_attachedItem;
}
-void QCocoaMenu::setItemTargetAction(QCocoaMenuItem *item) const
-{
- auto *nsItem = item->nsItem();
- nsItem.target = m_nativeMenu;
- nsItem.action = @selector(qt_itemFired:);
-}
-
QT_END_NAMESPACE