• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (PipelineBase::GetCurrentContext() &&
60                    PipelineBase::GetCurrentContext()->GetMinPlatformVersion() > 9) {
61             space = Dimension();
62         }
63     }
64     VerticalAlignDeclaration* declaration = nullptr;
65     if (info.Length() > 0 && info[0]->IsObject()) {
66         JSRef<JSObject> obj = JSRef<JSObject>::Cast(info[0]);
67         JSRef<JSVal> useAlign = obj->GetProperty("useAlign");
68         if (useAlign->IsObject()) {
69             declaration = JSRef<JSObject>::Cast(useAlign)->Unwrap<VerticalAlignDeclaration>();
70         }
71     }
72 
73     RowModel::GetInstance()->Create(space, declaration, "");
74 }
75 
CreateWithWrap(const JSCallbackInfo & info)76 void JSRow::CreateWithWrap(const JSCallbackInfo& info)
77 {
78     RowModel::GetInstance()->CreateWithWrap();
79 }
80 
SetAlignItems(int32_t value)81 void JSRow::SetAlignItems(int32_t value)
82 {
83     if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
84         (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) ||
85         (value == static_cast<int32_t>(FlexAlign::STRETCH))) {
86         RowModel::GetInstance()->SetAlignItems(static_cast<FlexAlign>(value));
87     } else if (PipelineBase::GetCurrentContext() && PipelineBase::GetCurrentContext()->GetMinPlatformVersion() > 9) {
88         RowModel::GetInstance()->SetAlignItems(FlexAlign::CENTER);
89         // FIXME: we have a design issue here, setters return void, can not signal error to JS
90         LOGE("invalid value for justifyContent");
91     }
92 }
93 
SetJustifyContent(int32_t value)94 void JSRow::SetJustifyContent(int32_t value)
95 {
96     if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
97         (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) ||
98         (value == static_cast<int32_t>(FlexAlign::SPACE_BETWEEN)) ||
99         (value == static_cast<int32_t>(FlexAlign::SPACE_AROUND)) ||
100         (value == static_cast<int32_t>(FlexAlign::SPACE_EVENLY))) {
101         RowModel::GetInstance()->SetJustifyContent(static_cast<FlexAlign>(value));
102     } else if (PipelineBase::GetCurrentContext() && PipelineBase::GetCurrentContext()->GetMinPlatformVersion() > 9) {
103         RowModel::GetInstance()->SetJustifyContent(FlexAlign::FLEX_START);
104         LOGE("invalid value for justifyContent");
105     }
106 }
107 
JSBind(BindingTarget globalObj)108 void JSRow::JSBind(BindingTarget globalObj)
109 {
110     JSClass<JSRow>::Declare("Row");
111     MethodOptions opt = MethodOptions::NONE;
112     JSClass<JSRow>::StaticMethod("create", &JSRow::Create, opt);
113     JSClass<JSRow>::StaticMethod("createWithWrap", &JSRow::CreateWithWrap, opt);
114     JSClass<JSRow>::StaticMethod("fillParent", &JSFlex::SetFillParent, opt);
115     JSClass<JSRow>::StaticMethod("wrapContent", &JSFlex::SetWrapContent, opt);
116     JSClass<JSRow>::StaticMethod("justifyContent", &JSRow::SetJustifyContent, opt);
117     JSClass<JSRow>::StaticMethod("alignItems", &JSRow::SetAlignItems, opt);
118     JSClass<JSRow>::StaticMethod("alignContent", &JSFlex::SetAlignContent, opt);
119     JSClass<JSRow>::StaticMethod("height", &JSFlex::JsHeight, opt);
120     JSClass<JSRow>::StaticMethod("width", &JSFlex::JsWidth, opt);
121     JSClass<JSRow>::StaticMethod("size", &JSFlex::JsSize, opt);
122     JSClass<JSRow>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
123     JSClass<JSRow>::StaticMethod("onHover", &JSInteractableView::JsOnHover);
124     JSClass<JSRow>::StaticMethod("onKeyEvent", &JSInteractableView::JsOnKey);
125     JSClass<JSRow>::StaticMethod("onDeleteEvent", &JSInteractableView::JsOnDelete);
126     JSClass<JSRow>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
127     JSClass<JSRow>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
128     JSClass<JSRow>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
129     JSClass<JSRow>::StaticMethod("remoteMessage", &JSInteractableView::JsCommonRemoteMessage);
130     JSClass<JSRow>::InheritAndBind<JSContainerBase>(globalObj);
131 
132     JSClass<VerticalAlignDeclaration>::Declare("VerticalAlignDeclaration");
133     JSClass<VerticalAlignDeclaration>::Bind(
134         globalObj, VerticalAlignDeclaration::ConstructorCallback, VerticalAlignDeclaration::DestructorCallback);
135 }
136 
ConstructorCallback(const JSCallbackInfo & args)137 void VerticalAlignDeclaration::ConstructorCallback(const JSCallbackInfo& args)
138 {
139     auto align = VerticalAlign::CENTER;
140     if (args.Length() > 0 && args[0]->IsNumber()) {
141         auto value = args[0]->ToNumber<int32_t>();
142         if (value >= static_cast<int32_t>(VerticalAlign::TOP) && value <= static_cast<int32_t>(VerticalAlign::BOTTOM)) {
143             align = static_cast<VerticalAlign>(value);
144         }
145     }
146     auto obj = new VerticalAlignDeclaration(align);
147     args.SetReturnValue(obj);
148 }
149 
DestructorCallback(VerticalAlignDeclaration * obj)150 void VerticalAlignDeclaration::DestructorCallback(VerticalAlignDeclaration* obj)
151 {
152     delete obj;
153 }
154 
155 } // namespace OHOS::Ace::Framework
156