Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
4
#
include
"qohossettings.h"
5
#
include
<
QtCore
/
private
/
qcore_ohos_p
.
h
>
6
#
include
<
QtCore
/
private
/
qohoscommon_p
.
h
>
7
#
include
<
cmath
>
8
#
include
<
cstring
>
9
#
include
<
functional
>
10
#
include
<
optional
>
11
#
include
<
qohosdeviceinfo_p
.
h
>
12
#
include
<
qohosjsutils
.
h
>
13
#
include
<
qohosutils
.
h
>
14
#
include
<
string
>
15
16
QT_BEGIN_NAMESPACE
17
18
namespace
{
19
20
constexpr
const
char
*
fontSizeScalePropertyName
=
"font_scale"
;
21
constexpr
const
char
*
windowPcModeSwitchStatusPropertyName
=
"window_pcmode_switch_status"
;
22
23
std
::optional<
std
::string>
tryGetDataItemValue
(
const
std
::string &name,
const
std
::string &domainName)
24
{
25
return
QOhosJsThreadGateway::evalWithPromise<std::optional<std::string>>(
26
[&](QOhosJsState &jsState,
auto
evalPromise) {
27
auto
optDefaultQAbility = jsState.defaultQAbility();
28
if
(!optDefaultQAbility) {
29
evalPromise({});
30
return
;
31
}
32
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);
39
thenPromise(result);
40
})
41
.onCatch([catchPromise = std::move(thenCatchPromises.second), name, domainName](
const
QOhosCallbackInfo &) {
42
qOhosPrintfError(
43
"Got error from @ohos.settings.getValue(..., '%s', '%s')."
,
44
name.c_str(), domainName.c_str());
45
catchPromise({});
46
});
47
},
48
Q_FUNC_INFO);
49
}
50
51
template
<
typename
T>
52
std
::optional<T>
tryGetDataItemTypedValue
(
const
std
::string &name,
const
std
::string &domainName);
53
54
template
<>
55
std
::optional<
double
>
tryGetDataItemTypedValue
(
const
std
::string &name,
const
std
::string &domainName)
56
{
57
auto
optStringValue = tryGetDataItemValue(name, domainName);
58
auto
optDoubleValue = optStringValue.has_value()
59
?
QtOhos
::
tryParseStringAsFiniteDouble
(
optStringValue.value()
)
60
:
std
::nullopt;
61
62
if
(optStringValue.has_value() && !optDoubleValue.has_value()) {
63
qOhosPrintfError(
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());
66
}
67
68
return
optDoubleValue;
69
}
70
71
std
::string
getOhosSettingsUserPropertyDomainName
()
72
{
73
return
QOhosJsThreadGateway::eval([](QOhosJsState &jsState) {
74
return
jsState.eval<QNapi::String>(
"@ohos.settings.domainName.USER_PROPERTY"
).Utf8Value();
75
},
76
Q_FUNC_INFO);
77
}
78
79
std
::string
settingsUserPropertyDomainName
(QOhosJsState &jsState)
80
{
81
return
jsState.eval<QNapi::String>(
"@ohos.settings.domainName.USER_PROPERTY"
);
82
}
83
84
std
::
optional
<
QNapi
::
Object
>
settingsContext
(QOhosJsState &jsState)
85
{
86
auto
optQAbility = jsState.defaultQAbility();
87
return
optQAbility
88
? std::optional(optQAbility->eval<QNapi::Object>(
"context"
))
89
: std::nullopt;
90
}
91
92
std
::string
readSettingValue
(
93
QOhosJsState &jsState,
const
QNapi::Object &context,
const
std
::string &name)
94
{
95
return
jsState.eval<QNapi::String>(
96
"@ohos.settings.getValueSync(*)"
,
97
{context, name,
""
, settingsUserPropertyDomainName(jsState)});
98
}
99
100
std
::shared_ptr<
void
>
registerSettingsKeyObserver
(
101
QOhosJsState &jsState, QNapi::Object context,
const
std
::string &name,
102
std::function<
void
(QOhosJsState &)> onChanged)
103
{
104
const
std
::string domainName = settingsUserPropertyDomainName(jsState);
105
106
const
bool
registered = jsState.eval<QNapi::Boolean>(
107
"@ohos.settings.registerKeyObserver(*)"
,
108
{
109
context, name, domainName,
110
[onChanged = std::move(onChanged)](
const
QOhosCallbackInfo &cbInfo) {
111
onChanged(cbInfo.jsState());
112
}
113
});
114
if
(!registered) {
115
qOhosPrintfWarning(
116
"Failed to register observer for settings key '%s' in domain '%s'."
,
117
name.c_str(), domainName.c_str());
118
return
nullptr
;
119
}
120
121
auto
contextRefPtr = QtOhos::moveToSharedPtr(QNapi::Reference<>::makePersistentFrom(context));
122
return
std::shared_ptr<
void
>(
123
nullptr
,
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});
131
},
132
Q_FUNC_INFO);
133
});
134
}
135
136
bool
readWindowPcModeEnabled
(QOhosJsState &jsState)
137
{
138
auto
optContext = settingsContext(jsState);
139
if
(!optContext)
140
return
false
;
141
142
return
readSettingValue(jsState, optContext.value(), windowPcModeSwitchStatusPropertyName) ==
"true"
;
143
}
144
145
QOhosSupplier
<
bool
>
makeWindowPcModeEnabledSupplier
()
146
{
147
if
(
QOhosDeviceInfo
::
is2in1
(
)
)
148
return
[] {
return
true
; };
149
150
return
makeQOhosDataSource<
bool
>(
151
readWindowPcModeEnabled,
152
[](QOhosJsState &jsState, QOhosConsumer<
bool
> valueChangedConsumer) ->
std
::shared_ptr<
void
> {
153
// FIXME: the observer must use the launching UIAbility's context. By
154
// API definition @ohos.settings does not accept the application
155
// context, and free-window state is not part of the app Configuration.
156
// That context can be destroyed while the process lives, orphaning the
157
// observer. The orphaned observer keeps firing (verified on device), so
158
// the cached value stays correct. Revisit when the platform exposes
159
// settings observation on the application context.
160
auto
optContext = settingsContext(jsState);
161
if
(!optContext)
162
return
nullptr
;
163
164
return
registerSettingsKeyObserver(
165
jsState, optContext.value(), windowPcModeSwitchStatusPropertyName,
166
[valueChangedConsumer = std::move(valueChangedConsumer)](QOhosJsState &jsState) {
167
valueChangedConsumer(readWindowPcModeEnabled(jsState));
168
});
169
},
170
makeQOhosNoOpConsumer(),
171
QtOhos::invokeInQtThread,
172
Q_FUNC_INFO);
173
}
174
175
QOhosSupplier
<
bool
>
makeUncachedWindowPcModeEnabledSupplier
()
176
{
177
return
[] {
178
return
QOhosDeviceInfo::is2in1()
179
|| QOhosJsThreadGateway::eval(readWindowPcModeEnabled, Q_FUNC_INFO);
180
};
181
}
182
183
}
184
185
QOhosSettings
&
QOhosSettings
::
instance
()
186
{
187
static
QOhosSettings
instance;
188
return
instance;
189
}
190
191
std
::shared_ptr<
void
>
QOhosSettings
::installSettingsCache()
192
{
193
return
QtOhos::makeDestroyNotifier(
194
[
this
, previousSupplier = std::exchange(m_windowPcModeEnabled, makeWindowPcModeEnabledSupplier())]()
mutable
{
195
m_windowPcModeEnabled = std::move(previousSupplier);
196
});
197
}
198
199
QOhosSettings
::QOhosSettings()
200
: m_windowPcModeEnabled(makeUncachedWindowPcModeEnabledSupplier())
201
{
202
}
203
204
double
QOhosSettings
::
fontSizeScale
()
const
205
{
206
constexpr
double
defaultFontSizeScale = 1.0;
207
const
auto
maybeFontSizeScaleSetting = tryGetDataItemTypedValue<
double
>(
208
fontSizeScalePropertyName
,
getOhosSettingsUserPropertyDomainName
(
)
);
209
210
if
(!maybeFontSizeScaleSetting.has_value()) {
211
qOhosPrintfWarning(
212
"Cannot obtain '%s' property. Assuming its default fallback mode value %f"
,
213
fontSizeScalePropertyName
, defaultFontSizeScale);
214
return
defaultFontSizeScale;
215
}
216
217
return
maybeFontSizeScaleSetting.value();
218
}
219
220
bool
QOhosSettings
::
isWindowPcModeEnabled
()
const
221
{
222
return
m_windowPcModeEnabled();
223
}
224
225
QT_END_NAMESPACE
QOhosSettings
Definition
qohossettings.h:14
QOhosSettings::isWindowPcModeEnabled
bool isWindowPcModeEnabled() const
Definition
qohossettings.cpp:220
QOhosSettings::instance
static QOhosSettings & instance()
Definition
qohossettings.cpp:185
QOhosSettings::fontSizeScale
double fontSizeScale() const
Definition
qohossettings.cpp:204
QOhosDeviceInfo
Definition
qohosdeviceinfo.cpp:9
QOhosDeviceInfo::is2in1
bool is2in1()
Definition
qohosdeviceinfo.cpp:65
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
QT_BEGIN_NAMESPACE::settingsUserPropertyDomainName
std::string settingsUserPropertyDomainName(QOhosJsState &jsState)
Definition
qohossettings.cpp:79
QT_BEGIN_NAMESPACE::tryGetDataItemValue
std::optional< std::string > tryGetDataItemValue(const std::string &name, const std::string &domainName)
Definition
qohossettings.cpp:23
QT_BEGIN_NAMESPACE::registerSettingsKeyObserver
std::shared_ptr< void > registerSettingsKeyObserver(QOhosJsState &jsState, QNapi::Object context, const std::string &name, std::function< void(QOhosJsState &)> onChanged)
Definition
qohossettings.cpp:100
QT_BEGIN_NAMESPACE::readSettingValue
std::string readSettingValue(QOhosJsState &jsState, const QNapi::Object &context, const std::string &name)
Definition
qohossettings.cpp:92
QT_BEGIN_NAMESPACE::windowPcModeSwitchStatusPropertyName
constexpr const char * windowPcModeSwitchStatusPropertyName
Definition
qohossettings.cpp:21
QT_BEGIN_NAMESPACE::makeWindowPcModeEnabledSupplier
QOhosSupplier< bool > makeWindowPcModeEnabledSupplier()
Definition
qohossettings.cpp:145
QT_BEGIN_NAMESPACE::readWindowPcModeEnabled
bool readWindowPcModeEnabled(QOhosJsState &jsState)
Definition
qohossettings.cpp:136
QT_BEGIN_NAMESPACE::settingsContext
std::optional< QNapi::Object > settingsContext(QOhosJsState &jsState)
Definition
qohossettings.cpp:84
QT_BEGIN_NAMESPACE::fontSizeScalePropertyName
constexpr const char * fontSizeScalePropertyName
Definition
qohossettings.cpp:20
QT_BEGIN_NAMESPACE::getOhosSettingsUserPropertyDomainName
std::string getOhosSettingsUserPropertyDomainName()
Definition
qohossettings.cpp:71
QT_BEGIN_NAMESPACE::tryGetDataItemTypedValue
std::optional< double > tryGetDataItemTypedValue(const std::string &name, const std::string &domainName)
Definition
qohossettings.cpp:55
QT_BEGIN_NAMESPACE::tryGetDataItemTypedValue
std::optional< T > tryGetDataItemTypedValue(const std::string &name, const std::string &domainName)
QT_BEGIN_NAMESPACE::makeUncachedWindowPcModeEnabledSupplier
QOhosSupplier< bool > makeUncachedWindowPcModeEnabledSupplier()
Definition
qohossettings.cpp:175
QtOhos
Definition
qcore_ohos.cpp:59
QtOhos::tryParseStringAsFiniteDouble
std::optional< double > tryParseStringAsFiniteDouble(const std::string &inputString)
Definition
qohosutils.cpp:13
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qtbase
src
plugins
platforms
ohos
qohossettings.cpp
Generated on
for Qt by
1.18.0