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 <qohosplugincore.h>
8#include <qohosutils.h>
9#include <QtCore/qpointer.h>
10#include <QtCore/private/qohoscommon_p.h>
11#include <QtCore/qglobal.h>
12#include <QtCore/qvariant.h>
13#include <QtGui/qwindow.h>
14#include <memory>
15#include <utility>
16
18
19template<typename T>
20class QOhosPropertyDescriptor
21{
22public:
23 constexpr QOhosPropertyDescriptor();
24};
25
26template<typename T>
27constexpr QOhosPropertyDescriptor<T>::QOhosPropertyDescriptor()
28{
29 qMetaTypeId<T>();
30}
31
32template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
33void setQOhosPropertyOnQObject(QObject *qObject, T propertyValue);
34
35template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
37
39{
40public:
41 explicit QOhosPropertiesStore(QObject *qObject);
42
43 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
45
46 void notifyPropertyWrite(const QByteArray &propertyName);
47
48 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
49 std::shared_ptr<void> addPropertyWriteCallback(QOhosConsumer<T> propertyWriteCallback);
50
51private:
52 template<typename T>
53 class PropertyWriteConsumersStore
54 {
55 public:
56 void notify(T value);
57 std::shared_ptr<void> addConsumer(QOhosConsumer<T> consumer);
58
59 private:
60 std::vector<std::weak_ptr<QOhosConsumer<T>>> m_consumers;
61 };
62
63 QObject *objectOrFail() const;
64
65 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
66 void notifyPropertyWriteInternal();
67
68 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
69 std::shared_ptr<PropertyWriteConsumersStore<T>> getOrCreatePropertyWriteConsumersStore();
70
71 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
72 std::shared_ptr<PropertyWriteConsumersStore<T>> getPropertyWriteConsumersStoreOrNull();
73
74 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
75 void registerPropertyNotifierIfMissing();
76
77 QPointer<QObject> m_object;
78 std::map<const void *, std::shared_ptr<void>> m_propertyWriteConsumersStoresMap;
80};
81
83{
84public:
86
87 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
89
90 template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
91 std::shared_ptr<void> addPropertyWriteCallback(QOhosConsumer<T> propertyWriteCallback);
92
93private:
94 QOhosPropertiesStore *m_store;
95};
96
98
99template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
100const char *qObjectOhosPropertyName();
101
102template<typename T>
103QOhosOptional<T> tryMapFromQVariant(QVariant variant);
104
105}
106
107template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
108void setQOhosPropertyOnQObject(QObject *qObject, T propertyValue)
109{
110 qObject->setProperty(
111 qohoswindowproperty_h_detail::qObjectOhosPropertyName<T, propertyPtr>(),
112 QVariant::fromValue(propertyValue));
113}
114
115template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
117{
118 const char *propertyName = qohoswindowproperty_h_detail::qObjectOhosPropertyName<T, propertyPtr>();
119 const auto propertyTypeId = qMetaTypeId<T>();
120 auto value = qObject->property(propertyName);
121
122 if (!value.isValid())
124
125 auto optValue = qohoswindowproperty_h_detail::tryMapFromQVariant<T>(value);
126 if (!optValue.hasValue()) {
127 qOhosPrintfError(
128 "Property \"%s\" type mismatch, expected: %d, got: %d",
129 propertyName, propertyTypeId, value.userType());
130 }
131
132 return optValue;
133}
134
137{
138}
139
140template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
142{
143 return tryGetQOhosPropertyFromQObject<T, propertyPtr>(objectOrFail());
144}
145
146inline void QOhosPropertiesStore::notifyPropertyWrite(const QByteArray &propertyName)
147{
148 auto notifierIt = m_propertyNameToNotifierMap.find(propertyName);
149 if (notifierIt != m_propertyNameToNotifierMap.end())
150 notifierIt->second();
151}
152
153template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
154std::shared_ptr<void> QOhosPropertiesStore::addPropertyWriteCallback(QOhosConsumer<T> propertyWriteCallback)
155{
156 registerPropertyNotifierIfMissing<T, propertyPtr>();
157 return getOrCreatePropertyWriteConsumersStore<T, propertyPtr>()->addConsumer(std::move(propertyWriteCallback));
158}
159
160template<typename T>
161void QOhosPropertiesStore::PropertyWriteConsumersStore<T>::notify(T value)
162{
163 auto weakConsumersPendingNotify = std::exchange(m_consumers, {});
164 std::vector<std::weak_ptr<QOhosConsumer<T>>> weakConsumersToAdd;
165 for (auto &weakConsumerPendingNotify: weakConsumersPendingNotify) {
166 auto sharedConsumer = weakConsumerPendingNotify.lock();
167 if (sharedConsumer) {
168 weakConsumersToAdd.push_back(weakConsumerPendingNotify);
169 (*sharedConsumer)(value);
170 }
171 }
172 m_consumers.insert(m_consumers.begin(), weakConsumersToAdd.begin(), weakConsumersToAdd.end());
173}
174
175template<typename T>
176std::shared_ptr<void> QOhosPropertiesStore::PropertyWriteConsumersStore<T>::addConsumer(QOhosConsumer<T> consumer)
177{
178 auto sharedConsumer = QtOhos::moveToSharedPtr(std::move(consumer));
179 m_consumers.emplace_back(sharedConsumer);
180 return sharedConsumer;
181}
182
183inline QObject *QOhosPropertiesStore::objectOrFail() const
184{
185 QObject *object = m_object;
186 if (object == nullptr)
187 qOhosReportFatalErrorAndAbort("%s: m_object was null", Q_FUNC_INFO);
188 return object;
189}
190
191template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
192void QOhosPropertiesStore::notifyPropertyWriteInternal()
193{
194 auto propertyWriteConsumersStore = getPropertyWriteConsumersStoreOrNull<T, propertyPtr>();
195 if (!propertyWriteConsumersStore)
196 return;
197
198 auto propertyValue = tryGetProperty<T, propertyPtr>();
199 if (propertyValue.hasValue())
200 propertyWriteConsumersStore->notify(propertyValue.value());
201}
202
203template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
204std::shared_ptr<QOhosPropertiesStore::PropertyWriteConsumersStore<T>> QOhosPropertiesStore::getOrCreatePropertyWriteConsumersStore()
205{
206 auto propertyWriteConsumersStore = getPropertyWriteConsumersStoreOrNull<T, propertyPtr>();
207 if (!propertyWriteConsumersStore) {
208 propertyWriteConsumersStore = std::make_shared<PropertyWriteConsumersStore<T>>();
209 m_propertyWriteConsumersStoresMap.insert({propertyPtr, propertyWriteConsumersStore});
210 }
211
212 return propertyWriteConsumersStore;
213}
214
215template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
216std::shared_ptr<QOhosPropertiesStore::PropertyWriteConsumersStore<T>> QOhosPropertiesStore::getPropertyWriteConsumersStoreOrNull()
217{
218 auto propertyWriteConsumersStoresMapIt = m_propertyWriteConsumersStoresMap.find(propertyPtr);
219 if (propertyWriteConsumersStoresMapIt == m_propertyWriteConsumersStoresMap.end())
220 return nullptr;
221 return std::static_pointer_cast<PropertyWriteConsumersStore<T>>(propertyWriteConsumersStoresMapIt->second);
222}
223
224template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
225void QOhosPropertiesStore::registerPropertyNotifierIfMissing()
226{
227 auto propertyName = QByteArray(qohoswindowproperty_h_detail::qObjectOhosPropertyName<T, propertyPtr>());
228 auto propertyNotifierIt = m_propertyNameToNotifierMap.find(propertyName);
229 if (propertyNotifierIt == m_propertyNameToNotifierMap.end()) {
230 m_propertyNameToNotifierMap.emplace(
231 propertyName,
232 [this]() {
233 this->notifyPropertyWriteInternal<T, propertyPtr>();
234 });
235 }
236}
237
239 : m_store(&store)
240{
241}
242
243template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
245{
246 return m_store->tryGetProperty<T, propertyPtr>();
247}
248
249template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
250std::shared_ptr<void> QOhosPropertiesProvider::addPropertyWriteCallback(QOhosConsumer<T> propertyWriteCallback)
251{
252 return m_store->addPropertyWriteCallback<T, propertyPtr>(std::move(propertyWriteCallback));
253}
254
256
257template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
259{
260 return QtOhos::printfToString(
261 "_q_platform_ohos_%d_%p", qMetaTypeId<T>(), static_cast<const void *>(propertyPtr));
262}
263
264template<typename T, const QOhosPropertyDescriptor<T> *propertyPtr>
266{
267 static const std::string propertyName = makeQObjectOhosPropertyName<T, propertyPtr>();
268 return propertyName.c_str();
269}
270
271template<typename T>
273{
274 return qMetaTypeId<T>() == variant.userType()
275 ? makeQOhosOptional(variant.value<T>())
277}
278
279template<>
281{
282 if (!variant.canConvert<QWindow *>())
283 return makeEmptyQOhosOptional();
284 return !variant.isNull()
285 ? makeQOhosOptional(reinterpret_cast<QWindow *>(variant.value<QObject*>()))
286 : makeQOhosOptional<QWindow *>(nullptr);
287}
288
289}
290
291QT_END_NAMESPACE
292
293#endif
std::enable_if_t< qohosplugincore_h_detail::isQOhosOptional< QOhosInvokeResult< Func, T > >, QOhosInvokeResult< Func, T > > andThen(Func &&func) const
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
static void setWindowOrWidgetNativeNodeRenderFitPolicyHint(QObject *windowOrWidget, NativeNodeRenderFitPolicy renderFitPolicy)
bool mainWindowTagValueOrFalse() const
Qt::WindowStates windowStates() const
static QOhosOptional< T > tryGetWindowOrWidgetProperty(QObject *windowOrWidget)
void setDisplayIdFromOhos(QOhosOptional< QOhosDisplayInfo::JsDisplayId > displayId)
bool setMouseGrabEnabled(bool grab) override
static void setWindowKeepScreenOn(QObject *windowOrWidget, bool keepScreenOn)
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 void setSurfaceBackgroundColor(QObject *windowOrWidget, const QColor &color)
static void setSaturation(QObject *windowOrWidget, int saturation)
QOhosOptional< QOhosDisplayInfo::JsDisplayId > tryTakeLastRequestedDisplayId()
static std::shared_ptr< void > setSurfaceConsumer(QWindow *targetWindow, QObject *surfaceConsumerContext, std::function< void(QOhosOptional< void * >)> surfaceConsumer)
QOhosOptional< QCursor > m_cursor
static const QOhosPropertyDescriptor< int > windowSaturationProperty
static const QOhosPropertyDescriptor< int > windowContrastProperty
static QOhosPlatformWindow * fromQWindow(QWindow *window)
QOhosPropertiesProvider propertiesProvider()
static const QOhosPropertyDescriptor< bool > floatWindowTagProperty
bool isWindowBeingClosedOrDestroyed(QWindow *window) const
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 void tagWindowOrWidgetAsSubWindowOf(QObject *windowOrWidgetToTag, QWindow *targetMainWindow)
virtual void onWindowFlagsChanged(Qt::WindowFlags previousWindowFlags, Qt::WindowFlags currentWindowFlags)
static const QOhosPropertyDescriptor< NativeNodeRenderFitPolicy > nativeNodeRenderFitPolicyHintProperty
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 void setWindowCornerRadius(QObject *windowOrWidget, double radius)
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)
static void setBrightness(QObject *windowOrWidget, int brightness)
bool shouldDisplayAsOhosWindow() const
std::unique_ptr< QMargins > m_optFrameMargins
void setWindowGeometryFromOhos(const QRect &nativeWindowDrawGeometry)
ScreenChangeResult tryChangeScreen(QOhosPlatformScreen *screen)
QMargins frameMargins() const override
void initialize() override
Called as part of QWindow::create(), after constructing the window.
QPlatformScreen * screen() const override
static void setContrast(QObject *windowOrWidget, int contrast)
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
static void setWindowPrivacyMode(QObject *window, bool privacyModeEnabled)
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)
QOhosOptional< T > tryGetProperty() const
std::shared_ptr< void > addPropertyWriteCallback(QOhosConsumer< T > propertyWriteCallback)
QOhosPropertiesStore(QObject *qObject)
std::shared_ptr< void > addPropertyWriteCallback(QOhosConsumer< T > propertyWriteCallback)
void notifyPropertyWrite(const QByteArray &propertyName)
QOhosOptional< T > tryGetProperty() const
bool sendEvent(WindowSystemEvent *event) override
Combined button and popup list for selecting options.
QOhosView * mapQWindowToViewOrNull(QWindow *window)
QOhosOptional< T > tryMapFromQVariant(QVariant variant)
std::shared_ptr< QWindowSystemEventHandler > makeApplicationStateTracker()
QT_END_NAMESPACE Q_DECLARE_METATYPE(QT_PREPEND_NAMESPACE(QOhosPlatformWindow::NativeNodeRenderFitPolicy))
QOhosOptional< void > makeEmptyQOhosOptional()
void setQOhosPropertyOnQObject(QObject *qObject, T propertyValue)
QOhosOptional< T > tryGetQOhosPropertyFromQObject(QObject *qObject)