• 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 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_native_timepicker_bridge.h"
16 
17 #include "core/interfaces/native/node/api.h"
18 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
19 
20 namespace OHOS::Ace::NG {
21 const std::string DEFAULT_ERR_CODE = "-1";
22 const std::string FORMAT_FONT = "%s|%s|%s";
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 constexpr uint32_t DEFAULT_TIME_PICKER_TEXT_COLOR = 0xFF182431;
30 constexpr uint32_t DEFAULT_TIME_PICKER_SELECTED_TEXT_COLOR = 0xFF007DFF;
31 
SetTimepickerBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)32 ArkUINativeModuleValue TimepickerBridge::SetTimepickerBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
33 {
34     EcmaVM* vm = runtimeCallInfo->GetVM();
35     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
36     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
37     Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
38     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
39     uint32_t color = secondArg->Uint32Value(vm);
40     GetArkUIInternalNodeAPI()->GetTimepickerModifier().SetTimepickerBackgroundColor(nativeNode, color);
41     return panda::JSValueRef::Undefined(vm);
42 }
43 
ResetTimepickerBackgroundColor(ArkUIRuntimeCallInfo * runtimeCallInfo)44 ArkUINativeModuleValue TimepickerBridge::ResetTimepickerBackgroundColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
45 {
46     EcmaVM* vm = runtimeCallInfo->GetVM();
47     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
48     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
49     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
50     GetArkUIInternalNodeAPI()->GetTimepickerModifier().ResetTimepickerBackgroundColor(nativeNode);
51     return panda::JSValueRef::Undefined(vm);
52 }
53 
SetTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)54 ArkUINativeModuleValue TimepickerBridge::SetTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
55 {
56     EcmaVM* vm = runtimeCallInfo->GetVM();
57     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
58     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
59     Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(NUM_1);
60     Local<JSValueRef> fontSizeArg = runtimeCallInfo->GetCallArgRef(NUM_2);
61     Local<JSValueRef> fontWeightArg = runtimeCallInfo->GetCallArgRef(NUM_3);
62     Local<JSValueRef> fontFamilyArg = runtimeCallInfo->GetCallArgRef(NUM_4);
63     Local<JSValueRef> fontStyleArg = runtimeCallInfo->GetCallArgRef(NUM_5);
64     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
65     Color color;
66     if (colorArg->IsNull() || colorArg->IsUndefined() || !ArkTSUtils::ParseJsColorAlpha(vm, colorArg, color)) {
67         color.SetValue(DEFAULT_TIME_PICKER_TEXT_COLOR);
68     }
69     CalcDimension size;
70     if (fontSizeArg->IsNull() || fontSizeArg->IsUndefined()) {
71         size = Dimension(-1);
72     } else {
73         if (!ArkTSUtils::ParseJsDimensionNG(vm, fontSizeArg, size, DimensionUnit::FP, false)) {
74             size = Dimension(-1);
75         }
76     }
77     std::string weight = DEFAULT_ERR_CODE;
78     if (!fontWeightArg->IsNull() && !fontWeightArg->IsUndefined()) {
79         if (fontWeightArg->IsNumber()) {
80             weight = std::to_string(fontWeightArg->Int32Value(vm));
81         } else {
82             if (!ArkTSUtils::ParseJsString(vm, fontWeightArg, weight) || weight.empty()) {
83                 weight = DEFAULT_ERR_CODE;
84             }
85         }
86     }
87     std::string fontFamily;
88     if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, fontFamilyArg, fontFamily) || fontFamily.empty()) {
89         fontFamily = DEFAULT_ERR_CODE;
90     }
91     int32_t styleVal = 0;
92     if (!fontStyleArg->IsNull() && !fontStyleArg->IsUndefined()) {
93         styleVal = fontStyleArg->Int32Value(vm);
94     }
95     std::string fontSizeStr = size.ToString();
96     std::string fontInfo = StringUtils::FormatString(
97         FORMAT_FONT.c_str(), fontSizeStr.c_str(), weight.c_str(), fontFamily.c_str());
98     GetArkUIInternalNodeAPI()->GetTimepickerModifier().SetTimepickerTextStyle(
99         nativeNode, color.GetValue(), fontInfo.c_str(), styleVal);
100     return panda::JSValueRef::Undefined(vm);
101 }
102 
SetSelectedTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)103 ArkUINativeModuleValue TimepickerBridge::SetSelectedTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
104 {
105     EcmaVM* vm = runtimeCallInfo->GetVM();
106     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
107     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
108     Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(NUM_1);
109     Local<JSValueRef> fontSizeArg = runtimeCallInfo->GetCallArgRef(NUM_2);
110     Local<JSValueRef> fontWeightArg = runtimeCallInfo->GetCallArgRef(NUM_3);
111     Local<JSValueRef> fontFamilyArg = runtimeCallInfo->GetCallArgRef(NUM_4);
112     Local<JSValueRef> fontStyleArg = runtimeCallInfo->GetCallArgRef(NUM_5);
113     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
114     Color color;
115     if (colorArg->IsNull() || colorArg->IsUndefined() || !ArkTSUtils::ParseJsColorAlpha(vm, colorArg, color)) {
116         color.SetValue(DEFAULT_TIME_PICKER_SELECTED_TEXT_COLOR);
117     }
118     CalcDimension size;
119     if (fontSizeArg->IsNull() || fontSizeArg->IsUndefined()) {
120         size = Dimension(-1);
121     } else {
122         if (!ArkTSUtils::ParseJsDimensionNG(vm, fontSizeArg, size, DimensionUnit::FP, false)) {
123             size = Dimension(-1);
124         }
125     }
126     std::string weight = DEFAULT_ERR_CODE;
127     if (!fontWeightArg->IsNull() && !fontWeightArg->IsUndefined()) {
128         if (fontWeightArg->IsNumber()) {
129             weight = std::to_string(fontWeightArg->Int32Value(vm));
130         } else {
131             if (!ArkTSUtils::ParseJsString(vm, fontWeightArg, weight) || weight.empty()) {
132                 weight = DEFAULT_ERR_CODE;
133             }
134         }
135     }
136     std::string fontFamily;
137     if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, fontFamilyArg, fontFamily) || fontFamily.empty()) {
138         fontFamily = DEFAULT_ERR_CODE;
139     }
140     int32_t styleVal = 0;
141     if (!fontStyleArg->IsNull() && !fontStyleArg->IsUndefined()) {
142         styleVal = fontStyleArg->Int32Value(vm);
143     }
144     std::string fontSizeStr = size.ToString();
145     std::string fontInfo = StringUtils::FormatString(
146         FORMAT_FONT.c_str(), fontSizeStr.c_str(), weight.c_str(), fontFamily.c_str());
147     GetArkUIInternalNodeAPI()->GetTimepickerModifier().SetTimepickerSelectedTextStyle(
148         nativeNode, color.GetValue(), fontInfo.c_str(), styleVal);
149     return panda::JSValueRef::Undefined(vm);
150 }
151 
SetDisappearTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)152 ArkUINativeModuleValue TimepickerBridge::SetDisappearTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
153 {
154     EcmaVM* vm = runtimeCallInfo->GetVM();
155     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
156     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
157     Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(NUM_1);
158     Local<JSValueRef> fontSizeArg = runtimeCallInfo->GetCallArgRef(NUM_2);
159     Local<JSValueRef> fontWeightArg = runtimeCallInfo->GetCallArgRef(NUM_3);
160     Local<JSValueRef> fontFamilyArg = runtimeCallInfo->GetCallArgRef(NUM_4);
161     Local<JSValueRef> fontStyleArg = runtimeCallInfo->GetCallArgRef(NUM_5);
162     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
163     Color color;
164     if (colorArg->IsNull() || colorArg->IsUndefined() || !ArkTSUtils::ParseJsColorAlpha(vm, colorArg, color)) {
165         color.SetValue(DEFAULT_TIME_PICKER_TEXT_COLOR);
166     }
167     CalcDimension size;
168     if (fontSizeArg->IsNull() || fontSizeArg->IsUndefined()) {
169         size = Dimension(-1);
170     } else {
171         if (!ArkTSUtils::ParseJsDimensionNG(vm, fontSizeArg, size, DimensionUnit::FP, false)) {
172             size = Dimension(-1);
173         }
174     }
175     std::string weight = DEFAULT_ERR_CODE;
176     if (!fontWeightArg->IsNull() && !fontWeightArg->IsUndefined()) {
177         if (fontWeightArg->IsNumber()) {
178             weight = std::to_string(fontWeightArg->Int32Value(vm));
179         } else {
180             if (!ArkTSUtils::ParseJsString(vm, fontWeightArg, weight) || weight.empty()) {
181                 weight = DEFAULT_ERR_CODE;
182             }
183         }
184     }
185     std::string fontFamily;
186     if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, fontFamilyArg, fontFamily) || fontFamily.empty()) {
187         fontFamily = DEFAULT_ERR_CODE;
188     }
189     int32_t styleVal = 0;
190     if (!fontStyleArg->IsNull() && !fontStyleArg->IsUndefined()) {
191         styleVal = fontStyleArg->Int32Value(vm);
192     }
193     std::string fontSizeStr = size.ToString();
194     std::string fontInfo = StringUtils::FormatString(
195         FORMAT_FONT.c_str(), fontSizeStr.c_str(), weight.c_str(), fontFamily.c_str());
196     GetArkUIInternalNodeAPI()->GetTimepickerModifier().SetTimepickerDisappearTextStyle(
197         nativeNode, color.GetValue(), fontInfo.c_str(), styleVal);
198     return panda::JSValueRef::Undefined(vm);
199 }
200 
ResetTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)201 ArkUINativeModuleValue TimepickerBridge::ResetTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
202 {
203     EcmaVM* vm = runtimeCallInfo->GetVM();
204     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
205     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
206     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
207     GetArkUIInternalNodeAPI()->GetTimepickerModifier().ResetTimepickerTextStyle(nativeNode);
208     return panda::JSValueRef::Undefined(vm);
209 }
210 
ResetSelectedTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)211 ArkUINativeModuleValue TimepickerBridge::ResetSelectedTextStyle(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     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
217     GetArkUIInternalNodeAPI()->GetTimepickerModifier().ResetTimepickerSelectedTextStyle(nativeNode);
218     return panda::JSValueRef::Undefined(vm);
219 }
220 
ResetDisappearTextStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)221 ArkUINativeModuleValue TimepickerBridge::ResetDisappearTextStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
222 {
223     EcmaVM* vm = runtimeCallInfo->GetVM();
224     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
225     Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
226     void* nativeNode = firstArg->ToNativePointer(vm)->Value();
227     GetArkUIInternalNodeAPI()->GetTimepickerModifier().ResetTimepickerDisappearTextStyle(nativeNode);
228     return panda::JSValueRef::Undefined(vm);
229 }
230 
SetTimepickerUseMilitaryTime(ArkUIRuntimeCallInfo * runtimeCallInfo)231 ArkUINativeModuleValue TimepickerBridge::SetTimepickerUseMilitaryTime(ArkUIRuntimeCallInfo* runtimeCallInfo)
232 {
233     EcmaVM* vm = runtimeCallInfo->GetVM();
234     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
235     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
236     Local<JSValueRef> useMilitaryArg = runtimeCallInfo->GetCallArgRef(NUM_1);
237     void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
238     if (useMilitaryArg->IsBoolean()) {
239         bool value = useMilitaryArg->ToBoolean(vm)->Value();
240         GetArkUIInternalNodeAPI()->GetTimepickerModifier().SetTimepickerUseMilitaryTime(nativeNode, value);
241     } else {
242         GetArkUIInternalNodeAPI()->GetTimepickerModifier().ResetTimepickerUseMilitaryTime(nativeNode);
243     }
244     return panda::JSValueRef::Undefined(vm);
245 }
246 
ResetTimepickerUseMilitaryTime(ArkUIRuntimeCallInfo * runtimeCallInfo)247 ArkUINativeModuleValue TimepickerBridge::ResetTimepickerUseMilitaryTime(ArkUIRuntimeCallInfo* runtimeCallInfo)
248 {
249     EcmaVM* vm = runtimeCallInfo->GetVM();
250     CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
251     Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
252     void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
253     GetArkUIInternalNodeAPI()->GetTimepickerModifier().ResetTimepickerUseMilitaryTime(nativeNode);
254     return panda::JSValueRef::Undefined(vm);
255 }
256 } // namespace OHOS::Ace::NG