• 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_flex.h"
17 
18 #include "core/components_ng/base/view_abstract_model.h"
19 #include "core/components_ng/pattern/flex/flex_model.h"
20 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
21 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
22 #include "frameworks/bridge/declarative_frontend/jsview/models/flex_model_impl.h"
23 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
24 #include "frameworks/core/components_ng/pattern/flex/flex_model_ng.h"
25 
26 namespace OHOS::Ace {
27 
28 std::unique_ptr<FlexModel> FlexModel::instance_ = nullptr;
29 std::mutex FlexModel::mutex_;
30 
GetInstance()31 FlexModel* FlexModel::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::FlexModelNG());
38 #else
39             if (Container::IsCurrentUseNewPipeline()) {
40                 instance_.reset(new NG::FlexModelNG());
41             } else {
42                 instance_.reset(new Framework::FlexModelImpl());
43             }
44 #endif
45         }
46     }
47     return instance_.get();
48 }
49 
50 } // namespace OHOS::Ace
51 namespace OHOS::Ace::Framework {
52 
SetFillParent()53 void JSFlex::SetFillParent()
54 {
55     /* Deprecated */
56 }
57 
SetWrapContent()58 void JSFlex::SetWrapContent()
59 {
60     /* Deprecated */
61 }
62 
SetJustifyContent(int32_t value)63 void JSFlex::SetJustifyContent(int32_t value)
64 {
65     if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
66         (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) ||
67         (value == static_cast<int32_t>(FlexAlign::SPACE_AROUND)) ||
68         (value == static_cast<int32_t>(FlexAlign::SPACE_BETWEEN)) ||
69         (value == static_cast<int32_t>(FlexAlign::SPACE_EVENLY))) {
70         FlexModel::GetInstance()->SetJustifyContent(value);
71     } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
72         FlexModel::GetInstance()->SetJustifyContent(static_cast<int32_t>(FlexAlign::FLEX_START));
73         LOGE("invalid value for justifyContent");
74     }
75 }
76 
SetAlignItems(int32_t value)77 void JSFlex::SetAlignItems(int32_t value)
78 {
79     if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
80         (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) ||
81         (value == static_cast<int32_t>(FlexAlign::STRETCH))) {
82         FlexModel::GetInstance()->SetAlignItems(value);
83     } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
84         FlexModel::GetInstance()->SetAlignItems(static_cast<int32_t>(FlexAlign::FLEX_START));
85         LOGE("invalid value for alignItems");
86     }
87 }
88 
SetAlignContent(int32_t value)89 void JSFlex::SetAlignContent(int32_t value)
90 {
91     if ((value == static_cast<int32_t>(WrapAlignment::START)) ||
92         (value == static_cast<int32_t>(WrapAlignment::CENTER)) || (value == static_cast<int32_t>(WrapAlignment::END)) ||
93         (value == static_cast<int32_t>(WrapAlignment::SPACE_AROUND)) ||
94         (value == static_cast<int32_t>(WrapAlignment::SPACE_BETWEEN)) ||
95         (value == static_cast<int32_t>(WrapAlignment::STRETCH))) {
96         FlexModel::GetInstance()->SetAlignContent(value);
97     } else {
98         LOGE("invalid value for alignContent");
99     }
100 }
101 
JsHeight(const JSCallbackInfo & info)102 void JSFlex::JsHeight(const JSCallbackInfo& info)
103 {
104     if (info.Length() < 1) {
105         LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
106         return;
107     }
108 
109     SetHeight(info[0]);
110 }
111 
SetHeight(const JSRef<JSVal> & jsValue)112 void JSFlex::SetHeight(const JSRef<JSVal>& jsValue)
113 {
114     FlexModel::GetInstance()->SetHeightLayoutPolicy(static_cast<uint8_t>(LayoutCalPolicy::NO_MATCH));
115     if (!JSViewAbstract::JsHeight(jsValue)) {
116         // JsHeight return false, check if set LayoutPolicy before return.
117         if (!jsValue->IsObject()) {
118             return;
119         }
120         JSRef<JSObject> object = JSRef<JSObject>::Cast(jsValue);
121         JSRef<JSVal> layoutPolicy = object->GetProperty("id_");
122         if (layoutPolicy->IsString() && layoutPolicy->ToString() == "matchParent") {
123             FlexModel::GetInstance()->SetHeightLayoutPolicy(
124                 static_cast<uint8_t>(LayoutCalPolicy::MATCH_PARENT));
125             FlexModel::GetInstance()->SetHasHeight();
126         }
127         return;
128     }
129     FlexModel::GetInstance()->SetHasHeight();
130 }
131 
JsWidth(const JSCallbackInfo & info)132 void JSFlex::JsWidth(const JSCallbackInfo& info)
133 {
134     if (info.Length() < 1) {
135         LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
136         return;
137     }
138 
139     SetWidth(info[0]);
140 }
141 
SetWidth(const JSRef<JSVal> & jsValue)142 void JSFlex::SetWidth(const JSRef<JSVal>& jsValue)
143 {
144     FlexModel::GetInstance()->SetWidthLayoutPolicy(static_cast<uint8_t>(LayoutCalPolicy::NO_MATCH));
145     if (!JSViewAbstract::JsWidth(jsValue)) {
146         // JsWidth return false, check if set LayoutPolicy before return.
147         if (!jsValue->IsObject()) {
148             return;
149         }
150         JSRef<JSObject> object = JSRef<JSObject>::Cast(jsValue);
151         JSRef<JSVal> layoutPolicy = object->GetProperty("id_");
152         if (layoutPolicy->IsString() && layoutPolicy->ToString() == "matchParent") {
153             FlexModel::GetInstance()->SetWidthLayoutPolicy(
154                 static_cast<uint8_t>(LayoutCalPolicy::MATCH_PARENT));
155             FlexModel::GetInstance()->SetHasWidth();
156         }
157         return;
158     }
159     FlexModel::GetInstance()->SetHasWidth();
160 }
161 
JsSize(const JSCallbackInfo & info)162 void JSFlex::JsSize(const JSCallbackInfo& info)
163 {
164     if (info.Length() < 1) {
165         LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
166         return;
167     }
168 
169     if (info[0]->IsUndefined()) {
170         ViewAbstractModel::GetInstance()->ClearWidthOrHeight(true);
171         ViewAbstractModel::GetInstance()->ClearWidthOrHeight(false);
172         return;
173     }
174 
175     if (!info[0]->IsObject()) {
176         LOGE("arg is not Object or String.");
177         return;
178     }
179 
180     JSRef<JSObject> sizeObj = JSRef<JSObject>::Cast(info[0]);
181     SetWidth(sizeObj->GetProperty("width"));
182     SetHeight(sizeObj->GetProperty("height"));
183 }
184 
185 }; // namespace OHOS::Ace::Framework
186