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
qcocoawindow.h
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#ifndef QCOCOAWINDOW_H
6#define QCOCOAWINDOW_H
7
8#include <qpa/qplatformwindow.h>
9#include <qpa/qplatformwindow_p.h>
10#include <QRect>
11#include <QPointer>
12
13#ifndef QT_NO_OPENGL
15#endif
16#include "qnsview.h"
17#include "qnswindow.h"
18
19#if QT_CONFIG(vulkan)
20#include <MoltenVK/mvk_vulkan.h>
21#endif
22
23#include <QtCore/qhash.h>
24#include <QtCore/private/qflatmap_p.h>
25
26Q_FORWARD_DECLARE_OBJC_CLASS(NSWindow);
30
31#if !defined(__OBJC__)
32using NSInteger = long;
33using NSUInteger = unsigned long;
34#endif
35
36QT_BEGIN_NAMESPACE
37
38#ifndef QT_NO_DEBUG_STREAM
39class QDebug;
40#endif
41
42// QCocoaWindow
43//
44// QCocoaWindow is an NSView (not an NSWindow!) in the sense
45// that it relies on an NSView for all event handling and
46// graphics output and does not require an NSWindow, except for
47// the window-related functions like setWindowTitle.
48//
49// As a consequence of this it is possible to embed the QCocoaWindow
50// in an NSView hierarchy by getting a pointer to the "backing"
51// NSView and not calling QCocoaWindow::show():
52//
53// QWindow *qtWindow = new MyWindow();
54// qtWindow->create();
55// QPlatformNativeInterface *platformNativeInterface = QGuiApplication::platformNativeInterface();
56// NSView *qtView = (NSView *)platformNativeInterface->nativeResourceForWindow("nsview", qtWindow);
57// [parentView addSubview:qtView];
58//
59// See the qt_on_cocoa manual tests for a working example, located
60// in tests/manual/cocoa at the time of writing.
61
62#ifdef Q_MOC_RUN
63#define Q_NOTIFICATION_HANDLER(notification) Q_INVOKABLE Q_COCOA_NOTIFICATION_##notification
64#else
65#define Q_NOTIFICATION_HANDLER(notification)
66#define Q_NOTIFICATION_PREFIX QT_STRINGIFY2(Q_COCOA_NOTIFICATION_)
67#endif
68
69class QCocoaMenuBar;
70
71class QCocoaWindow : public QObject, public QPlatformWindow,
73{
75public:
77 ~QCocoaWindow();
78
79 void initialize() override;
80
81 void setGeometry(const QRect &rect) override;
82 QRect geometry() const override;
83 QRect normalGeometry() const override;
84 void setCocoaGeometry(const QRect &rect);
85
86 QMargins safeAreaMargins() const override;
87
88 void setVisible(bool visible) override;
89 void setWindowFlags(Qt::WindowFlags flags) override;
90 void setWindowState(Qt::WindowStates state) override;
91 void setWindowTitle(const QString &title) override;
92 void setWindowFilePath(const QString &filePath) override;
93 void setWindowIcon(const QIcon &icon) override;
94 void setAlertState(bool enabled) override;
95 bool isAlertState() const override;
96 void raise() override;
97 void lower() override;
98 bool isExposed() const override;
99 bool isEmbedded() const override;
100 bool isOpaque() const;
102 void setOpacity(qreal level) override;
103 void setMask(const QRegion &region) override;
104 bool setKeyboardGrabEnabled(bool grab) override;
105 bool setMouseGrabEnabled(bool grab) override;
106 QMargins frameMargins() const override;
107 QSurfaceFormat format() const override;
108
109 bool isForeignWindow() const override;
110
111 void requestUpdate() override;
112 bool updatesWithDisplayLink() const;
114
116
117 WId winId() const override;
118 void setParent(const QPlatformWindow *window) override;
119
120 NSView *view() const;
121 NSWindow *nativeWindow() const;
122
124
125 Q_NOTIFICATION_HANDLER(NSViewFrameDidChangeNotification) void viewDidChangeFrame();
126 Q_NOTIFICATION_HANDLER(NSViewGlobalFrameDidChangeNotification) void viewDidChangeGlobalFrame();
127
128 Q_NOTIFICATION_HANDLER(NSWindowDidMoveNotification) void windowDidMove();
129 Q_NOTIFICATION_HANDLER(NSWindowDidResizeNotification) void windowDidResize();
130 Q_NOTIFICATION_HANDLER(NSWindowWillStartLiveResizeNotification) void windowWillStartLiveResize();
131 Q_NOTIFICATION_HANDLER(NSWindowDidEndLiveResizeNotification) void windowDidEndLiveResize();
132 Q_NOTIFICATION_HANDLER(NSWindowDidBecomeKeyNotification) void windowDidBecomeKey();
133 Q_NOTIFICATION_HANDLER(NSWindowDidResignKeyNotification) void windowDidResignKey();
134 Q_NOTIFICATION_HANDLER(NSWindowDidMiniaturizeNotification) void windowDidMiniaturize();
135 Q_NOTIFICATION_HANDLER(NSWindowDidDeminiaturizeNotification) void windowDidDeminiaturize();
136 Q_NOTIFICATION_HANDLER(NSWindowWillEnterFullScreenNotification) void windowWillEnterFullScreen();
137 Q_NOTIFICATION_HANDLER(NSWindowDidEnterFullScreenNotification) void windowDidEnterFullScreen();
138 Q_NOTIFICATION_HANDLER(NSWindowWillExitFullScreenNotification) void windowWillExitFullScreen();
139 Q_NOTIFICATION_HANDLER(NSWindowDidExitFullScreenNotification) void windowDidExitFullScreen();
140 Q_NOTIFICATION_HANDLER(NSWindowDidOrderOnScreenAndFinishAnimatingNotification) void windowDidOrderOnScreen();
141 Q_NOTIFICATION_HANDLER(NSWindowDidOrderOffScreenNotification) void windowDidOrderOffScreen();
142 Q_NOTIFICATION_HANDLER(NSWindowDidChangeOcclusionStateNotification) void windowDidChangeOcclusionState();
143 Q_NOTIFICATION_HANDLER(NSWindowDidChangeScreenNotification) void windowDidChangeScreen();
144
145 void windowWillZoom();
146
147 bool windowShouldClose();
148 bool windowIsPopupType(Qt::WindowType type = Qt::Widget) const;
149
150 NSInteger windowLevel(Qt::WindowFlags flags);
151 NSUInteger windowStyleMask(Qt::WindowFlags flags);
152 void updateTitleBarButtons(Qt::WindowFlags flags);
153 bool isFixedSize() const;
154
155 bool setWindowModified(bool modified) override;
156
157 void setFrameStrutEventsEnabled(bool enabled) override;
160
161 void setMenubar(QCocoaMenuBar *mb);
162 QCocoaMenuBar *menubar() const;
163
164 void setWindowCursor(NSCursor *cursor);
165
166 void registerTouch(bool enable);
167
168 void registerContentBorderArea(quintptr identifier, int upper, int lower);
169 void setContentBorderAreaEnabled(quintptr identifier, bool enable);
170 void setContentBorderEnabled(bool enable) override;
171 bool testContentBorderAreaPosition(int position) const;
172 void applyContentBorderThickness(NSWindow *window = nullptr);
173
174 qreal devicePixelRatio() const override;
175 QWindow *childWindowAt(QPoint windowPoint);
177
178 bool windowEvent(QEvent *event) override;
179
181
183
192 Q_DECLARE_FLAGS(RecreationReasons, RecreationReason)
194
196
197protected:
199 QCocoaNSWindow *createNSWindow(bool shouldBePanel);
200
201 Qt::WindowStates windowState() const;
202 void applyWindowState(Qt::WindowStates newState);
203 void toggleMaximized();
204 void toggleFullScreen();
205 bool isTransitioningToFullScreen() const;
206
208
209// private:
210public: // for QNSView
211 friend class QCocoaBackingStore;
213
214 bool isContentView() const;
215
216 bool alwaysShowToolWindow() const;
217
222
225 void handleExposeEvent(const QRegion &region);
226
227 static void closeAllPopups();
228 static void setupPopupMonitor();
229 static void removePopupMonitor();
230
231 CALayer *contentLayer() const override;
232
233 void manageVisualEffectArea(quintptr identifier, const QRect &rect,
234 NSVisualEffectMaterial material, NSVisualEffectBlendingMode blendMode,
235 NSVisualEffectState activationState) override;
237
239 QCocoaNSWindow *m_nsWindow = nil;
240
243
245
246 bool m_initialized = false;
247 bool m_inSetVisible = false;
248 bool m_inSetGeometry = false;
249 bool m_inSetStyleMask = false;
250 bool m_inLiveResize = false;
251
253
259
263
264 static const int NoAlertRequest;
266
268
269 struct BorderRange {
270 BorderRange(quintptr i, int u, int l) : identifier(i), upper(u), lower(l) { }
272 int upper;
273 int lower;
274 bool operator<(BorderRange const& right) const {
275 return upper < right.upper;
276 }
277 };
278 QHash<quintptr, BorderRange> m_contentBorderAreas; // identifier -> uppper/lower
279 QHash<quintptr, bool> m_enabledContentBorderAreas; // identifier -> enabled state (true/false)
280
281 static inline id s_globalMouseMonitor = 0;
283
284#if QT_CONFIG(vulkan)
286#endif
287};
288
290
291#ifndef QT_NO_DEBUG_STREAM
292QDebug operator<<(QDebug debug, const QCocoaWindow *window);
293#endif
294
295QT_END_NAMESPACE
296
297#endif // QCOCOAWINDOW_H
static void clearCurrentThreadCocoaEventDispatcherInterruptFlag()
void * nativeResourceForWindow(const QByteArray &resourceString, QWindow *window) override
NativeResourceForIntegrationFunction nativeResourceFunctionForIntegration(const QByteArray &resource) override
void handleWindowStateChanged(HandleFlags flags=NoHandleFlags)
static void setupPopupMonitor()
void setContentBorderAreaEnabled(quintptr identifier, bool enable)
bool alwaysShowToolWindow() const
void setMenubar(QCocoaMenuBar *mb)
void toggleMaximized()
QWindow * childWindowAt(QPoint windowPoint)
void setAlertState(bool enabled) override
Reimplement this method to set whether the window demands attention (for example, by flashing the tas...
int m_registerTouchCount
Qt::WindowModality m_windowModality
QMargins safeAreaMargins() const override
The safe area margins of a window represent the area that is safe to place content within,...
void setWindowTitle(const QString &title) override
Reimplement to set the window title to title.
void setWindowCursor(NSCursor *cursor)
bool testContentBorderAreaPosition(int position) const
NSView * view() const
QRect normalGeometry() const override
the geometry of the window as it will appear when shown as a normal (not maximized or full screen) to...
QMacKeyValueObserver m_safeAreaInsetsObserver
bool updatesWithDisplayLink() const
bool m_inSetStyleMask
WId winId() const override
Reimplement in subclasses to return a handle to the native window.
bool isForeignWindow() const override
bool setMouseGrabEnabled(bool grab) override
QCocoaNSWindow * m_nsWindow
void propagateSizeHints() override
Reimplement to propagate the size hints of the QWindow.
void setOpacity(qreal level) override
Reimplement to be able to let Qt set the opacity level of a window.
QCocoaNSWindow * createNSWindow(bool shouldBePanel)
void requestActivateWindow() override
Reimplement to let Qt be able to request activation/focus for a window.
static id s_globalMouseMonitor
void setMask(const QRegion &region) override
Reimplement to be able to let Qt set the mask of a window.
QHash< quintptr, bool > m_enabledContentBorderAreas
void setFrameStrutEventsEnabled(bool enabled) override
Reimplement this method to set whether frame strut events should be sent to enabled.
void toggleFullScreen()
QCocoaMenuBar * menubar() const
void setWindowFilePath(const QString &filePath) override
Reimplement to set the window file path to filePath.
void setCocoaGeometry(const QRect &rect)
QRect geometry() const override
Returns the current geometry of a window.
NSInteger windowLevel(Qt::WindowFlags flags)
void setContentBorderEnabled(bool enable) override
bool isOpaque() const
void setParent(const QPlatformWindow *window) override
This function is called to enable native child window in QPA.
static void removePopupMonitor()
bool m_frameStrutEventsEnabled
bool setWindowModified(bool modified) override
Reimplement to be able to let Qt indicate that the window has been modified.
NSWindow * nativeWindow() const
void applyContentBorderThickness(NSWindow *window=nullptr)
void setWindowState(Qt::WindowStates state) override
Changes the state of the NSWindow, going in/out of minimize/zoomed/fullscreen.
void updateSafeAreaMarginsIfNeeded()
bool windowShouldClose()
void registerTouch(bool enable)
void updateTitleBarButtons(Qt::WindowFlags flags)
QMargins frameMargins() const override
void setGeometry(const QRect &rect) override
This function is called by Qt whenever a window is moved or resized using the QWindow API.
void updateNormalGeometry()
bool isFixedSize() const
bool isAlertState() const override
Reimplement this method return whether the window is in an alert state.
bool isExposed() const override
Returns if this window is exposed in the windowing system.
QFlatMap< quintptr, NSVisualEffectView * > m_effectViews
void deliverUpdateRequest() override
Delivers an QEvent::UpdateRequest event to the window.
bool isEmbedded() const override
Returns true if the window is a child of a non-Qt window.
void setWindowFlags(Qt::WindowFlags flags) override
Requests setting the window flags of this surface to flags.
void lower() override
Reimplement to be able to let Qt lower windows to the bottom of the desktop.
void manageVisualEffectArea(quintptr identifier, const QRect &rect, NSVisualEffectMaterial material, NSVisualEffectBlendingMode blendMode, NSVisualEffectState activationState) override
bool shouldRefuseKeyWindowAndFirstResponder()
void requestUpdate() override
Requests an QEvent::UpdateRequest event.
static QPointer< QCocoaWindow > s_windowUnderMouse
static void closeAllPopups()
void setWindowIcon(const QIcon &icon) override
Reimplement to set the window icon to icon.
bool isContentView() const
Checks if the window is the content view of its immediate NSWindow.
NSInteger m_alertRequest
QRect m_normalGeometry
void recreateWindowIfNeeded()
Recreates (or removes) the NSWindow for this QWindow, if needed.
void handleGeometryChange()
bool m_drawContentBorderGradient
bool m_resizableTransientParent
NSUInteger windowStyleMask(Qt::WindowFlags flags)
QCocoaMenuBar * m_menubar
void setVisible(bool visible) override
Reimplemented in subclasses to show the surface if visible is true, and hide it if visible is false.
bool startSystemMove() override
Reimplement this method to start a system move operation if the system supports it and return true to...
static const int NoAlertRequest
CALayer * contentLayer() const override
QRect m_exposedRect
bool windowEvent(QEvent *event) override
Reimplement this method to be able to do any platform specific event handling.
void raise() override
Reimplement to be able to let Qt raise windows to the top of the desktop.
void windowWillZoom()
bool setKeyboardGrabEnabled(bool grab) override
bool windowIsPopupType(Qt::WindowType type=Qt::Widget) const
void registerContentBorderArea(quintptr identifier, int upper, int lower)
void setEmbeddedInForeignView()
void applyWindowState(Qt::WindowStates newState)
QPoint bottomLeftClippedByNSWindowOffset() const override
bool isTransitioningToFullScreen() const
QHash< quintptr, BorderRange > m_contentBorderAreas
Qt::WindowStates windowState() const
qreal devicePixelRatio() const override
Reimplement this function in subclass to return the device pixel ratio for the window.
void initialize() override
Called as part of QWindow::create(), after constructing the window.
Qt::WindowStates m_lastReportedWindowState
QSurfaceFormat format() const override
Returns the actual surface format of the window.
bool frameStrutEventsEnabled() const override
Reimplement this method to return whether frame strut events are enabled.
static id s_applicationActivationObserver
NSView * m_view
void handleExposeEvent(const QRegion &region)
QMargins m_lastReportedSafeAreaMargins
unsigned long NSUInteger
long NSInteger
Q_FORWARD_DECLARE_OBJC_CLASS(NSVisualEffectView)
Q_FORWARD_DECLARE_OBJC_CLASS(NSView)
Q_FORWARD_DECLARE_OBJC_CLASS(NSCursor)
const NSNotificationName QCocoaWindowWillReleaseQNSViewNotification
#define Q_NOTIFICATION_HANDLER(notification)
bool operator<(BorderRange const &right) const
BorderRange(quintptr i, int u, int l)