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 if (!JSViewAbstract::JsHeight(jsValue)) {
115 // JsHeight return false, just return.
116 return;
117 }
118 FlexModel::GetInstance()->SetHasHeight();
119 }
120
JsWidth(const JSCallbackInfo & info)121 void JSFlex::JsWidth(const JSCallbackInfo& info)
122 {
123 if (info.Length() < 1) {
124 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
125 return;
126 }
127
128 SetWidth(info[0]);
129 }
130
SetWidth(const JSRef<JSVal> & jsValue)131 void JSFlex::SetWidth(const JSRef<JSVal>& jsValue)
132 {
133 if (!JSViewAbstract::JsWidth(jsValue)) {
134 // JsWidth return false, just return.
135 return;
136 }
137 FlexModel::GetInstance()->SetHasWidth();
138 }
139
JsSize(const JSCallbackInfo & info)140 void JSFlex::JsSize(const JSCallbackInfo& info)
141 {
142 if (info.Length() < 1) {
143 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
144 return;
145 }
146
147 if (info[0]->IsUndefined()) {
148 ViewAbstractModel::GetInstance()->ClearWidthOrHeight(true);
149 ViewAbstractModel::GetInstance()->ClearWidthOrHeight(false);
150 return;
151 }
152
153 if (!info[0]->IsObject()) {
154 LOGE("arg is not Object or String.");
155 return;
156 }
157
158 JSRef<JSObject> sizeObj = JSRef<JSObject>::Cast(info[0]);
159 SetWidth(sizeObj->GetProperty("width"));
160 SetHeight(sizeObj->GetProperty("height"));
161 }
162
163 }; // namespace OHOS::Ace::Framework
164