• 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 {
GetInstance()27 FlexModel* FlexModel::GetInstance()
28 {
29 #ifdef NG_BUILD
30     static NG::FlexModelNG instance;
31     return &instance;
32 #else
33     if (Container::IsCurrentUseNewPipeline()) {
34         static NG::FlexModelNG instance;
35         return &instance;
36     } else {
37         static Framework::FlexModelImpl instance;
38         return &instance;
39     }
40 #endif
41 }
42 } // namespace OHOS::Ace
43 namespace OHOS::Ace::Framework {
44 
SetFillParent()45 void JSFlex::SetFillParent()
46 {
47     /* Deprecated */
48 }
49 
SetWrapContent()50 void JSFlex::SetWrapContent()
51 {
52     /* Deprecated */
53 }
54 
SetJustifyContent(int32_t value)55 void JSFlex::SetJustifyContent(int32_t value)
56 {
57     if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
58         (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) ||
59         (value == static_cast<int32_t>(FlexAlign::SPACE_AROUND)) ||
60         (value == static_cast<int32_t>(FlexAlign::SPACE_BETWEEN)) ||
61         (value == static_cast<int32_t>(FlexAlign::SPACE_EVENLY))) {
62         FlexModel::GetInstance()->SetJustifyContent(value);
63     } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
64         FlexModel::GetInstance()->SetJustifyContent(static_cast<int32_t>(FlexAlign::FLEX_START));
65     }
66 }
67 
SetAlignItems(int32_t value)68 void JSFlex::SetAlignItems(int32_t value)
69 {
70     if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
71         (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) ||
72         (value == static_cast<int32_t>(FlexAlign::STRETCH))) {
73         FlexModel::GetInstance()->SetAlignItems(value);
74     } else if (Container::GreatOrEqualAPIVersion(PlatformVersion::VERSION_TEN)) {
75         FlexModel::GetInstance()->SetAlignItems(static_cast<int32_t>(FlexAlign::FLEX_START));
76     }
77 }
78 
SetAlignContent(int32_t value)79 void JSFlex::SetAlignContent(int32_t value)
80 {
81     if ((value == static_cast<int32_t>(WrapAlignment::START)) ||
82         (value == static_cast<int32_t>(WrapAlignment::CENTER)) || (value == static_cast<int32_t>(WrapAlignment::END)) ||
83         (value == static_cast<int32_t>(WrapAlignment::SPACE_AROUND)) ||
84         (value == static_cast<int32_t>(WrapAlignment::SPACE_BETWEEN)) ||
85         (value == static_cast<int32_t>(WrapAlignment::STRETCH))) {
86         FlexModel::GetInstance()->SetAlignContent(value);
87     }
88 }
89 
JsHeight(const JSCallbackInfo & info)90 void JSFlex::JsHeight(const JSCallbackInfo& info)
91 {
92     if (info.Length() < 1) {
93         return;
94     }
95 
96     SetHeight(info[0]);
97 }
98 
SetHeight(const JSRef<JSVal> & jsValue)99 void JSFlex::SetHeight(const JSRef<JSVal>& jsValue)
100 {
101     FlexModel::GetInstance()->SetHeightLayoutPolicy(static_cast<uint8_t>(LayoutCalPolicy::NO_MATCH));
102     if (!JSViewAbstract::JsHeight(jsValue)) {
103         // JsHeight return false, check if set LayoutPolicy before return.
104         if (!jsValue->IsObject()) {
105             return;
106         }
107         JSRef<JSObject> object = JSRef<JSObject>::Cast(jsValue);
108         JSRef<JSVal> layoutPolicy = object->GetProperty("id_");
109         if (layoutPolicy->IsString() && layoutPolicy->ToString() == "matchParent") {
110             FlexModel::GetInstance()->SetHeightLayoutPolicy(
111                 static_cast<uint8_t>(LayoutCalPolicy::MATCH_PARENT));
112             FlexModel::GetInstance()->SetHasHeight();
113         }
114         return;
115     }
116     FlexModel::GetInstance()->SetHasHeight();
117 }
118 
JsWidth(const JSCallbackInfo & info)119 void JSFlex::JsWidth(const JSCallbackInfo& info)
120 {
121     if (info.Length() < 1) {
122         return;
123     }
124 
125     SetWidth(info[0]);
126 }
127 
SetWidth(const JSRef<JSVal> & jsValue)128 void JSFlex::SetWidth(const JSRef<JSVal>& jsValue)
129 {
130     FlexModel::GetInstance()->SetWidthLayoutPolicy(static_cast<uint8_t>(LayoutCalPolicy::NO_MATCH));
131     if (!JSViewAbstract::JsWidth(jsValue)) {
132         // JsWidth return false, check if set LayoutPolicy before return.
133         if (!jsValue->IsObject()) {
134             return;
135         }
136         JSRef<JSObject> object = JSRef<JSObject>::Cast(jsValue);
137         JSRef<JSVal> layoutPolicy = object->GetProperty("id_");
138         if (layoutPolicy->IsString() && layoutPolicy->ToString() == "matchParent") {
139             FlexModel::GetInstance()->SetWidthLayoutPolicy(
140                 static_cast<uint8_t>(LayoutCalPolicy::MATCH_PARENT));
141             FlexModel::GetInstance()->SetHasWidth();
142         }
143         return;
144     }
145     FlexModel::GetInstance()->SetHasWidth();
146 }
147 
JsSize(const JSCallbackInfo & info)148 void JSFlex::JsSize(const JSCallbackInfo& info)
149 {
150     if (info.Length() < 1) {
151         return;
152     }
153 
154     if (info[0]->IsUndefined()) {
155         ViewAbstractModel::GetInstance()->ClearWidthOrHeight(true);
156         ViewAbstractModel::GetInstance()->ClearWidthOrHeight(false);
157         return;
158     }
159 
160     if (!info[0]->IsObject()) {
161         return;
162     }
163 
164     JSRef<JSObject> sizeObj = JSRef<JSObject>::Cast(info[0]);
165     SetWidth(sizeObj->GetProperty("width"));
166     SetHeight(sizeObj->GetProperty("height"));
167 }
168 
169 }; // namespace OHOS::Ace::Framework
170