diff options
Diffstat (limited to 'src/plugins/platforms/cocoa/qnswindow.mm')
-rw-r--r-- | src/plugins/platforms/cocoa/qnswindow.mm | 221 |
1 files changed, 156 insertions, 65 deletions
diff --git a/src/plugins/platforms/cocoa/qnswindow.mm b/src/plugins/platforms/cocoa/qnswindow.mm index 6b4e110af2..74ba6f65ac 100644 --- a/src/plugins/platforms/cocoa/qnswindow.mm +++ b/src/plugins/platforms/cocoa/qnswindow.mm @@ -1,48 +1,15 @@ -/**************************************************************************** -** -** Copyright (C) 2017 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2017 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #if !defined(QNSWINDOW_PROTOCOL_IMPLMENTATION) +#include <AppKit/AppKit.h> + #include "qnswindow.h" #include "qcocoawindow.h" #include "qcocoahelpers.h" #include "qcocoaeventdispatcher.h" +#include "qcocoaintegration.h" #include <qpa/qwindowsysteminterface.h> #include <qoperatingsystemversion.h> @@ -91,6 +58,15 @@ static bool isMouseEvent(NSEvent *ev) } @end + +NSWindow<QNSWindowProtocol> *qnswindow_cast(NSWindow *window) +{ + if ([window conformsToProtocol:@protocol(QNSWindowProtocol)]) + return static_cast<QCocoaNSWindow *>(window); + else + return nil; +} + @implementation QNSWindow #define QNSWINDOW_PROTOCOL_IMPLMENTATION 1 #include "qnswindow.mm" @@ -104,7 +80,7 @@ static bool isMouseEvent(NSEvent *ev) // Unfortunately there's no NSWindowListOrderedBackToFront, // so we have to manually reverse the order using an array. - NSMutableArray<NSWindow *> *windows = [NSMutableArray<NSWindow *> new]; + NSMutableArray<NSWindow *> *windows = [[NSMutableArray<NSWindow *> new] autorelease]; [application enumerateWindowsWithOptions:NSWindowListOrderedFrontToBack usingBlock:^(NSWindow *window, BOOL *) { // For some reason AppKit will give us nil-windows, skip those @@ -131,9 +107,10 @@ static bool isMouseEvent(NSEvent *ev) continue; if ([window conformsToProtocol:@protocol(QNSWindowProtocol)]) { - QCocoaWindow *cocoaWindow = static_cast<QCocoaNSWindow *>(window).platformWindow; - window.level = notification.name == NSApplicationWillResignActiveNotification ? - NSNormalWindowLevel : cocoaWindow->windowLevel(cocoaWindow->window()->flags()); + if (QCocoaWindow *cocoaWindow = static_cast<QCocoaNSWindow *>(window).platformWindow) { + window.level = notification.name == NSApplicationWillResignActiveNotification ? + NSNormalWindowLevel : cocoaWindow->windowLevel(cocoaWindow->window()->flags()); + } } // The documentation says that "when a window enters a new level, it’s ordered @@ -158,6 +135,46 @@ static bool isMouseEvent(NSEvent *ev) #define QNSWINDOW_PROTOCOL_IMPLMENTATION 1 #include "qnswindow.mm" #undef QNSWINDOW_PROTOCOL_IMPLMENTATION + +- (BOOL)worksWhenModal +{ + if (!m_platformWindow) + return NO; + + // Conceptually there are two sets of windows we need consider: + // + // - windows 'lower' in the modal session stack + // - windows 'within' the current modal session + // + // The first set of windows should always be blocked by the current + // modal session, regardless of window type. The latter set may contain + // windows with a transient parent, which from Qt's point of view makes + // them 'child' windows, so we treat them as operable within the current + // modal session. + + if (!NSApp.modalWindow) + return NO; + + // Special case popup windows (menus, completions, etc), as these usually + // don't have a transient parent set, and we don't want to block them. The + // assumption is that these windows are only opened intermittently, from + // within windows that can already be interacted with in this modal session. + Qt::WindowType type = m_platformWindow->window()->type(); + if (type == Qt::Popup) + return YES; + + // If the current modal window (top level modal session) is not a Qt window we + // have no way of knowing if this window is transient child of the modal window. + if (![NSApp.modalWindow conformsToProtocol:@protocol(QNSWindowProtocol)]) + return NO; + + if (auto *modalWindow = static_cast<QCocoaNSWindow *>(NSApp.modalWindow).platformWindow) { + if (modalWindow->window()->isAncestorOf(m_platformWindow->window(), QWindow::IncludeTransients)) + return YES; + } + + return NO; +} @end #else // QNSWINDOW_PROTOCOL_IMPLMENTATION @@ -167,6 +184,7 @@ static bool isMouseEvent(NSEvent *ev) { // Member variables QPointer<QCocoaWindow> m_platformWindow; + bool m_isMinimizing; } - (instancetype)initWithContentRect:(NSRect)contentRect styleMask:(NSWindowStyleMask)style @@ -178,6 +196,8 @@ static bool isMouseEvent(NSEvent *ev) // we can properly reflect the window's state during initialization. m_platformWindow = window; + m_isMinimizing = false; + return [super initWithContentRect:contentRect styleMask:style backing:backingStoreType defer:defer screen:screen]; } @@ -186,6 +206,29 @@ static bool isMouseEvent(NSEvent *ev) return m_platformWindow; } +- (void)setContentView:(NSView*)view +{ + [super setContentView:view]; + + if (!qnsview_cast(self.contentView)) + return; + + // Now that we're the content view, we can apply the properties of + // the QWindow. We do this here, instead of in init, so that we can + // use the same code paths for setting these properties during + // NSWindow initialization as we do when setting them later on. + const QWindow *window = m_platformWindow->window(); + qCDebug(lcQpaWindow) << "Reflecting" << window << "state to" << self; + + m_platformWindow->propagateSizeHints(); + m_platformWindow->setWindowFlags(window->flags()); + m_platformWindow->setWindowTitle(window->title()); + m_platformWindow->setWindowFilePath(window->filePath()); // Also sets window icon + m_platformWindow->setWindowState(window->windowState()); + m_platformWindow->setOpacity(window->opacity()); + m_platformWindow->setVisible(window->isVisible()); +} + - (NSString *)description { NSMutableString *description = [NSMutableString stringWithString:[super description]]; @@ -227,25 +270,12 @@ static bool isMouseEvent(NSEvent *ev) - (BOOL)canBecomeMainWindow { - BOOL canBecomeMain = YES; // By default, windows can become the main window - // Windows with a transient parent (such as combobox popup windows) // cannot become the main window: if (!m_platformWindow || m_platformWindow->window()->transientParent()) - canBecomeMain = NO; - - return canBecomeMain; -} - -- (BOOL)worksWhenModal -{ - if (m_platformWindow && [self isKindOfClass:[QNSPanel class]]) { - Qt::WindowType type = m_platformWindow->window()->type(); - if (type == Qt::Popup || type == Qt::Dialog || type == Qt::Tool) - return YES; - } + return NO; - return [super worksWhenModal]; + return [super canBecomeMainWindow]; } - (BOOL)isOpaque @@ -255,8 +285,23 @@ static bool isMouseEvent(NSEvent *ev) - (NSColor *)backgroundColor { - return self.styleMask == NSWindowStyleMaskBorderless ? - [NSColor clearColor] : [super backgroundColor]; + // FIXME: Plumb to a WA_NoSystemBackground-like window flag, + // or a QWindow::backgroundColor() property. In the meantime + // we assume that if you have translucent content, without a + // frame then you intend to do all background drawing yourself. + const QWindow *window = m_platformWindow ? m_platformWindow->window() : nullptr; + if (!self.opaque && window) { + // Qt::Popup also requires clearColor - in qmacstyle + // we fill background using a special path with rounded corners. + if (window->flags().testFlag(Qt::FramelessWindowHint) + || (window->flags() & Qt::WindowType_Mask) == Qt::Popup) + return [NSColor clearColor]; + } + + // This still allows you to have translucent content with a frame, + // where the system background (or color set via NSWindow) will + // shine through. + return [super backgroundColor]; } - (void)sendEvent:(NSEvent*)theEvent @@ -281,18 +326,64 @@ static bool isMouseEvent(NSEvent *ev) return; } + const bool mouseEventInFrameStrut = [theEvent, self]{ + if (isMouseEvent(theEvent)) { + const NSPoint loc = theEvent.locationInWindow; + const NSRect windowFrame = [self convertRectFromScreen:self.frame]; + const NSRect contentFrame = self.contentView.frame; + if (NSMouseInRect(loc, windowFrame, NO) && !NSMouseInRect(loc, contentFrame, NO)) + return true; + } + return false; + }(); + // Any mouse-press in the frame of the window, including the title bar buttons, should + // close open popups. Presses within the window's content are handled to do that in the + // NSView::mouseDown implementation. + if (theEvent.type == NSEventTypeLeftMouseDown && mouseEventInFrameStrut) + QGuiApplicationPrivate::instance()->closeAllPopups(); + [super sendEvent:theEvent]; if (!m_platformWindow) return; // Platform window went away while processing event - if (m_platformWindow->frameStrutEventsEnabled() && isMouseEvent(theEvent)) { - NSPoint loc = [theEvent locationInWindow]; - NSRect windowFrame = [self convertRectFromScreen:self.frame]; - NSRect contentFrame = self.contentView.frame; - if (NSMouseInRect(loc, windowFrame, NO) && !NSMouseInRect(loc, contentFrame, NO)) - [qnsview_cast(m_platformWindow->view()) handleFrameStrutMouseEvent:theEvent]; + // Cocoa will not deliver mouse events to a window that is modally blocked (by Cocoa, + // not Qt). However, an active popup is expected to grab any mouse event within the + // application, so we need to handle those explicitly and trust Qt's isWindowBlocked + // implementation to eat events that shouldn't be delivered anyway. + if (isMouseEvent(theEvent) && QGuiApplicationPrivate::instance()->popupActive() + && QGuiApplicationPrivate::instance()->isWindowBlocked(m_platformWindow->window(), nullptr)) { + qCDebug(lcQpaWindow) << "Mouse event over modally blocked window" << m_platformWindow->window() + << "while popup is open - redirecting"; + [qnsview_cast(m_platformWindow->view()) handleMouseEvent:theEvent]; } + if (m_platformWindow->frameStrutEventsEnabled() && mouseEventInFrameStrut) + [qnsview_cast(m_platformWindow->view()) handleFrameStrutMouseEvent:theEvent]; +} + +- (void)miniaturize:(id)sender +{ + QBoolBlocker miniaturizeTracker(m_isMinimizing, true); + [super miniaturize:sender]; +} + +- (NSButton *)standardWindowButton:(NSWindowButton)buttonType +{ + NSButton *button = [super standardWindowButton:buttonType]; + + // When an NSWindow is asked to minimize it will check the + // NSWindowMiniaturizeButton for enablement before continuing, + // even if the style mask includes NSWindowStyleMaskMiniaturizable. + // To ensure that a window can be minimized, even when the + // minimize button has been disabled in response to the user + // setting CustomizeWindowHint, we temporarily return a default + // minimize-button that we haven't modified in updateTitleBarButtons. + // This ensures the window can be minimized, without visually + // toggling the actual minimize-button on and off. + if (buttonType == NSWindowMiniaturizeButton && m_isMinimizing && !button.enabled) + return [NSWindow standardWindowButton:buttonType forStyleMask:self.styleMask]; + + return button; } - (void)closeAndRelease |