Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qnsview_menus.mm
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5// This file is included from qnsview.mm, and only used to organize the code
6
8#include "qcocoansmenu.h"
10#include "qcocoamenu.h"
11#include "qcocoamenubar.h"
12
13@implementation QNSView (Menus)
14
15// Qt does not (yet) have a mechanism for propagating generic actions,
16// so we can only support actions that originate from a QCocoaNSMenuItem,
17// where we can forward the action by emitting QPlatformMenuItem::activated().
18// But waiting for forwardInvocation to check that the sender is a
19// QCocoaNSMenuItem is too late, as AppKit has at that point chosen
20// our view as the target for the action, and if we can't handle it
21// the action will not propagate up the responder chain as it should.
22// Instead, we hook in early in the process of determining the target
23// via the supplementalTargetForAction API, and if we can support the
24// action we forward it to a helper. The helper must be tied to the
25// view, as the menu validation logic depends on the view's state.
26
27- (id)supplementalTargetForAction:(SEL)action sender:(id)sender
28{
29 qCDebug(lcQpaMenus) << "Resolving action target for" << action << "from" << sender << "via" << self;
30
31 if (qt_objc_cast<QCocoaNSMenuItem *>(sender)) {
32 // The supplemental target must support the selector, but we
33 // determine so dynamically, so check here before continuing.
34 if ([self.menuHelper respondsToSelector:action])
35 return self.menuHelper;
36 } else {
37 qCDebug(lcQpaMenus) << "Ignoring action for menu item we didn't create";
38 }
39
40 return [super supplementalTargetForAction:action sender:sender];
41}
42
43#if QT_APPLE_SDK_EQUAL_OR_ABOVE(MACOS(15))
44- (void)showContextMenuForSelection:(id)sender
45{
46 QPointF windowPoint;
47 QPointF screenPoint;
48 [self convertFromScreen:[NSEvent mouseLocation]
49 toWindowPoint:&windowPoint andScreenPoint:&screenPoint];
50
51 auto *keyMapper = QCocoaIntegration::instance()->keyMapper();
52 auto keyboardModifiers = keyMapper->queryKeyboardModifiers();
53
54 // We get here via contextMenuKeyDown for the system context menu
55 // shortcut, or via plain keyDown, for user-defined Cocoa Text key
56 // bindings (~/Library/KeyBindings/DefaultKeyBinding.dict), which
57 // both originate from the keyboard. The docs for the callback also
58 // mention the accessibility 'ShowMenu' action being a trigger, but
59 // our QNSView is not itself accessible, and QMacAccessibilityElement
60 // handles the NSAccessibilityShowMenuAction directly via
61 // accessibilityPerformAction, so we only need to handle the
62 // keyboard case here.
63 const auto *device = QInputDevice::primaryKeyboard();
64
65 qCDebug(lcQpaMenus) << "Initiating context menu at"
66 << windowPoint.toPoint() << "in" << m_platformWindow
67 << "with" << keyboardModifiers;
68
69 bool accepted = QWindowSystemInterface::handleContextMenuEvent<
70 QWindowSystemInterface::SynchronousDelivery>(
71 m_platformWindow->window(), device,
72 windowPoint.toPoint(), screenPoint.toPoint(),
73 keyboardModifiers);
74
75 qCDebug(lcQpaMenus) << "Context menu event accepted =" << accepted;
76
77 // If the view does not support a context menu we should pass
78 // the request up the responder chain.
79 if (!accepted)
80 [[self nextResponder] tryToPerform:_cmd with:sender];
81}
82#endif
83
84@end
85
86@interface QNSViewMenuHelper ()
87@property (assign) QNSView* view;
88@end
89
90@implementation QNSViewMenuHelper
91
92- (instancetype)initWithView:(QNSView *)theView
93{
94 if ((self = [super init]))
95 self.view = theView;
96
97 return self;
98}
99
100- (BOOL)validateMenuItem:(NSMenuItem*)item
101{
102 qCDebug(lcQpaMenus) << "Validating" << item << "for" << self.view;
103
104 auto *nativeItem = qt_objc_cast<QCocoaNSMenuItem *>(item);
105 if (!nativeItem)
106 return item.enabled; // FIXME Test with with Qt as plugin or embedded QWindow.
107
108 auto *platformItem = nativeItem.platformMenuItem;
109 if (!platformItem)
110 return NO;
111
112 // Menu-holding items are always enabled, as it's conventional in Cocoa
113 if (platformItem->menu())
114 return YES;
115
116 // Check if a modal dialog is active. If so, enable only menu
117 // items explicitly belonging to this window's own menu bar, or to the window.
118 if (QGuiApplication::modalWindow() && QGuiApplication::modalWindow()->isActive()) {
119 QCocoaMenuBar *menubar = nullptr;
120 QCocoaWindow *menuWindow = nullptr;
121
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;
127 }
128
129 if ((!menuWindow || menuWindow->window() != QGuiApplication::modalWindow())
130 && (!menubar || menubar->cocoaWindow() != self.view.platformWindow))
131 return NO;
132 }
133
134 return platformItem->isEnabled();
135}
136
137- (BOOL)respondsToSelector:(SEL)selector
138{
139 // See QCocoaMenuItem::resolveTargetAction()
140
141 if (selector == @selector(cut:)
142 || selector == @selector(copy:)
143 || selector == @selector(paste:)
144 || selector == @selector(selectAll:)) {
145 // Not exactly true. Both copy: and selectAll: can work on non key views.
146 return NSApp.keyWindow == self.view.window
147 && self.view.window.firstResponder == self.view;
148 }
149
150 if (selector == @selector(qt_itemFired:))
151 return YES;
152
153 return [super respondsToSelector:selector];
154}
155
156- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
157{
158 // Double check, in case something has cached that we respond
159 // to the selector, but the result has changed since then.
160 if (![self respondsToSelector:selector])
161 return nil;
162
163 auto *appDelegate = [QCocoaApplicationDelegate sharedDelegate];
164 return [appDelegate methodSignatureForSelector:@selector(qt_itemFired:)];
165}
166
167- (void)forwardInvocation:(NSInvocation *)invocation
168{
169 NSObject *sender;
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]];
175}
176
177@end