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
qohosjsutils.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 QOHOSJSUTILS_H
5#define QOHOSJSUTILS_H
6
7#include <QtCore/private/qcore_ohos_p.h>
8#include <QtCore/private/qnapi_p.h>
9#include <QtCore/private/qohoscommon_p.h>
10#include <QtCore/private/qohosjstools_p.h>
11#include <QtCore/qglobal.h>
12#include <chrono>
13#include <cstdint>
14#include <functional>
15#include <memory>
16#include <optional>
17#include <qohosutils.h>
18#include <string>
19#include <type_traits>
20
22
24
25namespace QtOhos {
26
27template<typename T>
29 QOhosJsState &jsState, std::function<QNapi::Value(QOhosJsState &, T)> promiseValueFactory,
31
35
36int setJsTimeout(
37 QOhosJsState &jsState, std::function<void(const QOhosCallbackInfo &)> timeoutFunc,
38 std::chrono::milliseconds delay);
39
40void clearJsTimeout(QOhosJsState &jsState, int timerId);
41
42QNapi::Promise makeResolvedPromise(QNapi::Value valueForResolve);
43
44// Makes a Promise resolved to undefined, the JS way of saying "no particular value".
45QNapi::Promise makeResolvedPromise(Napi::Env env);
46
47template<typename T>
48std::optional<T> getOptionalProperty(const QNapi::Object &object, const std::string &propName);
49
50std::optional<std::uint32_t> tryGetCodeFromJsBusinessError(const Napi::Error &error);
51
52// On JS business error matching suppressedErrorCode warns and returns, otherwise rethrows.
53// Call from a catch block.
55 const Napi::Error &error, std::uint32_t suppressedErrorCode, const char *callerContextName);
56
57// Runs action; on JS business error matching suppressedErrorCode warns and returns, otherwise rethrows.
58// Returns true if action completed, false if error was swallowed.
60 QOhosJsState &, std::uint32_t suppressedErrorCode, const char *callerContextName,
61 const std::function<void()> &action);
62
63template<typename T>
65 QOhosJsState &jsState, std::function<QNapi::Value(QOhosJsState &, T)> promiseValueFactory,
67{
68 auto promiseDeferred = QNapi::Promise::Deferred::New(jsState.env());
69
70 asyncCallFunc(
71 jsState,
72 makeCallOnceConsumerWrapper<QOhosJsState &, T>(
73 [promiseDeferred, promiseValueFactory = std::move(promiseValueFactory)](QOhosJsState &jsState, T result) {
74 promiseDeferred.Resolve(promiseValueFactory(jsState, result));
75 }));
76
77 return promiseDeferred.Promise();
78}
79
80template<typename T>
81std::optional<T> getOptionalProperty(const QNapi::Object &object, const std::string &propName)
82{
83 auto optPropValue = QNapi::getOptionalPropOrEmpty<T>(object, propName);
84 return !optPropValue.IsEmpty()
85 ? std::optional(optPropValue)
86 : std::nullopt;
87}
88
89template<typename ConfigValue>
91 std::function<ConfigValue(QOhosJsState &)> initValueSupplier,
92 std::function<ConfigValue(QOhosJsState &, const QNapi::Object &)> valueFetcher,
93 QOhosConsumer<ConfigValue> valueChangedHandler)
94{
95 return makeQOhosDataSource<ConfigValue>(
96 std::move(initValueSupplier),
97 [valueFetcher = std::move(valueFetcher)](QOhosJsState &jsState, QOhosConsumer<ConfigValue> valueUpdatesConsumer) mutable {
98 return registerOhosAppContextEnvironmentCallback(
99 jsState,
100 {
101 {
102 "onConfigurationUpdated",
103 [valueFetcher = std::move(valueFetcher), valueUpdatesConsumer = std::move(valueUpdatesConsumer)](const QOhosCallbackInfo &cbInfo) {
104 auto config = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
105 valueUpdatesConsumer(valueFetcher(cbInfo.jsState(), config));
106 }
107 },
108 });
109 },
110 std::move(valueChangedHandler),
111 QtOhos::invokeInQtThread,
112 Q_FUNC_INFO);
113}
114
115}
116
117QT_END_NAMESPACE
118
119#endif // QOHOSJSUTILS_H
std::vector< QOhosDisplayInfo > getRegisteredDisplayInfos()
static std::shared_ptr< QOhosDisplayManager > create(QOhosJsState &jsState, CreateInfo createInfo)
QOhosDisplayInfo::JsDisplayId JsDisplayId
QPoint mapFromDisplayToGlobal(const QPoint &displayOffset, QOhosDisplayInfo::JsDisplayId jsDisplayId)
Combined button and popup list for selecting options.
std::optional< std::uint32_t > tryGetCodeFromJsBusinessError(const Napi::Error &error)
bool runIgnoringJsBusinessError(QOhosJsState &, std::uint32_t suppressedErrorCode, const char *callerContextName, const std::function< void()> &action)
QNapi::Promise adaptAsyncCallResultToJsPromise(QOhosJsState &jsState, std::function< QNapi::Value(QOhosJsState &, T)> promiseValueFactory, const QOhosConsumer< QOhosJsState &, QOhosConsumer< QOhosJsState &, T > > &asyncCallFunc)
std::optional< T > getOptionalProperty(const QNapi::Object &object, const std::string &propName)
QNapi::Promise makeResolvedPromise(QNapi::Value valueForResolve)
void rethrowUnlessJsBusinessErrorIs(const Napi::Error &error, std::uint32_t suppressedErrorCode, const char *callerContextName)
int setJsTimeout(QOhosJsState &jsState, std::function< void(const QOhosCallbackInfo &)> timeoutFunc, std::chrono::milliseconds delay)
QOhosSupplier< ConfigValue > makeOhosConfigValueDataSource(std::function< ConfigValue(QOhosJsState &)> initValueSupplier, std::function< ConfigValue(QOhosJsState &, const QNapi::Object &)> valueFetcher, QOhosConsumer< ConfigValue > valueChangedHandler)
void clearJsTimeout(QOhosJsState &jsState, int timerId)
QOhosConsumer< QOhosJsState &, JsDisplayId, QRectF > displayAvailableAreaChangedCb