Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
4// This file is included from qnsview.mm, and only used to organize the code
5
7#include "qcocoansmenu.h"
8#include "qcocoamenuitem.h"
9#include "qcocoamenu.h"
10#include "qcocoamenubar.h"
11
12@implementation QNSView (Menus)
13
14// Qt does not (yet) have a mechanism for propagating generic actions,
15// so we can only support actions that originate from a QCocoaNSMenuItem,
16// where we can forward the action by emitting QPlatformMenuItem::activated().
17// But waiting for forwardInvocation to check that the sender is a
18// QCocoaNSMenuItem is too late, as AppKit has at that point chosen
19// our view as the target for the action, and if we can't handle it
20// the action will not propagate up the responder chain as it should.
21// Instead, we hook in early in the process of determining the target
22// via the supplementalTargetForAction API, and if we can support the
23// action we forward it to a helper. The helper must be tied to the
24// view, as the menu validation logic depends on the view's state.
25
26- (id)supplementalTargetForAction:(SEL)action sender:(id)sender
27{
28 qCDebug(lcQpaMenus) << "Resolving action target for" << action << "from" << sender << "via" << self;
29
30 if (qt_objc_cast<QCocoaNSMenuItem *>(sender)) {
31 // The supplemental target must support the selector, but we
32 // determine so dynamically, so check here before continuing.
33 if ([self.menuHelper respondsToSelector:action])
34 return self.menuHelper;
35 } else {
36 qCDebug(lcQpaMenus) << "Ignoring action for menu item we didn't create";
37 }
38
39 return [super supplementalTargetForAction:action sender:sender];
40}
41
42@end
43
44@interface QNSViewMenuHelper ()
45@property (assign) QNSView* view;
46@end
47
48@implementation QNSViewMenuHelper
49
50- (instancetype)initWithView:(QNSView *)theView
51{
52 if ((self = [super init]))
53 self.view = theView;
54
55 return self;
56}
57
58- (BOOL)validateMenuItem:(NSMenuItem*)item
59{
60 qCDebug(lcQpaMenus) << "Validating" << item << "for" << self.view;
61
62 auto *nativeItem = qt_objc_cast<QCocoaNSMenuItem *>(item);
63 if (!nativeItem)
64 return item.enabled; // FIXME Test with with Qt as plugin or embedded QWindow.
65
66 auto *platformItem = nativeItem.platformMenuItem;
67 if (!platformItem)
68 return NO;
69
70 // Menu-holding items are always enabled, as it's conventional in Cocoa
71 if (platformItem->menu())
72 return YES;
73
74 // Check if a modal dialog is active. If so, enable only menu
75 // items explicitly belonging to this window's own menu bar, or to the window.
77 QCocoaMenuBar *menubar = nullptr;
78 QCocoaWindow *menuWindow = nullptr;
79
80 QObject *menuParent = platformItem->menuParent();
81 while (menuParent && !(menubar = qobject_cast<QCocoaMenuBar *>(menuParent))) {
82 menuWindow = qobject_cast<QCocoaWindow *>(menuParent);
83 auto *menuObject = dynamic_cast<QCocoaMenuObject *>(menuParent);
84 menuParent = menuObject ? menuObject->menuParent() : nullptr;
85 }
86
87 if ((!menuWindow || menuWindow->window() != QGuiApplication::modalWindow())
88 && (!menubar || menubar->cocoaWindow() != self.view.platformWindow))
89 return NO;
90 }
91
92 return platformItem->isEnabled();
93}
94
95- (BOOL)respondsToSelector:(SEL)selector
96{
97 // See QCocoaMenuItem::resolveTargetAction()
98
99 if (selector == @selector(cut:)
100 || selector == @selector(copy:)
101 || selector == @selector(paste:)
102 || selector == @selector(selectAll:)) {
103 // Not exactly true. Both copy: and selectAll: can work on non key views.
104 return NSApp.keyWindow == self.view.window
105 && self.view.window.firstResponder == self.view;
106 }
107
108 if (selector == @selector(qt_itemFired:))
109 return YES;
110
111 return [super respondsToSelector:selector];
112}
113
114- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
115{
116 // Double check, in case something has cached that we respond
117 // to the selector, but the result has changed since then.
118 if (![self respondsToSelector:selector])
119 return nil;
120
121 auto *appDelegate = [QCocoaApplicationDelegate sharedDelegate];
122 return [appDelegate methodSignatureForSelector:@selector(qt_itemFired:)];
123}
124
125- (void)forwardInvocation:(NSInvocation *)invocation
126{
127 NSObject *sender;
128 [invocation getArgument:&sender atIndex:2];
129 qCDebug(lcQpaMenus) << "Forwarding" << invocation.selector << "from" << sender;
130 Q_ASSERT(qt_objc_cast<QCocoaNSMenuItem *>(sender));
131 invocation.selector = @selector(qt_itemFired:);
132 [invocation invokeWithTarget:[QCocoaApplicationDelegate sharedDelegate]];
133}
134
135@end
bool isActive
QCocoaWindow * cocoaWindow() const
static QWindow * modalWindow()
Returns the most recently shown modal window.
\inmodule QtCore
Definition qobject.h:103
QString self
Definition language.cpp:58
static jboolean cut(JNIEnv *, jobject)
static jboolean copy(JNIEnv *, jobject)
static jboolean paste(JNIEnv *, jobject)
static jboolean selectAll(JNIEnv *, jobject)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
#define qCDebug(category,...)
GLenum GLuint id
[7]
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
static QT_BEGIN_NAMESPACE void init(QTextBoundaryFinder::BoundaryType type, QStringView str, QCharAttributes *attributes)
QFileSelector selector
[1]
QGraphicsItem * item
QQuickView * view
[0]