• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/components_ng/base/geometry_node.h"
17 
18 #include "core/components_ng/property/measure_utils.h"
19 namespace OHOS::Ace::NG {
20 
Reset()21 void GeometryNode::Reset()
22 {
23     frame_.Reset();
24     margin_.reset();
25     padding_.reset();
26     content_.reset();
27     parentGlobalOffset_.Reset();
28     parentAbsoluteOffset_.Reset();
29     parentLayoutConstraint_.reset();
30     resolvedSingleSafeAreaPadding_.reset();
31     accumulatedSafeAreaExpand_.reset();
32 }
33 
Clone() const34 RefPtr<GeometryNode> GeometryNode::Clone() const
35 {
36     auto node = MakeRefPtr<GeometryNode>();
37     node->frame_ = frame_;
38     if (margin_) {
39         node->margin_ = std::make_unique<MarginPropertyF>(*margin_);
40     }
41     if (padding_) {
42         node->padding_ = std::make_unique<MarginPropertyF>(*padding_);
43     }
44     if (content_) {
45         node->content_ = std::make_unique<GeometryProperty>(*content_);
46     }
47     if (accumulatedSafeAreaExpand_) {
48         node->accumulatedSafeAreaExpand_ = std::make_unique<PaddingPropertyF>(*accumulatedSafeAreaExpand_);
49     }
50     if (resolvedSingleSafeAreaPadding_) {
51         node->resolvedSingleSafeAreaPadding_ = std::make_unique<PaddingPropertyF>(*resolvedSingleSafeAreaPadding_);
52     }
53     node->parentGlobalOffset_ = parentGlobalOffset_;
54     node->parentLayoutConstraint_ = parentLayoutConstraint_;
55     node->parentAbsoluteOffset_ = parentAbsoluteOffset_;
56     node->selfAdjust_ = selfAdjust_;
57     return node;
58 }
59 
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const60 void GeometryNode::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
61 {
62 #if defined(PREVIEW)
63     auto frameSize = frame_.rect_.GetSize();
64     json->Put("width", std::to_string(frameSize.Width()).c_str());
65     json->Put("height", std::to_string(frameSize.Height()).c_str());
66 
67     auto jsonSize = JsonUtil::Create(true);
68     jsonSize->Put("width", std::to_string(frameSize.Width()).c_str());
69     jsonSize->Put("height", std::to_string(frameSize.Height()).c_str());
70     json->Put("size", jsonSize);
71 #endif
72 }
73 
SetAccumulatedSafeAreaEdges(const ExpandEdges & safeAreaPadding)74 void GeometryNode::SetAccumulatedSafeAreaEdges(const ExpandEdges& safeAreaPadding)
75 {
76     if (!accumulatedSafeAreaExpand_) {
77         accumulatedSafeAreaExpand_ = std::make_unique<ExpandEdges>(safeAreaPadding);
78         return;
79     }
80     if (safeAreaPadding.left) {
81         accumulatedSafeAreaExpand_->left = safeAreaPadding.left;
82     }
83     if (safeAreaPadding.right) {
84         accumulatedSafeAreaExpand_->right = safeAreaPadding.right;
85     }
86     if (safeAreaPadding.top) {
87         accumulatedSafeAreaExpand_->top = safeAreaPadding.top;
88     }
89     if (safeAreaPadding.bottom) {
90         accumulatedSafeAreaExpand_->bottom = safeAreaPadding.bottom;
91     }
92 }
93 
GetAccumulatedSafeAreaExpand() const94 const std::unique_ptr<ExpandEdges>& GeometryNode::GetAccumulatedSafeAreaExpand() const
95 {
96     return accumulatedSafeAreaExpand_;
97 }
98 
ResetAccumulatedSafeAreaPadding()99 void GeometryNode::ResetAccumulatedSafeAreaPadding()
100 {
101     CHECK_NULL_VOID(accumulatedSafeAreaExpand_);
102     accumulatedSafeAreaExpand_.reset();
103 }
104 
SetResolvedSingleSafeAreaPadding(const PaddingPropertyF & safeAreaPadding)105 void GeometryNode::SetResolvedSingleSafeAreaPadding(const PaddingPropertyF& safeAreaPadding)
106 {
107     resolvedSingleSafeAreaPadding_ = std::make_unique<PaddingPropertyF>(safeAreaPadding);
108 }
109 
GetResolvedSingleSafeAreaPadding() const110 const std::unique_ptr<PaddingPropertyF>& GeometryNode::GetResolvedSingleSafeAreaPadding() const
111 {
112     return resolvedSingleSafeAreaPadding_;
113 }
114 
ResetResolvedSelfSafeAreaPadding()115 void GeometryNode::ResetResolvedSelfSafeAreaPadding()
116 {
117     resolvedSingleSafeAreaPadding_.reset();
118 }
119 
GetParentAdjust() const120 RectF GeometryNode::GetParentAdjust() const
121 {
122     return parentAdjust_;
123 }
124 
SetParentAdjust(RectF parentAdjust)125 void GeometryNode::SetParentAdjust(RectF parentAdjust)
126 {
127     parentAdjust_ = parentAdjust;
128 }
129 
GetSelfAdjust() const130 RectF GeometryNode::GetSelfAdjust() const
131 {
132     return selfAdjust_;
133 }
134 
SetSelfAdjust(RectF selfAdjust)135 void GeometryNode::SetSelfAdjust(RectF selfAdjust)
136 {
137     selfAdjust_ = selfAdjust;
138 }
139 
GetMarginPreFrameSize(bool withSafeArea) const140 SizeF GeometryNode::GetMarginPreFrameSize(bool withSafeArea) const
141 {
142     auto size = preFrameSize_;
143     if (withSafeArea) {
144         size += selfAdjust_.GetSize();
145     }
146     if (margin_) {
147         AddPaddingToSize(*margin_, size);
148     }
149     return size;
150 }
151 
GetMarginFrameSize(bool withSafeArea) const152 SizeF GeometryNode::GetMarginFrameSize(bool withSafeArea) const
153 {
154     auto size = frame_.rect_.GetSize();
155     if (withSafeArea) {
156         size += selfAdjust_.GetSize();
157     }
158     if (margin_) {
159         AddPaddingToSize(*margin_, size);
160     }
161     return size;
162 }
163 
GetMarginFrameOffset(bool withSafeArea) const164 OffsetF GeometryNode::GetMarginFrameOffset(bool withSafeArea) const
165 {
166     auto offset = frame_.rect_.GetOffset();
167     if (withSafeArea) {
168         offset += selfAdjust_.GetOffset();
169     }
170     if (margin_) {
171         offset -= OffsetF(margin_->left.value_or(0), margin_->top.value_or(0));
172     }
173     return offset;
174 }
175 
GetMarginFrameRect(bool withSafeArea) const176 RectF GeometryNode::GetMarginFrameRect(bool withSafeArea) const
177 {
178     auto offset = frame_.rect_.GetOffset();
179     auto size = frame_.rect_.GetSize();
180     if (withSafeArea) {
181         offset += selfAdjust_.GetOffset();
182         size += selfAdjust_.GetSize();
183     }
184     if (margin_) {
185         offset -= OffsetF(margin_->left.value_or(0), margin_->top.value_or(0));
186         AddPaddingToSize(*margin_, size);
187     }
188     return RectF(offset, size);
189 }
190 
SetMarginFrameOffset(const OffsetF & translate)191 void GeometryNode::SetMarginFrameOffset(const OffsetF& translate)
192 {
193     OffsetF offset;
194     if (margin_) {
195         offset += OffsetF(margin_->left.value_or(0), margin_->top.value_or(0));
196     }
197     frame_.rect_.SetOffset(translate + offset);
198 }
199 
GetFrameRect(bool withSafeArea) const200 RectF GeometryNode::GetFrameRect(bool withSafeArea) const
201 {
202     auto result = frame_.rect_;
203     if (withSafeArea) {
204         result += selfAdjust_;
205     }
206     return result;
207 }
208 
GetFrameSize(bool withSafeArea) const209 SizeF GeometryNode::GetFrameSize(bool withSafeArea) const
210 {
211     auto result = frame_.rect_.GetSize();
212     if (withSafeArea) {
213         result += selfAdjust_.GetSize();
214     }
215     return result;
216 }
217 
GetFrameOffset(bool withSafeArea) const218 OffsetF GeometryNode::GetFrameOffset(bool withSafeArea) const
219 {
220     auto result = frame_.rect_.GetOffset();
221     if (withSafeArea) {
222         result += selfAdjust_.GetOffset();
223     }
224     return result;
225 }
226 
SetMarginFrameOffsetX(int32_t offsetX)227 void GeometryNode::SetMarginFrameOffsetX(int32_t offsetX)
228 {
229     float offset = offsetX;
230     if (margin_) {
231         offset += margin_->left.value_or(0);
232     }
233     frame_.rect_.SetLeft(offset);
234 }
235 
SetMarginFrameOffsetY(int32_t offsetY)236 void GeometryNode::SetMarginFrameOffsetY(int32_t offsetY)
237 {
238     float offset = offsetY;
239     if (margin_) {
240         offset += margin_->top.value_or(0);
241     }
242     frame_.rect_.SetTop(offset);
243 }
244 
GetPaddingSize(bool withSafeArea) const245 SizeF GeometryNode::GetPaddingSize(bool withSafeArea) const
246 {
247     auto size = frame_.rect_.GetSize();
248     if (withSafeArea) {
249         size += selfAdjust_.GetSize();
250     }
251     if (padding_) {
252         MinusPaddingToSize(*padding_, size);
253     }
254     return size;
255 }
256 
GetPaddingOffset(bool withSafeArea) const257 OffsetF GeometryNode::GetPaddingOffset(bool withSafeArea) const
258 {
259     auto offset = frame_.rect_.GetOffset();
260     if (withSafeArea) {
261         offset += selfAdjust_.GetOffset();
262     }
263     if (padding_) {
264         offset += OffsetF(padding_->left.value_or(0), padding_->top.value_or(0));
265     }
266     return offset;
267 }
268 
GetPaddingRect(bool withSafeArea) const269 RectF GeometryNode::GetPaddingRect(bool withSafeArea) const
270 {
271     auto rect = frame_.rect_;
272     if (withSafeArea) {
273         rect += selfAdjust_;
274     }
275     if (padding_) {
276         auto size = rect.GetSize();
277         MinusPaddingToSize(*padding_, size);
278         rect.SetSize(size);
279         auto offset = rect.GetOffset();
280         offset += OffsetF(padding_->left.value_or(0), padding_->top.value_or(0));
281         rect.SetOffset(offset);
282     }
283     return rect;
284 }
285 
SetContentSize(const SizeF & size)286 void GeometryNode::SetContentSize(const SizeF& size)
287 {
288     if (!content_) {
289         content_ = std::make_unique<GeometryProperty>();
290     }
291     content_->rect_.SetSize(size);
292 }
293 
SetContentOffset(const OffsetF & translate)294 void GeometryNode::SetContentOffset(const OffsetF& translate)
295 {
296     if (!content_) {
297         content_ = std::make_unique<GeometryProperty>();
298     }
299     content_->rect_.SetOffset(translate);
300 }
301 
UpdateMargin(const MarginPropertyF & margin)302 void GeometryNode::UpdateMargin(const MarginPropertyF& margin)
303 {
304     if (!margin_) {
305         margin_ = std::make_unique<MarginPropertyF>(margin);
306         return;
307     }
308     margin_->Reset();
309     if (margin.left) {
310         margin_->left = margin.left;
311     }
312     if (margin.right) {
313         margin_->right = margin.right;
314     }
315     if (margin.top) {
316         margin_->top = margin.top;
317     }
318     if (margin.bottom) {
319         margin_->bottom = margin.bottom;
320     }
321 }
322 
UpdatePaddingWithBorder(const PaddingPropertyF & padding)323 void GeometryNode::UpdatePaddingWithBorder(const PaddingPropertyF& padding)
324 {
325     if (!padding_) {
326         padding_ = std::make_unique<PaddingPropertyF>(padding);
327         return;
328     }
329     padding_->Reset();
330     if (padding.left) {
331         padding_->left = padding.left;
332     }
333     if (padding.right) {
334         padding_->right = padding.right;
335     }
336     if (padding.top) {
337         padding_->top = padding.top;
338     }
339     if (padding.bottom) {
340         padding_->bottom = padding.bottom;
341     }
342 }
343 
GetIgnoreAdjust() const344 OffsetF GeometryNode::GetIgnoreAdjust() const
345 {
346     return ignoreAdjust_;
347 }
348 
SetIgnoreAdjust(const OffsetF & ignoreAdjust)349 void GeometryNode::SetIgnoreAdjust(const OffsetF& ignoreAdjust)
350 {
351     ignoreAdjust_ = ignoreAdjust;
352 }
353 } // namespace OHOS::Ace::NG
354