• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/interfaces/native/node/api.h"
18 #include "core/components/calendar/calendar_theme.h"
19 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
20 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_common_bridge.h"
21 
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 SIZE_OF_TWO = 2;
28 constexpr Dimension DEFAULT_TEXTSTYLE_FONTSIZE = 16.0_fp;
29 
ParseCalendarPickerPadding(const EcmaVM * vm,const Local<JSValueRef> & value,CalcDimension & dimen,ArkUISizeType & result)30 void ParseCalendarPickerPadding(
31     const EcmaVM* vm, const Local<JSValueRef>& value, CalcDimension& dimen, ArkUISizeType& result)
32 {
33     if (value->IsNull() || value->IsUndefined()) {
34         dimen.SetValue(-1);
35         dimen.SetUnit(DimensionUnit::VP);
36         result.unit = static_cast<int8_t>(dimen.Unit());
37         result.value = dimen.Value();
38     }
39     if (ArkTSUtils::ParseJsDimensionVp(vm, value, dimen)) {
40         if (LessOrEqual(dimen.Value(), 0.0)) {
41             dimen.SetValue(-1);
42             dimen.SetUnit(DimensionUnit::VP);
43         }
44         result.unit = static_cast<int8_t>(dimen.Unit());
45         if (dimen.CalcValue() != "") {
46             result.string = dimen.CalcValue().c_str();
47         } else {
48             result.value = dimen.Value();
49         }
50     }
51 }
52 
SetTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)53 ArkUINativeModuleValue CalendarPickerBridge::SetTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
54 {
55     EcmaVM* vm = runtimeCallInfo->GetVM();
56     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
57     auto pipeline = PipelineBase::GetCurrentContext();
58     CHECK_NULL_RETURN(pipeline, panda::NativePointerRef::New(vm, nullptr));
59     RefPtr<CalendarTheme> calendarTheme = pipeline->GetTheme<CalendarTheme>();
60     CHECK_NULL_RETURN(calendarTheme, panda::NativePointerRef::New(vm, nullptr));
61     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
62     Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(NUM_1);
63     Local<JSValueRef> fontSizeArg = runtimeCallInfo->GetCallArgRef(NUM_2);
64     Local<JSValueRef> fontWeightArg = runtimeCallInfo->GetCallArgRef(NUM_3);
65     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
66     Color textColor = calendarTheme->GetEntryFontColor();
67     if (!colorArg->IsUndefined()) {
68         ArkTSUtils::ParseJsColorAlpha(vm, colorArg, textColor);
69     }
70     CalcDimension fontSizeData(DEFAULT_TEXTSTYLE_FONTSIZE);
71     std::string fontSize = fontSizeData.ToString();
72     if (ArkTSUtils::ParseJsDimensionFp(vm, fontSizeArg, fontSizeData) && !fontSizeData.IsNegative() &&
73         fontSizeData.Unit() != DimensionUnit::PERCENT) {
74         fontSize = fontSizeData.ToString();
75     }
76     std::string fontWeight = "regular";
77     if (fontWeightArg->IsString() || fontWeightArg->IsNumber()) {
78         fontWeight = fontWeightArg->ToString(vm)->ToString();
79     }
80     GetArkUIInternalNodeAPI()->GetCalendarPickerModifier().SetTextStyle(
81         nativeNode, textColor.GetValue(), fontSize.c_str(), fontWeight.c_str());
82     return panda::JSValueRef::Undefined(vm);
83 }
84 
ResetTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)85 ArkUINativeModuleValue CalendarPickerBridge::ResetTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
86 {
87     EcmaVM* vm = runtimeCallInfo->GetVM();
88     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
89     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
90     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
91     GetArkUIInternalNodeAPI()->GetCalendarPickerModifier().ResetTextStyle(nativeNode);
92     return panda::JSValueRef::Undefined(vm);
93 }
94 
SetEdgeAlign(ArkUIRuntimeCallInfo * runtimeCallInfo)95 ArkUINativeModuleValue CalendarPickerBridge::SetEdgeAlign(ArkUIRuntimeCallInfo* runtimeCallInfo)
96 {
97     EcmaVM* vm = runtimeCallInfo->GetVM();
98     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
99     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
100     Local<JSValueRef> alignTypeArg = runtimeCallInfo->GetCallArgRef(NUM_1);
101     Local<JSValueRef> dxArg = runtimeCallInfo->GetCallArgRef(NUM_2);
102     Local<JSValueRef> dyArg = runtimeCallInfo->GetCallArgRef(NUM_3);
103     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
104     if (!alignTypeArg->IsNull() && !alignTypeArg->IsUndefined() && alignTypeArg->IsNumber()) {
105         int alignType = alignTypeArg->ToNumber(vm)->Value();
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 
115         double values[SIZE_OF_TWO];
116         int units[SIZE_OF_TWO];
117 
118         values[NUM_0] = dx.Value();
119         units[NUM_0] = static_cast<int>(dx.Unit());
120         values[NUM_1] = dy.Value();
121         units[NUM_1] = static_cast<int>(dy.Unit());
122 
123         GetArkUIInternalNodeAPI()->GetCalendarPickerModifier().SetEdgeAlign(
124             nativeNode, values, units, SIZE_OF_TWO, alignType);
125     } else {
126         GetArkUIInternalNodeAPI()->GetCalendarPickerModifier().ResetEdgeAlign(nativeNode);
127     }
128     return panda::JSValueRef::Undefined(vm);
129 }
130 
ResetEdgeAlign(ArkUIRuntimeCallInfo * runtimeCallInfo)131 ArkUINativeModuleValue CalendarPickerBridge::ResetEdgeAlign(ArkUIRuntimeCallInfo* runtimeCallInfo)
132 {
133     EcmaVM* vm = runtimeCallInfo->GetVM();
134     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
135     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
136     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
137     GetArkUIInternalNodeAPI()->GetCalendarPickerModifier().ResetEdgeAlign(nativeNode);
138     return panda::JSValueRef::Undefined(vm);
139 }
140 
SetCalendarPickerPadding(ArkUIRuntimeCallInfo * runtimeCallInfo)141 ArkUINativeModuleValue CalendarPickerBridge::SetCalendarPickerPadding(ArkUIRuntimeCallInfo* runtimeCallInfo)
142 {
143     EcmaVM* vm = runtimeCallInfo->GetVM();
144     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
145     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
146     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
147     Local<JSValueRef> topArg = runtimeCallInfo->GetCallArgRef(1);    // 1: index of parameter top
148     Local<JSValueRef> rightArg = runtimeCallInfo->GetCallArgRef(2);  // 2: index of parameter right
149     Local<JSValueRef> bottomArg = runtimeCallInfo->GetCallArgRef(3); // 3: index of parameter bottom
150     Local<JSValueRef> leftArg = runtimeCallInfo->GetCallArgRef(4);   // 4: index of parameter left
151     if (leftArg->IsUndefined() && rightArg->IsUndefined() && topArg->IsUndefined() && bottomArg->IsUndefined()) {
152         GetArkUIInternalNodeAPI()->GetCalendarPickerModifier().ResetCalendarPickerPadding(nativeNode);
153         return panda::JSValueRef::Undefined(vm);
154     }
155 
156     struct ArkUISizeType top = { 0.0, static_cast<int8_t>(DimensionUnit::VP) };
157     struct ArkUISizeType right = { 0.0, static_cast<int8_t>(DimensionUnit::VP) };
158     struct ArkUISizeType bottom = { 0.0, static_cast<int8_t>(DimensionUnit::VP) };
159     struct ArkUISizeType left = { 0.0, static_cast<int8_t>(DimensionUnit::VP) };
160 
161     CalcDimension topDimen(0, DimensionUnit::VP);
162     CalcDimension rightDimen(0, DimensionUnit::VP);
163     CalcDimension bottomDimen(0, DimensionUnit::VP);
164     CalcDimension leftDimen(0, DimensionUnit::VP);
165     ParseCalendarPickerPadding(vm, topArg, topDimen, top);
166     ParseCalendarPickerPadding(vm, rightArg, rightDimen, right);
167     ParseCalendarPickerPadding(vm, bottomArg, bottomDimen, bottom);
168     ParseCalendarPickerPadding(vm, leftArg, leftDimen, left);
169     GetArkUIInternalNodeAPI()->GetCalendarPickerModifier().SetCalendarPickerPadding(
170         nativeNode, &top, &right, &bottom, &left);
171 
172     return panda::JSValueRef::Undefined(vm);
173 }
174 
ResetCalendarPickerPadding(ArkUIRuntimeCallInfo * runtimeCallInfo)175 ArkUINativeModuleValue CalendarPickerBridge::ResetCalendarPickerPadding(ArkUIRuntimeCallInfo* runtimeCallInfo)
176 {
177     EcmaVM* vm = runtimeCallInfo->GetVM();
178     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
179     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
180     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
181     GetArkUIInternalNodeAPI()->GetCalendarPickerModifier().ResetCalendarPickerPadding(nativeNode);
182     return panda::JSValueRef::Undefined(vm);
183 }
184 
SetCalendarPickerBorder(ArkUIRuntimeCallInfo * runtimeCallInfo)185 ArkUINativeModuleValue CalendarPickerBridge::SetCalendarPickerBorder(ArkUIRuntimeCallInfo* runtimeCallInfo)
186 {
187     EcmaVM* vm = runtimeCallInfo->GetVM();
188     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
189     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
190     auto nativeNode = firstArg->ToNativePointer(vm)->Value();
191 
192     Local<JSValueRef> leftArg = runtimeCallInfo->GetCallArgRef(5);   // 5: index of parameter left color
193     Local<JSValueRef> rightArg = runtimeCallInfo->GetCallArgRef(6);  // 6: index of parameter right color
194     Local<JSValueRef> topArg = runtimeCallInfo->GetCallArgRef(7);    // 7: index of parameter top color
195     Local<JSValueRef> bottomArg = runtimeCallInfo->GetCallArgRef(8); // 8: index of parameter bottom color
196 
197     CommonBridge::SetBorder(runtimeCallInfo);
198 
199     if (leftArg->IsUndefined() && rightArg->IsUndefined() && topArg->IsUndefined() && bottomArg->IsUndefined()) {
200         auto pipeline = PipelineBase::GetCurrentContext();
201         CHECK_NULL_RETURN(pipeline, panda::NativePointerRef::New(vm, nullptr));
202         RefPtr<CalendarTheme> calendarTheme = pipeline->GetTheme<CalendarTheme>();
203         CHECK_NULL_RETURN(calendarTheme, panda::NativePointerRef::New(vm, nullptr));
204         GetArkUIInternalNodeAPI()->GetCalendarPickerModifier().SetCalendarPickerBorder(
205             nativeNode, calendarTheme->GetEntryBorderColor().GetValue());
206     }
207     return panda::JSValueRef::Undefined(vm);
208 }
209 
ResetCalendarPickerBorder(ArkUIRuntimeCallInfo * runtimeCallInfo)210 ArkUINativeModuleValue CalendarPickerBridge::ResetCalendarPickerBorder(ArkUIRuntimeCallInfo* runtimeCallInfo)
211 {
212     EcmaVM* vm = runtimeCallInfo->GetVM();
213     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
214     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
215     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
216     GetArkUIInternalNodeAPI()->GetCalendarPickerModifier().ResetCalendarPickerBorder(nativeNode);
217     return panda::JSValueRef::Undefined(vm);
218 }
219 } // namespace OHOS::Ace::NG
220