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 <string>
19 #include <utility>
20
21 #include "core/components_ng/base/inspector_filter.h"
22 #include "core/components_ng/layout/layout_wrapper.h"
23
24 namespace OHOS::Ace::NG {
25
Reset()26 void GeometryNode::Reset()
27 {
28 frame_.Reset();
29 margin_.reset();
30 padding_.reset();
31 content_.reset();
32 parentGlobalOffset_.Reset();
33 parentAbsoluteOffset_.Reset();
34 parentLayoutConstraint_.reset();
35 resolvedSingleSafeAreaPadding_.reset();
36 accumulatedSafeAreaExpand_.reset();
37 }
38
Clone() const39 RefPtr<GeometryNode> GeometryNode::Clone() const
40 {
41 auto node = MakeRefPtr<GeometryNode>();
42 node->frame_ = frame_;
43 if (margin_) {
44 node->margin_ = std::make_unique<MarginPropertyF>(*margin_);
45 }
46 if (padding_) {
47 node->padding_ = std::make_unique<MarginPropertyF>(*padding_);
48 }
49 if (content_) {
50 node->content_ = std::make_unique<GeometryProperty>(*content_);
51 }
52 if (accumulatedSafeAreaExpand_) {
53 node->accumulatedSafeAreaExpand_ = std::make_unique<PaddingPropertyF>(*accumulatedSafeAreaExpand_);
54 }
55 if (resolvedSingleSafeAreaPadding_) {
56 node->resolvedSingleSafeAreaPadding_ = std::make_unique<PaddingPropertyF>(*resolvedSingleSafeAreaPadding_);
57 }
58 node->parentGlobalOffset_ = parentGlobalOffset_;
59 node->parentLayoutConstraint_ = parentLayoutConstraint_;
60 node->parentAbsoluteOffset_ = parentAbsoluteOffset_;
61 return node;
62 }
63
ToJsonValue(std::unique_ptr<JsonValue> & json,const InspectorFilter & filter) const64 void GeometryNode::ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const
65 {
66 #if defined(PREVIEW)
67 auto frameSize = frame_.rect_.GetSize();
68 json->Put("width", std::to_string(frameSize.Width()).c_str());
69 json->Put("height", std::to_string(frameSize.Height()).c_str());
70
71 auto jsonSize = JsonUtil::Create(true);
72 jsonSize->Put("width", std::to_string(frameSize.Width()).c_str());
73 jsonSize->Put("height", std::to_string(frameSize.Height()).c_str());
74 json->Put("size", jsonSize);
75 #endif
76 }
77
SetAccumulatedSafeAreaEdges(const ExpandEdges & safeAreaPadding)78 void GeometryNode::SetAccumulatedSafeAreaEdges(const ExpandEdges& safeAreaPadding)
79 {
80 if (!accumulatedSafeAreaExpand_) {
81 accumulatedSafeAreaExpand_ = std::make_unique<ExpandEdges>(safeAreaPadding);
82 return;
83 }
84 if (safeAreaPadding.left) {
85 accumulatedSafeAreaExpand_->left = safeAreaPadding.left;
86 }
87 if (safeAreaPadding.right) {
88 accumulatedSafeAreaExpand_->right = safeAreaPadding.right;
89 }
90 if (safeAreaPadding.top) {
91 accumulatedSafeAreaExpand_->top = safeAreaPadding.top;
92 }
93 if (safeAreaPadding.bottom) {
94 accumulatedSafeAreaExpand_->bottom = safeAreaPadding.bottom;
95 }
96 }
97
GetAccumulatedSafeAreaExpand() const98 const std::unique_ptr<ExpandEdges>& GeometryNode::GetAccumulatedSafeAreaExpand() const
99 {
100 return accumulatedSafeAreaExpand_;
101 }
102
ResetAccumulatedSafeAreaPadding()103 void GeometryNode::ResetAccumulatedSafeAreaPadding()
104 {
105 CHECK_NULL_VOID(accumulatedSafeAreaExpand_);
106 accumulatedSafeAreaExpand_.reset();
107 }
108
SetResolvedSingleSafeAreaPadding(const PaddingPropertyF & safeAreaPadding)109 void GeometryNode::SetResolvedSingleSafeAreaPadding(const PaddingPropertyF& safeAreaPadding)
110 {
111 resolvedSingleSafeAreaPadding_ = std::make_unique<PaddingPropertyF>(safeAreaPadding);
112 }
113
GetResolvedSingleSafeAreaPadding() const114 const std::unique_ptr<PaddingPropertyF>& GeometryNode::GetResolvedSingleSafeAreaPadding() const
115 {
116 return resolvedSingleSafeAreaPadding_;
117 }
118
ResetResolvedSelfSafeAreaPadding()119 void GeometryNode::ResetResolvedSelfSafeAreaPadding()
120 {
121 resolvedSingleSafeAreaPadding_.reset();
122 }
123
GetParentAdjust() const124 RectF GeometryNode::GetParentAdjust() const
125 {
126 return parentAdjust_;
127 }
128
SetParentAdjust(RectF parentAdjust)129 void GeometryNode::SetParentAdjust(RectF parentAdjust)
130 {
131 parentAdjust_ = parentAdjust;
132 }
133
GetSelfAdjust() const134 RectF GeometryNode::GetSelfAdjust() const
135 {
136 return selfAdjust_;
137 }
138
SetSelfAdjust(RectF selfAdjust)139 void GeometryNode::SetSelfAdjust(RectF selfAdjust)
140 {
141 selfAdjust_ = selfAdjust;
142 }
143 } // namespace OHOS::Ace::NG
144