1 /*
2 * Copyright (c) 2021 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/wrap/wrap_component.h"
19 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
20 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
21 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
22
23 namespace OHOS::Ace::Framework {
24
SetFillParent()25 void JSFlex::SetFillParent()
26 {
27 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
28 auto flex = AceType::DynamicCast<FlexComponent>(component);
29 if (flex) {
30 flex->SetMainAxisSize(MainAxisSize::MAX);
31 }
32 }
33
SetWrapContent()34 void JSFlex::SetWrapContent()
35 {
36 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
37 auto flex = AceType::DynamicCast<FlexComponent>(component);
38 if (flex) {
39 flex->SetMainAxisSize(MainAxisSize::MIN);
40 }
41 }
42
SetJustifyContent(int32_t value)43 void JSFlex::SetJustifyContent(int32_t value)
44 {
45 if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
46 (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) ||
47 (value == static_cast<int32_t>(FlexAlign::SPACE_AROUND)) ||
48 (value == static_cast<int32_t>(FlexAlign::SPACE_BETWEEN)) ||
49 (value == static_cast<int32_t>(FlexAlign::SPACE_EVENLY))) {
50 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
51 auto flex = AceType::DynamicCast<FlexComponent>(component);
52 if (flex) {
53 flex->SetMainAxisAlign((FlexAlign)value);
54 } else {
55 auto wrap = AceType::DynamicCast<WrapComponent>(component);
56 if (wrap) {
57 wrap->SetMainAlignment((WrapAlignment)value);
58 }
59 }
60 } else {
61 // FIXME: we have a design issue here, setters return void, can not signal error to JS
62 LOGE("invalid value for justifyContent");
63 }
64 }
65
SetAlignItems(int32_t value)66 void JSFlex::SetAlignItems(int32_t value)
67 {
68 if ((value == static_cast<int32_t>(FlexAlign::FLEX_START)) ||
69 (value == static_cast<int32_t>(FlexAlign::FLEX_END)) || (value == static_cast<int32_t>(FlexAlign::CENTER)) ||
70 (value == static_cast<int32_t>(FlexAlign::STRETCH))) {
71 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
72 auto flex = AceType::DynamicCast<FlexComponent>(component);
73 if (flex) {
74 flex->SetCrossAxisAlign((FlexAlign)value);
75 } else {
76 auto wrap = AceType::DynamicCast<WrapComponent>(component);
77 if (wrap) {
78 wrap->SetCrossAlignment((WrapAlignment)value);
79 }
80 }
81 } else {
82 // FIXME: we have a design issue here, setters return void, can not signal error to JS
83 LOGE("invalid value for justifyContent");
84 }
85 }
86
SetAlignContent(int32_t value)87 void JSFlex::SetAlignContent(int32_t value)
88 {
89 if ((value == static_cast<int32_t>(WrapAlignment::START)) ||
90 (value == static_cast<int32_t>(WrapAlignment::CENTER)) || (value == static_cast<int32_t>(WrapAlignment::END)) ||
91 (value == static_cast<int32_t>(WrapAlignment::SPACE_AROUND)) ||
92 (value == static_cast<int32_t>(WrapAlignment::SPACE_BETWEEN)) ||
93 (value == static_cast<int32_t>(WrapAlignment::STRETCH))) {
94 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
95 auto wrap = AceType::DynamicCast<WrapComponent>(component);
96 if (wrap) {
97 wrap->SetAlignment((WrapAlignment)value);
98 }
99 } else {
100 // FIXME: we have a design issue here, setters return void, can not signal error to JS
101 LOGE("invalid value for justifyContent");
102 }
103 }
104
JsHeight(const JSCallbackInfo & info)105 void JSFlex::JsHeight(const JSCallbackInfo& info)
106 {
107 if (info.Length() < 1) {
108 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
109 return;
110 }
111
112 SetHeight(info[0]);
113 }
114
SetHeight(const JSRef<JSVal> & jsValue)115 void JSFlex::SetHeight(const JSRef<JSVal>& jsValue)
116 {
117 JSViewAbstract::JsHeight(jsValue);
118
119 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
120 auto columComponent = AceType::DynamicCast<ColumnComponent>(component);
121 auto rowComponent = AceType::DynamicCast<RowComponent>(component);
122 if (columComponent) {
123 columComponent->SetMainAxisSize(MainAxisSize::MAX);
124 }
125 if (rowComponent) {
126 rowComponent->SetCrossAxisSize(CrossAxisSize::MAX);
127 }
128 }
129
JsWidth(const JSCallbackInfo & info)130 void JSFlex::JsWidth(const JSCallbackInfo& info)
131 {
132 if (info.Length() < 1) {
133 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
134 return;
135 }
136
137 SetWidth(info[0]);
138 }
139
SetWidth(const JSRef<JSVal> & jsValue)140 void JSFlex::SetWidth(const JSRef<JSVal>& jsValue)
141 {
142 JSViewAbstract::JsWidth(jsValue);
143
144 auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
145 auto columComponent = AceType::DynamicCast<ColumnComponent>(component);
146 auto rowComponent = AceType::DynamicCast<RowComponent>(component);
147 if (columComponent) {
148 columComponent->SetCrossAxisSize(CrossAxisSize::MAX);
149 }
150 if (rowComponent) {
151 rowComponent->SetMainAxisSize(MainAxisSize::MAX);
152 }
153 }
154
JsSize(const JSCallbackInfo & info)155 void JSFlex::JsSize(const JSCallbackInfo& info)
156 {
157 if (info.Length() < 1) {
158 LOGE("The arg is wrong, it is supposed to have atleast 1 arguments");
159 return;
160 }
161
162 if (!info[0]->IsObject()) {
163 LOGE("arg is not Object or String.");
164 return;
165 }
166
167 JSRef<JSObject> sizeObj = JSRef<JSObject>::Cast(info[0]);
168 SetWidth(sizeObj->GetProperty("width"));
169 SetHeight(sizeObj->GetProperty("height"));
170 }
171
172 }; // namespace OHOS::Ace::Framework
173