1 /*
2 * Copyright (c) 2021-2022 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 "frameworks/bridge/declarative_frontend/jsview/js_stack.h"
17
18 #include "base/log/ace_trace.h"
19 #include "base/log/log_wrapper.h"
20 #include "core/common/container.h"
21 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
22 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
23 #include "frameworks/bridge/declarative_frontend/jsview/models/stack_model_impl.h"
24 #include "frameworks/core/components_ng/pattern/stack/stack_model_ng.h"
25
26 namespace OHOS::Ace {
27
28 std::unique_ptr<StackModel> StackModel::instance_ = nullptr;
29 std::mutex StackModel::mutex_;
30
GetInstance()31 StackModel* StackModel::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::StackModelNG());
38 #else
39 if (Container::IsCurrentUseNewPipeline()) {
40 instance_.reset(new NG::StackModelNG());
41 } else {
42 instance_.reset(new Framework::StackModelImpl());
43 }
44 #endif
45 }
46 }
47 return instance_.get();
48 }
49 } // namespace OHOS::Ace
50 namespace OHOS::Ace::Framework {
51
52 #define SET_ALIGNMENT(info, name) \
53 do { \
54 Alignment alignment = Alignment::TOP_LEFT; \
55 if (!GetAlignment(info, alignment)) { \
56 break; \
57 } \
58 StackModel::GetInstance()->Set##name(alignment); \
59 } while (false)
60
61 const static std::array<Alignment, 9> ALIGNMENT_ARR { Alignment::TOP_LEFT, Alignment::TOP_CENTER, Alignment::TOP_RIGHT,
62 Alignment::CENTER_LEFT, Alignment::CENTER, Alignment::CENTER_RIGHT, Alignment::BOTTOM_LEFT,
63 Alignment::BOTTOM_CENTER, Alignment::BOTTOM_RIGHT };
64
SetStackFit(int value)65 void JSStack::SetStackFit(int value)
66 {
67 if (value >= static_cast<int>(StackFit::KEEP) && value <= static_cast<int>(StackFit::FIRST_CHILD)) {
68 StackModel::GetInstance()->SetStackFit(static_cast<StackFit>(value));
69 } else {
70 LOGE("Invalid value for stackfit");
71 }
72 }
73
SetOverflow(int value)74 void JSStack::SetOverflow(int value)
75 {
76 if (value >= static_cast<int>(Overflow::CLIP) && value <= static_cast<int>(Overflow::OBSERVABLE)) {
77 StackModel::GetInstance()->SetOverflow(static_cast<Overflow>(value));
78 } else {
79 LOGE("Invalid value for overflow");
80 }
81 }
82
GetAlignment(const JSCallbackInfo & info,Alignment & alignment)83 bool GetAlignment(const JSCallbackInfo& info, Alignment& alignment)
84 {
85 if (info.Length() != 1 || !info[0]->IsNumber()) {
86 StackModel::GetInstance()->SetAlignment(Alignment::CENTER);
87 return false;
88 }
89 auto value = info[0]->ToNumber<size_t>();
90 alignment = Alignment::TOP_LEFT;
91
92 switch (value) {
93 case 0:
94 alignment = Alignment::TOP_LEFT;
95 break;
96 case 1:
97 alignment = Alignment::TOP_CENTER;
98 break;
99 case 2:
100 alignment = Alignment::TOP_RIGHT;
101 break;
102 case 3:
103 alignment = Alignment::CENTER_LEFT;
104 break;
105 case 4:
106 alignment = Alignment::CENTER;
107 break;
108 case 5:
109 alignment = Alignment::CENTER_RIGHT;
110 break;
111 case 6:
112 alignment = Alignment::BOTTOM_LEFT;
113 break;
114 case 7:
115 alignment = Alignment::BOTTOM_CENTER;
116 break;
117 case 8:
118 alignment = Alignment::BOTTOM_RIGHT;
119 break;
120 default:
121 LOGE("Invalid value for alignment");
122 return false;
123 }
124 return true;
125 }
126
SetAlignment(const JSCallbackInfo & info)127 void JSStack::SetAlignment(const JSCallbackInfo& info)
128 {
129 SET_ALIGNMENT(info, Alignment);
130 }
131
SetWidth(const JSCallbackInfo & info)132 void JSStack::SetWidth(const JSCallbackInfo& info)
133 {
134 if (info.Length() < 1) {
135 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
136 return;
137 }
138
139 SetWidth(info[0]);
140 }
141
SetWidth(const JSRef<JSVal> & jsValue)142 void JSStack::SetWidth(const JSRef<JSVal>& jsValue)
143 {
144 JSViewAbstract::JsWidth(jsValue);
145 StackModel::GetInstance()->SetHasWidth();
146 }
147
SetHeight(const JSCallbackInfo & info)148 void JSStack::SetHeight(const JSCallbackInfo& info)
149 {
150 if (info.Length() < 1) {
151 LOGE("The arg is wrong, it is supposed to have at least 1 arguments");
152 return;
153 }
154
155 SetHeight(info[0]);
156 }
157
SetHeight(const JSRef<JSVal> & jsValue)158 void JSStack::SetHeight(const JSRef<JSVal>& jsValue)
159 {
160 JSViewAbstract::JsHeight(jsValue);
161 StackModel::GetInstance()->SetHasHeight();
162 }
163
SetSize(const JSCallbackInfo & info)164 void JSStack::SetSize(const JSCallbackInfo& info)
165 {
166 if (info.Length() < 1) {
167 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
168 return;
169 }
170
171 if (!info[0]->IsObject()) {
172 LOGE("arg is not Object or String.");
173 return;
174 }
175
176 JSRef<JSObject> sizeObj = JSRef<JSObject>::Cast(info[0]);
177 SetWidth(sizeObj->GetProperty("width"));
178 SetHeight(sizeObj->GetProperty("height"));
179 }
180
Create(const JSCallbackInfo & info)181 void JSStack::Create(const JSCallbackInfo& info)
182 {
183 Alignment alignment = Alignment::CENTER;
184
185 if (info.Length() > 0 && info[0]->IsObject()) {
186 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
187 JSRef<JSVal> stackAlign = obj->GetProperty("alignContent");
188 if (stackAlign->IsNumber()) {
189 int32_t value = stackAlign->ToNumber<int32_t>();
190 if (value >= 0 && value < static_cast<int>(ALIGNMENT_ARR.size())) {
191 alignment = ALIGNMENT_ARR[value];
192 } else {
193 StackModel::GetInstance()->Create();
194 return;
195 }
196 } else {
197 StackModel::GetInstance()->Create();
198 return;
199 }
200 } else {
201 StackModel::GetInstance()->Create();
202 return;
203 }
204
205 StackModel::GetInstance()->Create(alignment);
206 }
207
JSBind(BindingTarget globalObj)208 void JSStack::JSBind(BindingTarget globalObj)
209 {
210 JSClass<JSStack>::Declare("Stack");
211
212 MethodOptions opt = MethodOptions::NONE;
213 JSClass<JSStack>::StaticMethod("create", &JSStack::Create, opt);
214 JSClass<JSStack>::StaticMethod("stackFit", &JSStack::SetStackFit, opt);
215 JSClass<JSStack>::StaticMethod("overflow", &JSStack::SetOverflow, opt);
216 JSClass<JSStack>::StaticMethod("align", &JSStack::SetAlignment, opt);
217 JSClass<JSStack>::StaticMethod("alignContent", &JSStack::SetAlignment, opt);
218 JSClass<JSStack>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
219 JSClass<JSStack>::StaticMethod("width", SetWidth);
220 JSClass<JSStack>::StaticMethod("height", SetHeight);
221 JSClass<JSStack>::StaticMethod("size", SetSize);
222 JSClass<JSStack>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
223 JSClass<JSStack>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
224 JSClass<JSStack>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
225 JSClass<JSStack>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
226 JSClass<JSStack>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
227 JSClass<JSStack>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
228 JSClass<JSStack>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
229 JSClass<JSStack>::InheritAndBind<JSContainerBase>(globalObj);
230 }
231
232 } // namespace OHOS::Ace::Framework
233