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
qohossettings.cpp
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
5#include <QtCore/private/qohoscommon_p.h>
6#include <cmath>
7#include <cstring>
8#include <functional>
9#include <qohosdeviceinfo_p.h>
10#include <qohosplugincore.h>
11#include <qohosutils.h>
12#include <stdexcept>
13#include <string>
14
16
17namespace {
18
19constexpr const char *fontSizeScalePropertyName = "font_scale";
20constexpr const char *windowPcModeSwitchStatusPropertyName = "window_pcmode_switch_status";
21
22QOhosOptional<std::string> tryGetDataItemValue(const std::string &name, const std::string &domainName)
23{
24 return QtOhos::evalInJsThreadWithConsumer<QOhosOptional<std::string>>(
25 [&](QtOhos::JsState &jsState, auto resultConsumer) {
26 auto defaultQAbility = jsState.defaultQAbilityPeer()->qAbility();
27 if (defaultQAbility.IsEmpty()) {
28 resultConsumer(makeEmptyQOhosOptional());
29 return;
30 }
31
32 jsState.eval<QNapi::Promise>(
33 "@ohos.settings.getValue(*)",
34 {defaultQAbility.get("context"), name, domainName})
35 .withContext(std::move(resultConsumer))
36 .onThenWithContext([](const QtOhos::CallbackInfo &cbInfo, auto &resultConsumer) {
37 std::string result = cbInfo.getFirstArg<QNapi::String>(Q_FUNC_INFO);
38 resultConsumer(makeQOhosOptional(result));
39 })
40 .onCatchWithContext([name, domainName](const QtOhos::CallbackInfo &, auto &resultConsumer) {
41 qOhosPrintfError(
42 "Got error from @ohos.settings.getValue(..., '%s', '%s').",
43 name.c_str(), domainName.c_str());
44 resultConsumer(makeEmptyQOhosOptional());
45 });
46 });
47}
48
49template<typename T>
50QOhosOptional<T> tryGetDataItemTypedValue(const std::string &name, const std::string &domainName);
51
52template<>
53QOhosOptional<double> tryGetDataItemTypedValue(const std::string &name, const std::string &domainName)
54{
55 auto optStringValue = tryGetDataItemValue(name, domainName);
56 auto optDoubleValue = optStringValue.hasValue()
57 ? QtOhos::tryParseStringAsFiniteDouble(optStringValue.value())
59
60 if (optStringValue.hasValue() && !optDoubleValue.hasValue()) {
61 qOhosPrintfError(
62 "OHOS settings value %s/%s ('%s') is not correct double value, assuming empty setting",
63 name.c_str(), domainName.c_str(), optStringValue.value().c_str());
64 }
65
66 return optDoubleValue;
67}
68
70{
71 return QtOhos::evalInJsThread([](QtOhos::JsState &jsState) {
72 return jsState.eval<QNapi::String>("@ohos.settings.domainName.USER_PROPERTY").Utf8Value();
73 });
74}
75
76}
77
78namespace QOhosSettings {
79
81{
82 constexpr double defaultFontSizeScale = 1.0;
83 const auto maybeFontSizeScaleSetting = tryGetDataItemTypedValue<double>(
85
86 if (!maybeFontSizeScaleSetting.hasValue()) {
87 qOhosPrintfWarning(
88 "Cannot obtain '%s' property. Assuming its default fallback mode value %f",
89 fontSizeScalePropertyName, defaultFontSizeScale);
90 return defaultFontSizeScale;
91 }
92
93 return maybeFontSizeScaleSetting.value();
94}
95
97{
99 return true;
100
101 constexpr auto statusTrueStr = "true";
102 constexpr auto statusFalseStr = "false";
103
104 auto maybeWindowPcModeSwitchStatus = tryGetDataItemValue(
105 windowPcModeSwitchStatusPropertyName, getOhosSettingsUserPropertyDomainName());
106
107 if (!maybeWindowPcModeSwitchStatus.hasValue()) {
108 qOhosPrintfWarning(
109 "Cannot obtain '%s' property. Assuming it is NOT enabled.",
111 return false;
112 }
113
114 auto windowPcModeSwitchStatus = maybeWindowPcModeSwitchStatus.value();
115 if (windowPcModeSwitchStatus != statusTrueStr && windowPcModeSwitchStatus != statusFalseStr) {
116 qOhosPrintfError(
117 "Unexpected value of '%s': %s (expected: '%s' or '%s').",
118 windowPcModeSwitchStatusPropertyName, windowPcModeSwitchStatus.c_str(),
119 statusTrueStr, statusFalseStr);
120 }
121
122 return windowPcModeSwitchStatus == statusTrueStr;
123}
124
125}
126
127QT_END_NAMESPACE
std::enable_if_t< qohosplugincore_h_detail::isQOhosOptional< QOhosInvokeResult< Func, T > >, QOhosInvokeResult< Func, T > > andThen(Func &&func) const
double fontSizeScale()
bool isWindowPcModeEnabled()
Combined button and popup list for selecting options.
QOhosOptional< std::string > tryGetDataItemValue(const std::string &name, const std::string &domainName)
constexpr const char * windowPcModeSwitchStatusPropertyName
QOhosOptional< T > tryGetDataItemTypedValue(const std::string &name, const std::string &domainName)
QOhosOptional< double > tryGetDataItemTypedValue(const std::string &name, const std::string &domainName)
constexpr const char * fontSizeScalePropertyName
std::string getOhosSettingsUserPropertyDomainName()
QOhosOptional< double > tryParseStringAsFiniteDouble(const std::string &inputString)
QOhosOptional< void > makeEmptyQOhosOptional()