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