1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_textpicker_bridge.h"
16
17 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_common_bridge.h"
18 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
19 #include "core/interfaces/native/node/api.h"
20
21 namespace OHOS::Ace::NG {
22 const std::string FORMAT_FONT = "%s|%s|%s";
23 const std::string DEFAULT_ERR_CODE = "-1";
24 const int32_t DEFAULT_NEGATIVE_NUM = -1;
25 constexpr uint32_t DEFAULT_TIME_PICKER_TEXT_COLOR = 0xFF182431;
26 constexpr uint32_t DEFAULT_TIME_PICKER_SELECTED_TEXT_COLOR = 0xFF007DFF;
27
SetBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)28 ArkUINativeModuleValue TextpickerBridge::SetBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
29 {
30 EcmaVM* vm = runtimeCallInfo->GetVM();
31 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
32 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
33 Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(1);
34 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
35 CommonBridge::SetBackgroundColor(runtimeCallInfo);
36 Color color;
37 if (!ArkTSUtils::ParseJsColorAlpha(vm, colorArg, color)) {
38 GetArkUIInternalNodeAPI()->GetTextpickerModifier().ResetTextpickerBackgroundColor(nativeNode);
39 } else {
40 GetArkUIInternalNodeAPI()->GetTextpickerModifier().SetTextpickerBackgroundColor(nativeNode, color.GetValue());
41 }
42 return panda::JSValueRef::Undefined(vm);
43 }
44
ResetBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)45 ArkUINativeModuleValue TextpickerBridge::ResetBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
46 {
47 EcmaVM* vm = runtimeCallInfo->GetVM();
48 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
49 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
50 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
51 CommonBridge::ResetBackgroundColor(runtimeCallInfo);
52 GetArkUIInternalNodeAPI()->GetTextpickerModifier().ResetTextpickerBackgroundColor(nativeNode);
53 return panda::JSValueRef::Undefined(vm);
54 }
55
SetCanLoop(ArkUIRuntimeCallInfo * runtimeCallInfo)56 ArkUINativeModuleValue TextpickerBridge::SetCanLoop(ArkUIRuntimeCallInfo* runtimeCallInfo)
57 {
58 EcmaVM* vm = runtimeCallInfo->GetVM();
59 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
60 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
61 Local<JSValueRef> canLoopArg = runtimeCallInfo->GetCallArgRef(1);
62 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
63 bool canLoop = true;
64 if (canLoopArg->IsBoolean()) {
65 canLoop = canLoopArg->ToBoolean(vm)->Value();
66 }
67 GetArkUIInternalNodeAPI()->GetTextpickerModifier().SetTextpickerCanLoop(nativeNode, canLoop);
68 return panda::JSValueRef::Undefined(vm);
69 }
70
SetSelectedIndex(ArkUIRuntimeCallInfo * runtimeCallInfo)71 ArkUINativeModuleValue TextpickerBridge::SetSelectedIndex(ArkUIRuntimeCallInfo* runtimeCallInfo)
72 {
73 EcmaVM* vm = runtimeCallInfo->GetVM();
74 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
75 Local<JSValueRef> indexArg = runtimeCallInfo->GetCallArgRef(1);
76 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
77 std::vector<uint32_t> selectedValues;
78
79 if (indexArg->IsArray(vm)) {
80 if (!ArkTSUtils::ParseJsIntegerArray(vm, indexArg, selectedValues)) {
81 selectedValues.clear();
82 GetArkUIInternalNodeAPI()->GetTextpickerModifier().ResetTextpickerSelected(nativeNode);
83 return panda::JSValueRef::Undefined(vm);
84 }
85 if (selectedValues.size() > 0) {
86 GetArkUIInternalNodeAPI()->GetTextpickerModifier().SetTextpickerSelectedIndex(
87 nativeNode, selectedValues.data(), selectedValues.size());
88 } else {
89 GetArkUIInternalNodeAPI()->GetTextpickerModifier().ResetTextpickerSelected(nativeNode);
90 }
91 } else {
92 uint32_t selectedValue = 0;
93 if (indexArg->IsNumber()) {
94 selectedValue = indexArg->Uint32Value(vm);
95 selectedValues.emplace_back(selectedValue);
96 GetArkUIInternalNodeAPI()->GetTextpickerModifier().SetTextpickerSelectedIndex(
97 nativeNode, selectedValues.data(), DEFAULT_NEGATIVE_NUM); // represent this is a number.
98 } else {
99 selectedValues.clear();
100 GetArkUIInternalNodeAPI()->GetTextpickerModifier().ResetTextpickerSelected(nativeNode);
101 }
102 }
103 return panda::JSValueRef::Undefined(vm);
104 }
105
SetTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)106 ArkUINativeModuleValue TextpickerBridge::SetTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
107 {
108 EcmaVM* vm = runtimeCallInfo->GetVM();
109 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
110 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
111 Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(1); // text color
112 Local<JSValueRef> fontSizeArg = runtimeCallInfo->GetCallArgRef(2); // text font size
113 Local<JSValueRef> fontWeightArg = runtimeCallInfo->GetCallArgRef(3); // text font weight
114 Local<JSValueRef> fontFamilyArg = runtimeCallInfo->GetCallArgRef(4); // text font family
115 Local<JSValueRef> fontStyleArg = runtimeCallInfo->GetCallArgRef(5); // text font style
116 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
117 Color textColor;
118 if (colorArg->IsNull() || colorArg->IsUndefined() || !ArkTSUtils::ParseJsColorAlpha(vm, colorArg, textColor)) {
119 textColor.SetValue(DEFAULT_TIME_PICKER_TEXT_COLOR);
120 }
121 CalcDimension size;
122 if (fontSizeArg->IsNull() || fontSizeArg->IsUndefined()) {
123 size = Dimension(DEFAULT_NEGATIVE_NUM);
124 } else {
125 if (!ArkTSUtils::ParseJsDimensionNG(vm, fontSizeArg, size, DimensionUnit::FP, false)) {
126 size = Dimension(DEFAULT_NEGATIVE_NUM);
127 }
128 }
129 std::string weight = DEFAULT_ERR_CODE;
130 if (!fontWeightArg->IsNull() && !fontWeightArg->IsUndefined()) {
131 if (fontWeightArg->IsNumber()) {
132 weight = std::to_string(fontWeightArg->Int32Value(vm));
133 } else {
134 if (!ArkTSUtils::ParseJsString(vm, fontWeightArg, weight) || weight.empty()) {
135 weight = DEFAULT_ERR_CODE;
136 }
137 }
138 }
139 std::string fontFamily;
140 if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, fontFamilyArg, fontFamily) || fontFamily.empty()) {
141 fontFamily = DEFAULT_ERR_CODE;
142 }
143 int32_t styleVal = 0;
144 if (!fontStyleArg->IsNull() && !fontStyleArg->IsUndefined()) {
145 styleVal = fontStyleArg->Int32Value(vm);
146 }
147 std::string fontSizeStr = size.ToString();
148 std::string fontInfo =
149 StringUtils::FormatString(FORMAT_FONT.c_str(), fontSizeStr.c_str(), weight.c_str(), fontFamily.c_str());
150 GetArkUIInternalNodeAPI()->GetTextpickerModifier().SetTextpickerTextStyle(
151 nativeNode, textColor.GetValue(), fontInfo.c_str(), styleVal);
152 return panda::JSValueRef::Undefined(vm);
153 }
154
SetSelectedTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)155 ArkUINativeModuleValue TextpickerBridge::SetSelectedTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
156 {
157 EcmaVM* vm = runtimeCallInfo->GetVM();
158 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
159 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
160 Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(1); // text color
161 Local<JSValueRef> fontSizeArg = runtimeCallInfo->GetCallArgRef(2); // text font size
162 Local<JSValueRef> fontWeightArg = runtimeCallInfo->GetCallArgRef(3); // text font weight
163 Local<JSValueRef> fontFamilyArg = runtimeCallInfo->GetCallArgRef(4); // text font family
164 Local<JSValueRef> fontStyleArg = runtimeCallInfo->GetCallArgRef(5); // text font style
165 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
166 Color textColor;
167 if (colorArg->IsNull() || colorArg->IsUndefined() || !ArkTSUtils::ParseJsColorAlpha(vm, colorArg, textColor)) {
168 textColor.SetValue(DEFAULT_TIME_PICKER_SELECTED_TEXT_COLOR);
169 }
170 CalcDimension size;
171 if (fontSizeArg->IsNull() || fontSizeArg->IsUndefined()) {
172 size = Dimension(DEFAULT_NEGATIVE_NUM);
173 } else {
174 if (!ArkTSUtils::ParseJsDimensionNG(vm, fontSizeArg, size, DimensionUnit::FP, false)) {
175 size = Dimension(DEFAULT_NEGATIVE_NUM);
176 }
177 }
178 std::string weight = DEFAULT_ERR_CODE;
179 if (!fontWeightArg->IsNull() && !fontWeightArg->IsUndefined()) {
180 if (fontWeightArg->IsNumber()) {
181 weight = std::to_string(fontWeightArg->Int32Value(vm));
182 } else {
183 if (!ArkTSUtils::ParseJsString(vm, fontWeightArg, weight) || weight.empty()) {
184 weight = DEFAULT_ERR_CODE;
185 }
186 }
187 }
188 std::string fontFamily;
189 if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, fontFamilyArg, fontFamily) || fontFamily.empty()) {
190 fontFamily = DEFAULT_ERR_CODE;
191 }
192 int32_t styleVal = 0;
193 if (!fontStyleArg->IsNull() && !fontStyleArg->IsUndefined()) {
194 styleVal = fontStyleArg->Int32Value(vm);
195 }
196 std::string fontSizeStr = size.ToString();
197 std::string fontInfo =
198 StringUtils::FormatString(FORMAT_FONT.c_str(), fontSizeStr.c_str(), weight.c_str(), fontFamily.c_str());
199 GetArkUIInternalNodeAPI()->GetTextpickerModifier().SetTextpickerSelectedTextStyle(
200 nativeNode, textColor.GetValue(), fontInfo.c_str(), styleVal);
201 return panda::JSValueRef::Undefined(vm);
202 }
203
SetDisappearTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)204 ArkUINativeModuleValue TextpickerBridge::SetDisappearTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
205 {
206 EcmaVM* vm = runtimeCallInfo->GetVM();
207 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
208 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
209 Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(1); // text color
210 Local<JSValueRef> fontSizeArg = runtimeCallInfo->GetCallArgRef(2); // text font size
211 Local<JSValueRef> fontWeightArg = runtimeCallInfo->GetCallArgRef(3); // text font weight
212 Local<JSValueRef> fontFamilyArg = runtimeCallInfo->GetCallArgRef(4); // text font family
213 Local<JSValueRef> fontStyleArg = runtimeCallInfo->GetCallArgRef(5); // text font style
214 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
215 Color textColor;
216 if (colorArg->IsNull() || colorArg->IsUndefined() || !ArkTSUtils::ParseJsColorAlpha(vm, colorArg, textColor)) {
217 textColor.SetValue(DEFAULT_TIME_PICKER_TEXT_COLOR);
218 }
219 CalcDimension size;
220 if (fontSizeArg->IsNull() || fontSizeArg->IsUndefined()) {
221 size = Dimension(DEFAULT_NEGATIVE_NUM);
222 } else {
223 if (!ArkTSUtils::ParseJsDimensionNG(vm, fontSizeArg, size, DimensionUnit::FP, false)) {
224 size = Dimension(DEFAULT_NEGATIVE_NUM);
225 }
226 }
227 std::string weight = DEFAULT_ERR_CODE;
228 if (!fontWeightArg->IsNull() && !fontWeightArg->IsUndefined()) {
229 if (fontWeightArg->IsNumber()) {
230 weight = std::to_string(fontWeightArg->Int32Value(vm));
231 } else {
232 if (!ArkTSUtils::ParseJsString(vm, fontWeightArg, weight) || weight.empty()) {
233 weight = DEFAULT_ERR_CODE;
234 }
235 }
236 }
237 std::string fontFamily;
238 if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, fontFamilyArg, fontFamily) || fontFamily.empty()) {
239 fontFamily = DEFAULT_ERR_CODE;
240 }
241 int32_t styleVal = 0;
242 if (!fontStyleArg->IsNull() && !fontStyleArg->IsUndefined()) {
243 styleVal = fontStyleArg->Int32Value(vm);
244 }
245 std::string fontSizeStr = size.ToString();
246 std::string fontInfo =
247 StringUtils::FormatString(FORMAT_FONT.c_str(), fontSizeStr.c_str(), weight.c_str(), fontFamily.c_str());
248 GetArkUIInternalNodeAPI()->GetTextpickerModifier().SetTextpickerDisappearTextStyle(
249 nativeNode, textColor.GetValue(), fontInfo.c_str(), styleVal);
250 return panda::JSValueRef::Undefined(vm);
251 }
252
SetDefaultPickerItemHeight(ArkUIRuntimeCallInfo * runtimeCallInfo)253 ArkUINativeModuleValue TextpickerBridge::SetDefaultPickerItemHeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
254 {
255 EcmaVM* vm = runtimeCallInfo->GetVM();
256 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
257 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
258 Local<JSValueRef> itemHeightValue = runtimeCallInfo->GetCallArgRef(1);
259 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
260
261 CalcDimension height;
262 if (itemHeightValue->IsNumber() || itemHeightValue->IsString()) {
263 if (!ArkTSUtils::ParseJsDimensionFp(vm, itemHeightValue, height)) {
264 GetArkUIInternalNodeAPI()->GetTextpickerModifier().ResetTextpickerDefaultPickerItemHeight(nativeNode);
265 return panda::JSValueRef::Undefined(vm);
266 }
267 }
268
269 GetArkUIInternalNodeAPI()->GetTextpickerModifier().SetTextpickerDefaultPickerItemHeight(
270 nativeNode, height.Value(), static_cast<int32_t>(height.Unit()));
271
272 return panda::JSValueRef::Undefined(vm);
273 }
274
ResetCanLoop(ArkUIRuntimeCallInfo * runtimeCallInfo)275 ArkUINativeModuleValue TextpickerBridge::ResetCanLoop(ArkUIRuntimeCallInfo* runtimeCallInfo)
276 {
277 EcmaVM* vm = runtimeCallInfo->GetVM();
278 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
279 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
280 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
281 GetArkUIInternalNodeAPI()->GetTextpickerModifier().ResetTextpickerCanLoop(nativeNode);
282 return panda::JSValueRef::Undefined(vm);
283 }
284
ResetSelectedIndex(ArkUIRuntimeCallInfo * runtimeCallInfo)285 ArkUINativeModuleValue TextpickerBridge::ResetSelectedIndex(ArkUIRuntimeCallInfo* runtimeCallInfo)
286 {
287 EcmaVM* vm = runtimeCallInfo->GetVM();
288 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
289 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
290 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
291 GetArkUIInternalNodeAPI()->GetTextpickerModifier().ResetTextpickerSelected(nativeNode);
292 return panda::JSValueRef::Undefined(vm);
293 }
294
ResetTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)295 ArkUINativeModuleValue TextpickerBridge::ResetTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
296 {
297 EcmaVM* vm = runtimeCallInfo->GetVM();
298 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
299 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
300 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
301 GetArkUIInternalNodeAPI()->GetTextpickerModifier().ResetTextpickerTextStyle(nativeNode);
302 return panda::JSValueRef::Undefined(vm);
303 }
304
ResetSelectedTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)305 ArkUINativeModuleValue TextpickerBridge::ResetSelectedTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
306 {
307 EcmaVM* vm = runtimeCallInfo->GetVM();
308 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
309 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
310 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
311 GetArkUIInternalNodeAPI()->GetTextpickerModifier().ResetTextpickerSelectedTextStyle(nativeNode);
312 return panda::JSValueRef::Undefined(vm);
313 }
314
ResetDisappearTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)315 ArkUINativeModuleValue TextpickerBridge::ResetDisappearTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
316 {
317 EcmaVM* vm = runtimeCallInfo->GetVM();
318 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
319 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
320 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
321 GetArkUIInternalNodeAPI()->GetTextpickerModifier().ResetTextpickerDisappearTextStyle(nativeNode);
322 return panda::JSValueRef::Undefined(vm);
323 }
324
ResetDefaultPickerItemHeight(ArkUIRuntimeCallInfo * runtimeCallInfo)325 ArkUINativeModuleValue TextpickerBridge::ResetDefaultPickerItemHeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
326 {
327 EcmaVM* vm = runtimeCallInfo->GetVM();
328 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
329 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
330 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
331 GetArkUIInternalNodeAPI()->GetTextpickerModifier().ResetTextpickerDefaultPickerItemHeight(nativeNode);
332 return panda::JSValueRef::Undefined(vm);
333 }
334 } // namespace OHOS::Ace::NG