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_text_timer_bridge.h"
16 #include "base/utils/string_utils.h"
17 #include "base/utils/utils.h"
18 #include "core/interfaces/native/node/api.h"
19 #include "frameworks/base/geometry/calc_dimension.h"
20 #include "frameworks/base/geometry/dimension.h"
21 #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_value_conversions.h"
22 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
23
24 namespace OHOS::Ace::NG {
25 namespace {
26 constexpr int32_t NUM_0 = 0;
27 constexpr int32_t NUM_1 = 1;
28 const std::string DEFAULT_STR = "-1";
29 } // namespace
30
SetFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)31 ArkUINativeModuleValue TextTimerBridge::SetFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
32 {
33 EcmaVM* vm = runtimeCallInfo->GetVM();
34 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
35 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
36 Local<JSValueRef> paramArg = runtimeCallInfo->GetCallArgRef(NUM_1);
37 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
38 Color color;
39 if (!ArkTSUtils::ParseJsColorAlpha(vm, paramArg, color)) {
40 GetArkUIInternalNodeAPI()->GetTextTimerModifier().ResetFontColor(nativeNode);
41 } else {
42 GetArkUIInternalNodeAPI()->GetTextTimerModifier().SetFontColor(nativeNode, color.GetValue());
43 }
44 return panda::JSValueRef::Undefined(vm);
45 }
46
ResetFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)47 ArkUINativeModuleValue TextTimerBridge::ResetFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
48 {
49 EcmaVM* vm = runtimeCallInfo->GetVM();
50 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
51 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
52 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
53 GetArkUIInternalNodeAPI()->GetTextTimerModifier().ResetFontColor(nativeNode);
54 return panda::JSValueRef::Undefined(vm);
55 }
56
SetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)57 ArkUINativeModuleValue TextTimerBridge::SetFontSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
58 {
59 EcmaVM* vm = runtimeCallInfo->GetVM();
60 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
61 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
62 Local<JSValueRef> fontSizeArg = runtimeCallInfo->GetCallArgRef(NUM_1);
63 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
64 CalcDimension fontSize;
65 if (!ArkTSUtils::ParseJsDimensionFp(vm, fontSizeArg, fontSize) || fontSize.Value() < 0 ||
66 fontSize.Unit() == DimensionUnit::PERCENT) {
67 GetArkUIInternalNodeAPI()->GetTextTimerModifier().ResetFontSize(nativeNode);
68 } else {
69 GetArkUIInternalNodeAPI()->GetTextTimerModifier().SetFontSize(
70 nativeNode, fontSize.Value(), static_cast<int32_t>(fontSize.Unit()));
71 }
72 return panda::JSValueRef::Undefined(vm);
73 }
74
ResetFontSize(ArkUIRuntimeCallInfo * runtimeCallInfo)75 ArkUINativeModuleValue TextTimerBridge::ResetFontSize(ArkUIRuntimeCallInfo* runtimeCallInfo)
76 {
77 EcmaVM* vm = runtimeCallInfo->GetVM();
78 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
79 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
80 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
81 GetArkUIInternalNodeAPI()->GetTextTimerModifier().ResetFontSize(nativeNode);
82 return panda::JSValueRef::Undefined(vm);
83 }
84
SetFontStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)85 ArkUINativeModuleValue TextTimerBridge::SetFontStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
86 {
87 EcmaVM* vm = runtimeCallInfo->GetVM();
88 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
89 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
90 Local<JSValueRef> paramArg = runtimeCallInfo->GetCallArgRef(NUM_1);
91 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
92 if (paramArg->IsNumber()) {
93 uint32_t fontStyle = paramArg->Uint32Value(vm);
94 if (fontStyle < static_cast<uint32_t>(OHOS::Ace::FontStyle::NORMAL) ||
95 fontStyle > static_cast<uint32_t>(OHOS::Ace::FontStyle::ITALIC)) {
96 fontStyle = static_cast<uint32_t>(OHOS::Ace::FontStyle::NORMAL);
97 }
98 GetArkUIInternalNodeAPI()->GetTextTimerModifier().SetFontStyle(nativeNode, fontStyle);
99 } else {
100 GetArkUIInternalNodeAPI()->GetTextTimerModifier().ResetFontStyle(nativeNode);
101 }
102 return panda::JSValueRef::Undefined(vm);
103 }
104
ResetFontStyle(ArkUIRuntimeCallInfo * runtimeCallInfo)105 ArkUINativeModuleValue TextTimerBridge::ResetFontStyle(ArkUIRuntimeCallInfo* runtimeCallInfo)
106 {
107 EcmaVM* vm = runtimeCallInfo->GetVM();
108 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
109 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
110 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
111 GetArkUIInternalNodeAPI()->GetTextTimerModifier().ResetFontStyle(nativeNode);
112 return panda::JSValueRef::Undefined(vm);
113 }
114
SetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)115 ArkUINativeModuleValue TextTimerBridge::SetFontWeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
116 {
117 EcmaVM* vm = runtimeCallInfo->GetVM();
118 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
119 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
120 Local<JSValueRef> fontWeightArg = runtimeCallInfo->GetCallArgRef(NUM_1);
121 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
122 std::string fontWeight;
123 if (!fontWeightArg->IsNull()) {
124 if (fontWeightArg->IsNumber()) {
125 fontWeight = std::to_string(fontWeightArg->Int32Value(vm));
126 } else if (fontWeightArg->IsString()) {
127 fontWeight = fontWeightArg->ToString(vm)->ToString();
128 }
129 }
130 GetArkUIInternalNodeAPI()->GetTextTimerModifier().SetFontWeight(nativeNode, fontWeight.c_str());
131 return panda::JSValueRef::Undefined(vm);
132 }
133
ResetFontWeight(ArkUIRuntimeCallInfo * runtimeCallInfo)134 ArkUINativeModuleValue TextTimerBridge::ResetFontWeight(ArkUIRuntimeCallInfo* runtimeCallInfo)
135 {
136 EcmaVM* vm = runtimeCallInfo->GetVM();
137 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
138 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
139 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
140 GetArkUIInternalNodeAPI()->GetTextTimerModifier().ResetFontWeight(nativeNode);
141 return panda::JSValueRef::Undefined(vm);
142 }
143
SetFontFamily(ArkUIRuntimeCallInfo * runtimeCallInfo)144 ArkUINativeModuleValue TextTimerBridge::SetFontFamily(ArkUIRuntimeCallInfo* runtimeCallInfo)
145 {
146 EcmaVM* vm = runtimeCallInfo->GetVM();
147 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
148 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
149 Local<JSValueRef> fontFamilyArg = runtimeCallInfo->GetCallArgRef(NUM_1);
150 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
151
152 std::string fontFamilyStr;
153 if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, fontFamilyArg, fontFamilyStr)) {
154 GetArkUIInternalNodeAPI()->GetTextTimerModifier().ResetFontFamily(nativeNode);
155 return panda::JSValueRef::Undefined(vm);
156 }
157 GetArkUIInternalNodeAPI()->GetTextTimerModifier().SetFontFamily(nativeNode, fontFamilyStr.c_str());
158 return panda::JSValueRef::Undefined(vm);
159 }
160
ResetFontFamily(ArkUIRuntimeCallInfo * runtimeCallInfo)161 ArkUINativeModuleValue TextTimerBridge::ResetFontFamily(ArkUIRuntimeCallInfo* runtimeCallInfo)
162 {
163 EcmaVM* vm = runtimeCallInfo->GetVM();
164 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
165 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(NUM_0);
166 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
167
168 GetArkUIInternalNodeAPI()->GetTextTimerModifier().ResetFontFamily(nativeNode);
169 return panda::JSValueRef::Undefined(vm);
170 }
171
SetFormat(ArkUIRuntimeCallInfo * runtimeCallInfo)172 ArkUINativeModuleValue TextTimerBridge::SetFormat(ArkUIRuntimeCallInfo* runtimeCallInfo)
173 {
174 EcmaVM* vm = runtimeCallInfo->GetVM();
175 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
176 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
177 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(NUM_1);
178 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
179 if (!secondArg->IsString()) {
180 GetArkUIInternalNodeAPI()->GetTextTimerModifier().ResetTextTimerFormat(nativeNode);
181 }
182 std::string format = secondArg->ToString(vm)->ToString();
183 GetArkUIInternalNodeAPI()->GetTextTimerModifier().SetTextTimerFormat(nativeNode, format.c_str());
184 return panda::JSValueRef::Undefined(vm);
185 }
186
ResetFormat(ArkUIRuntimeCallInfo * runtimeCallInfo)187 ArkUINativeModuleValue TextTimerBridge::ResetFormat(ArkUIRuntimeCallInfo* runtimeCallInfo)
188 {
189 EcmaVM* vm = runtimeCallInfo->GetVM();
190 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
191 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(NUM_0);
192 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
193 GetArkUIInternalNodeAPI()->GetTextTimerModifier().ResetTextTimerFormat(nativeNode);
194 return panda::JSValueRef::Undefined(vm);
195 }
196 } // namespace OHOS::Ace::NG
197