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 "core/components/stack/render_stack.h"
17
18 #include "base/utils/utils.h"
19 #include "core/components/positioned/render_positioned.h"
20 #include "core/components/stack/stack_component.h"
21 #include "core/pipeline/base/position_layout_utils.h"
22
23 namespace OHOS::Ace {
24
Update(const RefPtr<Component> & component)25 void RenderStack::Update(const RefPtr<Component>& component)
26 {
27 const auto stack = AceType::DynamicCast<StackComponent>(component);
28 if (!stack) {
29 return;
30 }
31 align_ = stack->GetAlignment();
32 fit_ = stack->GetStackFit();
33 overflow_ = stack->GetOverflow();
34 mainStackSize_ = stack->GetMainStackSize();
35 MarkNeedLayout();
36 }
37
PerformLayout()38 void RenderStack::PerformLayout()
39 {
40 Size maxSize = GetLayoutParam().GetMaxSize();
41 bool hasNonPositionedItem = false;
42 if (GetChildren().empty()) {
43 LOGD("RenderStack: No child in Stack. Use max size of LayoutParam.");
44 SetLayoutSize(maxSize);
45 return;
46 }
47 LayoutParam innerLayout;
48 // layout children
49 RefPtr<RenderNode> firstChild;
50 std::list<RefPtr<RenderNode>> percentChild;
51 for (const auto& item : GetChildren()) {
52 if (item->GetIsPercentSize()) {
53 percentChild.emplace_back(item);
54 continue;
55 }
56 auto positionedItem = AceType::DynamicCast<RenderPositioned>(item);
57 if (!positionedItem) {
58 hasNonPositionedItem = true;
59 innerLayout = MakeNonPositionedInnerLayoutParam(firstChild);
60 item->Layout(innerLayout);
61 } else {
62 innerLayout = MakePositionedInnerLayoutParam(positionedItem, firstChild);
63 positionedItem->Layout(innerLayout);
64 }
65 if (!firstChild) {
66 firstChild = item;
67 }
68 }
69 // determine the stack size
70 DetermineStackSize(hasNonPositionedItem);
71
72 auto context = context_.Upgrade();
73 if (context && context->GetIsDeclarative()) {
74 auto layoutParam = GetLayoutParam();
75 layoutParam.SetMaxSize(GetLayoutSize());
76 SetLayoutParam(layoutParam);
77 }
78
79 // secondnary layout for percentchild
80 for (const auto& item : percentChild) {
81 innerLayout.SetMaxSize(GetLayoutSize());
82 item->Layout(innerLayout);
83 }
84
85 SetChildrenStatus();
86 // place children
87 for (const auto& item : GetChildren()) {
88 auto positionedItem = AceType::DynamicCast<RenderPositioned>(item);
89 if (!positionedItem) {
90 if (item->GetPositionType() == PositionType::ABSOLUTE) {
91 auto itemOffset = PositionLayoutUtils::GetAbsoluteOffset(Claim(this), item);
92 item->SetAbsolutePosition(itemOffset);
93 continue;
94 }
95 item->SetPosition(GetNonPositionedChildOffset(item->GetLayoutSize()));
96 continue;
97 }
98 Offset offset = GetPositionedChildOffset(positionedItem);
99 if (offset.GetX() < 0.0 || offset.GetY() < 0.0 ||
100 offset.GetX() + positionedItem->GetLayoutSize().Width() > GetLayoutSize().Width() ||
101 offset.GetY() + positionedItem->GetLayoutSize().Height() > GetLayoutSize().Height()) {
102 isChildOverflow_ = true;
103 }
104 positionedItem->SetPosition(offset);
105 }
106 }
107
DetermineStackSize(bool hasNonPositioned)108 void RenderStack::DetermineStackSize(bool hasNonPositioned)
109 {
110 Size maxSize = GetLayoutParam().GetMaxSize();
111 if (maxSize.IsWidthInfinite()) {
112 maxSize.SetWidth(viewPort_.Width());
113 }
114 if (maxSize.IsHeightInfinite()) {
115 maxSize.SetHeight(viewPort_.Height());
116 }
117
118 if (mainStackSize_ == MainStackSize::MAX && !maxSize.IsInfinite()) {
119 SetLayoutSize(maxSize);
120 return;
121 }
122 double width = GetLayoutParam().GetMinSize().Width();
123 double height = GetLayoutParam().GetMinSize().Height();
124 double maxX = 0.0;
125 double maxY = 0.0;
126 double lastChildWidth = width;
127 double lastChildHeight = height;
128 for (const auto& item : GetChildren()) {
129 if (item->GetIsPercentSize()) {
130 continue;
131 }
132 double constrainedWidth = std::clamp(item->GetLayoutSize().Width(), GetLayoutParam().GetMinSize().Width(),
133 GetLayoutParam().GetMaxSize().Width());
134 double constrainedHeight = std::clamp(item->GetLayoutSize().Height(), GetLayoutParam().GetMinSize().Height(),
135 GetLayoutParam().GetMaxSize().Height());
136 width = std::max(width, constrainedWidth);
137 height = std::max(height, constrainedHeight);
138 lastChildWidth = constrainedWidth;
139 lastChildHeight = constrainedHeight;
140 maxX = std::max(maxX, item->GetLayoutSize().Width() + NormalizePercentToPx(item->GetLeft(), false));
141 maxY = std::max(maxY, item->GetLayoutSize().Height() + NormalizePercentToPx(item->GetTop(), true));
142 }
143 if (mainStackSize_ == MainStackSize::NORMAL && !hasNonPositioned && !maxSize.IsInfinite()) {
144 SetLayoutSize(maxSize);
145 return;
146 }
147 // Usually used in SemiModal for determining current height.
148 if (mainStackSize_ == MainStackSize::LAST_CHILD_HEIGHT) {
149 SetLayoutSize(Size(maxSize.Width(), lastChildHeight));
150 return;
151 }
152 if (mainStackSize_ == MainStackSize::MATCH_CHILDREN) {
153 SetLayoutSize(GetLayoutParam().Constrain(Size(maxX, maxY)));
154 return;
155 }
156 if (mainStackSize_ == MainStackSize::MAX_X) {
157 auto maxSizeX = maxSize.Width();
158 SetLayoutSize(Size(maxSizeX, maxY));
159 return;
160 }
161 if (mainStackSize_ == MainStackSize::MAX_Y) {
162 auto maxSizeY = maxSize.Height();
163 SetLayoutSize(Size(maxX, maxSizeY));
164 return;
165 }
166 SetLayoutSize(Size(width, height));
167 }
168
MakeNonPositionedInnerLayoutParam(const RefPtr<RenderNode> & firstChild) const169 LayoutParam RenderStack::MakeNonPositionedInnerLayoutParam(const RefPtr<RenderNode>& firstChild) const
170 {
171 LayoutParam innerLayout;
172 switch (fit_) {
173 case StackFit::STRETCH:
174 innerLayout.SetFixedSize(GetLayoutParam().GetMaxSize());
175 break;
176 case StackFit::KEEP:
177 innerLayout.SetMaxSize(GetLayoutParam().GetMaxSize());
178 break;
179 case StackFit::INHERIT:
180 innerLayout = GetLayoutParam();
181 break;
182 case StackFit::FIRST_CHILD:
183 innerLayout = GetLayoutParam();
184 if (firstChild) {
185 innerLayout.SetMaxSize(firstChild->GetLayoutSize());
186 }
187 break;
188 default:
189 LOGD("RenderStack: No such StackFit support. Use KEEP.");
190 innerLayout.SetMaxSize(GetLayoutParam().GetMaxSize());
191 break;
192 }
193 return innerLayout;
194 }
195
MakePositionedInnerLayoutParam(const RefPtr<RenderPositioned> & item,const RefPtr<RenderNode> & firstChild) const196 LayoutParam RenderStack::MakePositionedInnerLayoutParam(
197 const RefPtr<RenderPositioned>& item, const RefPtr<RenderNode>& firstChild) const
198 {
199 LayoutParam innerLayout;
200 double width = std::clamp(item->GetWidth(), innerLayout.GetMinSize().Width(), innerLayout.GetMaxSize().Width());
201 double height = std::clamp(item->GetHeight(), innerLayout.GetMinSize().Height(), innerLayout.GetMaxSize().Height());
202 if (!NearZero(width) && !NearZero(height)) {
203 innerLayout.SetFixedSize(Size(width, height));
204 } else if (!NearZero(width)) {
205 innerLayout.SetMinSize(Size(width, innerLayout.GetMinSize().Height()));
206 innerLayout.SetMaxSize(Size(width, innerLayout.GetMaxSize().Height()));
207 } else if (!NearZero(height)) {
208 innerLayout.SetMinSize(Size(innerLayout.GetMinSize().Width(), height));
209 innerLayout.SetMaxSize(Size(innerLayout.GetMaxSize().Width(), height));
210 } else {
211 LOGD("RenderStack: No width or height set in positioned component. Make NonpositionedInnerLayoutParam.");
212 innerLayout = MakeNonPositionedInnerLayoutParam(firstChild);
213 }
214 return innerLayout;
215 }
216
GetNonPositionedChildOffset(const Size & childSize)217 Offset RenderStack::GetNonPositionedChildOffset(const Size& childSize)
218 {
219 return Alignment::GetAlignPosition(GetLayoutSize(), childSize, align_);
220 }
221
GetPositionedChildOffset(const RefPtr<RenderPositioned> & item)222 Offset RenderStack::GetPositionedChildOffset(const RefPtr<RenderPositioned>& item)
223 {
224 double deltaX = 0.0;
225 if (item->HasLeft()) {
226 deltaX = NormalizePercentToPx(item->GetLeft(), false);
227 } else if (item->HasRight()) {
228 deltaX =
229 GetLayoutSize().Width() - NormalizePercentToPx(item->GetRight(), false) - item->GetLayoutSize().Width();
230 } else {
231 deltaX = GetNonPositionedChildOffset(item->GetLayoutSize()).GetX();
232 }
233 double deltaY = 0.0;
234 if (item->HasTop()) {
235 deltaY = NormalizePercentToPx(item->GetTop(), true);
236 } else if (item->HasBottom()) {
237 deltaY =
238 GetLayoutSize().Height() - NormalizePercentToPx(item->GetBottom(), true) - item->GetLayoutSize().Height();
239 } else {
240 deltaY = GetNonPositionedChildOffset(item->GetLayoutSize()).GetY();
241 }
242 return Offset(deltaX, deltaY);
243 }
244
OnAttachContext()245 void RenderStack::OnAttachContext()
246 {
247 RenderNode::OnAttachContext();
248 auto context = context_.Upgrade();
249 if (context && context->GetIsDeclarative()) {
250 SetExclusiveEventForChild(true);
251 }
252 }
253
254 } // namespace OHOS::Ace