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_calendar_picker_bridge.h"
17 #include "core/components/calendar/calendar_theme.h"
18 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
19 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_common_bridge.h"
20 
21 namespace OHOS::Ace::NG {
22 constexpr int NUM_0 = 0;
23 constexpr int NUM_1 = 1;
24 constexpr int NUM_2 = 2;
25 constexpr int NUM_3 = 3;
26 constexpr int SIZE_OF_TWO = 2;
27 constexpr Dimension DEFAULT_TEXTSTYLE_FONTSIZE = 16.0_fp;
28 
ParseCalendarPickerPadding(const EcmaVM * vm,const Local<JSValueRef> & value,CalcDimension & dim,ArkUISizeType & result)29 void ParseCalendarPickerPadding(
30     const EcmaVM* vm, const Local<JSValueRef>& value, CalcDimension& dim, ArkUISizeType& result)
31 {
32     if (ArkTSUtils::ParseJsDimensionVpNG(vm, value, dim)) {
33         if (LessOrEqual(dim.Value(), 0.0)) {
34             dim.SetValue(-1);
35             dim.SetUnit(DimensionUnit::VP);
36         }
37         result.unit = static_cast<int8_t>(dim.Unit());
38         if (dim.CalcValue() != "") {
39             result.string = dim.CalcValue().c_str();
40         } else {
41             result.value = dim.Value();
42         }
43     } else {
44         dim.SetValue(-1);
45         dim.SetUnit(DimensionUnit::VP);
46         result.unit = static_cast<int8_t>(dim.Unit());
47         result.value = dim.Value();
48     }
49 }
50 
SetTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)51 ArkUINativeModuleValue CalendarPickerBridge::SetTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
52 {
53     EcmaVM* vm = runtimeCallInfo->GetVM();
54     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
55     auto pipeline = PipelineBase::GetCurrentContext();
56     CHECK_NULL_RETURN(pipeline, panda::NativePointerRef::New(vm, nullptr));
57     RefPtr<CalendarTheme> calendarTheme = pipeline->GetTheme<CalendarTheme>();
58     CHECK_NULL_RETURN(calendarTheme, panda::NativePointerRef::New(vm, nullptr));
59     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
60     Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(NUM_1);
61     Local<JSValueRef> fontSizeArg = runtimeCallInfo->GetCallArgRef(NUM_2);
62     Local<JSValueRef> fontWeightArg = runtimeCallInfo->GetCallArgRef(NUM_3);
63     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
64     Color textColor = calendarTheme->GetEntryFontColor();
65     if (!colorArg->IsUndefined()) {
66         ArkTSUtils::ParseJsColorAlpha(vm, colorArg, textColor);
67     }
68     CalcDimension fontSizeData(DEFAULT_TEXTSTYLE_FONTSIZE);
69     std::string fontSize = fontSizeData.ToString();
70     if (ArkTSUtils::ParseJsDimensionFp(vm, fontSizeArg, fontSizeData) && !fontSizeData.IsNegative() &&
71         fontSizeData.Unit() != DimensionUnit::PERCENT) {
72         fontSize = fontSizeData.ToString();
73     }
74     std::string fontWeight = "regular";
75     if (fontWeightArg->IsString(vm) || fontWeightArg->IsNumber()) {
76         fontWeight = fontWeightArg->ToString(vm)->ToString(vm);
77     }
78     GetArkUINodeModifiers()->getCalendarPickerModifier()->setTextStyle(
79         nativeNode, textColor.GetValue(), fontSize.c_str(), fontWeight.c_str());
80     return panda::JSValueRef::Undefined(vm);
81 }
82 
ResetTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)83 ArkUINativeModuleValue CalendarPickerBridge::ResetTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
84 {
85     EcmaVM* vm = runtimeCallInfo->GetVM();
86     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
87     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
88     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
89     GetArkUINodeModifiers()->getCalendarPickerModifier()->resetTextStyle(nativeNode);
90     return panda::JSValueRef::Undefined(vm);
91 }
92 
SetEdgeAlign(ArkUIRuntimeCallInfo * runtimeCallInfo)93 ArkUINativeModuleValue CalendarPickerBridge::SetEdgeAlign(ArkUIRuntimeCallInfo* runtimeCallInfo)
94 {
95     EcmaVM* vm = runtimeCallInfo->GetVM();
96     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
97     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
98     Local<JSValueRef> alignTypeArg = runtimeCallInfo->GetCallArgRef(NUM_1);
99     Local<JSValueRef> dxArg = runtimeCallInfo->GetCallArgRef(NUM_2);
100     Local<JSValueRef> dyArg = runtimeCallInfo->GetCallArgRef(NUM_3);
101     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
102     int alignType = NUM_2;
103     if (!alignTypeArg->IsNull() && !alignTypeArg->IsUndefined() && alignTypeArg->IsNumber()) {
104         alignType = alignTypeArg->ToNumber(vm)->Value();
105     }
106     CalcDimension dx;
107     CalcDimension dy;
108     if (!dxArg->IsNull() && !dxArg->IsUndefined()) {
109         ArkTSUtils::ParseJsDimensionVp(vm, dxArg, dx);
110     }
111     if (!dyArg->IsNull() && !dyArg->IsUndefined()) {
112         ArkTSUtils::ParseJsDimensionVp(vm, dyArg, dy);
113     }
114     ArkUI_Float32 values[SIZE_OF_TWO];
115     int units[SIZE_OF_TWO];
116     values[NUM_0] = dx.Value();
117     units[NUM_0] = static_cast<int>(dx.Unit());
118     values[NUM_1] = dy.Value();
119     units[NUM_1] = static_cast<int>(dy.Unit());
120     GetArkUINodeModifiers()->getCalendarPickerModifier()->setEdgeAlign(
121         nativeNode, values, units, SIZE_OF_TWO, alignType);
122     return panda::JSValueRef::Undefined(vm);
123 }
124 
ResetEdgeAlign(ArkUIRuntimeCallInfo * runtimeCallInfo)125 ArkUINativeModuleValue CalendarPickerBridge::ResetEdgeAlign(ArkUIRuntimeCallInfo* runtimeCallInfo)
126 {
127     EcmaVM* vm = runtimeCallInfo->GetVM();
128     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
129     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
130     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
131     GetArkUINodeModifiers()->getCalendarPickerModifier()->resetEdgeAlign(nativeNode);
132     return panda::JSValueRef::Undefined(vm);
133 }
134 
SetCalendarPickerPadding(ArkUIRuntimeCallInfo * runtimeCallInfo)135 ArkUINativeModuleValue CalendarPickerBridge::SetCalendarPickerPadding(ArkUIRuntimeCallInfo* runtimeCallInfo)
136 {
137     EcmaVM* vm = runtimeCallInfo->GetVM();
138     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
139     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
140     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
141     Local<JSValueRef> topArg = runtimeCallInfo->GetCallArgRef(1);    // 1: index of parameter top
142     Local<JSValueRef> rightArg = runtimeCallInfo->GetCallArgRef(2);  // 2: index of parameter right
143     Local<JSValueRef> bottomArg = runtimeCallInfo->GetCallArgRef(3); // 3: index of parameter bottom
144     Local<JSValueRef> leftArg = runtimeCallInfo->GetCallArgRef(4);   // 4: index of parameter left
145     if (leftArg->IsUndefined() && rightArg->IsUndefined() && topArg->IsUndefined() && bottomArg->IsUndefined()) {
146         GetArkUINodeModifiers()->getCalendarPickerModifier()->resetCalendarPickerPadding(nativeNode);
147         return panda::JSValueRef::Undefined(vm);
148     }
149 
150     struct ArkUISizeType top = { 0.0, static_cast<int8_t>(DimensionUnit::VP) };
151     struct ArkUISizeType right = { 0.0, static_cast<int8_t>(DimensionUnit::VP) };
152     struct ArkUISizeType bottom = { 0.0, static_cast<int8_t>(DimensionUnit::VP) };
153     struct ArkUISizeType left = { 0.0, static_cast<int8_t>(DimensionUnit::VP) };
154 
155     CalcDimension topDim(0, DimensionUnit::VP);
156     CalcDimension rightDim(0, DimensionUnit::VP);
157     CalcDimension bottomDim(0, DimensionUnit::VP);
158     CalcDimension leftDim(0, DimensionUnit::VP);
159     ParseCalendarPickerPadding(vm, topArg, topDim, top);
160     ParseCalendarPickerPadding(vm, rightArg, rightDim, right);
161     ParseCalendarPickerPadding(vm, bottomArg, bottomDim, bottom);
162     ParseCalendarPickerPadding(vm, leftArg, leftDim, left);
163     GetArkUINodeModifiers()->getCalendarPickerModifier()->setCalendarPickerPadding(
164         nativeNode, &top, &right, &bottom, &left);
165 
166     return panda::JSValueRef::Undefined(vm);
167 }
168 
ResetCalendarPickerPadding(ArkUIRuntimeCallInfo * runtimeCallInfo)169 ArkUINativeModuleValue CalendarPickerBridge::ResetCalendarPickerPadding(ArkUIRuntimeCallInfo* runtimeCallInfo)
170 {
171     EcmaVM* vm = runtimeCallInfo->GetVM();
172     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
173     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
174     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
175     GetArkUINodeModifiers()->getCalendarPickerModifier()->resetCalendarPickerPadding(nativeNode);
176     return panda::JSValueRef::Undefined(vm);
177 }
178 
SetCalendarPickerBorder(ArkUIRuntimeCallInfo * runtimeCallInfo)179 ArkUINativeModuleValue CalendarPickerBridge::SetCalendarPickerBorder(ArkUIRuntimeCallInfo* runtimeCallInfo)
180 {
181     EcmaVM* vm = runtimeCallInfo->GetVM();
182     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
183     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
184     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
185 
186     Local<JSValueRef> leftArg = runtimeCallInfo->GetCallArgRef(5);   // 5: index of parameter left color
187     Local<JSValueRef> rightArg = runtimeCallInfo->GetCallArgRef(6);  // 6: index of parameter right color
188     Local<JSValueRef> topArg = runtimeCallInfo->GetCallArgRef(7);    // 7: index of parameter top color
189     Local<JSValueRef> bottomArg = runtimeCallInfo->GetCallArgRef(8); // 8: index of parameter bottom color
190 
191     CommonBridge::SetBorder(runtimeCallInfo);
192 
193     CalcDimension borderWidth;
194     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
195     if (!ArkTSUtils::ParseJsDimensionVpNG(vm, secondArg, borderWidth) || !borderWidth.IsValid()) {
196         GetArkUINodeModifiers()->getCalendarPickerModifier()->resetCalendarPickerBorderWidth(nativeNode);
197         return panda::JSValueRef::Undefined(vm);
198     }
199 
200     if (leftArg->IsUndefined() && rightArg->IsUndefined() && topArg->IsUndefined() && bottomArg->IsUndefined()) {
201         auto pipeline = PipelineBase::GetCurrentContext();
202         CHECK_NULL_RETURN(pipeline, panda::NativePointerRef::New(vm, nullptr));
203         RefPtr<CalendarTheme> calendarTheme = pipeline->GetTheme<CalendarTheme>();
204         CHECK_NULL_RETURN(calendarTheme, panda::NativePointerRef::New(vm, nullptr));
205         GetArkUINodeModifiers()->getCalendarPickerModifier()->setCalendarPickerBorder(
206             nativeNode, calendarTheme->GetEntryBorderColor().GetValue());
207     }
208     return panda::JSValueRef::Undefined(vm);
209 }
210 
ResetCalendarPickerBorder(ArkUIRuntimeCallInfo * runtimeCallInfo)211 ArkUINativeModuleValue CalendarPickerBridge::ResetCalendarPickerBorder(ArkUIRuntimeCallInfo* runtimeCallInfo)
212 {
213     EcmaVM* vm = runtimeCallInfo->GetVM();
214     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
215     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
216     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
217     GetArkUINodeModifiers()->getCalendarPickerModifier()->resetCalendarPickerBorder(nativeNode);
218     return panda::JSValueRef::Undefined(vm);
219 }
220 
SetCalendarPickerHeight(ArkUIRuntimeCallInfo * runtimeCallInfo)221 ArkUINativeModuleValue CalendarPickerBridge::SetCalendarPickerHeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
222 {
223     EcmaVM* vm = runtimeCallInfo->GetVM();
224     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
225     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
226     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
227     Local<JSValueRef> jsValue = runtimeCallInfo->GetCallArgRef(NUM_1);
228     CalcDimension height;
229     std::string calcStr;
230     if (!ArkTSUtils::ParseJsDimensionVpNG(vm, jsValue, height)) {
231         GetArkUINodeModifiers()->getCalendarPickerModifier()->resetCalendarPickerHeight(nativeNode);
232     } else {
233         if (LessNotEqual(height.Value(), 0.0)) {
234             if (AceApplicationInfo::GetInstance().GreatOrEqualTargetAPIVersion(PlatformVersion::VERSION_TWELVE)) {
235                 GetArkUINodeModifiers()->getCalendarPickerModifier()->resetCalendarPickerHeight(nativeNode);
236                 return panda::JSValueRef::Undefined(vm);
237             }
238             height.SetValue(0.0);
239         }
240         if (height.Unit() == DimensionUnit::CALC) {
241             GetArkUINodeModifiers()->getCalendarPickerModifier()->setCalendarPickerHeight(
242                 nativeNode, height.Value(), static_cast<int32_t>(height.Unit()));
243         } else {
244             GetArkUINodeModifiers()->getCalendarPickerModifier()->setCalendarPickerHeight(
245                 nativeNode, height.Value(), static_cast<int32_t>(height.Unit()));
246         }
247     }
248     return panda::JSValueRef::Undefined(vm);
249 }
250 
ResetCalendarPickerHeight(ArkUIRuntimeCallInfo * runtimeCallInfo)251 ArkUINativeModuleValue CalendarPickerBridge::ResetCalendarPickerHeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
252 {
253     EcmaVM* vm = runtimeCallInfo->GetVM();
254     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
255     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
256     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
257     GetArkUINodeModifiers()->getCalendarPickerModifier()->resetCalendarPickerHeight(nativeNode);
258     return panda::JSValueRef::Undefined(vm);
259 }
260 
SetCalendarPickerBorderColor(ArkUIRuntimeCallInfo * runtimeCallInfo)261 ArkUINativeModuleValue CalendarPickerBridge::SetCalendarPickerBorderColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
262 {
263     EcmaVM* vm = runtimeCallInfo->GetVM();
264     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
265     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
266     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
267     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
268     Color color;
269     if (!ArkTSUtils::ParseJsColorAlpha(vm, secondArg, color)) {
270         GetArkUINodeModifiers()->getCalendarPickerModifier()->resetCalendarPickerBorderColor(nativeNode);
271     } else {
272         GetArkUINodeModifiers()->getCalendarPickerModifier()->setCalendarPickerBorderColor(
273             nativeNode, color.GetValue());
274     }
275     return panda::JSValueRef::Undefined(vm);
276 }
277 
ResetCalendarPickerBorderColor(ArkUIRuntimeCallInfo * runtimeCallInfo)278 ArkUINativeModuleValue CalendarPickerBridge::ResetCalendarPickerBorderColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
279 {
280     EcmaVM* vm = runtimeCallInfo->GetVM();
281     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
282     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
283     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
284     GetArkUINodeModifiers()->getCalendarPickerModifier()->resetCalendarPickerBorderColor(nativeNode);
285     return panda::JSValueRef::Undefined(vm);
286 }
287 
SetCalendarPickerBorderRadius(ArkUIRuntimeCallInfo * runtimeCallInfo)288 ArkUINativeModuleValue CalendarPickerBridge::SetCalendarPickerBorderRadius(ArkUIRuntimeCallInfo* runtimeCallInfo)
289 {
290     EcmaVM* vm = runtimeCallInfo->GetVM();
291     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
292     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
293     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
294     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
295 
296     CalcDimension borderRadius;
297     if (!ArkTSUtils::ParseJsDimensionVpNG(vm, secondArg, borderRadius, true)) {
298         GetArkUINodeModifiers()->getCalendarPickerModifier()->resetCalendarPickerBorderRadius(nativeNode);
299         return panda::JSValueRef::Undefined(vm);
300     }
301 
302     if (LessNotEqual(borderRadius.Value(), 0.0)) {
303         GetArkUINodeModifiers()->getCalendarPickerModifier()->resetCalendarPickerBorderRadius(nativeNode);
304         return panda::JSValueRef::Undefined(vm);
305     }
306 
307     GetArkUINodeModifiers()->getCalendarPickerModifier()->setCalendarPickerBorderRadius(nativeNode,
308         borderRadius.Value(), static_cast<int>(borderRadius.Unit()));
309     return panda::JSValueRef::Undefined(vm);
310 }
311 
ResetCalendarPickerBorderRadius(ArkUIRuntimeCallInfo * runtimeCallInfo)312 ArkUINativeModuleValue CalendarPickerBridge::ResetCalendarPickerBorderRadius(ArkUIRuntimeCallInfo* runtimeCallInfo)
313 {
314     EcmaVM* vm = runtimeCallInfo->GetVM();
315     CHECK_NULL_RETURN(vm, panda::JSValueRef::Undefined(vm));
316     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
317     auto nativeNode = nodePtr(firstArg->ToNativePointer(vm)->Value());
318     GetArkUINodeModifiers()->getCalendarPickerModifier()->resetCalendarPickerBorderRadius(nativeNode);
319     return panda::JSValueRef::Undefined(vm);
320 }
321 } // namespace OHOS::Ace::NG
322