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 
16 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_date_picker_bridge.h"
17 
18 #include "interfaces/napi/kits/utils/napi_utils.h"
19 
20 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_common_bridge.h"
21 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
22 namespace OHOS::Ace::NG {
23 constexpr int NUM_0 = 0;
24 constexpr int NUM_1 = 1;
25 constexpr int NUM_2 = 2;
26 constexpr int NUM_3 = 3;
27 constexpr int NUM_4 = 4;
28 constexpr int NUM_5 = 5;
29 const std::string FORMAT_FONT = "%s|%s|%s";
30 const std::string DEFAULT_FAMILY = "HarmonyOS Sans";
SetSelectedTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)31 ArkUINativeModuleValue DatePickerBridge::SetSelectedTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
32 {
33     EcmaVM* vm = runtimeCallInfo->GetVM();
34     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
35     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
36     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
37     Local<JSValueRef> textColorArgs = runtimeCallInfo->GetCallArgRef(NUM_1);
38     Local<JSValueRef> fontSizeArgs = runtimeCallInfo->GetCallArgRef(NUM_2);
39     Local<JSValueRef> fontWeightArgs = runtimeCallInfo->GetCallArgRef(NUM_3);
40     Local<JSValueRef> fontFamilyArgs = runtimeCallInfo->GetCallArgRef(NUM_4);
41     Local<JSValueRef> fontStyleArgs = runtimeCallInfo->GetCallArgRef(NUM_5);
42 
43     if (textColorArgs->IsUndefined() && fontSizeArgs->IsUndefined() && fontWeightArgs->IsUndefined() &&
44         fontFamilyArgs->IsUndefined() && fontStyleArgs->IsUndefined()) {
45         GetArkUINodeModifiers()->getDatePickerModifier()->resetSelectedTextStyle(nativeNode);
46     }
47 
48     CalcDimension fontSizeData;
49     std::string fontSize;
50     if (!ArkTSUtils::ParseJsDimensionFp(vm, fontSizeArgs, fontSizeData, true, false) ||
51         fontSizeData.Unit() == DimensionUnit::PERCENT) {
52         fontSizeData = Dimension(-1);
53     }
54     fontSize = fontSizeData.ToString();
55 
56     std::string weight = "FontWeight.Medium";
57     if (fontWeightArgs->IsString(vm) || fontWeightArgs->IsNumber()) {
58         weight = fontWeightArgs->ToString(vm)->ToString(vm);
59     }
60 
61     std::string fontFamily;
62     if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, fontFamilyArgs, fontFamily) || fontFamily.empty()) {
63         fontFamily = DEFAULT_FAMILY;
64     }
65     int32_t fontStyle = 0;
66     if (fontStyleArgs->IsNumber()) {
67         fontStyle = fontStyleArgs->Int32Value(vm);
68     }
69     Color color;
70     if (!ArkTSUtils::ParseJsColorAlpha(vm, textColorArgs, color)) {
71         Color::ParseColorString("#ff0a59f7", color);
72     }
73     std::string fontInfo =
74         StringUtils::FormatString(FORMAT_FONT.c_str(), fontSize.c_str(), weight.c_str(), fontFamily.c_str());
75     GetArkUINodeModifiers()->getDatePickerModifier()->setSelectedTextStyle(
76         nativeNode, fontInfo.c_str(), color.GetValue(), fontStyle);
77     return panda::JSValueRef::Undefined(vm);
78 }
79 
ResetSelectedTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)80 ArkUINativeModuleValue DatePickerBridge::ResetSelectedTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
81 {
82     EcmaVM* vm = runtimeCallInfo->GetVM();
83     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
84     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
85     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
86     GetArkUINodeModifiers()->getDatePickerModifier()->resetSelectedTextStyle(nativeNode);
87     return panda::JSValueRef::Undefined(vm);
88 }
89 
SetTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)90 ArkUINativeModuleValue DatePickerBridge::SetTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
91 {
92     EcmaVM* vm = runtimeCallInfo->GetVM();
93     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
94     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
95     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
96     Local<JSValueRef> textColorArgs = runtimeCallInfo->GetCallArgRef(NUM_1);
97     Local<JSValueRef> fontSizeArgs = runtimeCallInfo->GetCallArgRef(NUM_2);
98     Local<JSValueRef> fontWeightArgs = runtimeCallInfo->GetCallArgRef(NUM_3);
99     Local<JSValueRef> fontFamilyArgs = runtimeCallInfo->GetCallArgRef(NUM_4);
100     Local<JSValueRef> fontStyleArgs = runtimeCallInfo->GetCallArgRef(NUM_5);
101 
102     if (textColorArgs->IsUndefined() && fontSizeArgs->IsUndefined() && fontWeightArgs->IsUndefined() &&
103         fontFamilyArgs->IsUndefined() && fontStyleArgs->IsUndefined()) {
104         GetArkUINodeModifiers()->getDatePickerModifier()->resetSelectedTextStyle(nativeNode);
105     }
106     CalcDimension fontSizeData;
107     std::string fontSize;
108     if (!ArkTSUtils::ParseJsDimensionFp(vm, fontSizeArgs, fontSizeData, true, false) ||
109         fontSizeData.Unit() == DimensionUnit::PERCENT) {
110         fontSizeData = Dimension(-1);
111     }
112     fontSize = fontSizeData.ToString();
113 
114     std::string weight = "FontWeight.Regular";
115     if (fontWeightArgs->IsString(vm) || fontWeightArgs->IsNumber()) {
116         weight = fontWeightArgs->ToString(vm)->ToString(vm);
117     }
118 
119     std::string fontFamily;
120     if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, fontFamilyArgs, fontFamily) || fontFamily.empty()) {
121         fontFamily = DEFAULT_FAMILY;
122     }
123     int32_t fontStyle = 0;
124     if (fontStyleArgs->IsNumber()) {
125         fontStyle = fontStyleArgs->Int32Value(vm);
126     }
127     Color color;
128     if (!ArkTSUtils::ParseJsColorAlpha(vm, textColorArgs, color)) {
129         Color::ParseColorString("#ff182431", color);
130     }
131     std::string fontInfo =
132         StringUtils::FormatString(FORMAT_FONT.c_str(), fontSize.c_str(), weight.c_str(), fontFamily.c_str());
133     GetArkUINodeModifiers()->getDatePickerModifier()->setDatePickerTextStyle(
134         nativeNode, fontInfo.c_str(), color.GetValue(), fontStyle);
135     return panda::JSValueRef::Undefined(vm);
136 }
137 
ResetTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)138 ArkUINativeModuleValue DatePickerBridge::ResetTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
139 {
140     EcmaVM* vm = runtimeCallInfo->GetVM();
141     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
142     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
143     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
144     GetArkUINodeModifiers()->getDatePickerModifier()->resetDatePickerTextStyle(nativeNode);
145     return panda::JSValueRef::Undefined(vm);
146 }
147 
SetDisappearTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)148 ArkUINativeModuleValue DatePickerBridge::SetDisappearTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
149 {
150     EcmaVM* vm = runtimeCallInfo->GetVM();
151     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
152     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
153     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
154     Local<JSValueRef> textColorArgs = runtimeCallInfo->GetCallArgRef(NUM_1);
155     Local<JSValueRef> fontSizeArgs = runtimeCallInfo->GetCallArgRef(NUM_2);
156     Local<JSValueRef> fontWeightArgs = runtimeCallInfo->GetCallArgRef(NUM_3);
157     Local<JSValueRef> fontFamilyArgs = runtimeCallInfo->GetCallArgRef(NUM_4);
158     Local<JSValueRef> fontStyleArgs = runtimeCallInfo->GetCallArgRef(NUM_5);
159 
160     if (textColorArgs->IsUndefined() && fontSizeArgs->IsUndefined() && fontWeightArgs->IsUndefined() &&
161         fontFamilyArgs->IsUndefined() && fontStyleArgs->IsUndefined()) {
162         GetArkUINodeModifiers()->getDatePickerModifier()->resetSelectedTextStyle(nativeNode);
163     }
164     CalcDimension fontSizeData;
165     std::string fontSize;
166     if (!ArkTSUtils::ParseJsDimensionFp(vm, fontSizeArgs, fontSizeData, true, false) ||
167         fontSizeData.Unit() == DimensionUnit::PERCENT) {
168         fontSizeData = Dimension(-1);
169     }
170     fontSize = fontSizeData.ToString();
171 
172     std::string weight = "FontWeight.Regular";
173     if (fontWeightArgs->IsString(vm) || fontWeightArgs->IsNumber()) {
174         weight = fontWeightArgs->ToString(vm)->ToString(vm);
175     }
176 
177     std::string fontFamily;
178     if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, fontFamilyArgs, fontFamily) || fontFamily.empty()) {
179         fontFamily = DEFAULT_FAMILY;
180     }
181     int32_t fontStyle = 0;
182     if (fontStyleArgs->IsNumber()) {
183         fontStyle = fontStyleArgs->Int32Value(vm);
184     }
185     Color color;
186     if (!ArkTSUtils::ParseJsColorAlpha(vm, textColorArgs, color)) {
187         Color::ParseColorString("#ff182431", color);
188     }
189     std::string fontInfo =
190         StringUtils::FormatString(FORMAT_FONT.c_str(), fontSize.c_str(), weight.c_str(), fontFamily.c_str());
191     GetArkUINodeModifiers()->getDatePickerModifier()->setDisappearTextStyle(
192         nativeNode, fontInfo.c_str(), color.GetValue(), fontStyle);
193     return panda::JSValueRef::Undefined(vm);
194 }
195 
ResetDisappearTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)196 ArkUINativeModuleValue DatePickerBridge::ResetDisappearTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
197 {
198     EcmaVM* vm = runtimeCallInfo->GetVM();
199     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
200     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
201     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
202     GetArkUINodeModifiers()->getDatePickerModifier()->resetDisappearTextStyle(nativeNode);
203     return panda::JSValueRef::Undefined(vm);
204 }
205 
SetLunar(ArkUIRuntimeCallInfo * runtimeCallInfo)206 ArkUINativeModuleValue DatePickerBridge::SetLunar(ArkUIRuntimeCallInfo* runtimeCallInfo)
207 {
208     EcmaVM* vm = runtimeCallInfo->GetVM();
209     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
210     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
211     Local<JSValueRef> isLunarArg = runtimeCallInfo->GetCallArgRef(NUM_1);
212     auto nativeNode = nodePtr(nodeArg->ToNativePointer(vm)->Value());
213     if (isLunarArg->IsUndefined() || !isLunarArg->IsBoolean()) {
214         GetArkUINodeModifiers()->getDatePickerModifier()->resetLunar(nativeNode);
215         return panda::JSValueRef::Undefined(vm);
216     }
217     bool isLunar = isLunarArg->ToBoolean(vm)->Value();
218     GetArkUINodeModifiers()->getDatePickerModifier()->setLunar(nativeNode, isLunar);
219     return panda::JSValueRef::Undefined(vm);
220 }
221 
ResetLunar(ArkUIRuntimeCallInfo * runtimeCallInfo)222 ArkUINativeModuleValue DatePickerBridge::ResetLunar(ArkUIRuntimeCallInfo* runtimeCallInfo)
223 {
224     EcmaVM* vm = runtimeCallInfo->GetVM();
225     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
226     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
227     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
228     GetArkUINodeModifiers()->getDatePickerModifier()->resetLunar(nativeNode);
229     return panda::JSValueRef::Undefined(vm);
230 }
231 
SetBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)232 ArkUINativeModuleValue DatePickerBridge::SetBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
233 {
234     CommonBridge::SetBackgroundColor(runtimeCallInfo);
235 
236     EcmaVM* vm = runtimeCallInfo->GetVM();
237     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
238     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
239     Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(1);
240     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
241     Color color;
242     if (!ArkTSUtils::ParseJsColorAlpha(vm, colorArg, color)) {
243         GetArkUINodeModifiers()->getDatePickerModifier()->resetDatePickerBackgroundColor(nativeNode);
244     } else {
245         GetArkUINodeModifiers()->getDatePickerModifier()->setDatePickerBackgroundColor(nativeNode, color.GetValue());
246     }
247     return panda::JSValueRef::Undefined(vm);
248 }
249 
ResetBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)250 ArkUINativeModuleValue DatePickerBridge::ResetBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
251 {
252     CommonBridge::ResetBackgroundColor(runtimeCallInfo);
253     EcmaVM* vm = runtimeCallInfo->GetVM();
254     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
255     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
256     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
257     GetArkUINodeModifiers()->getDatePickerModifier()->resetDatePickerBackgroundColor(nativeNode);
258     return panda::JSValueRef::Undefined(vm);
259 }
260 } // namespace OHOS::Ace::NG
261