13@implementation QNSView (Menus)
27- (id)supplementalTargetForAction:(SEL)action sender:(id)sender
29 qCDebug(lcQpaMenus) <<
"Resolving action target for" << action <<
"from" << sender <<
"via" << self;
31 if (qt_objc_cast<QCocoaNSMenuItem *>(sender)) {
34 if ([self.menuHelper respondsToSelector:action])
35 return self.menuHelper;
37 qCDebug(lcQpaMenus) <<
"Ignoring action for menu item we didn't create";
40 return [super supplementalTargetForAction:action sender:sender];
43#if QT_APPLE_SDK_EQUAL_OR_ABOVE(MACOS(15
))
44- (
void)showContextMenuForSelection:(id)sender
48 [self convertFromScreen:[NSEvent mouseLocation]
49 toWindowPoint:&windowPoint andScreenPoint:&screenPoint];
51 auto *keyMapper = QCocoaIntegration::instance()->keyMapper();
52 auto keyboardModifiers = keyMapper->queryKeyboardModifiers();
63 const auto *device = QInputDevice::primaryKeyboard();
65 qCDebug(lcQpaMenus) <<
"Initiating context menu at"
66 << windowPoint.toPoint() <<
"in" << m_platformWindow
67 <<
"with" << keyboardModifiers;
69 bool accepted = QWindowSystemInterface::handleContextMenuEvent<
70 QWindowSystemInterface::SynchronousDelivery>(
71 m_platformWindow->window(), device,
72 windowPoint.toPoint(), screenPoint.toPoint(),
75 qCDebug(lcQpaMenus) <<
"Context menu event accepted =" << accepted;
80 [[self nextResponder] tryToPerform:_cmd with:sender];
86@interface QNSViewMenuHelper ()
87@property (assign) QNSView* view;
90@implementation QNSViewMenuHelper
92- (instancetype)initWithView:(QNSView *)theView
94 if ((self = [super init]))
100- (BOOL)validateMenuItem:(NSMenuItem*)item
102 qCDebug(lcQpaMenus) <<
"Validating" << item <<
"for" << self.view;
104 auto *nativeItem = qt_objc_cast<QCocoaNSMenuItem *>(item);
108 auto *platformItem = nativeItem.platformMenuItem;
113 if (platformItem->menu())
118 if (QGuiApplication::modalWindow() && QGuiApplication::modalWindow()->isActive()) {
119 QCocoaMenuBar *menubar =
nullptr;
120 QCocoaWindow *menuWindow =
nullptr;
122 QObject *menuParent = platformItem->menuParent();
123 while (menuParent && !(menubar = qobject_cast<QCocoaMenuBar *>(menuParent))) {
124 menuWindow = qobject_cast<QCocoaWindow *>(menuParent);
125 auto *menuObject =
dynamic_cast<QCocoaMenuObject *>(menuParent);
126 menuParent = menuObject ? menuObject->menuParent() :
nullptr;
129 if ((!menuWindow || menuWindow->window() != QGuiApplication::modalWindow())
130 && (!menubar || menubar->cocoaWindow() != self.view.platformWindow))
134 return platformItem->isEnabled();
137- (BOOL)respondsToSelector:(SEL)selector
141 if (selector == @selector(cut:)
142 || selector == @selector(copy:)
143 || selector == @selector(paste:)
144 || selector == @selector(selectAll:)) {
146 return NSApp.keyWindow == self.view.window
147 && self.view.window.firstResponder == self.view;
150 if (selector == @selector(qt_itemFired:))
153 return [super respondsToSelector:selector];
156- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
160 if (![self respondsToSelector:selector])
163 auto *appDelegate = [QCocoaApplicationDelegate sharedDelegate];
164 return [appDelegate methodSignatureForSelector:@selector(qt_itemFired:)];
167- (
void)forwardInvocation:(NSInvocation *)invocation
170 [invocation getArgument:&sender atIndex:2];
171 qCDebug(lcQpaMenus) <<
"Forwarding" << invocation.selector <<
"from" << sender;
172 Q_ASSERT(qt_objc_cast<QCocoaNSMenuItem *>(sender));
173 invocation.selector = @selector(qt_itemFired:);
174 [invocation invokeWithTarget:[QCocoaApplicationDelegate sharedDelegate]];