4#ifndef QOHOSWINDOWPROPERTY_H
5#define QOHOSWINDOWPROPERTY_H
7#include <qohosplugincore.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>
20class QOhosPropertyDescriptor
23 constexpr QOhosPropertyDescriptor();
27constexpr QOhosPropertyDescriptor<T>::QOhosPropertyDescriptor()
32template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
35template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
43 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
48 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
53 class PropertyWriteConsumersStore
57 std::shared_ptr<
void> addConsumer(QOhosConsumer<T> consumer);
60 std::vector<std::weak_ptr<QOhosConsumer<T>>> m_consumers;
63 QObject *objectOrFail()
const;
65 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
66 void notifyPropertyWriteInternal();
68 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
69 std::shared_ptr<PropertyWriteConsumersStore<T>> getOrCreatePropertyWriteConsumersStore();
71 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
72 std::shared_ptr<PropertyWriteConsumersStore<T>> getPropertyWriteConsumersStoreOrNull();
74 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
75 void registerPropertyNotifierIfMissing();
77 QPointer<QObject> m_object;
78 std::map<
const void *, std::shared_ptr<
void>> m_propertyWriteConsumersStoresMap;
87 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
90 template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
99template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
107template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
110 qObject->setProperty(
112 QVariant::fromValue(propertyValue));
115template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
119 const auto propertyTypeId = qMetaTypeId<T>();
120 auto value = qObject->property(propertyName);
122 if (!value.isValid())
126 if (!optValue.hasValue()) {
128 "Property \"%s\" type mismatch, expected: %d, got: %d",
129 propertyName, propertyTypeId, value.userType());
140template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
143 return tryGetQOhosPropertyFromQObject<T, propertyPtr>(objectOrFail());
148 auto notifierIt = m_propertyNameToNotifierMap.find(propertyName);
149 if (notifierIt != m_propertyNameToNotifierMap.end())
150 notifierIt->second();
153template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
156 registerPropertyNotifierIfMissing<T, propertyPtr>();
157 return getOrCreatePropertyWriteConsumersStore<T, propertyPtr>()->addConsumer(
std::move(propertyWriteCallback));
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);
172 m_consumers.insert(m_consumers.begin(), weakConsumersToAdd.begin(), weakConsumersToAdd.end());
176std::shared_ptr<
void>
QOhosPropertiesStore::PropertyWriteConsumersStore<T>::addConsumer(QOhosConsumer<T> consumer)
178 auto sharedConsumer = QtOhos::moveToSharedPtr(std::move(consumer));
179 m_consumers.emplace_back(sharedConsumer);
180 return sharedConsumer;
185 QObject *object = m_object;
186 if (object ==
nullptr)
187 qOhosReportFatalErrorAndAbort(
"%s: m_object was null", Q_FUNC_INFO);
191template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
194 auto propertyWriteConsumersStore = getPropertyWriteConsumersStoreOrNull<T, propertyPtr>();
195 if (!propertyWriteConsumersStore)
198 auto propertyValue = tryGetProperty<T, propertyPtr>();
199 if (propertyValue.hasValue())
200 propertyWriteConsumersStore->notify(propertyValue.value());
203template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
206 auto propertyWriteConsumersStore = getPropertyWriteConsumersStoreOrNull<T, propertyPtr>();
207 if (!propertyWriteConsumersStore) {
208 propertyWriteConsumersStore =
std::make_shared<PropertyWriteConsumersStore<T>>();
209 m_propertyWriteConsumersStoresMap.insert({propertyPtr, propertyWriteConsumersStore});
212 return propertyWriteConsumersStore;
215template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
218 auto propertyWriteConsumersStoresMapIt = m_propertyWriteConsumersStoresMap.find(propertyPtr);
219 if (propertyWriteConsumersStoresMapIt == m_propertyWriteConsumersStoresMap.end())
221 return std::static_pointer_cast<PropertyWriteConsumersStore<T>>(propertyWriteConsumersStoresMapIt->second);
224template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
228 auto propertyNotifierIt = m_propertyNameToNotifierMap.find(propertyName);
229 if (propertyNotifierIt == m_propertyNameToNotifierMap.end()) {
230 m_propertyNameToNotifierMap.emplace(
233 this->notifyPropertyWriteInternal<T, propertyPtr>();
243template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
246 return m_store->tryGetProperty<T, propertyPtr>();
249template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
252 return m_store->addPropertyWriteCallback<T, propertyPtr>(
std::move(propertyWriteCallback));
257template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
260 return QtOhos::printfToString(
261 "_q_platform_ohos_%d_%p", qMetaTypeId<T>(),
static_cast<
const void *>(propertyPtr));
264template<
typename T,
const QOhosPropertyDescriptor<T> *propertyPtr>
267 static const std::string propertyName = makeQObjectOhosPropertyName<T, propertyPtr>();
268 return propertyName.c_str();
274 return qMetaTypeId<T>() == variant.userType()
275 ? makeQOhosOptional(variant.value<T>())
std::enable_if_t< qohosplugincore_h_detail::isQOhosOptional< QOhosInvokeResult< Func, T > >, QOhosInvokeResult< Func, T > > andThen(Func &&func) const
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.
@ ForwardToGuiApplication
QOhosView * mapQWindowToViewOrNull(QWindow *window)
std::string makeQObjectOhosPropertyName()
QOhosOptional< T > tryMapFromQVariant(QVariant variant)
const char * qObjectOhosPropertyName()
std::shared_ptr< QWindowSystemEventHandler > makeApplicationStateTracker()
QOhosOptional< void > makeEmptyQOhosOptional()
void setQOhosPropertyOnQObject(QObject *qObject, T propertyValue)
QOhosOptional< T > tryGetQOhosPropertyFromQObject(QObject *qObject)