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
17
18template<typename T>
19class QOhosPropertyDescriptor
20{
21public:
22 explicit QOhosPropertyDescriptor(const char *name);
23
24 const char *name() const;
25
26private:
27 const char *m_name;
28};
29
30template<typename T>
31QOhosPropertyDescriptor<T>::QOhosPropertyDescriptor(const char *name)
32 : m_name(name)
33{
34 qMetaTypeId<T>();
35}
36
37template<typename T>
38const char *QOhosPropertyDescriptor<T>::name() const
39{
40 return m_name;
41}
42
43template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
44void setQOhosPropertyOnQObject(QObject *qObject, T propertyValue);
45
46template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
47std::optional<T> tryGetQOhosPropertyFromQObject(QObject *qObject);
48
50{
51public:
52 explicit QOhosPropertiesStore(QObject *qObject);
53
54 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
55 std::optional<T> tryGetProperty() const;
56
57 void notifyPropertyWrite(const QByteArray &propertyName);
58
59 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
60 std::shared_ptr<void> addPropertyWriteCallback(QOhosConsumer<T> propertyWriteCallback);
61
62private:
63 template<typename T>
64 class PropertyWriteConsumersStore
65 {
66 public:
67 void notify(T value);
68 std::shared_ptr<void> addConsumer(QOhosConsumer<T> consumer);
69
70 private:
71 std::vector<std::weak_ptr<QOhosConsumer<T>>> m_consumers;
72 };
73
74 QObject *objectOrFail() const;
75
76 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
77 void notifyPropertyWriteInternal();
78
79 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
80 std::shared_ptr<PropertyWriteConsumersStore<T>> getOrCreatePropertyWriteConsumersStore();
81
82 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
83 std::shared_ptr<PropertyWriteConsumersStore<T>> getPropertyWriteConsumersStoreOrNull();
84
85 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
86 void registerPropertyNotifierIfMissing();
87
88 QPointer<QObject> m_object;
89 std::map<const void *, std::shared_ptr<void>> m_propertyWriteConsumersStoresMap;
91};
92
94{
95public:
97
98 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
99 std::optional<T> tryGetProperty() const;
100
101 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
102 std::shared_ptr<void> addPropertyWriteCallback(QOhosConsumer<T> propertyWriteCallback);
103
104private:
105 QOhosPropertiesStore *m_store;
106};
107
109
110template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
111const char *qObjectOhosPropertyName();
112
113template<typename T>
114std::optional<T> tryMapFromQVariant(QVariant variant);
115
116}
117
118template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
119void setQOhosPropertyOnQObject(QObject *qObject, T propertyValue)
120{
121 qObject->setProperty(
122 qohoswindowproperty_h_detail::qObjectOhosPropertyName<T, propertyPtr>(),
123 QVariant::fromValue(propertyValue));
124}
125
126template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
127std::optional<T> tryGetQOhosPropertyFromQObject(QObject *qObject)
128{
129 const char *propertyName = qohoswindowproperty_h_detail::qObjectOhosPropertyName<T, propertyPtr>();
130 const auto propertyTypeId = qMetaTypeId<T>();
131 auto value = qObject->property(propertyName);
132
133 if (!value.isValid())
134 return {};
135
136 auto optValue = qohoswindowproperty_h_detail::tryMapFromQVariant<T>(value);
137 if (!optValue.has_value()) {
138 qOhosPrintfError(
139 "Property \"%s\" type mismatch, expected: %d, got: %d",
140 propertyName, propertyTypeId, value.userType());
141 }
142
143 return optValue;
144}
145
148{
149}
150
151template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
153{
154 return tryGetQOhosPropertyFromQObject<T, propertyPtr>(objectOrFail());
155}
156
157inline void QOhosPropertiesStore::notifyPropertyWrite(const QByteArray &propertyName)
158{
159 auto notifierIt = m_propertyNameToNotifierMap.find(propertyName);
160 if (notifierIt != m_propertyNameToNotifierMap.end())
161 notifierIt->second();
162}
163
164template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
165std::shared_ptr<void> QOhosPropertiesStore::addPropertyWriteCallback(QOhosConsumer<T> propertyWriteCallback)
166{
167 registerPropertyNotifierIfMissing<T, propertyPtr>();
168 return getOrCreatePropertyWriteConsumersStore<T, propertyPtr>()->addConsumer(std::move(propertyWriteCallback));
169}
170
171template<typename T>
172void QOhosPropertiesStore::PropertyWriteConsumersStore<T>::notify(T value)
173{
174 auto weakConsumersPendingNotify = std::exchange(m_consumers, {});
175 std::vector<std::weak_ptr<QOhosConsumer<T>>> weakConsumersToAdd;
176 for (auto &weakConsumerPendingNotify: weakConsumersPendingNotify) {
177 auto sharedConsumer = weakConsumerPendingNotify.lock();
178 if (sharedConsumer) {
179 weakConsumersToAdd.push_back(weakConsumerPendingNotify);
180 (*sharedConsumer)(value);
181 }
182 }
183 m_consumers.insert(m_consumers.begin(), weakConsumersToAdd.begin(), weakConsumersToAdd.end());
184}
185
186template<typename T>
187std::shared_ptr<void> QOhosPropertiesStore::PropertyWriteConsumersStore<T>::addConsumer(QOhosConsumer<T> consumer)
188{
189 auto sharedConsumer = QtOhos::moveToSharedPtr(std::move(consumer));
190 m_consumers.emplace_back(sharedConsumer);
191 return sharedConsumer;
192}
193
194inline QObject *QOhosPropertiesStore::objectOrFail() const
195{
196 QObject *object = m_object;
197 if (object == nullptr)
198 qOhosReportFatalErrorAndAbort("%s: m_object was null", Q_FUNC_INFO);
199 return object;
200}
201
202template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
203void QOhosPropertiesStore::notifyPropertyWriteInternal()
204{
205 auto propertyWriteConsumersStore = getPropertyWriteConsumersStoreOrNull<T, propertyPtr>();
206 if (!propertyWriteConsumersStore)
207 return;
208
209 auto propertyValue = tryGetProperty<T, propertyPtr>();
210 if (propertyValue.has_value())
211 propertyWriteConsumersStore->notify(propertyValue.value());
212}
213
214template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
215std::shared_ptr<QOhosPropertiesStore::PropertyWriteConsumersStore<T>> QOhosPropertiesStore::getOrCreatePropertyWriteConsumersStore()
216{
217 auto propertyWriteConsumersStore = getPropertyWriteConsumersStoreOrNull<T, propertyPtr>();
218 if (!propertyWriteConsumersStore) {
219 propertyWriteConsumersStore = std::make_shared<PropertyWriteConsumersStore<T>>();
220 m_propertyWriteConsumersStoresMap.insert({propertyPtr, propertyWriteConsumersStore});
221 }
222
223 return propertyWriteConsumersStore;
224}
225
226template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
227std::shared_ptr<QOhosPropertiesStore::PropertyWriteConsumersStore<T>> QOhosPropertiesStore::getPropertyWriteConsumersStoreOrNull()
228{
229 auto propertyWriteConsumersStoresMapIt = m_propertyWriteConsumersStoresMap.find(propertyPtr);
230 if (propertyWriteConsumersStoresMapIt == m_propertyWriteConsumersStoresMap.end())
231 return nullptr;
232 return std::static_pointer_cast<PropertyWriteConsumersStore<T>>(propertyWriteConsumersStoresMapIt->second);
233}
234
235template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
236void QOhosPropertiesStore::registerPropertyNotifierIfMissing()
237{
238 auto propertyName = QByteArray(qohoswindowproperty_h_detail::qObjectOhosPropertyName<T, propertyPtr>());
239 auto propertyNotifierIt = m_propertyNameToNotifierMap.find(propertyName);
240 if (propertyNotifierIt == m_propertyNameToNotifierMap.end()) {
241 m_propertyNameToNotifierMap.emplace(
242 propertyName,
243 [this]() {
244 this->notifyPropertyWriteInternal<T, propertyPtr>();
245 });
246 }
247}
248
250 : m_store(&store)
251{
252}
253
254template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
256{
257 return m_store->tryGetProperty<T, propertyPtr>();
258}
259
260template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
261std::shared_ptr<void> QOhosPropertiesProvider::addPropertyWriteCallback(QOhosConsumer<T> propertyWriteCallback)
262{
263 return m_store->addPropertyWriteCallback<T, propertyPtr>(std::move(propertyWriteCallback));
264}
265
267
268template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
270{
271 return propertyPtr->name();
272}
273
274template<typename T>
275std::optional<T> tryMapFromQVariant(QVariant variant)
276{
277 return qMetaTypeId<T>() == variant.userType()
278 ? std::optional(variant.value<T>())
279 : std::nullopt;
280}
281
282template<>
284{
285 if (!variant.canConvert<QWindow *>())
286 return {};
287 return !variant.isNull()
288 ? std::optional(reinterpret_cast<QWindow *>(variant.value<QObject*>()))
289 : std::optional<QWindow *>(nullptr);
290}
291
292}
293
294QT_END_NAMESPACE
295
296#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)