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
123 Q_NOTIFICATION_HANDLER(NSViewFrameDidChangeNotification) void viewDidChangeFrame();
124 Q_NOTIFICATION_HANDLER(NSViewGlobalFrameDidChangeNotification) void viewDidChangeGlobalFrame();
125
126 Q_NOTIFICATION_HANDLER(NSWindowDidMoveNotification) void windowDidMove();
127 Q_NOTIFICATION_HANDLER(NSWindowDidResizeNotification) void windowDidResize();
128 Q_NOTIFICATION_HANDLER(NSWindowWillStartLiveResizeNotification) void windowWillStartLiveResize();
129 Q_NOTIFICATION_HANDLER(NSWindowDidEndLiveResizeNotification) void windowDidEndLiveResize();
130 Q_NOTIFICATION_HANDLER(NSWindowDidBecomeKeyNotification) void windowDidBecomeKey();
131 Q_NOTIFICATION_HANDLER(NSWindowDidResignKeyNotification) void windowDidResignKey();
132 Q_NOTIFICATION_HANDLER(NSWindowDidMiniaturizeNotification) void windowDidMiniaturize();
133 Q_NOTIFICATION_HANDLER(NSWindowDidDeminiaturizeNotification) void windowDidDeminiaturize();
134 Q_NOTIFICATION_HANDLER(NSWindowWillEnterFullScreenNotification) void windowWillEnterFullScreen();
135 Q_NOTIFICATION_HANDLER(NSWindowDidEnterFullScreenNotification) void windowDidEnterFullScreen();
136 Q_NOTIFICATION_HANDLER(NSWindowWillExitFullScreenNotification) void windowWillExitFullScreen();
137 Q_NOTIFICATION_HANDLER(NSWindowDidExitFullScreenNotification) void windowDidExitFullScreen();
138 Q_NOTIFICATION_HANDLER(NSWindowDidOrderOnScreenAndFinishAnimatingNotification) void windowDidOrderOnScreen();
139 Q_NOTIFICATION_HANDLER(NSWindowDidOrderOffScreenNotification) void windowDidOrderOffScreen();
140 Q_NOTIFICATION_HANDLER(NSWindowDidChangeOcclusionStateNotification) void windowDidChangeOcclusionState();
141 Q_NOTIFICATION_HANDLER(NSWindowDidChangeScreenNotification) void windowDidChangeScreen();
142
143 void windowWillZoom();
144
145 bool windowShouldClose();
146 bool windowIsPopupType(Qt::WindowType type = Qt::Widget) const;
147
148 NSInteger windowLevel(Qt::WindowFlags flags);
149 NSUInteger windowStyleMask(Qt::WindowFlags flags);
150 void updateTitleBarButtons(Qt::WindowFlags flags);
151 bool isFixedSize() const;
152
153 bool setWindowModified(bool modified) override;
154
155 void setFrameStrutEventsEnabled(bool enabled) override;
158
159 void setMenubar(QCocoaMenuBar *mb);
160 QCocoaMenuBar *menubar() const;
161
162 void setWindowCursor(NSCursor *cursor);
163
164 void registerTouch(bool enable);
165
166 void registerContentBorderArea(quintptr identifier, int upper, int lower);
167 void setContentBorderAreaEnabled(quintptr identifier, bool enable);
168 void setContentBorderEnabled(bool enable) override;
169 bool testContentBorderAreaPosition(int position) const;
170 void applyContentBorderThickness(NSWindow *window = nullptr);
171
172 qreal devicePixelRatio() const override;
173 QWindow *childWindowAt(QPoint windowPoint);
175
176 bool windowEvent(QEvent *event) override;
177
179
181
192 Q_DECLARE_FLAGS(RecreationReasons, RecreationReason)
194
196
197 QPoint mapToGlobal(const QPoint &pos) const override;
198 QPoint mapFromGlobal(const QPoint &pos) const override;
199
200 // Maps between Qt coordinates and potentially non-flipped NSView coordinates
201 static CGPoint mapToNative(const QPointF &pos, NSView *referenceView);
202 static CGRect mapToNative(const QRectF &rect, NSView *referenceView);
203 static QPointF mapFromNative(CGPoint pos, NSView *referenceView);
204 static QRectF mapFromNative(CGRect rect, NSView *referenceView);
205
206protected:
207 QCocoaNSWindow *createNSWindow(bool shouldBePanel);
208
209 Qt::WindowStates windowState() const;
210 void applyWindowState(Qt::WindowStates newState);
211 void toggleMaximized();
212 void toggleFullScreen();
213 bool isTransitioningToFullScreen() const;
214
216
217// private:
218public: // for QNSView
219 friend class QCocoaBackingStore;
221
222 bool isContentView() const;
223
224 bool alwaysShowToolWindow() const;
225
230
233 void handleExposeEvent(const QRegion &region);
234
235 static void closeAllPopups();
236 static void setupPopupMonitor();
237 static void removePopupMonitor();
238
239 CALayer *contentLayer() const override;
240
241 void manageVisualEffectArea(quintptr identifier, const QRect &rect,
242 NSVisualEffectMaterial material, NSVisualEffectBlendingMode blendMode,
243 NSVisualEffectState activationState) override;
245
247 QCocoaNSWindow *m_nsWindow = nil;
248
251
253
254 bool m_initialized = false;
255 bool m_inSetVisible = false;
256 bool m_inSetGeometry = false;
257 bool m_inSetStyleMask = false;
258 bool m_inLiveResize = false;
259
261
267
271
272 static const int NoAlertRequest;
274
276
277 struct BorderRange {
278 BorderRange(quintptr i, int u, int l) : identifier(i), upper(u), lower(l) { }
280 int upper;
281 int lower;
282 bool operator<(BorderRange const& right) const {
283 return upper < right.upper;
284 }
285 };
286 QHash<quintptr, BorderRange> m_contentBorderAreas; // identifier -> uppper/lower
287 QHash<quintptr, bool> m_enabledContentBorderAreas; // identifier -> enabled state (true/false)
288
289 static inline id s_globalMouseMonitor = 0;
291
292#if QT_CONFIG(vulkan)
294#endif
295};
296
298
299#ifndef QT_NO_DEBUG_STREAM
300QDebug operator<<(QDebug debug, const QCocoaWindow *window);
301#endif
302
303QT_END_NAMESPACE
304
305#endif // QCOCOAWINDOW_H
static void clearCurrentThreadCocoaEventDispatcherInterruptFlag()
void * nativeResourceForWindow(const QByteArray &resourceString, QWindow *window) override
NativeResourceForIntegrationFunction nativeResourceFunctionForIntegration(const QByteArray &resource) override
QPoint mapToGlobal(const QPoint &pos) const override
Translates the window coordinate pos to global screen coordinates using native methods.
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.
QPoint mapFromGlobal(const QPoint &pos) const override
Translates the global screen coordinate pos to window coordinates using native methods.
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()
static CGPoint mapToNative(const QPointF &pos, NSView *referenceView)
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.
static CGRect mapToNative(const QRectF &rect, NSView *referenceView)
bool isContentView() const
Checks if the window is the content view of its immediate NSWindow.
static QPointF mapFromNative(CGPoint pos, NSView *referenceView)
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 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
\inmodule QtCore\reentrant
Definition qpoint.h:232
Combined button and popup list for selecting options.
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)