• 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 
16 #include "bridge/declarative_frontend/jsview/js_menu.h"
17 
18 #include "bridge/declarative_frontend/ark_theme/theme_apply/js_menu_theme.h"
19 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
20 #include "bridge/declarative_frontend/jsview/models/menu_model_impl.h"
21 #include "core/components_ng/layout/layout_property.h"
22 #include "core/components_ng/pattern/menu/menu_model.h"
23 #include "core/components_ng/pattern/menu/menu_model_ng.h"
24 #include "core/components_ng/property/measure_property.h"
25 #include "core/common/resource/resource_parse_utils.h"
26 
27 namespace OHOS::Ace {
28 std::unique_ptr<MenuModel> MenuModel::instance_ = nullptr;
29 std::mutex MenuModel::mutex_;
30 
GetInstance()31 MenuModel* MenuModel::GetInstance()
32 {
33     if (!instance_) {
34         std::lock_guard<std::mutex> lock(mutex_);
35         if (!instance_) {
36 #ifdef NG_BUILD
37             instance_.reset(new NG::MenuModelNG());
38 #else
39             if (Container::IsCurrentUseNewPipeline()) {
40                 instance_.reset(new NG::MenuModelNG());
41             } else {
42                 instance_.reset(new Framework::MenuModelImpl());
43             }
44 #endif
45         }
46     }
47     return instance_.get();
48 }
49 } // namespace OHOS::Ace
50 
51 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo &)52 void JSMenu::Create(const JSCallbackInfo& /* info */)
53 {
54     MenuModel::GetInstance()->Create();
55     JSMenuTheme::ApplyTheme();
56 }
57 
FontSize(const JSCallbackInfo & info)58 void JSMenu::FontSize(const JSCallbackInfo& info)
59 {
60     if (info.Length() < 1) {
61         return;
62     }
63     CalcDimension fontSize;
64     RefPtr<ResourceObject> fontSizeResObj;
65     if (ParseJsDimensionFp(info[0], fontSize, fontSizeResObj)) {
66         MenuModel::GetInstance()->SetFontSize(fontSize);
67     }
68     if (SystemProperties::ConfigChangePerform()) {
69         MenuModel::GetInstance()->CreateWithDimensionResourceObj(fontSizeResObj, MenuDimensionType::FONT_SIZE);
70     }
71 }
72 
Font(const JSCallbackInfo & info)73 void JSMenu::Font(const JSCallbackInfo& info)
74 {
75     CalcDimension fontSize;
76     std::string weight;
77     RefPtr<ResourceObject> fontSizeResObj;
78     if (!info[0]->IsObject()) {
79         if (AceApplicationInfo::GetInstance().GreatOrEqualTargetAPIVersion(PlatformVersion::VERSION_TWELVE)) {
80             MenuModel::GetInstance()->SetFontSize(CalcDimension());
81             MenuModel::GetInstance()->SetFontWeight(FontWeight::NORMAL);
82             MenuModel::GetInstance()->SetFontStyle(FontStyle::NORMAL);
83             MenuModel::GetInstance()->ResetFontFamily();
84         }
85         return;
86     } else {
87         HandleFontObject(info, fontSize, weight, fontSizeResObj);
88     }
89     MenuModel::GetInstance()->SetFontSize(fontSize);
90     if (SystemProperties::ConfigChangePerform()) {
91         MenuModel::GetInstance()->CreateWithDimensionResourceObj(fontSizeResObj, MenuDimensionType::FONT_SIZE);
92     }
93     MenuModel::GetInstance()->SetFontWeight(ConvertStrToFontWeight(weight));
94 }
95 
HandleFontObject(const JSCallbackInfo & info,CalcDimension & fontSize,std::string & weight,RefPtr<ResourceObject> & fontSizeResObj)96 void JSMenu::HandleFontObject(
97     const JSCallbackInfo& info, CalcDimension& fontSize, std::string& weight, RefPtr<ResourceObject>& fontSizeResObj)
98 {
99     if (info.Length() < 1 || !info[0]->IsObject()) {
100         return;
101     }
102     JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
103     JSRef<JSVal> size = obj->GetProperty("size");
104     if (!size->IsNull()) {
105         ParseJsDimensionFp(size, fontSize, fontSizeResObj);
106         if (fontSize.Unit() == DimensionUnit::PERCENT) {
107             // set zero for abnormal value
108             fontSize = CalcDimension();
109         }
110     }
111     auto jsWeight = obj->GetProperty("weight");
112     if (!jsWeight->IsNull()) {
113         if (jsWeight->IsNumber()) {
114             weight = std::to_string(jsWeight->ToNumber<int32_t>());
115         } else {
116             ParseJsString(jsWeight, weight);
117         }
118     }
119     auto jsStyle = obj->GetProperty("style");
120     if (!jsStyle->IsNull()) {
121         if (jsStyle->IsNumber()) {
122             MenuModel::GetInstance()->SetFontStyle(static_cast<FontStyle>(jsStyle->ToNumber<int32_t>()));
123         } else {
124             std::string style;
125             ParseJsString(jsStyle, style);
126             MenuModel::GetInstance()->SetFontStyle(ConvertStrToFontStyle(style));
127         }
128     }
129     auto jsFamily = obj->GetProperty("family");
130     if (!jsFamily->IsNull() && jsFamily->IsString()) {
131         RefPtr<ResourceObject> familyResObj;
132         std::vector<std::string> fontFamilies;
133         if (ParseJsFontFamilies(jsFamily, fontFamilies, familyResObj)) {
134             MenuModel::GetInstance()->SetFontFamily(fontFamilies);
135         }
136         if (SystemProperties::ConfigChangePerform()) {
137             MenuModel::GetInstance()->CreateWithFontFamilyResourceObj(familyResObj, MenuFamilyType::FONT_FAMILY);
138         }
139     }
140 }
141 
FontColor(const JSCallbackInfo & info)142 void JSMenu::FontColor(const JSCallbackInfo& info)
143 {
144     std::optional<Color> color = std::nullopt;
145     if (info.Length() < 1) {
146         return;
147     } else {
148         Color textColor;
149         RefPtr<ResourceObject> resObj;
150         if (ParseJsColor(info[0], textColor, resObj)) {
151             color = textColor;
152         }
153         MenuModel::GetInstance()->SetFontColor(color);
154         if (SystemProperties::ConfigChangePerform()) {
155             MenuModel::GetInstance()->CreateWithColorResourceObj(resObj, MenuColorType::FONT_COLOR);
156         }
157     }
158 }
159 
SetWidth(const JSCallbackInfo & info)160 void JSMenu::SetWidth(const JSCallbackInfo& info)
161 {
162     if (info.Length() < 1) {
163         return;
164     }
165 
166     const JSRef<JSVal>& jsValue = info[0];
167     if (jsValue->IsObject()) {
168         JSRef<JSObject> object = JSRef<JSObject>::Cast(jsValue);
169         JSRef<JSVal> layoutPolicy = object->GetProperty("id_");
170         if (layoutPolicy->IsString()) {
171             ViewAbstractModel::GetInstance()->ClearWidthOrHeight(true);
172             auto policy = ParseLayoutPolicy(layoutPolicy->ToString());
173             ViewAbstractModel::GetInstance()->UpdateLayoutPolicyProperty(policy, true);
174             return;
175         }
176     }
177 
178     CalcDimension width;
179     RefPtr<ResourceObject> resObj;
180     ParseJsDimensionVp(jsValue, width, resObj);
181     MenuModel::GetInstance()->SetWidth(width);
182     if (SystemProperties::ConfigChangePerform()) {
183         MenuModel::GetInstance()->CreateWithDimensionResourceObj(resObj, MenuDimensionType::WIDTH);
184     }
185 }
186 
HandleDifferentRadius(const JSRef<JSVal> & args)187 void JSMenu::HandleDifferentRadius(const JSRef<JSVal>& args)
188 {
189     std::optional<CalcDimension> radiusTopLeft;
190     std::optional<CalcDimension> radiusTopRight;
191     std::optional<CalcDimension> radiusBottomLeft;
192     std::optional<CalcDimension> radiusBottomRight;
193     NG::BorderRadiusProperty borderRadius;
194     if (args->IsObject()) {
195         JSRef<JSObject> object = JSRef<JSObject>::Cast(args);
196         CalcDimension topLeft;
197         RefPtr<ResourceObject> topLeftResObj;
198         if (ParseJsDimensionVp(object->GetProperty("topLeft"), topLeft, topLeftResObj)) {
199             radiusTopLeft = topLeft;
200         }
201         CalcDimension topRight;
202         RefPtr<ResourceObject> topRightResObj;
203         if (ParseJsDimensionVp(object->GetProperty("topRight"), topRight, topRightResObj)) {
204             radiusTopRight = topRight;
205         }
206         CalcDimension bottomLeft;
207         RefPtr<ResourceObject> bottomLeftResObj;
208         if (ParseJsDimensionVp(object->GetProperty("bottomLeft"), bottomLeft, bottomLeftResObj)) {
209             radiusBottomLeft = bottomLeft;
210         }
211         CalcDimension bottomRight;
212         RefPtr<ResourceObject> bottomRightResObj;
213         if (ParseJsDimensionVp(object->GetProperty("bottomRight"), bottomRight, bottomRightResObj)) {
214             radiusBottomRight = bottomRight;
215         }
216         if (!radiusTopLeft.has_value() && !radiusTopRight.has_value() && !radiusBottomLeft.has_value() &&
217             !radiusBottomRight.has_value()) {
218             return;
219         }
220         if (SystemProperties::ConfigChangePerform()) {
221             ParseBorderRadiusResourceObj(
222                 topLeftResObj, topRightResObj, bottomLeftResObj, bottomRightResObj, borderRadius);
223             borderRadius.radiusTopLeft = radiusTopLeft;
224             borderRadius.radiusTopRight = radiusTopRight;
225             borderRadius.radiusBottomLeft = radiusBottomLeft;
226             borderRadius.radiusBottomRight = radiusBottomRight;
227             borderRadius.multiValued = true;
228             MenuModel::GetInstance()->SetBorderRadius(borderRadius);
229         } else {
230             MenuModel::GetInstance()->SetBorderRadius(
231                 radiusTopLeft, radiusTopRight, radiusBottomLeft, radiusBottomRight);
232         }
233     }
234 }
235 
ParseBorderRadiusResourceObj(const RefPtr<ResourceObject> & topLeftResObj,const RefPtr<ResourceObject> & topRightResObj,const RefPtr<ResourceObject> & bottomLeftResObj,const RefPtr<ResourceObject> & bottomRightResObj,NG::BorderRadiusProperty & borderRadius)236 void JSMenu::ParseBorderRadiusResourceObj(const RefPtr<ResourceObject>& topLeftResObj,
237     const RefPtr<ResourceObject>& topRightResObj, const RefPtr<ResourceObject>& bottomLeftResObj,
238     const RefPtr<ResourceObject>& bottomRightResObj, NG::BorderRadiusProperty& borderRadius)
239 {
240     if (topLeftResObj) {
241         auto&& updateFunc = [](const RefPtr<ResourceObject>& resObj, NG::BorderRadiusProperty& borderRadiusProperty) {
242             CalcDimension topLeft;
243             if (!ResourceParseUtils::ParseResDimensionVp(resObj, topLeft)) {
244                 borderRadiusProperty.radiusTopLeft.reset();
245                 return;
246             }
247             borderRadiusProperty.radiusTopLeft = topLeft;
248         };
249         borderRadius.AddResource("borderRadius.topLeft", topLeftResObj, std::move(updateFunc));
250     }
251     if (topRightResObj) {
252         auto&& updateFunc = [](const RefPtr<ResourceObject>& resObj, NG::BorderRadiusProperty& borderRadiusProperty) {
253             CalcDimension topRight;
254             if (!ResourceParseUtils::ParseResDimensionVp(resObj, topRight)) {
255                 borderRadiusProperty.radiusTopRight.reset();
256                 return;
257             }
258             borderRadiusProperty.radiusTopRight = topRight;
259         };
260         borderRadius.AddResource("borderRadius.topRight", topRightResObj, std::move(updateFunc));
261     }
262     if (bottomLeftResObj) {
263         auto&& updateFunc = [](const RefPtr<ResourceObject>& resObj, NG::BorderRadiusProperty& borderRadiusProperty) {
264             CalcDimension bottomLeft;
265             if (!ResourceParseUtils::ParseResDimensionVp(resObj, bottomLeft)) {
266                 borderRadiusProperty.radiusBottomLeft.reset();
267                 return;
268             }
269             borderRadiusProperty.radiusBottomLeft = bottomLeft;
270         };
271         borderRadius.AddResource("borderRadius.bottomLeft", bottomLeftResObj, std::move(updateFunc));
272     }
273     if (bottomRightResObj) {
274         auto&& updateFunc = [](const RefPtr<ResourceObject>& resObj, NG::BorderRadiusProperty& borderRadiusProperty) {
275             CalcDimension bottomRight;
276             if (!ResourceParseUtils::ParseResDimensionVp(resObj, bottomRight)) {
277                 borderRadiusProperty.radiusBottomRight.reset();
278                 return;
279             }
280             borderRadiusProperty.radiusBottomRight = bottomRight;
281         };
282         borderRadius.AddResource("borderRadius.bottomRight", bottomRightResObj, std::move(updateFunc));
283     }
284 }
285 
SetRadius(const JSCallbackInfo & info)286 void JSMenu::SetRadius(const JSCallbackInfo& info)
287 {
288     if (info.Length() < 1) {
289         return;
290     }
291     CalcDimension radius;
292     if (info[0]->IsObject()) {
293         HandleDifferentRadius(info[0]);
294     } else {
295         if (!ParseJsDimensionVpNG(info[0], radius)) {
296             MenuModel::GetInstance()->ResetBorderRadius();
297             return;
298         }
299         if (LessNotEqual(radius.Value(), 0.0)) {
300             MenuModel::GetInstance()->ResetBorderRadius();
301             return;
302         }
303         MenuModel::GetInstance()->SetBorderRadius(radius);
304         if (SystemProperties::ConfigChangePerform()) {
305             RefPtr<ResourceObject> resObj;
306             ParseJsDimensionVp(info[0], radius, resObj);
307             MenuModel::GetInstance()->CreateWithDimensionResourceObj(resObj, MenuDimensionType::BORDER_RADIUS);
308         }
309     }
310 }
311 
SetExpandingMode(const JSCallbackInfo & info)312 void JSMenu::SetExpandingMode(const JSCallbackInfo& info)
313 {
314     if (info.Length() < 1 || info[0]->IsNull() || !info[0]->IsNumber()) {
315         return;
316     }
317 
318     auto mode = static_cast<SubMenuExpandingMode>(info[0]->ToNumber<int32_t>());
319     auto expandingMode = mode == SubMenuExpandingMode::EMBEDDED ? NG::SubMenuExpandingMode::EMBEDDED
320                          : mode == SubMenuExpandingMode::STACK  ? NG::SubMenuExpandingMode::STACK
321                                                                 : NG::SubMenuExpandingMode::SIDE;
322 
323     MenuModel::GetInstance()->SetExpandingMode(expandingMode);
324 }
325 
SetExpandSymbol(const JSCallbackInfo & info)326 void JSMenu::SetExpandSymbol(const JSCallbackInfo& info)
327 {
328     if (info.Length() < 1 || !info[0]->IsObject()) {
329         return;
330     }
331     std::function<void(WeakPtr<NG::FrameNode>)> expandSymbol;
332     JSViewAbstract::SetSymbolOptionApply(info, expandSymbol, info[0]);
333     MenuModel::GetInstance()->SetExpandSymbol(expandSymbol);
334 }
335 
SetItemGroupDivider(const JSCallbackInfo & args)336 void JSMenu::SetItemGroupDivider(const JSCallbackInfo& args)
337 {
338     auto mode = DividerMode::FLOATING_ABOVE_MENU;
339     auto divider = V2::ItemDivider {
340         .strokeWidth = Dimension(0.0f, DimensionUnit::INVALID),
341         .color = Color::FOREGROUND,
342     };
343     if (args.Length() >= 1 && args[0]->IsObject()) {
344         JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
345         CalcDimension value;
346         if (!ParseLengthMetricsToPositiveDimension(obj->GetProperty("strokeWidth"), value)) {
347             value.Reset();
348             value.SetUnit(DimensionUnit::INVALID);
349         }
350         if (value.IsNegative() || value.Unit() < DimensionUnit::PX || value.Unit() > DimensionUnit::LPX) {
351             value.Reset();
352             value.SetUnit(DimensionUnit::INVALID);
353         }
354         divider.strokeWidth = value;
355         if (!ParseLengthMetricsToPositiveDimension(obj->GetProperty("startMargin"), value)) {
356             value.Reset();
357             value.SetUnit(DimensionUnit::INVALID);
358         }
359         if (value.IsNegative() || value.Unit() < DimensionUnit::PX || value.Unit() > DimensionUnit::LPX) {
360             value.Reset();
361             value.SetUnit(DimensionUnit::INVALID);
362         }
363         divider.startMargin = value;
364         if (!ParseLengthMetricsToPositiveDimension(obj->GetProperty("endMargin"), value)) {
365             value.Reset();
366             value.SetUnit(DimensionUnit::INVALID);
367         }
368         if (value.IsNegative() || value.Unit() < DimensionUnit::PX || value.Unit() > DimensionUnit::LPX) {
369             value.Reset();
370             value.SetUnit(DimensionUnit::INVALID);
371         }
372         divider.endMargin = value;
373         if (!ConvertFromJSValue(obj->GetProperty("color"), divider.color)) {
374             divider.color = Color::FOREGROUND;
375         }
376         auto modeVal = obj->GetProperty("mode");
377         if (modeVal->IsNumber() && modeVal->ToNumber<int32_t>() == 1) {
378             mode = DividerMode::EMBEDDED_IN_MENU;
379         }
380         if (SystemProperties::ConfigChangePerform()) {
381             RefPtr<ResourceObject> resObj;
382             ParseJsColor(obj->GetProperty("color"), divider.color, resObj);
383             MenuModel::GetInstance()->CreateWithColorResourceObj(resObj, MenuColorType::GROUP_DIVIDER_COLOR);
384         }
385     }
386     MenuModel::GetInstance()->SetItemGroupDivider(divider, mode);
387     args.ReturnSelf();
388 }
389 
SetItemDivider(const JSCallbackInfo & args)390 void JSMenu::SetItemDivider(const JSCallbackInfo& args)
391 {
392     auto mode = DividerMode::FLOATING_ABOVE_MENU;
393     V2::ItemDivider divider;
394     if (args.Length() >= 1 && args[0]->IsObject()) {
395         JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
396         auto modeVal = obj->GetProperty("mode");
397         if (modeVal->IsNumber() && modeVal->ToNumber<int32_t>() == 1) {
398             mode = DividerMode::EMBEDDED_IN_MENU;
399         }
400         CalcDimension value;
401         if (!ParseLengthMetricsToPositiveDimension(obj->GetProperty("strokeWidth"), value)) {
402             value.Reset();
403             value.SetUnit(mode == DividerMode::EMBEDDED_IN_MENU ? DimensionUnit::INVALID : DimensionUnit::PX);
404         }
405         if (value.IsNegative()) {
406             value.Reset();
407             value.SetUnit(mode == DividerMode::EMBEDDED_IN_MENU ? DimensionUnit::INVALID : DimensionUnit::PX);
408         }
409         divider.strokeWidth = value;
410         if (!ParseLengthMetricsToPositiveDimension(obj->GetProperty("startMargin"), value)) {
411             value.Reset();
412             value.SetUnit(mode == DividerMode::EMBEDDED_IN_MENU ? DimensionUnit::INVALID : DimensionUnit::PX);
413         }
414         if (value.IsNegative()) {
415             value.Reset();
416             value.SetUnit(mode == DividerMode::EMBEDDED_IN_MENU ? DimensionUnit::INVALID : DimensionUnit::PX);
417         }
418         divider.startMargin = value;
419         if (!ParseLengthMetricsToPositiveDimension(obj->GetProperty("endMargin"), value)) {
420             value.Reset();
421             value.SetUnit(mode == DividerMode::EMBEDDED_IN_MENU ? DimensionUnit::INVALID : DimensionUnit::PX);
422         }
423         if (value.IsNegative()) {
424             value.Reset();
425             value.SetUnit(mode == DividerMode::EMBEDDED_IN_MENU ? DimensionUnit::INVALID : DimensionUnit::PX);
426         }
427         divider.endMargin = value;
428 
429         if (!ConvertFromJSValue(obj->GetProperty("color"), divider.color)) {
430             divider.color = Color::TRANSPARENT;
431         }
432         if (SystemProperties::ConfigChangePerform()) {
433             RefPtr<ResourceObject> resObj;
434             ParseJsColor(obj->GetProperty("color"), divider.color, resObj);
435             MenuModel::GetInstance()->CreateWithColorResourceObj(resObj, MenuColorType::DIVIDER_COLOR);
436         }
437     }
438     MenuModel::GetInstance()->SetItemDivider(divider, mode);
439     args.ReturnSelf();
440 }
441 
JSBind(BindingTarget globalObj)442 void JSMenu::JSBind(BindingTarget globalObj)
443 {
444     JSClass<JSMenu>::Declare("Menu");
445     MethodOptions opt = MethodOptions::NONE;
446     JSClass<JSMenu>::StaticMethod("create", &JSMenu::Create, opt);
447     JSClass<JSMenu>::StaticMethod("fontSize", &JSMenu::FontSize, opt);
448     JSClass<JSMenu>::StaticMethod("font", &JSMenu::Font, opt);
449     JSClass<JSMenu>::StaticMethod("fontColor", &JSMenu::FontColor, opt);
450     JSClass<JSMenu>::StaticMethod("width", &JSMenu::SetWidth, opt);
451     JSClass<JSMenu>::StaticMethod("radius", &JSMenu::SetRadius, opt);
452     JSClass<JSMenu>::StaticMethod("subMenuExpandingMode", &JSMenu::SetExpandingMode);
453     JSClass<JSMenu>::StaticMethod("subMenuExpandSymbol", &JSMenu::SetExpandSymbol);
454     JSClass<JSMenu>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
455     JSClass<JSMenu>::StaticMethod("menuItemDivider", &JSMenu::SetItemDivider);
456     JSClass<JSMenu>::StaticMethod("menuItemGroupDivider", &JSMenu::SetItemGroupDivider);
457     JSClass<JSMenu>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
458     JSClass<JSMenu>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
459     JSClass<JSMenu>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
460     JSClass<JSMenu>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
461     JSClass<JSMenu>::InheritAndBind<JSViewAbstract>(globalObj);
462 }
463 } // namespace OHOS::Ace::Framework
464