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/jsview/js_view_common_def.h"
19 #include "bridge/declarative_frontend/jsview/models/menu_model_impl.h"
20 #include "core/components_ng/layout/layout_property.h"
21 #include "core/components_ng/pattern/menu/menu_model.h"
22 #include "core/components_ng/pattern/menu/menu_model_ng.h"
23 #include "core/components_ng/property/measure_property.h"
24
25 namespace OHOS::Ace {
26 std::unique_ptr<MenuModel> MenuModel::instance_ = nullptr;
27 std::mutex MenuModel::mutex_;
28
GetInstance()29 MenuModel* MenuModel::GetInstance()
30 {
31 if (!instance_) {
32 std::lock_guard<std::mutex> lock(mutex_);
33 if (!instance_) {
34 #ifdef NG_BUILD
35 instance_.reset(new NG::MenuModelNG());
36 #else
37 if (Container::IsCurrentUseNewPipeline()) {
38 instance_.reset(new NG::MenuModelNG());
39 } else {
40 instance_.reset(new Framework::MenuModelImpl());
41 }
42 #endif
43 }
44 }
45 return instance_.get();
46 }
47 } // namespace OHOS::Ace
48
49 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo &)50 void JSMenu::Create(const JSCallbackInfo& /* info */)
51 {
52 MenuModel::GetInstance()->Create();
53 }
54
FontSize(const JSCallbackInfo & info)55 void JSMenu::FontSize(const JSCallbackInfo& info)
56 {
57 if (info.Length() < 1) {
58 return;
59 }
60 CalcDimension fontSize;
61 if (!ParseJsDimensionFp(info[0], fontSize)) {
62 return;
63 }
64 MenuModel::GetInstance()->SetFontSize(fontSize);
65 }
66
Font(const JSCallbackInfo & info)67 void JSMenu::Font(const JSCallbackInfo& info)
68 {
69 CalcDimension fontSize;
70 std::string weight;
71 if (info.Length() < 1 || !info[0]->IsObject()) {
72 return;
73 } else {
74 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
75 JSRef<JSVal> size = obj->GetProperty("size");
76 if (!size->IsNull()) {
77 ParseJsDimensionFp(size, fontSize);
78 if (fontSize.Unit() == DimensionUnit::PERCENT) {
79 // set zero for abnormal value
80 fontSize = CalcDimension();
81 }
82 }
83
84 auto jsWeight = obj->GetProperty("weight");
85 if (!jsWeight->IsNull()) {
86 if (jsWeight->IsNumber()) {
87 weight = std::to_string(jsWeight->ToNumber<int32_t>());
88 } else {
89 ParseJsString(jsWeight, weight);
90 }
91 }
92
93 auto jsStyle = obj->GetProperty("style");
94 if (!jsStyle->IsNull()) {
95 if (jsStyle->IsNumber()) {
96 MenuModel::GetInstance()->SetFontStyle(static_cast<FontStyle>(jsStyle->ToNumber<int32_t>()));
97 } else {
98 std::string style;
99 ParseJsString(jsStyle, style);
100 MenuModel::GetInstance()->SetFontStyle(ConvertStrToFontStyle(style));
101 }
102 }
103
104 auto jsFamily = obj->GetProperty("family");
105 if (!jsFamily->IsNull() && jsFamily->IsString()) {
106 auto familyVal = jsFamily->ToString();
107 auto fontFamilies = ConvertStrToFontFamilies(familyVal);
108 MenuModel::GetInstance()->SetFontFamily(fontFamilies);
109 }
110 }
111 MenuModel::GetInstance()->SetFontSize(fontSize);
112 MenuModel::GetInstance()->SetFontWeight(ConvertStrToFontWeight(weight));
113 }
114
FontColor(const JSCallbackInfo & info)115 void JSMenu::FontColor(const JSCallbackInfo& info)
116 {
117 std::optional<Color> color = std::nullopt;
118 if (info.Length() < 1) {
119 return;
120 } else {
121 Color textColor;
122 if (ParseJsColor(info[0], textColor)) {
123 color = textColor;
124 }
125 }
126 MenuModel::GetInstance()->SetFontColor(color);
127 }
128
SetWidth(const JSCallbackInfo & info)129 void JSMenu::SetWidth(const JSCallbackInfo& info)
130 {
131 if (info.Length() < 1) {
132 return;
133 }
134 CalcDimension width;
135 if (!ParseJsDimensionVp(info[0], width)) {
136 return;
137 }
138 MenuModel::GetInstance()->SetWidth(width);
139 }
140
HandleDifferentRadius(const JSRef<JSVal> & args)141 void JSMenu::HandleDifferentRadius(const JSRef<JSVal>& args)
142 {
143 std::optional<CalcDimension> radiusTopLeft;
144 std::optional<CalcDimension> radiusTopRight;
145 std::optional<CalcDimension> radiusBottomLeft;
146 std::optional<CalcDimension> radiusBottomRight;
147 if (args->IsObject()) {
148 JSRef<JSObject> object = JSRef<JSObject>::Cast(args);
149 CalcDimension topLeft;
150 if (ParseJsDimensionVp(object->GetProperty("topLeft"), topLeft)) {
151 radiusTopLeft = topLeft;
152 }
153 CalcDimension topRight;
154 if (ParseJsDimensionVp(object->GetProperty("topRight"), topRight)) {
155 radiusTopRight = topRight;
156 }
157 CalcDimension bottomLeft;
158 if (ParseJsDimensionVp(object->GetProperty("bottomLeft"), bottomLeft)) {
159 radiusBottomLeft = bottomLeft;
160 }
161 CalcDimension bottomRight;
162 if (ParseJsDimensionVp(object->GetProperty("bottomRight"), bottomRight)) {
163 radiusBottomRight = bottomRight;
164 }
165 if (!radiusTopLeft.has_value() && !radiusTopRight.has_value() && !radiusBottomLeft.has_value() &&
166 !radiusBottomRight.has_value()) {
167 return;
168 }
169 MenuModel::GetInstance()->SetBorderRadius(radiusTopLeft, radiusTopRight, radiusBottomLeft, radiusBottomRight);
170 }
171 }
172
SetRadius(const JSCallbackInfo & info)173 void JSMenu::SetRadius(const JSCallbackInfo& info)
174 {
175 if (info.Length() < 1) {
176 return;
177 }
178 CalcDimension radius;
179 ParseJsDimensionVp(info[0], radius);
180
181 if (LessNotEqual(radius.Value(), 0.0)) {
182 return;
183 }
184 MenuModel::GetInstance()->SetBorderRadius(radius);
185 HandleDifferentRadius(info[0]);
186 }
187
JSBind(BindingTarget globalObj)188 void JSMenu::JSBind(BindingTarget globalObj)
189 {
190 JSClass<JSMenu>::Declare("Menu");
191 MethodOptions opt = MethodOptions::NONE;
192 JSClass<JSMenu>::StaticMethod("create", &JSMenu::Create, opt);
193 JSClass<JSMenu>::StaticMethod("fontSize", &JSMenu::FontSize, opt);
194 JSClass<JSMenu>::StaticMethod("font", &JSMenu::Font, opt);
195 JSClass<JSMenu>::StaticMethod("fontColor", &JSMenu::FontColor, opt);
196 JSClass<JSMenu>::StaticMethod("width", &JSMenu::SetWidth, opt);
197 JSClass<JSMenu>::StaticMethod("radius", &JSMenu::SetRadius, opt);
198 JSClass<JSMenu>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
199 JSClass<JSMenu>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
200 JSClass<JSMenu>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
201 JSClass<JSMenu>::InheritAndBind<JSViewAbstract>(globalObj);
202 }
203 } // namespace OHOS::Ace::Framework
204