Qt 6.x
The Qt SDK
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 if ([window conformsToProtocol:@protocol(QNSWindowProtocol)])
27 return static_cast<QCocoaNSWindow *>(window).platformWindow;
28 return nullptr;
29}
30
31@implementation QNSWindowDelegate
32
33- (BOOL)windowShouldClose:(NSWindow *)window
34{
35 if (QCocoaWindow *platformWindow = toPlatformWindow(window))
36 return platformWindow->windowShouldClose();
37
38 return YES;
39}
47- (NSRect)windowWillUseStandardFrame:(NSWindow *)window defaultFrame:(NSRect)proposedFrame
48{
49 Q_UNUSED(proposedFrame);
50
51 QCocoaWindow *platformWindow = toPlatformWindow(window);
52 Q_ASSERT(platformWindow);
53 const QWindow *w = platformWindow->window();
54
55 // maximumSize() refers to the client size, but AppKit expects the full frame size
56 QSizeF maximumSize = w->maximumSize() + QSize(0, w->frameMargins().top());
57
58 // The window should never be larger than the current screen geometry
59 const QRectF screenGeometry = platformWindow->screen()->geometry();
60 maximumSize = maximumSize.boundedTo(screenGeometry.size());
61
62 // Use the current frame position for the initial maximized frame,
63 // so that the window stays put and just expand, in case its maximum
64 // size is within the screen bounds.
65 QRectF maximizedFrame = QRectF(w->framePosition(), maximumSize);
66
67 // But constrain the frame to the screen bounds in case the frame
68 // extends beyond the screen bounds as a result of starting out
69 // with the current frame position.
70 maximizedFrame.translate(QPoint(
71 qMax(screenGeometry.left() - maximizedFrame.left(), 0.0) +
72 qMin(screenGeometry.right() - maximizedFrame.right(), 0.0),
73 qMax(screenGeometry.top() - maximizedFrame.top(), 0.0) +
74 qMin(screenGeometry.bottom() - maximizedFrame.bottom(), 0.0)));
75
76 return QCocoaScreen::mapToNative(maximizedFrame);
77}
78
79- (BOOL)windowShouldZoom:(NSWindow*)window toFrame:(NSRect)newFrame
80{
81 QCocoaWindow *platformWindow = toPlatformWindow(window);
82 Q_ASSERT(platformWindow);
83 platformWindow->windowWillZoom();
84 return YES;
85}
86
87- (BOOL)window:(NSWindow *)window shouldPopUpDocumentPathMenu:(NSMenu *)menu
88{
90
91 QCocoaWindow *platformWindow = toPlatformWindow(window);
92 Q_ASSERT(platformWindow);
93
94 // Only pop up document path if the filename is non-empty. We allow whitespace, to
95 // allow faking a window icon by setting the file path to a single space character.
96 return !isWhiteSpace(platformWindow->window()->filePath());
97}
98
99- (BOOL)window:(NSWindow *)window shouldDragDocumentWithEvent:(NSEvent *)event from:(NSPoint)dragImageLocation withPasteboard:(NSPasteboard *)pasteboard
100{
102 Q_UNUSED(dragImageLocation);
103 Q_UNUSED(pasteboard);
104
105 QCocoaWindow *platformWindow = toPlatformWindow(window);
106 Q_ASSERT(platformWindow);
107
108 // Only allow drag if the filename is non-empty. We allow whitespace, to
109 // allow faking a window icon by setting the file path to a single space.
110 return !isWhiteSpace(platformWindow->window()->filePath());
111}
112@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:23
\inmodule QtCore\reentrant
Definition qrect.h:483
constexpr qreal bottom() const noexcept
Returns the y-coordinate of the rectangle's bottom edge.
Definition qrect.h:499
constexpr qreal left() const noexcept
Returns the x-coordinate of the rectangle's left edge.
Definition qrect.h:496
constexpr QSizeF size() const noexcept
Returns the size of the rectangle.
Definition qrect.h:721
constexpr void translate(qreal dx, qreal dy) noexcept
Moves the rectangle dx along the x-axis and dy along the y-axis, relative to the current position.
Definition qrect.h:724
constexpr qreal top() const noexcept
Returns the y-coordinate of the rectangle's top edge.
Definition qrect.h:497
constexpr qreal right() const noexcept
Returns the x-coordinate of the rectangle's right edge.
Definition qrect.h:498
\inmodule QtCore
Definition qsize.h:207
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:385
\inmodule QtCore
Definition qsize.h:25
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\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
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]