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#include <vector>
21
23
25
26namespace QtOhos {
27
28template<typename T>
30 QOhosJsState &jsState, std::function<QNapi::Value(QOhosJsState &, T)> promiseValueFactory,
32
36
37int setJsTimeout(
38 QOhosJsState &jsState, std::function<void(const QOhosCallbackInfo &)> timeoutFunc,
39 std::chrono::milliseconds delay);
40
41void clearJsTimeout(QOhosJsState &jsState, int timerId);
42
43QNapi::Promise makeResolvedPromise(QNapi::Value valueForResolve);
44
45// Makes a Promise resolved to undefined, the JS way of saying "no particular value".
46QNapi::Promise makeResolvedPromise(Napi::Env env);
47
48template<typename T>
49std::optional<T> getOptionalProperty(const QNapi::Object &object, const std::string &propName);
50
51std::optional<std::uint32_t> tryGetCodeFromJsBusinessError(const Napi::Error &error);
52
53// On JS business error matching one of suppressedErrorCodes warns and returns, otherwise rethrows.
54// Call from a catch block.
56 const Napi::Error &error, const std::vector<std::uint32_t> &suppressedErrorCodes,
57 const char *callerContextName);
58
59// Runs action; on JS business error matching one of suppressedErrorCodes warns and returns,
60// otherwise rethrows. Returns true if action completed, false if error was swallowed.
62 QOhosJsState &, const std::vector<std::uint32_t> &suppressedErrorCodes,
63 const char *callerContextName, const std::function<void()> &action);
64
65template<typename T>
67 QOhosJsState &jsState, std::function<QNapi::Value(QOhosJsState &, T)> promiseValueFactory,
69{
70 auto promiseDeferred = QNapi::Promise::Deferred::New(jsState.env());
71
72 asyncCallFunc(
73 jsState,
74 makeCallOnceConsumerWrapper<QOhosJsState &, T>(
75 [promiseDeferred, promiseValueFactory = std::move(promiseValueFactory)](QOhosJsState &jsState, T result) {
76 promiseDeferred.Resolve(promiseValueFactory(jsState, result));
77 }));
78
79 return promiseDeferred.Promise();
80}
81
82template<typename T>
83std::optional<T> getOptionalProperty(const QNapi::Object &object, const std::string &propName)
84{
85 auto optPropValue = QNapi::getOptionalPropOrEmpty<T>(object, propName);
86 return !optPropValue.IsEmpty()
87 ? std::optional(optPropValue)
88 : std::nullopt;
89}
90
91template<typename ConfigValue>
93 std::function<ConfigValue(QOhosJsState &)> initValueSupplier,
94 std::function<ConfigValue(QOhosJsState &, const QNapi::Object &)> valueFetcher,
95 QOhosConsumer<ConfigValue> valueChangedHandler)
96{
97 return makeQOhosDataSource<ConfigValue>(
98 std::move(initValueSupplier),
99 [valueFetcher = std::move(valueFetcher)](QOhosJsState &jsState, QOhosConsumer<ConfigValue> valueUpdatesConsumer) mutable {
100 return registerOhosAppContextEnvironmentCallback(
101 jsState,
102 {
103 {
104 "onConfigurationUpdated",
105 [valueFetcher = std::move(valueFetcher), valueUpdatesConsumer = std::move(valueUpdatesConsumer)](const QOhosCallbackInfo &cbInfo) {
106 auto config = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
107 valueUpdatesConsumer(valueFetcher(cbInfo.jsState(), config));
108 }
109 },
110 });
111 },
112 std::move(valueChangedHandler),
113 QtOhos::invokeInQtThread,
114 Q_FUNC_INFO);
115}
116
117}
118
119QT_END_NAMESPACE
120
121#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.
void rethrowUnlessJsBusinessErrorIsOneOf(const Napi::Error &error, const std::vector< std::uint32_t > &suppressedErrorCodes, const char *callerContextName)
std::optional< std::uint32_t > tryGetCodeFromJsBusinessError(const Napi::Error &error)
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)
int setJsTimeout(QOhosJsState &jsState, std::function< void(const QOhosCallbackInfo &)> timeoutFunc, std::chrono::milliseconds delay)
bool runIgnoringJsBusinessError(QOhosJsState &, const std::vector< std::uint32_t > &suppressedErrorCodes, const char *callerContextName, const std::function< void()> &action)
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