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_menu_bridge.h"
16
17 #include "core/interfaces/native/node/api.h"
18 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
19
20 namespace OHOS::Ace::NG {
21 const std::string FORMAT_FONT = "%s|%s|%s";
22 const std::string DEFAULT_ERR_CODE = "-1";
23
SetMenuFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)24 ArkUINativeModuleValue MenuBridge::SetMenuFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
25 {
26 EcmaVM* vm = runtimeCallInfo->GetVM();
27 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
28 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
29 Local<JSValueRef> secondArg = runtimeCallInfo->GetCallArgRef(1);
30 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
31 Color color;
32 if (!ArkTSUtils::ParseJsColorAlpha(vm, secondArg, color)) {
33 GetArkUIInternalNodeAPI()->GetMenuModifier().ResetMenuFontColor(nativeNode);
34 } else {
35 GetArkUIInternalNodeAPI()->GetMenuModifier().SetMenuFontColor(nativeNode, color.GetValue());
36 }
37
38 return panda::JSValueRef::Undefined(vm);
39 }
40
ResetMenuFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)41 ArkUINativeModuleValue MenuBridge::ResetMenuFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
42 {
43 EcmaVM* vm = runtimeCallInfo->GetVM();
44 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
45 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
46 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
47 GetArkUIInternalNodeAPI()->GetMenuModifier().ResetMenuFontColor(nativeNode);
48 return panda::JSValueRef::Undefined(vm);
49 }
50
SetFont(ArkUIRuntimeCallInfo * runtimeCallInfo)51 ArkUINativeModuleValue MenuBridge::SetFont(ArkUIRuntimeCallInfo* runtimeCallInfo)
52 {
53 EcmaVM* vm = runtimeCallInfo->GetVM();
54 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
55 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
56 Local<JSValueRef> sizeArg = runtimeCallInfo->GetCallArgRef(1); // 1: index of font size value
57 Local<JSValueRef> weightArg = runtimeCallInfo->GetCallArgRef(2); // 2: index of font weight value
58 Local<JSValueRef> familyArg = runtimeCallInfo->GetCallArgRef(3); // 3: index of font family value
59 Local<JSValueRef> styleArg = runtimeCallInfo->GetCallArgRef(4); // 4: index of font style value
60 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
61 if (sizeArg->IsUndefined() && weightArg->IsUndefined() && familyArg->IsUndefined() && styleArg->IsUndefined()) {
62 GetArkUIInternalNodeAPI()->GetMenuModifier().ResetFont(nativeNode);
63 return panda::JSValueRef::Undefined(vm);
64 }
65
66 CalcDimension fontSize;
67 if (!ArkTSUtils::ParseJsDimensionFp(vm, sizeArg, fontSize, false)) {
68 fontSize = Dimension(0.0);
69 }
70 std::string weight = DEFAULT_ERR_CODE;
71 if (weightArg->IsNumber()) {
72 weight = std::to_string(weightArg->Int32Value(vm));
73 } else {
74 if (!ArkTSUtils::ParseJsString(vm, weightArg, weight) || weight.empty()) {
75 weight = DEFAULT_ERR_CODE;
76 }
77 }
78
79 int32_t style = -1;
80 if (styleArg->IsNumber()) {
81 style = styleArg->Int32Value(vm);
82 }
83
84 std::string family;
85 if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, familyArg, family) || family.empty()) {
86 family = DEFAULT_ERR_CODE;
87 }
88 std::string fontSizeStr = fontSize.ToString();
89 std::string fontInfo =
90 StringUtils::FormatString(FORMAT_FONT.c_str(), fontSizeStr.c_str(), weight.c_str(), family.c_str());
91
92 GetArkUIInternalNodeAPI()->GetMenuModifier().SetFont(nativeNode, fontInfo.c_str(), style);
93 return panda::JSValueRef::Undefined(vm);
94 }
95
ResetFont(ArkUIRuntimeCallInfo * runtimeCallInfo)96 ArkUINativeModuleValue MenuBridge::ResetFont(ArkUIRuntimeCallInfo* runtimeCallInfo)
97 {
98 EcmaVM* vm = runtimeCallInfo->GetVM();
99 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
100 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
101 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
102 GetArkUIInternalNodeAPI()->GetMenuModifier().ResetFont(nativeNode);
103 return panda::JSValueRef::Undefined(vm);
104 }
105
SetRadius(ArkUIRuntimeCallInfo * runtimeCallInfo)106 ArkUINativeModuleValue MenuBridge::SetRadius(ArkUIRuntimeCallInfo* runtimeCallInfo)
107 {
108 EcmaVM* vm = runtimeCallInfo->GetVM();
109 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
110 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
111 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
112 Local<JSValueRef> topLeftArgs = runtimeCallInfo->GetCallArgRef(1); // 1: index of top left value
113 Local<JSValueRef> topRightArgs = runtimeCallInfo->GetCallArgRef(2); // 2: index of top right value
114 Local<JSValueRef> bottomLeftArgs = runtimeCallInfo->GetCallArgRef(3); // 3: index of bottom left value
115 Local<JSValueRef> bottomRightArgs = runtimeCallInfo->GetCallArgRef(4); // 4: index of bottom right value
116 if (topLeftArgs->IsUndefined() && topRightArgs->IsUndefined() && bottomLeftArgs->IsUndefined() &&
117 bottomRightArgs->IsUndefined()) {
118 GetArkUIInternalNodeAPI()->GetMenuModifier().ResetRadius(nativeNode);
119 return panda::JSValueRef::Undefined(vm);
120 }
121
122 CalcDimension topLeft;
123 CalcDimension topRight;
124 CalcDimension bottomLeft;
125 CalcDimension bottomRight;
126 if (!ArkTSUtils::ParseJsDimensionVpNG(vm, topLeftArgs, topLeft, true)) {
127 topLeft = CalcDimension(0.0, DimensionUnit::VP);
128 }
129
130 if (!ArkTSUtils::ParseJsDimensionVpNG(vm, topRightArgs, topRight, true)) {
131 topRight = CalcDimension(0.0, DimensionUnit::VP);
132 }
133
134 if (!ArkTSUtils::ParseJsDimensionVpNG(vm, bottomLeftArgs, bottomLeft, true)) {
135 bottomLeft = CalcDimension(0.0, DimensionUnit::VP);
136 }
137
138 if (!ArkTSUtils::ParseJsDimensionVpNG(vm, bottomRightArgs, bottomRight, true)) {
139 bottomRight = CalcDimension(0.0, DimensionUnit::VP);
140 }
141
142 std::vector<double> radiusValues;
143 std::vector<int32_t> radiusUnits;
144 radiusUnits.push_back(static_cast<int32_t>(topLeft.Unit()));
145 radiusUnits.push_back(static_cast<int32_t>(topRight.Unit()));
146 radiusUnits.push_back(static_cast<int32_t>(bottomLeft.Unit()));
147 radiusUnits.push_back(static_cast<int32_t>(bottomRight.Unit()));
148 radiusValues.push_back(topLeft.Value());
149 radiusValues.push_back(topRight.Value());
150 radiusValues.push_back(bottomLeft.Value());
151 radiusValues.push_back(bottomRight.Value());
152 GetArkUIInternalNodeAPI()->GetMenuModifier().SetRadius(nativeNode, radiusValues.data(), radiusUnits.data());
153 return panda::JSValueRef::Undefined(vm);
154 }
155
ResetRadius(ArkUIRuntimeCallInfo * runtimeCallInfo)156 ArkUINativeModuleValue MenuBridge::ResetRadius(ArkUIRuntimeCallInfo* runtimeCallInfo)
157 {
158 EcmaVM* vm = runtimeCallInfo->GetVM();
159 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
160 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
161 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
162 GetArkUIInternalNodeAPI()->GetMenuModifier().ResetRadius(nativeNode);
163 return panda::JSValueRef::Undefined(vm);
164 }
165
SetWidth(ArkUIRuntimeCallInfo * runtimeCallInfo)166 ArkUINativeModuleValue MenuBridge::SetWidth(ArkUIRuntimeCallInfo* runtimeCallInfo)
167 {
168 EcmaVM* vm = runtimeCallInfo->GetVM();
169 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
170 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
171 Local<JSValueRef> widthArg = runtimeCallInfo->GetCallArgRef(1);
172 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
173 CalcDimension width;
174 if (!ArkTSUtils::ParseJsDimensionVp(vm, widthArg, width, false)) {
175 GetArkUIInternalNodeAPI()->GetMenuModifier().ResetMenuWidth(nativeNode);
176 return panda::JSValueRef::Undefined(vm);
177 }
178 GetArkUIInternalNodeAPI()->GetMenuModifier().SetMenuWidth(
179 nativeNode, width.Value(), static_cast<int32_t>(width.Unit()));
180 return panda::JSValueRef::Undefined(vm);
181 }
182
ResetWidth(ArkUIRuntimeCallInfo * runtimeCallInfo)183 ArkUINativeModuleValue MenuBridge::ResetWidth(ArkUIRuntimeCallInfo* runtimeCallInfo)
184 {
185 EcmaVM* vm = runtimeCallInfo->GetVM();
186 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
187 Local<JSValueRef> firstArg = runtimeCallInfo->GetCallArgRef(0);
188 void* nativeNode = firstArg->ToNativePointer(vm)->Value();
189 GetArkUIInternalNodeAPI()->GetMenuModifier().ResetMenuWidth(nativeNode);
190 return panda::JSValueRef::Undefined(vm);
191 }
192 } // namespace OHOS::Ace::NG