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
qohoswindowproperty.h
Go to the documentation of this file.
1// Copyright (C) 2025 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#ifndef QOHOSWINDOWPROPERTY_H
5#define QOHOSWINDOWPROPERTY_H
6
7#include <qohosutils.h>
8#include <QtCore/qpointer.h>
9#include <QtCore/private/qohoscommon_p.h>
10#include <QtCore/qglobal.h>
11#include <QtCore/qvariant.h>
12#include <QtGui/qwindow.h>
13#include <memory>
14#include <optional>
15#include <utility>
16
18
19template<typename T>
20class QOhosPropertyDescriptor
21{
22public:
23 explicit QOhosPropertyDescriptor(const char *name);
24
25 const char *name() const;
26
27private:
28 const char *m_name;
29};
30
31template<typename T>
32QOhosPropertyDescriptor<T>::QOhosPropertyDescriptor(const char *name)
33 : m_name(name)
34{
35 qMetaTypeId<T>();
36}
37
38template<typename T>
39const char *QOhosPropertyDescriptor<T>::name() const
40{
41 return m_name;
42}
43
44template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
45void setQOhosPropertyOnQObject(QObject *qObject, T propertyValue);
46
47template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
48std::optional<T> tryGetQOhosPropertyFromQObject(QObject *qObject);
49
51{
52public:
53 explicit QOhosPropertiesStore(QObject *qObject);
54
55 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
56 std::optional<T> tryGetProperty() const;
57
58 void notifyPropertyWrite(const QByteArray &propertyName);
59
60 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
61 std::shared_ptr<void> addPropertyWriteCallback(QOhosConsumer<T> propertyWriteCallback);
62
63private:
64 template<typename T>
65 class PropertyWriteConsumersStore
66 {
67 public:
68 void notify(T value);
69 std::shared_ptr<void> addConsumer(QOhosConsumer<T> consumer);
70
71 private:
72 std::vector<std::weak_ptr<QOhosConsumer<T>>> m_consumers;
73 };
74
75 QObject *objectOrFail() const;
76
77 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
78 void notifyPropertyWriteInternal();
79
80 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
81 std::shared_ptr<PropertyWriteConsumersStore<T>> getOrCreatePropertyWriteConsumersStore();
82
83 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
84 std::shared_ptr<PropertyWriteConsumersStore<T>> getPropertyWriteConsumersStoreOrNull();
85
86 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
87 void registerPropertyNotifierIfMissing();
88
89 QPointer<QObject> m_object;
90 std::map<const void *, std::shared_ptr<void>> m_propertyWriteConsumersStoresMap;
92};
93
95{
96public:
98
99 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
100 std::optional<T> tryGetProperty() const;
101
102 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
103 std::shared_ptr<void> addPropertyWriteCallback(QOhosConsumer<T> propertyWriteCallback);
104
105private:
106 QOhosPropertiesStore *m_store;
107};
108
110
111template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
112const char *qObjectOhosPropertyName();
113
114template<typename T>
115std::optional<T> tryMapFromQVariant(QVariant variant);
116
117}
118
119template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
120void setQOhosPropertyOnQObject(QObject *qObject, T propertyValue)
121{
122 qObject->setProperty(
123 qohoswindowproperty_h_detail::qObjectOhosPropertyName<T, propertyPtr>(),
124 QVariant::fromValue(propertyValue));
125}
126
127template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
128std::optional<T> tryGetQOhosPropertyFromQObject(QObject *qObject)
129{
130 const char *propertyName = qohoswindowproperty_h_detail::qObjectOhosPropertyName<T, propertyPtr>();
131 const auto propertyTypeId = qMetaTypeId<T>();
132 auto value = qObject->property(propertyName);
133
134 if (!value.isValid())
135 return {};
136
137 auto optValue = qohoswindowproperty_h_detail::tryMapFromQVariant<T>(value);
138 if (!optValue.has_value()) {
139 qOhosPrintfError(
140 "Property \"%s\" type mismatch, expected: %d, got: %d",
141 propertyName, propertyTypeId, value.userType());
142 }
143
144 return optValue;
145}
146
149{
150}
151
152template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
154{
155 return tryGetQOhosPropertyFromQObject<T, propertyPtr>(objectOrFail());
156}
157
158inline void QOhosPropertiesStore::notifyPropertyWrite(const QByteArray &propertyName)
159{
160 auto notifierIt = m_propertyNameToNotifierMap.find(propertyName);
161 if (notifierIt != m_propertyNameToNotifierMap.end())
162 notifierIt->second();
163}
164
165template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
166std::shared_ptr<void> QOhosPropertiesStore::addPropertyWriteCallback(QOhosConsumer<T> propertyWriteCallback)
167{
168 registerPropertyNotifierIfMissing<T, propertyPtr>();
169 return getOrCreatePropertyWriteConsumersStore<T, propertyPtr>()->addConsumer(std::move(propertyWriteCallback));
170}
171
172template<typename T>
173void QOhosPropertiesStore::PropertyWriteConsumersStore<T>::notify(T value)
174{
175 auto weakConsumersPendingNotify = std::exchange(m_consumers, {});
176 std::vector<std::weak_ptr<QOhosConsumer<T>>> weakConsumersToAdd;
177 for (auto &weakConsumerPendingNotify: weakConsumersPendingNotify) {
178 auto sharedConsumer = weakConsumerPendingNotify.lock();
179 if (sharedConsumer) {
180 weakConsumersToAdd.push_back(weakConsumerPendingNotify);
181 (*sharedConsumer)(value);
182 }
183 }
184 m_consumers.insert(m_consumers.begin(), weakConsumersToAdd.begin(), weakConsumersToAdd.end());
185}
186
187template<typename T>
188std::shared_ptr<void> QOhosPropertiesStore::PropertyWriteConsumersStore<T>::addConsumer(QOhosConsumer<T> consumer)
189{
190 auto sharedConsumer = QtOhos::moveToSharedPtr(std::move(consumer));
191 m_consumers.emplace_back(sharedConsumer);
192 return sharedConsumer;
193}
194
195inline QObject *QOhosPropertiesStore::objectOrFail() const
196{
197 QObject *object = m_object;
198 if (object == nullptr)
199 qOhosReportFatalErrorAndAbort("%s: m_object was null", Q_FUNC_INFO);
200 return object;
201}
202
203template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
204void QOhosPropertiesStore::notifyPropertyWriteInternal()
205{
206 auto propertyWriteConsumersStore = getPropertyWriteConsumersStoreOrNull<T, propertyPtr>();
207 if (!propertyWriteConsumersStore)
208 return;
209
210 auto propertyValue = tryGetProperty<T, propertyPtr>();
211 if (propertyValue.has_value())
212 propertyWriteConsumersStore->notify(propertyValue.value());
213}
214
215template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
216std::shared_ptr<QOhosPropertiesStore::PropertyWriteConsumersStore<T>> QOhosPropertiesStore::getOrCreatePropertyWriteConsumersStore()
217{
218 auto propertyWriteConsumersStore = getPropertyWriteConsumersStoreOrNull<T, propertyPtr>();
219 if (!propertyWriteConsumersStore) {
220 propertyWriteConsumersStore = std::make_shared<PropertyWriteConsumersStore<T>>();
221 m_propertyWriteConsumersStoresMap.insert({propertyPtr, propertyWriteConsumersStore});
222 }
223
224 return propertyWriteConsumersStore;
225}
226
227template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
228std::shared_ptr<QOhosPropertiesStore::PropertyWriteConsumersStore<T>> QOhosPropertiesStore::getPropertyWriteConsumersStoreOrNull()
229{
230 auto propertyWriteConsumersStoresMapIt = m_propertyWriteConsumersStoresMap.find(propertyPtr);
231 if (propertyWriteConsumersStoresMapIt == m_propertyWriteConsumersStoresMap.end())
232 return nullptr;
233 return std::static_pointer_cast<PropertyWriteConsumersStore<T>>(propertyWriteConsumersStoresMapIt->second);
234}
235
236template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
237void QOhosPropertiesStore::registerPropertyNotifierIfMissing()
238{
239 auto propertyName = QByteArray(qohoswindowproperty_h_detail::qObjectOhosPropertyName<T, propertyPtr>());
240 auto propertyNotifierIt = m_propertyNameToNotifierMap.find(propertyName);
241 if (propertyNotifierIt == m_propertyNameToNotifierMap.end()) {
242 m_propertyNameToNotifierMap.emplace(
243 propertyName,
244 [this]() {
245 this->notifyPropertyWriteInternal<T, propertyPtr>();
246 });
247 }
248}
249
251 : m_store(&store)
252{
253}
254
255template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
257{
258 return m_store->tryGetProperty<T, propertyPtr>();
259}
260
261template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
262std::shared_ptr<void> QOhosPropertiesProvider::addPropertyWriteCallback(QOhosConsumer<T> propertyWriteCallback)
263{
264 return m_store->addPropertyWriteCallback<T, propertyPtr>(std::move(propertyWriteCallback));
265}
266
268
269template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
271{
272 return propertyPtr->name();
273}
274
275template<typename T>
276std::optional<T> tryMapFromQVariant(QVariant variant)
277{
278 return qMetaTypeId<T>() == variant.userType()
279 ? std::optional(variant.value<T>())
280 : std::nullopt;
281}
282
283template<>
285{
286 if (!variant.canConvert<QWindow *>())
287 return {};
288 return !variant.isNull()
289 ? std::optional(reinterpret_cast<QWindow *>(variant.value<QObject*>()))
290 : std::optional<QWindow *>(nullptr);
291}
292
293}
294
295QT_END_NAMESPACE
296
297#endif
static const QOhosPropertyDescriptor< bool > windowKeepScreenOnProperty
static const QOhosPropertyDescriptor< QColor > surfaceBackgroundColorProperty
void setWindowTitle(const QString &title) override
Reimplement to set the window title to title.
QOhosPlatformScreen * platformScreen() const
bool mainWindowTagValueOrFalse() const
Qt::WindowStates windowStates() const
std::optional< QCursor > m_cursor
void setDisplayIdFromOhos(std::optional< QOhosDisplayInfo::JsDisplayId > displayId)
bool setMouseGrabEnabled(bool grab) override
static std::optional< T > tryGetWindowOrWidgetProperty(QObject *windowOrWidget)
void setWindowStateFromOhos(Qt::WindowStates state)
static const QOhosPropertyDescriptor< int > windowBrightnessProperty
static const QOhosPropertyDescriptor< bool > mainWindowTagProperty
static const QOhosPropertyDescriptor< QWindow * > subWindowOfTagProperty
void setGeometry(const QRect &rect) override
This function is called by Qt whenever a window is moved or resized using the QWindow API.
bool setKeyboardGrabEnabled(bool grab) override
static const QOhosPropertyDescriptor< bool > windowPrivacyModeSettingProperty
bool windowEvent(QEvent *event) override
Reimplement this method to be able to do any platform specific event handling.
QtOhos::InternalWindowId m_windowId
static const QOhosPropertyDescriptor< double > windowCornerRadiusProperty
static bool isWindowBeingClosedOrDestroyed(QWindow *window)
static std::shared_ptr< void > setSurfaceConsumer(QWindow *targetWindow, QObject *surfaceConsumerContext, std::function< void(std::optional< void * >)> surfaceConsumer)
QRect lastRequestedWindowFrameGeometry() const
static const QOhosPropertyDescriptor< int > windowSaturationProperty
void setExposedFromOhos(bool exposed)
static const QOhosPropertyDescriptor< int > windowContrastProperty
static QOhosPlatformWindow * fromQWindow(QWindow *window)
QOhosPropertiesProvider propertiesProvider()
static const QOhosPropertyDescriptor< bool > floatWindowTagProperty
void setVisible(bool visible) override
Reimplemented in subclasses to show the surface if visible is true, and hide it if visible is false.
static void tagWindowOrWidgetAsFloatWindow(QObject *windowOrWidgetToTag, bool showAsFloatWindow)
static const QOhosPropertyDescriptor< bool > windowDragResizableProperty
static void tagWindowOrWidgetAsSubWindowOf(QObject *windowOrWidgetToTag, QWindow *targetMainWindow)
virtual void onWindowFlagsChanged(Qt::WindowFlags previousWindowFlags, Qt::WindowFlags currentWindowFlags)
QtOhos::InternalWindowId internalWindowId() const
static void tagWindowOrWidgetAsMainWindow(QObject *windowOrWidgetToTag, bool forceMainWindow)
Qt::WindowFlags m_windowFlags
void propagateSizeHints() override
Reimplement to propagate the size hints of the QWindow.
static QOhosPlatformWindow * fromQWindowOrNull(QWindow *window)
virtual QOhosSurface * ownedSurfaceOrNull() const
static QWindow * getWindowOrWidgetAsSubWindowOfTagValue(QObject *windowOrWidget)
virtual void onWindowStateChanged(Qt::WindowStates oldWindowState, Qt::WindowStates currentWindowState)
void setParent(const QPlatformWindow *newParent) override
This function is called to enable native child window in QPA.
QOhosPlatformWindow(QWindow *window)
void setCursor(const QCursor &cursor)
std::optional< Qt::WindowStates > m_lastWindowState
bool shouldDisplayAsOhosWindow() const
static const QOhosPropertyDescriptor< int > nativeNodeRenderFitPolicyHintProperty
std::unique_ptr< QMargins > m_optFrameMargins
void setWindowGeometryFromOhos(const QRect &nativeWindowDrawGeometry)
std::optional< double > windowId() const override
QMargins frameMargins() const override
void initialize() override
Called as part of QWindow::create(), after constructing the window.
QPlatformScreen * screen() const override
void setWindowFlags(Qt::WindowFlags flags) override
Requests setting the window flags of this surface to flags.
bool floatWindowTagValueOrFalse() const
void setWindowState(Qt::WindowStates state) override
Requests setting the window state of this surface to type.
void notifyInputSystemsWindowActiveStatusChanged(bool active)
static const QOhosPropertyDescriptor< bool > windowFixedSizeStateProperty
bool isExposed() const final
Returns if this window is exposed in the windowing system.
void requestActivateWindow() override
Reimplement to let Qt be able to request activation/focus for a window.
Qt::WindowStates m_windowState
DecorationPreset decorationPreset() const
QWindow * validSubWindowOfTagValueOrNull() const
Qt::WindowFlags windowFlags() const
static void setWindowOrWidgetProperty(QObject *windowOrWidget, T propertyValue)
bool shouldShowWindowWithoutActivating() const
virtual QOhosView * ownedViewOrNull() const
void setWindowMarginsFromOhos(const QMargins &margins)
static Qt::WindowFlags platformWindowFlagsForQWindow(QWindow *window)
QOhosPropertiesProvider(QOhosPropertiesStore &store)
std::optional< T > tryGetProperty() const
std::shared_ptr< void > addPropertyWriteCallback(QOhosConsumer< T > propertyWriteCallback)
QOhosPropertiesStore(QObject *qObject)
std::shared_ptr< void > addPropertyWriteCallback(QOhosConsumer< T > propertyWriteCallback)
std::optional< T > tryGetProperty() const
void notifyPropertyWrite(const QByteArray &propertyName)
bool sendEvent(WindowSystemEvent *event) override
Combined button and popup list for selecting options.
QOhosView * mapQWindowToViewOrNull(QWindow *window)
std::optional< T > tryMapFromQVariant(QVariant variant)
std::shared_ptr< QWindowSystemEventHandler > makeApplicationStateTracker()
void setQOhosPropertyOnQObject(QObject *qObject, T propertyValue)
std::optional< T > tryGetQOhosPropertyFromQObject(QObject *qObject)