49#if QT_CONFIG(sessionmanager)
57#include <qpa/qwindowsysteminterface.h>
63 NSObject <NSApplicationDelegate> *reflectionDelegate;
67+ (instancetype)sharedDelegate
70 static dispatch_once_t onceToken;
71 dispatch_once(&onceToken, ^{
72 shared = [[
self alloc] init];
93 if (reflectionDelegate) {
94 [[NSApplication sharedApplication] setDelegate:reflectionDelegate];
95 [reflectionDelegate release];
97 [[NSNotificationCenter defaultCenter] removeObserver:self];
102- (NSMenu *)applicationDockMenu:(NSApplication *)sender
107 [
self.dockMenu.delegate menuWillOpen:self.dockMenu];
108 return [[
self.dockMenu retain] autorelease];
112- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
114 if ([reflectionDelegate respondsToSelector:_cmd])
115 return [reflectionDelegate applicationShouldTerminate:sender];
121 qCDebug(lcQpaApplication) <<
"No running event loops, terminating now";
122 return NSTerminateNow;
125#if QT_CONFIG(sessionmanager)
127 cocoaSessionManager->resetCancellation();
128 cocoaSessionManager->appCommitData();
130 if (cocoaSessionManager->wasCanceled()) {
131 qCDebug(lcQpaApplication) <<
"Session management canceled application termination";
132 return NSTerminateCancel;
136 if (!QWindowSystemInterface::handleApplicationTermination<QWindowSystemInterface::SynchronousDelivery>()) {
137 qCDebug(lcQpaApplication) <<
"Application termination canceled";
138 return NSTerminateCancel;
145 qCDebug(lcQpaApplication) <<
"Termination accepted, but returning to runloop for exit through main()";
146 return NSTerminateCancel;
149- (
void)applicationWillFinishLaunching:(NSNotification *)notification
151 if ([reflectionDelegate respondsToSelector:_cmd])
152 [reflectionDelegate applicationWillFinishLaunching:notification];
168 NSAppleEventManager *eventManager = [NSAppleEventManager sharedAppleEventManager];
169 [eventManager setEventHandler:self
170 andSelector:@selector(getUrl:withReplyEvent:)
171 forEventClass:kInternetEventClass
172 andEventID:kAEGetURL];
176- (
void)removeAppleEventHandlers
178 NSAppleEventManager *eventManager = [NSAppleEventManager sharedAppleEventManager];
179 [eventManager removeEventHandlerForEventClass:kInternetEventClass andEventID:kAEGetURL];
187- (
void)applicationDidFinishLaunching:(NSNotification *)aNotification
189 if ([reflectionDelegate respondsToSelector:_cmd])
190 [reflectionDelegate applicationDidFinishLaunching:aNotification];
195 auto frontmostApplication = NSWorkspace.sharedWorkspace.frontmostApplication;
196 auto currentApplication = NSRunningApplication.currentApplication;
197 if (frontmostApplication != currentApplication) {
205 qCDebug(lcQpaApplication) <<
"Launched with" << frontmostApplication
206 <<
"as frontmost application. Activating" << currentApplication <<
"instead.";
207 [NSApplication.sharedApplication activateIgnoringOtherApps:YES];
216 [currentApplication activateWithOptions:NSApplicationActivateAllWindows];
222- (
void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
234 if (
qApp->arguments().contains(qtFileName))
240 if ([reflectionDelegate respondsToSelector:_cmd])
241 [reflectionDelegate application:sender
openFiles:filenames];
245- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
247 if ([reflectionDelegate respondsToSelector:_cmd])
248 return [reflectionDelegate applicationShouldTerminateAfterLastWindowClosed:sender];
253- (
void)applicationDidBecomeActive:(NSNotification *)notification
256 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:QCocoaWindow::s_applicationActivationObserver];
260 if ([reflectionDelegate respondsToSelector:_cmd])
261 [reflectionDelegate applicationDidBecomeActive:notification];
269 [view convertFromScreen:[NSEvent mouseLocation] toWindowPoint:&windowPoint andScreenPoint:&screenPoint];
271 qCInfo(lcQpaMouse) <<
"Application activated with mouse at" << windowPoint <<
"; sending" <<
QEvent::Enter <<
"to" << windowUnderMouse;
276- (
void)applicationDidResignActive:(NSNotification *)notification
278 if ([reflectionDelegate respondsToSelector:_cmd])
279 [reflectionDelegate applicationDidResignActive:notification];
285 qCInfo(lcQpaMouse) <<
"Application deactivated; sending" <<
QEvent::Leave <<
"to" << windowUnderMouse;
290- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
292 if ([reflectionDelegate respondsToSelector:_cmd])
293 return [reflectionDelegate applicationShouldHandleReopen:theApplication hasVisibleWindows:flag];
307- (
void)setReflectionDelegate:(NSObject <NSApplicationDelegate> *)oldDelegate
309 [oldDelegate retain];
310 [reflectionDelegate release];
311 reflectionDelegate = oldDelegate;
314- (NSMethodSignature *)methodSignatureForSelector:(
SEL)aSelector
316 NSMethodSignature *
result = [
super methodSignatureForSelector:aSelector];
317 if (!
result && reflectionDelegate) {
318 result = [reflectionDelegate methodSignatureForSelector:aSelector];
323- (BOOL)respondsToSelector:(
SEL)aSelector
325 return [
super respondsToSelector:aSelector] || [reflectionDelegate respondsToSelector:aSelector];
328- (
void)forwardInvocation:(NSInvocation *)invocation
330 SEL invocationSelector = [invocation selector];
331 if ([reflectionDelegate respondsToSelector:invocationSelector])
332 [invocation invokeWithTarget:reflectionDelegate];
334 [
self doesNotRecognizeSelector:invocationSelector];
337- (BOOL)application:(NSApplication *)application continueUserActivity:(NSUserActivity *)userActivity
338 restorationHandler:(
void(^)(NSArray<
id<NSUserActivityRestoring>> *restorableObjects))restorationHandler
341 if ([reflectionDelegate respondsToSelector:_cmd]
342 && [reflectionDelegate application:application continueUserActivity:userActivity
343 restorationHandler:restorationHandler] == YES) {
350 if ([userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
353 return cocoaIntegration->services()->handleUrl(QUrl::fromNSURL(userActivity.webpageURL));
359- (
void)getUrl:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
363 NSString *urlString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
364 const QString qurlString = QString::fromNSString(urlString);
366 if (
event.eventClass == kInternetEventClass &&
event.eventID == kAEGetURL) {
372 cocoaIntegration->services()->handleUrl(
QUrl(qurlString));
387- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)application
389 if (@available(macOS 12, *)) {
390 if ([reflectionDelegate respondsToSelector:_cmd])
391 return [reflectionDelegate applicationSupportsSecureRestorableState:application];
405@implementation QCocoaApplicationDelegate (Menus)
407- (BOOL)validateMenuItem:(NSMenuItem*)item
409 qCDebug(lcQpaMenus) <<
"Validating" <<
item <<
"for" << self;
411 auto *nativeItem = qt_objc_cast<QCocoaNSMenuItem *>(
item);
415 auto *platformItem = nativeItem.platformMenuItem;
417 return item.hasSubmenu || (
item.enabled && (
item.action !=
@selector(qt_itemFired:)));
420 if (platformItem->menu())
428@implementation QCocoaApplicationDelegate (MenuAPI)
437 auto *nativeItem = qt_objc_cast<QCocoaNSMenuItem *>(
item);
438 Q_ASSERT_X(nativeItem,
qPrintable(__FUNCTION__),
"Triggered menu item is not a QCocoaNSMenuItem.");
439 auto *platformItem = nativeItem.platformMenuItem;
442 if (!platformItem || platformItem->menu())
static QCocoaIntegration * instance()
static QCocoaSessionManager * instance()
static QPointer< QCocoaWindow > s_windowUnderMouse
static id s_applicationActivationObserver
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
bool isEnabled() const
Returns true if the item is enabled; otherwise, false is returned.
static Qt::KeyboardModifiers modifier_buttons
static QGuiApplicationPrivate * instance()
\inmodule QtCore\reentrant
\macro QT_RESTRICTED_CAST_FROM_ASCII
bool isValid() const
Returns true if the URL is non-empty and valid; otherwise returns false.
static void handleLeaveEvent(QWindow *window)
static void handleFileOpenEvent(const QString &fileName)
static void handleApplicationStateChanged(Qt::ApplicationState newState, bool forcePropagate=false)
static void handleEnterEvent(QWindow *window, const QPointF &local=QPointF(), const QPointF &global=QPointF())
void openFiles(const std::string &accept, FileSelectMode fileSelectMode, const std::function< void(int fileCount)> &fileDialogClosed, const std::function< char *(uint64_t size, const std::string &name)> &acceptFile, const std::function< void()> &fileDataReady)
QNSView * qnsview_cast(NSView *view)
Returns the view cast to a QNSview if possible.
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 qCInfo(category,...)
#define qCDebug(category,...)
#define Q_ASSERT_X(cond, x, msg)
#define qPrintable(string)
Q_CORE_EXPORT bool qEnvironmentVariableIsEmpty(const char *varName) noexcept
static QT_BEGIN_NAMESPACE void init(QTextBoundaryFinder::BoundaryType type, QStringView str, QCharAttributes *attributes)
QUrl url("example.com")
[constructor-url-reference]