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