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_item_bridge.h"
16
17 #include "core/components_ng/pattern/menu/menu_item/menu_item_model_ng.h"
18 #include "core/interfaces/native/node/api.h"
19 #include "frameworks/bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
20
21 using namespace OHOS::Ace::Framework;
22
23 namespace OHOS::Ace::NG {
24 const std::string FORMAT_FONT = "%s|%s|%s";
25 const std::string DEFAULT_ERR_CODE = "-1";
SetMenuItemSelected(ArkUIRuntimeCallInfo * runtimeCallInfo)26 ArkUINativeModuleValue MenuItemBridge::SetMenuItemSelected(ArkUIRuntimeCallInfo* runtimeCallInfo)
27 {
28 EcmaVM* vm = runtimeCallInfo->GetVM();
29 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
30 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
31 Local<JSValueRef> isSelectedArg = runtimeCallInfo->GetCallArgRef(1);
32 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
33 if (isSelectedArg->IsBoolean()) {
34 bool isSelected = isSelectedArg->ToBoolean(vm)->Value();
35 GetArkUIInternalNodeAPI()->GetMenuItemModifier().SetMenuItemSelected(nativeNode, isSelected);
36 } else {
37 GetArkUIInternalNodeAPI()->GetMenuItemModifier().ResetMenuItemSelected(nativeNode);
38 }
39 return panda::JSValueRef::Undefined(vm);
40 }
41
ResetMenuItemSelected(ArkUIRuntimeCallInfo * runtimeCallInfo)42 ArkUINativeModuleValue MenuItemBridge::ResetMenuItemSelected(ArkUIRuntimeCallInfo* runtimeCallInfo)
43 {
44 EcmaVM* vm = runtimeCallInfo->GetVM();
45 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
46 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
47 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
48 GetArkUIInternalNodeAPI()->GetMenuItemModifier().ResetMenuItemSelected(nativeNode);
49 return panda::JSValueRef::Undefined(vm);
50 }
51
SetLabelFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)52 ArkUINativeModuleValue MenuItemBridge::SetLabelFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
53 {
54 EcmaVM* vm = runtimeCallInfo->GetVM();
55 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
56 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
57 Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(1);
58 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
59 Color color;
60 if (!ArkTSUtils::ParseJsColorAlpha(vm, colorArg, color)) {
61 GetArkUIInternalNodeAPI()->GetMenuItemModifier().ResetLabelFontColor(nativeNode);
62 } else {
63 GetArkUIInternalNodeAPI()->GetMenuItemModifier().SetLabelFontColor(nativeNode, color.GetValue());
64 }
65 return panda::JSValueRef::Undefined(vm);
66 }
67
ResetLabelFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)68 ArkUINativeModuleValue MenuItemBridge::ResetLabelFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
69 {
70 EcmaVM* vm = runtimeCallInfo->GetVM();
71 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
72 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
73 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
74 GetArkUIInternalNodeAPI()->GetMenuItemModifier().ResetLabelFontColor(nativeNode);
75 return panda::JSValueRef::Undefined(vm);
76 }
77
SetContentFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)78 ArkUINativeModuleValue MenuItemBridge::SetContentFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
79 {
80 EcmaVM* vm = runtimeCallInfo->GetVM();
81 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
82 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
83 Local<JSValueRef> colorArg = runtimeCallInfo->GetCallArgRef(1);
84 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
85 Color color;
86 if (!ArkTSUtils::ParseJsColorAlpha(vm, colorArg, color)) {
87 GetArkUIInternalNodeAPI()->GetMenuItemModifier().ResetContentFontColor(nativeNode);
88 } else {
89 GetArkUIInternalNodeAPI()->GetMenuItemModifier().SetContentFontColor(nativeNode, color.GetValue());
90 }
91 return panda::JSValueRef::Undefined(vm);
92 }
93
ResetContentFontColor(ArkUIRuntimeCallInfo * runtimeCallInfo)94 ArkUINativeModuleValue MenuItemBridge::ResetContentFontColor(ArkUIRuntimeCallInfo* runtimeCallInfo)
95 {
96 EcmaVM* vm = runtimeCallInfo->GetVM();
97 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
98 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
99 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
100 GetArkUIInternalNodeAPI()->GetMenuItemModifier().ResetContentFontColor(nativeNode);
101 return panda::JSValueRef::Undefined(vm);
102 }
103
SetLabelFont(ArkUIRuntimeCallInfo * runtimeCallInfo)104 ArkUINativeModuleValue MenuItemBridge::SetLabelFont(ArkUIRuntimeCallInfo* runtimeCallInfo)
105 {
106 EcmaVM* vm = runtimeCallInfo->GetVM();
107 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
108 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
109 Local<JSValueRef> sizeArg = runtimeCallInfo->GetCallArgRef(1); // 1: label font size
110 Local<JSValueRef> weightArg = runtimeCallInfo->GetCallArgRef(2); // 2: label font weight
111 Local<JSValueRef> familyArg = runtimeCallInfo->GetCallArgRef(3); // 3: label font family
112 Local<JSValueRef> styleArg = runtimeCallInfo->GetCallArgRef(4); // 4: label font style
113 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
114 if (sizeArg->IsUndefined() && weightArg->IsUndefined() && familyArg->IsUndefined() && styleArg->IsUndefined()) {
115 GetArkUIInternalNodeAPI()->GetMenuItemModifier().ResetLabelFont(nativeNode);
116 return panda::JSValueRef::Undefined(vm);
117 }
118
119 CalcDimension fontSize;
120 if (!ArkTSUtils::ParseJsDimensionFp(vm, sizeArg, fontSize, false)) {
121 fontSize = Dimension(0.0);
122 }
123
124 std::string weight = DEFAULT_ERR_CODE;
125 if (weightArg->IsNumber()) {
126 weight = std::to_string(weightArg->Int32Value(vm));
127 } else {
128 if (!ArkTSUtils::ParseJsString(vm, weightArg, weight) || weight.empty()) {
129 weight = DEFAULT_ERR_CODE;
130 }
131 }
132
133 int32_t style = -1;
134 if (styleArg->IsNumber()) {
135 style = styleArg->Int32Value(vm);
136 }
137
138 std::string family;
139 if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, familyArg, family) || family.empty()) {
140 family = DEFAULT_ERR_CODE;
141 }
142
143 std::string fontSizeStr = fontSize.ToString();
144 std::string fontInfo =
145 StringUtils::FormatString(FORMAT_FONT.c_str(), fontSizeStr.c_str(), weight.c_str(), family.c_str());
146 GetArkUIInternalNodeAPI()->GetMenuItemModifier().SetLabelFont(nativeNode, fontInfo.c_str(), style);
147 return panda::JSValueRef::Undefined(vm);
148 }
149
ResetLabelFont(ArkUIRuntimeCallInfo * runtimeCallInfo)150 ArkUINativeModuleValue MenuItemBridge::ResetLabelFont(ArkUIRuntimeCallInfo* runtimeCallInfo)
151 {
152 EcmaVM* vm = runtimeCallInfo->GetVM();
153 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
154 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
155 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
156 GetArkUIInternalNodeAPI()->GetMenuItemModifier().ResetLabelFont(nativeNode);
157 return panda::JSValueRef::Undefined(vm);
158 }
159
SetContentFont(ArkUIRuntimeCallInfo * runtimeCallInfo)160 ArkUINativeModuleValue MenuItemBridge::SetContentFont(ArkUIRuntimeCallInfo* runtimeCallInfo)
161 {
162 EcmaVM* vm = runtimeCallInfo->GetVM();
163 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
164 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
165 Local<JSValueRef> sizeArg = runtimeCallInfo->GetCallArgRef(1); // 1: index of font size
166 Local<JSValueRef> weightArg = runtimeCallInfo->GetCallArgRef(2); // 2: index of font weight
167 Local<JSValueRef> familyArg = runtimeCallInfo->GetCallArgRef(3); // 3: index of font family
168 Local<JSValueRef> styleArg = runtimeCallInfo->GetCallArgRef(4); // 4: index of font style
169 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
170 if (sizeArg->IsUndefined() && weightArg->IsUndefined() && familyArg->IsUndefined() && styleArg->IsUndefined()) {
171 GetArkUIInternalNodeAPI()->GetMenuItemModifier().ResetContentFont(nativeNode);
172 return panda::JSValueRef::Undefined(vm);
173 }
174
175 CalcDimension fontSize;
176 if (!ArkTSUtils::ParseJsDimensionFp(vm, sizeArg, fontSize, false)) {
177 fontSize = Dimension(0.0);
178 }
179
180 std::string weight = DEFAULT_ERR_CODE;
181 if (weightArg->IsNumber()) {
182 weight = std::to_string(weightArg->Int32Value(vm));
183 } else {
184 if (!ArkTSUtils::ParseJsString(vm, weightArg, weight) || weight.empty()) {
185 weight = DEFAULT_ERR_CODE;
186 }
187 }
188
189 int32_t style = -1;
190 if (styleArg->IsNumber()) {
191 style = styleArg->Int32Value(vm);
192 }
193
194 std::string family;
195 if (!ArkTSUtils::ParseJsFontFamiliesToString(vm, familyArg, family) || family.empty()) {
196 family = DEFAULT_ERR_CODE;
197 }
198
199 std::string fontSizeStr = fontSize.ToString();
200 std::string fontInfo =
201 StringUtils::FormatString(FORMAT_FONT.c_str(), fontSizeStr.c_str(), weight.c_str(), family.c_str());
202 GetArkUIInternalNodeAPI()->GetMenuItemModifier().SetContentFont(nativeNode, fontInfo.c_str(), style);
203 return panda::JSValueRef::Undefined(vm);
204 }
205
ResetContentFont(ArkUIRuntimeCallInfo * runtimeCallInfo)206 ArkUINativeModuleValue MenuItemBridge::ResetContentFont(ArkUIRuntimeCallInfo* runtimeCallInfo)
207 {
208 EcmaVM* vm = runtimeCallInfo->GetVM();
209 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
210 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
211 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
212 GetArkUIInternalNodeAPI()->GetMenuItemModifier().ResetContentFont(nativeNode);
213 return panda::JSValueRef::Undefined(vm);
214 }
215
SetSelectIcon(ArkUIRuntimeCallInfo * runtimeCallInfo)216 ArkUINativeModuleValue MenuItemBridge::SetSelectIcon(ArkUIRuntimeCallInfo* runtimeCallInfo)
217 {
218 EcmaVM* vm = runtimeCallInfo->GetVM();
219 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
220 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
221 Local<JSValueRef> inputArg = runtimeCallInfo->GetCallArgRef(1);
222 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
223
224 bool isShow = false;
225 std::string icon;
226 if (inputArg->IsBoolean()) {
227 isShow = inputArg->ToBoolean(vm)->Value();
228 } else if (inputArg->IsString()) {
229 icon = inputArg->ToString(vm)->ToString();
230 isShow = true;
231 } else if (ArkTSUtils::ParseJsMedia(vm, inputArg, icon)) {
232 isShow = true;
233 }
234 GetArkUIInternalNodeAPI()->GetMenuItemModifier().SetSelectIcon(nativeNode, isShow);
235 GetArkUIInternalNodeAPI()->GetMenuItemModifier().SetSelectIconSrc(nativeNode, icon.c_str());
236 return panda::JSValueRef::Undefined(vm);
237 }
238
ResetSelectIcon(ArkUIRuntimeCallInfo * runtimeCallInfo)239 ArkUINativeModuleValue MenuItemBridge::ResetSelectIcon(ArkUIRuntimeCallInfo* runtimeCallInfo)
240 {
241 EcmaVM* vm = runtimeCallInfo->GetVM();
242 CHECK_NULL_RETURN(vm, panda::NativePointerRef::New(vm, nullptr));
243 Local<JSValueRef> nodeArg = runtimeCallInfo->GetCallArgRef(0);
244 void* nativeNode = nodeArg->ToNativePointer(vm)->Value();
245 GetArkUIInternalNodeAPI()->GetMenuItemModifier().ResetSelectIcon(nativeNode);
246 GetArkUIInternalNodeAPI()->GetMenuItemModifier().ResetSelectIconSrc(nativeNode);
247 return panda::JSValueRef::Undefined(vm);
248 }
249 } // namespace OHOS::Ace::NG