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
qnswindowdelegate.mm
Go to the documentation of this file.
1// Copyright (C) 2016 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#include <AppKit/AppKit.h>
6
9#include "qcocoawindow.h"
10#include "qcocoascreen.h"
11
12#include <QDebug>
13#include <QtCore/private/qcore_mac_p.h>
14#include <qpa/qplatformscreen.h>
15#include <qpa/qwindowsysteminterface.h>
16
17static inline bool isWhiteSpace(const QString &s)
18{
19 for (int i = 0; i < s.size(); ++i)
20 if (!s.at(i).isSpace())
21 return false;
22 return true;
23}
24
25static QCocoaWindow *toPlatformWindow(NSWindow *window)
26{
27 return qnswindow_cast(window).platformWindow;
28}
29
30@implementation QNSWindowDelegate
31
32- (BOOL)windowShouldClose:(NSWindow *)window
33{
34 if (QCocoaWindow *platformWindow = toPlatformWindow(window))
35 return platformWindow->windowShouldClose();
36
37 return YES;
38}
39/*!
40 Overridden to ensure that the zoomed state always results in a maximized
41 window, which would otherwise not be the case for borderless windows.
42
43 We also keep the window on the same screen as before; something AppKit
44 sometimes fails to do using its built in logic.
45*/
46- (NSRect)windowWillUseStandardFrame:(NSWindow *)window defaultFrame:(NSRect)proposedFrame
47{
48 Q_UNUSED(proposedFrame);
49
50 QCocoaWindow *platformWindow = toPlatformWindow(window);
51 Q_ASSERT(platformWindow);
52 const QWindow *w = platformWindow->window();
53
54 // maximumSize() refers to the client size, but AppKit expects the full frame size
55 QSizeF maximumSize = w->maximumSize() + QSize(0, w->frameMargins().top());
56
57 // The window should never be larger than the current screen geometry
58 const QRectF screenGeometry = platformWindow->screen()->geometry();
59 maximumSize = maximumSize.boundedTo(screenGeometry.size());
60
61 // Use the current frame position for the initial maximized frame,
62 // so that the window stays put and just expand, in case its maximum
63 // size is within the screen bounds.
64 QRectF maximizedFrame = QRectF(w->framePosition(), maximumSize);
65
66 // But constrain the frame to the screen bounds in case the frame
67 // extends beyond the screen bounds as a result of starting out
68 // with the current frame position.
69 maximizedFrame.translate(QPoint(
70 qMax(screenGeometry.left() - maximizedFrame.left(), 0.0) +
71 qMin(screenGeometry.right() - maximizedFrame.right(), 0.0),
72 qMax(screenGeometry.top() - maximizedFrame.top(), 0.0) +
73 qMin(screenGeometry.bottom() - maximizedFrame.bottom(), 0.0)));
74
75 return QCocoaScreen::mapToNative(maximizedFrame);
76}
77
78- (BOOL)windowShouldZoom:(NSWindow*)window toFrame:(NSRect)newFrame
79{
80 QCocoaWindow *platformWindow = toPlatformWindow(window);
81 Q_ASSERT(platformWindow);
82 platformWindow->windowWillZoom();
83 return YES;
84}
85
86- (BOOL)window:(NSWindow *)window shouldPopUpDocumentPathMenu:(NSMenu *)menu
87{
88 Q_UNUSED(menu);
89
90 QCocoaWindow *platformWindow = toPlatformWindow(window);
91 Q_ASSERT(platformWindow);
92
93 // Only pop up document path if the filename is non-empty. We allow whitespace, to
94 // allow faking a window icon by setting the file path to a single space character.
95 return !isWhiteSpace(platformWindow->window()->filePath());
96}
97
98- (BOOL)window:(NSWindow *)window shouldDragDocumentWithEvent:(NSEvent *)event from:(NSPoint)dragImageLocation withPasteboard:(NSPasteboard *)pasteboard
99{
100 Q_UNUSED(event);
101 Q_UNUSED(dragImageLocation);
102 Q_UNUSED(pasteboard);
103
104 QCocoaWindow *platformWindow = toPlatformWindow(window);
105 Q_ASSERT(platformWindow);
106
107 // Only allow drag if the filename is non-empty. We allow whitespace, to
108 // allow faking a window icon by setting the file path to a single space.
109 return !isWhiteSpace(platformWindow->window()->filePath());
110}
111@end
static bool isWhiteSpace(const QString &s)
static QCocoaWindow * toPlatformWindow(NSWindow *window)