25 return QOhosJsThreadGateway::evalWithPromise<std::optional<std::string>>(
26 [&](QOhosJsState &jsState,
auto evalPromise) {
27 auto optDefaultQAbility = jsState.defaultQAbility();
28 if (!optDefaultQAbility) {
33 auto thenCatchPromises = std::move(evalPromise).makeThenCatchBranches(Q_FUNC_INFO);
34 jsState.evalToPromiseOrRejectOnThrow(
35 "@ohos.settings.getValue(*)",
36 {optDefaultQAbility->get(
"context"), name, domainName})
37 .onThen([thenPromise = std::move(thenCatchPromises.first)](
const QOhosCallbackInfo &cbInfo) {
38 std::string result = cbInfo.getFirstArg<QNapi::String>(Q_FUNC_INFO);
41 .onCatch([catchPromise = std::move(thenCatchPromises.second), name, domainName](
const QOhosCallbackInfo &) {
43 "Got error from @ohos.settings.getValue(..., '%s', '%s').",
44 name.c_str(), domainName.c_str());
57 auto optStringValue = tryGetDataItemValue(name, domainName);
58 auto optDoubleValue = optStringValue.has_value()
62 if (optStringValue.has_value() && !optDoubleValue.has_value()) {
64 "OHOS settings value %s/%s ('%s') is not correct double value, assuming empty setting",
65 name.c_str(), domainName.c_str(), optStringValue.value().c_str());
68 return optDoubleValue;
101 QOhosJsState &jsState, QNapi::Object context,
const std::string &name,
102 std::function<
void(QOhosJsState &)> onChanged)
104 const std::string domainName = settingsUserPropertyDomainName(jsState);
106 const bool registered = jsState.eval<QNapi::Boolean>(
107 "@ohos.settings.registerKeyObserver(*)",
109 context, name, domainName,
110 [onChanged = std::move(onChanged)](
const QOhosCallbackInfo &cbInfo) {
111 onChanged(cbInfo.jsState());
116 "Failed to register observer for settings key '%s' in domain '%s'.",
117 name.c_str(), domainName.c_str());
121 auto contextRefPtr = QtOhos::moveToSharedPtr(QNapi::Reference<>::makePersistentFrom(context));
122 return std::shared_ptr<
void>(
124 [contextRefPtr, name, domainName](
auto) {
125 QOhosJsThreadGateway::runAndWait(
126 [&](QOhosJsState &jsState) {
127 auto contextRef = std::move(*contextRefPtr);
128 jsState.eval<QNapi::Value>(
129 "@ohos.settings.unregisterKeyObserver(*)",
130 {contextRef.Value(), name, domainName});
148 return [] {
return true; };
150 return makeQOhosDataSource<
bool>(
151 readWindowPcModeEnabled,
152 [](QOhosJsState &jsState, QOhosConsumer<
bool> valueChangedConsumer) ->
std::shared_ptr<
void> {
160 auto optContext = settingsContext(jsState);
164 return registerSettingsKeyObserver(
165 jsState, optContext.value(), windowPcModeSwitchStatusPropertyName,
166 [valueChangedConsumer = std::move(valueChangedConsumer)](QOhosJsState &jsState) {
167 valueChangedConsumer(readWindowPcModeEnabled(jsState));
170 makeQOhosNoOpConsumer(),
171 QtOhos::invokeInQtThread,