1 /*
2  * Copyright (c) 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 "bridge/declarative_frontend/jsview/models/flex_model_impl.h"
17 
18 #include "bridge/declarative_frontend/view_stack_processor.h"
19 #include "core/components/flex/flex_component.h"
20 #include "core/components/flex/flex_component_v2.h"
21 #include "core/components/wrap/wrap_component.h"
22 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
23 
24 namespace OHOS::Ace::Framework {
25 
CreateFlexRow()26 void FlexModelImpl::CreateFlexRow()
27 {
28     std::list<RefPtr<Component>> children;
29     RefPtr<FlexComponentV2> row = AceType::MakeRefPtr<OHOS::Ace::FlexComponentV2>(
30         FlexDirection::ROW, FlexAlign::FLEX_START, FlexAlign::STRETCH, children);
31     row->SetInspectorTag("FlexComponentV2");
32     ViewStackProcessor::GetInstance()->ClaimElementId(row);
33     ViewStackProcessor::GetInstance()->Push(row);
34 }
35 
CreateWrap()36 void FlexModelImpl::CreateWrap()
37 {
38     std::list<RefPtr<Component>> children;
39     auto wrapComponent = AceType::MakeRefPtr<WrapComponent>(children);
40     wrapComponent->SetMainAlignment(WrapAlignment::START);
41     wrapComponent->SetCrossAlignment(WrapAlignment::STRETCH);
42     ViewStackProcessor::GetInstance()->ClaimElementId(wrapComponent);
43     ViewStackProcessor::GetInstance()->Push(wrapComponent);
44 }
45 
SetFlexWidth()46 void FlexModelImpl::SetFlexWidth()
47 {
48     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
49     auto widthVal = box->GetWidth();
50     auto mainComponent = ViewStackProcessor::GetInstance()->GetMainComponent();
51     auto flex = AceType::DynamicCast<FlexComponent>(mainComponent);
52     if (flex) {
53         if (flex->GetDirection() == FlexDirection::ROW || flex->GetDirection() == FlexDirection::ROW_REVERSE) {
54             flex->SetMainAxisSize(widthVal.Value() < 0.0 ? MainAxisSize::MIN : MainAxisSize::MAX);
55         } else {
56             flex->SetCrossAxisSize(widthVal.Value() < 0.0 ? CrossAxisSize::MIN : CrossAxisSize::MAX);
57         }
58     } else {
59         auto wrap = AceType::DynamicCast<WrapComponent>(mainComponent);
60         if (wrap) {
61             wrap->SetHorizontalMeasure(widthVal.Value() < 0.0 ? MeasureType::CONTENT : MeasureType::PARENT);
62         }
63     }
64 }
65 
SetFlexHeight()66 void FlexModelImpl::SetFlexHeight()
67 {
68     auto box = ViewStackProcessor::GetInstance()->GetBoxComponent();
69     auto heightVal = box->GetHeight();
70     auto mainComponent = ViewStackProcessor::GetInstance()->GetMainComponent();
71     auto flex = AceType::DynamicCast<FlexComponent>(mainComponent);
72     if (flex) {
73         if (flex->GetDirection() == FlexDirection::COLUMN || flex->GetDirection() == FlexDirection::COLUMN_REVERSE) {
74             flex->SetMainAxisSize(heightVal.Value() < 0.0 ? MainAxisSize::MIN : MainAxisSize::MAX);
75         } else {
76             flex->SetCrossAxisSize(heightVal.Value() < 0.0 ? CrossAxisSize::MIN : CrossAxisSize::MAX);
77         }
78     } else {
79         auto wrap = AceType::DynamicCast<WrapComponent>(mainComponent);
80         if (wrap) {
81             wrap->SetVerticalMeasure(heightVal.Value() < 0.0 ? MeasureType::CONTENT : MeasureType::PARENT);
82         }
83     }
84 }
85 
SetJustifyContent(int32_t value)86 void FlexModelImpl::SetJustifyContent(int32_t value)
87 {
88     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
89     auto flex = AceType::DynamicCast<FlexComponent>(component);
90     if (flex) {
91         flex->SetMainAxisAlign(static_cast<FlexAlign>(value));
92     } else {
93         auto wrap = AceType::DynamicCast<WrapComponent>(component);
94         if (wrap) {
95             wrap->SetMainAlignment(static_cast<WrapAlignment>(value));
96         }
97     }
98 }
99 
SetAlignItems(int32_t value)100 void FlexModelImpl::SetAlignItems(int32_t value)
101 {
102     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
103     auto flex = AceType::DynamicCast<FlexComponent>(component);
104     if (flex) {
105         flex->SetCrossAxisAlign(static_cast<FlexAlign>(value));
106     } else {
107         auto wrap = AceType::DynamicCast<WrapComponent>(component);
108         if (wrap) {
109             wrap->SetCrossAlignment(static_cast<WrapAlignment>(value));
110         }
111     }
112 }
113 
SetAlignContent(int32_t value)114 void FlexModelImpl::SetAlignContent(int32_t value)
115 {
116     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
117     auto wrap = AceType::DynamicCast<WrapComponent>(component);
118     if (wrap) {
119         wrap->SetAlignment(static_cast<WrapAlignment>(value));
120     }
121 }
122 
SetWrapDirection(WrapDirection direction)123 void FlexModelImpl::SetWrapDirection(WrapDirection direction)
124 {
125     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
126     auto wrap = AceType::DynamicCast<WrapComponent>(component);
127     if (wrap) {
128         wrap->SetDirection(direction);
129     }
130 }
131 
SetDirection(FlexDirection direction)132 void FlexModelImpl::SetDirection(FlexDirection direction)
133 {
134     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
135     auto flex = AceType::DynamicCast<FlexComponent>(component);
136     if (flex) {
137         flex->SetDirection(direction);
138     }
139 }
140 
SetMainAxisAlign(FlexAlign flexAlign)141 void FlexModelImpl::SetMainAxisAlign(FlexAlign flexAlign)
142 {
143     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
144     auto flex = AceType::DynamicCast<FlexComponent>(component);
145     if (flex) {
146         flex->SetMainAxisAlign(flexAlign);
147     }
148 }
149 
SetWrapMainAlignment(WrapAlignment value)150 void FlexModelImpl::SetWrapMainAlignment(WrapAlignment value)
151 {
152     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
153     auto wrap = AceType::DynamicCast<WrapComponent>(component);
154     if (wrap) {
155         wrap->SetMainAlignment(static_cast<WrapAlignment>(value));
156     }
157 }
158 
SetCrossAxisAlign(FlexAlign flexAlign)159 void FlexModelImpl::SetCrossAxisAlign(FlexAlign flexAlign)
160 {
161     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
162     auto flex = AceType::DynamicCast<FlexComponent>(component);
163     if (flex) {
164         flex->SetCrossAxisAlign(flexAlign);
165     }
166 }
167 
SetWrapCrossAlignment(WrapAlignment value)168 void FlexModelImpl::SetWrapCrossAlignment(WrapAlignment value)
169 {
170     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
171     auto wrap = AceType::DynamicCast<WrapComponent>(component);
172     if (wrap) {
173         wrap->SetCrossAlignment(static_cast<WrapAlignment>(value));
174     }
175 }
176 
SetWrapAlignment(WrapAlignment value)177 void FlexModelImpl::SetWrapAlignment(WrapAlignment value)
178 {
179     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
180     auto wrap = AceType::DynamicCast<WrapComponent>(component);
181     if (wrap) {
182         wrap->SetAlignment(static_cast<WrapAlignment>(value));
183     }
184 }
185 
SetHasHeight()186 void FlexModelImpl::SetHasHeight()
187 {
188     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
189     auto columComponent = AceType::DynamicCast<ColumnComponent>(component);
190     auto rowComponent = AceType::DynamicCast<RowComponent>(component);
191     if (columComponent) {
192         columComponent->SetMainAxisSize(MainAxisSize::MAX);
193     }
194     if (rowComponent) {
195         rowComponent->SetCrossAxisSize(CrossAxisSize::MAX);
196     }
197 }
198 
SetHasWidth()199 void FlexModelImpl::SetHasWidth()
200 {
201     auto component = ViewStackProcessor::GetInstance()->GetMainComponent();
202     auto columComponent = AceType::DynamicCast<ColumnComponent>(component);
203     auto rowComponent = AceType::DynamicCast<RowComponent>(component);
204     if (columComponent) {
205         columComponent->SetCrossAxisSize(CrossAxisSize::MAX);
206     }
207     if (rowComponent) {
208         rowComponent->SetMainAxisSize(MainAxisSize::MAX);
209     }
210 }
211 
212 } // namespace OHOS::Ace::Framework
213