From 12045801f742dd956f17d1a6c72b21f8f245a671 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Thu, 15 Nov 2018 14:21:56 +0100 Subject: macOS: Use shared NSWindowDelegate instead of one per window MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit All the delegate callbacks give us the relevant NSWindow, so we don't need one delegate per window just to be able to resolve the correct platform window. Change-Id: I8e44186da63bf01f029bb0b1fefcd8880f49dda6 Fixes: QTBUG-65693 Reviewed-by: Morten Johan Sørvig Reviewed-by: Timur Pocheptsov --- src/plugins/platforms/cocoa/qnswindowdelegate.mm | 44 +++++++++++++----------- 1 file changed, 24 insertions(+), 20 deletions(-) (limited to 'src/plugins/platforms/cocoa/qnswindowdelegate.mm') diff --git a/src/plugins/platforms/cocoa/qnswindowdelegate.mm b/src/plugins/platforms/cocoa/qnswindowdelegate.mm index 97309ea990..14f1ca0114 100644 --- a/src/plugins/platforms/cocoa/qnswindowdelegate.mm +++ b/src/plugins/platforms/cocoa/qnswindowdelegate.mm @@ -49,23 +49,17 @@ static QRegExp whitespaceRegex = QRegExp(QStringLiteral("\\s*")); -@implementation QNSWindowDelegate { - QCocoaWindow *m_cocoaWindow; -} - -- (instancetype)initWithQCocoaWindow:(QCocoaWindow *)cocoaWindow +static QCocoaWindow *toPlatformWindow(NSWindow *window) { - if ((self = [self init])) - m_cocoaWindow = cocoaWindow; - return self; + return qnsview_cast(window.contentView).platformWindow; } -- (BOOL)windowShouldClose:(NSNotification *)notification +@implementation QNSWindowDelegate + +- (BOOL)windowShouldClose:(NSWindow *)window { - Q_UNUSED(notification); - if (m_cocoaWindow) { - return m_cocoaWindow->windowShouldClose(); - } + if (QCocoaWindow *platformWindow = toPlatformWindow(window)) + return platformWindow->windowShouldClose(); return YES; } @@ -79,14 +73,16 @@ static QRegExp whitespaceRegex = QRegExp(QStringLiteral("\\s*")); - (NSRect)windowWillUseStandardFrame:(NSWindow *)window defaultFrame:(NSRect)proposedFrame { Q_UNUSED(proposedFrame); - Q_ASSERT(window == m_cocoaWindow->nativeWindow()); - const QWindow *w = m_cocoaWindow->window(); + + QCocoaWindow *platformWindow = toPlatformWindow(window); + Q_ASSERT(platformWindow); + const QWindow *w = platformWindow->window(); // maximumSize() refers to the client size, but AppKit expects the full frame size QSizeF maximumSize = w->maximumSize() + QSize(0, w->frameMargins().top()); // The window should never be larger than the current screen geometry - const QRectF screenGeometry = m_cocoaWindow->screen()->geometry(); + const QRectF screenGeometry = platformWindow->screen()->geometry(); maximumSize = maximumSize.boundedTo(screenGeometry.size()); // Use the current frame position for the initial maximized frame, @@ -113,6 +109,8 @@ static QRegExp whitespaceRegex = QRegExp(QStringLiteral("\\s*")); #pragma clang diagnostic ignored "-Wdeprecated-declarations" - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)frameSize { + Q_ASSERT(toPlatformWindow(window)); + qCDebug(lcQpaWindow) << window << "will resize to" << QSizeF::fromCGSize(frameSize) << "- disabling screen updates temporarily"; @@ -131,6 +129,8 @@ static QRegExp whitespaceRegex = QRegExp(QStringLiteral("\\s*")); - (void)windowDidResize:(NSNotification *)notification { NSWindow *window = notification.object; + Q_ASSERT(toPlatformWindow(window)); + qCDebug(lcQpaWindow) << window << "was resized - re-enabling screen updates"; NSEnableScreenUpdates(); } @@ -138,23 +138,27 @@ static QRegExp whitespaceRegex = QRegExp(QStringLiteral("\\s*")); - (BOOL)window:(NSWindow *)window shouldPopUpDocumentPathMenu:(NSMenu *)menu { - Q_UNUSED(window); Q_UNUSED(menu); + QCocoaWindow *platformWindow = toPlatformWindow(window); + Q_ASSERT(platformWindow); + // Only pop up document path if the filename is non-empty. We allow whitespace, to // allow faking a window icon by setting the file path to a single space character. - return !whitespaceRegex.exactMatch(m_cocoaWindow->window()->filePath()); + return !whitespaceRegex.exactMatch(platformWindow->window()->filePath()); } - (BOOL)window:(NSWindow *)window shouldDragDocumentWithEvent:(NSEvent *)event from:(NSPoint)dragImageLocation withPasteboard:(NSPasteboard *)pasteboard { - Q_UNUSED(window); Q_UNUSED(event); Q_UNUSED(dragImageLocation); Q_UNUSED(pasteboard); + QCocoaWindow *platformWindow = toPlatformWindow(window); + Q_ASSERT(platformWindow); + // Only allow drag if the filename is non-empty. We allow whitespace, to // allow faking a window icon by setting the file path to a single space. - return !whitespaceRegex.exactMatch(m_cocoaWindow->window()->filePath()); + return !whitespaceRegex.exactMatch(platformWindow->window()->filePath()); } @end -- cgit v1.2.3