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
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
4#include <AppKit/AppKit.h>
5
6#include "qnswindowdelegate.h"
7#include "qcocoahelpers.h"
8#include "qcocoawindow.h"
9#include "qcocoascreen.h"
10
11#include <QDebug>
12#include <QtCore/private/qcore_mac_p.h>
13#include <qpa/qplatformscreen.h>
14#include <qpa/qwindowsysteminterface.h>
15
16static inline bool isWhiteSpace(const QString &s)
17{
18 for (int i = 0; i < s.size(); ++i)
19 if (!s.at(i).isSpace())
20 return false;
21 return true;
22}
23
25{
26 return qnswindow_cast(window).platformWindow;
27}
28
29@implementation QNSWindowDelegate
30
31- (BOOL)windowShouldClose:(NSWindow *)window
32{
33 if (QCocoaWindow *platformWindow = toPlatformWindow(window))
34 return platformWindow->windowShouldClose();
35
36 return YES;
37}
45- (NSRect)windowWillUseStandardFrame:(NSWindow *)window defaultFrame:(NSRect)proposedFrame
46{
47 Q_UNUSED(proposedFrame);
48
49 QCocoaWindow *platformWindow = toPlatformWindow(window);
50 Q_ASSERT(platformWindow);
51 const QWindow *w = platformWindow->window();
52
53 // maximumSize() refers to the client size, but AppKit expects the full frame size
54 QSizeF maximumSize = w->maximumSize() + QSize(0, w->frameMargins().top());
55
56 // The window should never be larger than the current screen geometry
57 const QRectF screenGeometry = platformWindow->screen()->geometry();
58 maximumSize = maximumSize.boundedTo(screenGeometry.size());
59
60 // Use the current frame position for the initial maximized frame,
61 // so that the window stays put and just expand, in case its maximum
62 // size is within the screen bounds.
63 QRectF maximizedFrame = QRectF(w->framePosition(), maximumSize);
64
65 // But constrain the frame to the screen bounds in case the frame
66 // extends beyond the screen bounds as a result of starting out
67 // with the current frame position.
68 maximizedFrame.translate(QPoint(
69 qMax(screenGeometry.left() - maximizedFrame.left(), 0.0) +
70 qMin(screenGeometry.right() - maximizedFrame.right(), 0.0),
71 qMax(screenGeometry.top() - maximizedFrame.top(), 0.0) +
72 qMin(screenGeometry.bottom() - maximizedFrame.bottom(), 0.0)));
73
74 return QCocoaScreen::mapToNative(maximizedFrame);
75}
76
77- (BOOL)windowShouldZoom:(NSWindow*)window toFrame:(NSRect)newFrame
78{
79 QCocoaWindow *platformWindow = toPlatformWindow(window);
80 Q_ASSERT(platformWindow);
81 platformWindow->windowWillZoom();
82 return YES;
83}
84
85- (BOOL)window:(NSWindow *)window shouldPopUpDocumentPathMenu:(NSMenu *)menu
86{
88
89 QCocoaWindow *platformWindow = toPlatformWindow(window);
90 Q_ASSERT(platformWindow);
91
92 // Only pop up document path if the filename is non-empty. We allow whitespace, to
93 // allow faking a window icon by setting the file path to a single space character.
94 return !isWhiteSpace(platformWindow->window()->filePath());
95}
96
97- (BOOL)window:(NSWindow *)window shouldDragDocumentWithEvent:(NSEvent *)event from:(NSPoint)dragImageLocation withPasteboard:(NSPasteboard *)pasteboard
98{
100 Q_UNUSED(dragImageLocation);
101 Q_UNUSED(pasteboard);
102
103 QCocoaWindow *platformWindow = toPlatformWindow(window);
104 Q_ASSERT(platformWindow);
105
106 // Only allow drag if the filename is non-empty. We allow whitespace, to
107 // allow faking a window icon by setting the file path to a single space.
108 return !isWhiteSpace(platformWindow->window()->filePath());
109}
110@end
static CGPoint mapToNative(const QPointF &pos, QCocoaScreen *screen=QCocoaScreen::primaryScreen())
void windowWillZoom()
virtual QRect geometry() const =0
Reimplement in subclass to return the pixel geometry of the screen.
QWindow * window() const
Returns the window which belongs to the QPlatformWindow.
QPlatformScreen * screen() const override
Returns the platform screen handle corresponding to this platform window, or null if the window is no...
\inmodule QtCore\reentrant
Definition qpoint.h:25
\inmodule QtCore\reentrant
Definition qrect.h:484
constexpr qreal bottom() const noexcept
Returns the y-coordinate of the rectangle's bottom edge.
Definition qrect.h:500
constexpr qreal left() const noexcept
Returns the x-coordinate of the rectangle's left edge.
Definition qrect.h:497
constexpr QSizeF size() const noexcept
Returns the size of the rectangle.
Definition qrect.h:735
constexpr qreal top() const noexcept
Returns the y-coordinate of the rectangle's top edge.
Definition qrect.h:498
constexpr qreal right() const noexcept
Returns the x-coordinate of the rectangle's right edge.
Definition qrect.h:499
\inmodule QtCore
Definition qsize.h:208
constexpr QSizeF boundedTo(const QSizeF &) const noexcept
Returns a size holding the minimum width and height of this size and the given otherSize.
Definition qsize.h:396
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtGui
Definition qwindow.h:63
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
NSWindow< QNSWindowProtocol > * qnswindow_cast(NSWindow *window)
Definition qnswindow.mm:62
static bool isWhiteSpace(const QString &s)
static QCocoaWindow * toPlatformWindow(NSWindow *window)
GLfloat GLfloat GLfloat w
[0]
GLdouble GLdouble GLdouble GLdouble top
struct _cl_event * event
GLdouble s
[6]
Definition qopenglext.h:235
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
#define Q_UNUSED(x)
aWidget window() -> setWindowTitle("New Window Title")
[2]
QMenu menu
[5]