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_row.h"
17
18 #include "base/log/ace_trace.h"
19 #include "core/components_ng/pattern/linear_layout/row_model.h"
20 #include "core/components_ng/pattern/linear_layout/row_model_ng.h"
21 #include "frameworks/bridge/declarative_frontend/jsview/models/row_model_impl.h"
22
23 namespace OHOS::Ace {
24
25 std::unique_ptr<RowModel> RowModel::instance_ = nullptr;
26 std::mutex RowModel::mutex_;
27
GetInstance()28 RowModel* RowModel::GetInstance()
29 {
30 if (!instance_) {
31 std::lock_guard<std::mutex> lock(mutex_);
32 if (!instance_) {
33 #ifdef NG_BUILD
34 instance_.reset(new NG::RowModelNG());
35 #else
36 if (Container::IsCurrentUseNewPipeline()) {
37 instance_.reset(new NG::RowModelNG());
38 } else {
39 instance_.reset(new Framework::RowModelImpl());
40 }
41 #endif
42 }
43 }
44 return instance_.get();
45 }
46 } // namespace OHOS::Ace
47
48 namespace OHOS::Ace::Framework {
49
Create(const JSCallbackInfo & info)50 void JSRow::Create(const JSCallbackInfo& info)
51 {
52 std::optional<CalcDimension> space;
53 if (info.Length() > 0 && info[0]->IsObject()) {
54 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
55 JSRef<JSVal> spaceVal = obj->GetProperty("space");
56 CalcDimension value;
57 if (ParseJsDimensionVp(spaceVal, value)) {
58 space = value;
59 } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
60 space = Dimension();
61 }
62 }
63 VerticalAlignDeclaration* declaration = nullptr;
64 if (info.Length() > 0 && info[0]->IsObject()) {
65 JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
66 JSRef<JSVal> useAlign = obj->GetProperty("useAlign");
67 if (useAlign->IsObject()) {
68 declaration = JSRef<JSObject>::Cast(useAlign)->Unwrap<VerticalAlignDeclaration>();
69 }
70 }
71
72 RowModel::GetInstance()->Create(space, declaration, "");
73 }
74
CreateWithWrap(const JSCallbackInfo & info)75 void JSRow::CreateWithWrap(const JSCallbackInfo& info)
76 {
77 RowModel::GetInstance()->CreateWithWrap();
78 }
79
SetAlignItems(int32_t value)80 void JSRow::SetAlignItems(int32_t value)
81 {
82 if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
83 (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) ||
84 (value == static_cast<int32_t>(FlexAlign::STRETCH))) {
85 RowModel::GetInstance()->SetAlignItems(static_cast<FlexAlign>(value));
86 } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
87 RowModel::GetInstance()->SetAlignItems(FlexAlign::CENTER);
88 // FIXME: we have a design issue here, setters return void, can not signal error to JS
89 LOGE("invalid value for justifyContent");
90 }
91 }
92
SetJustifyContent(int32_t value)93 void JSRow::SetJustifyContent(int32_t value)
94 {
95 if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
96 (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) ||
97 (value == static_cast<int32_t>(FlexAlign::SPACE_BETWEEN)) ||
98 (value == static_cast<int32_t>(FlexAlign::SPACE_AROUND)) ||
99 (value == static_cast<int32_t>(FlexAlign::SPACE_EVENLY))) {
100 RowModel::GetInstance()->SetJustifyContent(static_cast<FlexAlign>(value));
101 } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
102 RowModel::GetInstance()->SetJustifyContent(FlexAlign::FLEX_START);
103 LOGE("invalid value for justifyContent");
104 }
105 }
106
SetReverse(const JSCallbackInfo & info)107 void JSRow::SetReverse(const JSCallbackInfo& info)
108 {
109 if (info[0]->IsBoolean()) {
110 RowModel::GetInstance()->SetIsReverse(info[0]->ToBoolean());
111 } else {
112 RowModel::GetInstance()->SetIsReverse(true);
113 }
114 }
115
JSBind(BindingTarget globalObj)116 void JSRow::JSBind(BindingTarget globalObj)
117 {
118 JSClass<JSRow>::Declare("Row");
119 MethodOptions opt = MethodOptions::NONE;
120 JSClass<JSRow>::StaticMethod("create", &JSRow::Create, opt);
121 JSClass<JSRow>::StaticMethod("createWithWrap", &JSRow::CreateWithWrap, opt);
122 JSClass<JSRow>::StaticMethod("fillParent", &JSFlex::SetFillParent, opt);
123 JSClass<JSRow>::StaticMethod("wrapContent", &JSFlex::SetWrapContent, opt);
124 JSClass<JSRow>::StaticMethod("justifyContent", &JSRow::SetJustifyContent, opt);
125 JSClass<JSRow>::StaticMethod("alignItems", &JSRow::SetAlignItems, opt);
126 JSClass<JSRow>::StaticMethod("reverse", &JSRow::SetReverse, opt);
127 JSClass<JSRow>::StaticMethod("alignContent", &JSFlex::SetAlignContent, opt);
128 JSClass<JSRow>::StaticMethod("height", &JSFlex::JsHeight, opt);
129 JSClass<JSRow>::StaticMethod("width", &JSFlex::JsWidth, opt);
130 JSClass<JSRow>::StaticMethod("size", &JSFlex::JsSize, opt);
131 JSClass<JSRow>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
132 JSClass<JSRow>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
133 JSClass<JSRow>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
134 JSClass<JSRow>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
135 JSClass<JSRow>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
136 JSClass<JSRow>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
137 JSClass<JSRow>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
138 JSClass<JSRow>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
139 JSClass<JSRow>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
140 JSClass<JSRow>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
141 JSClass<JSRow>::StaticMethod("pointLight", &JSViewAbstract::JsPointLight, opt);
142 JSClass<JSRow>::InheritAndBind<JSContainerBase>(globalObj);
143
144 JSClass<VerticalAlignDeclaration>::Declare("VerticalAlignDeclaration");
145 JSClass<VerticalAlignDeclaration>::Bind(
146 globalObj, VerticalAlignDeclaration::ConstructorCallback, VerticalAlignDeclaration::DestructorCallback);
147 }
148
ConstructorCallback(const JSCallbackInfo & args)149 void VerticalAlignDeclaration::ConstructorCallback(const JSCallbackInfo& args)
150 {
151 auto align = VerticalAlign::CENTER;
152 if (args.Length() > 0 && args[0]->IsNumber()) {
153 auto value = args[0]->ToNumber<int32_t>();
154 if (value >= static_cast<int32_t>(VerticalAlign::TOP) && value <= static_cast<int32_t>(VerticalAlign::BOTTOM)) {
155 align = static_cast<VerticalAlign>(value);
156 }
157 }
158 auto obj = new VerticalAlignDeclaration(align);
159 args.SetReturnValue(obj);
160 }
161
DestructorCallback(VerticalAlignDeclaration * obj)162 void VerticalAlignDeclaration::DestructorCallback(VerticalAlignDeclaration* obj)
163 {
164 delete obj;
165 }
166
167 } // namespace OHOS::Ace::Framework
168