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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_GEOMETRY_NODE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_GEOMETRY_NODE_H 18 19 #include <list> 20 #include <memory> 21 22 #include "base/geometry/ng/offset_t.h" 23 #include "base/geometry/ng/rect_t.h" 24 #include "base/geometry/ng/size_t.h" 25 #include "base/memory/ace_type.h" 26 #include "base/memory/referenced.h" 27 #include "base/utils/macros.h" 28 #include "core/components_ng/layout/box_layout_algorithm.h" 29 #include "core/components_ng/property/geometry_property.h" 30 #include "core/components_ng/property/layout_constraint.h" 31 #include "core/components_ng/property/magic_layout_property.h" 32 #include "core/components_ng/property/measure_property.h" 33 #include "core/components_ng/property/measure_utils.h" 34 #include "core/components_ng/property/position_property.h" 35 36 namespace OHOS::Ace::NG { 37 // GeometryNode acts as a physical property of the size and position of the component 38 class ACE_EXPORT GeometryNode : public AceType { 39 DECLARE_ACE_TYPE(GeometryNode, AceType) 40 public: 41 GeometryNode() = default; 42 ~GeometryNode() override = default; 43 44 void Reset(); 45 CheckUnchanged(const GeometryNode & geometryNode)46 bool CheckUnchanged(const GeometryNode& geometryNode) 47 { 48 return (frame_ == geometryNode.frame_) && (margin_ == geometryNode.margin_) && 49 (content_ == geometryNode.content_) && (parentGlobalOffset_ == geometryNode.parentGlobalOffset_) && 50 (parentLayoutConstraint_ == geometryNode.parentLayoutConstraint_); 51 } 52 53 RefPtr<GeometryNode> Clone() const; 54 GetMarginFrameSize()55 SizeF GetMarginFrameSize() const 56 { 57 // TODO: add margin in negative. 58 auto size = frame_.rect_.GetSize(); 59 if (margin_) { 60 AddPaddingToSize(*margin_, size); 61 } 62 return size; 63 } 64 GetMarginFrameOffset()65 OffsetF GetMarginFrameOffset() const 66 { 67 // TODO: add margin in negative. 68 auto offset = frame_.rect_.GetOffset(); 69 if (margin_) { 70 offset -= OffsetF(margin_->left.value_or(0), margin_->top.value_or(0)); 71 } 72 return offset; 73 } 74 SetMarginFrameOffset(const OffsetF & translate)75 void SetMarginFrameOffset(const OffsetF& translate) 76 { 77 OffsetF offset; 78 if (margin_) { 79 offset += OffsetF(margin_->left.value_or(0), margin_->top.value_or(0)); 80 } 81 frame_.rect_.SetOffset(translate + offset); 82 } 83 GetFrameRect()84 const RectF& GetFrameRect() const 85 { 86 return frame_.rect_; 87 } 88 GetFrameSize()89 SizeF GetFrameSize() const 90 { 91 return frame_.rect_.GetSize(); 92 } 93 GetFrameOffset()94 OffsetF GetFrameOffset() const 95 { 96 return frame_.rect_.GetOffset(); 97 } 98 SetFrameOffset(const OffsetF & offset)99 void SetFrameOffset(const OffsetF& offset) 100 { 101 frame_.rect_.SetOffset(offset); 102 } 103 SetFrameSize(const SizeF & size)104 void SetFrameSize(const SizeF& size) 105 { 106 frame_.rect_.SetSize(size); 107 } 108 GetPaddingSize()109 SizeF GetPaddingSize() const 110 { 111 auto size = frame_.rect_.GetSize(); 112 if (padding_) { 113 MinusPaddingToSize(*padding_, size); 114 } 115 return size; 116 } 117 GetPaddingOffset()118 OffsetF GetPaddingOffset() const 119 { 120 auto offset = frame_.rect_.GetOffset(); 121 if (padding_) { 122 offset += OffsetF(padding_->left.value_or(0), padding_->top.value_or(0)); 123 } 124 return offset; 125 } 126 GetPaddingRect()127 RectF GetPaddingRect() const 128 { 129 auto rect = frame_.rect_; 130 if (padding_) { 131 auto size = rect.GetSize(); 132 MinusPaddingToSize(*padding_, size); 133 rect.SetSize(size); 134 auto offset = rect.GetOffset(); 135 offset += OffsetF(padding_->left.value_or(0), padding_->top.value_or(0)); 136 rect.SetOffset(offset); 137 } 138 return rect; 139 } 140 SetContentSize(const SizeF & size)141 void SetContentSize(const SizeF& size) 142 { 143 if (!content_) { 144 content_ = std::make_unique<GeometryProperty>(); 145 } 146 content_->rect_.SetSize(size); 147 } 148 SetContentOffset(const OffsetF & translate)149 void SetContentOffset(const OffsetF& translate) 150 { 151 if (!content_) { 152 content_ = std::make_unique<GeometryProperty>(); 153 } 154 content_->rect_.SetOffset(translate); 155 } 156 GetContentRect()157 RectF GetContentRect() const 158 { 159 return content_ ? content_->rect_ : RectF(); 160 } 161 GetContentSize()162 SizeF GetContentSize() const 163 { 164 return content_ ? content_->rect_.GetSize() : SizeF(); 165 } 166 GetContentOffset()167 OffsetF GetContentOffset() const 168 { 169 return content_ ? content_->rect_.GetOffset() : OffsetF(); 170 } 171 GetContent()172 const std::unique_ptr<GeometryProperty>& GetContent() const 173 { 174 return content_; 175 } 176 GetMargin()177 const std::unique_ptr<MarginPropertyF>& GetMargin() const 178 { 179 return margin_; 180 } 181 UpdateMargin(const MarginPropertyF & margin)182 void UpdateMargin(const MarginPropertyF& margin) 183 { 184 if (!margin_) { 185 margin_ = std::make_unique<MarginPropertyF>(margin); 186 return; 187 } 188 if (margin.left) { 189 margin_->left = margin.left; 190 } 191 if (margin.right) { 192 margin_->right = margin.right; 193 } 194 if (margin.top) { 195 margin_->top = margin.top; 196 } 197 if (margin.bottom) { 198 margin_->bottom = margin.bottom; 199 } 200 } 201 UpdatePaddingWithBorder(const PaddingPropertyF & padding)202 void UpdatePaddingWithBorder(const PaddingPropertyF& padding) 203 { 204 if (!padding_) { 205 padding_ = std::make_unique<PaddingPropertyF>(padding); 206 return; 207 } 208 if (padding.left) { 209 padding_->left = padding.left; 210 } 211 if (padding.right) { 212 padding_->right = padding.right; 213 } 214 if (padding.top) { 215 padding_->top = padding.top; 216 } 217 if (padding.bottom) { 218 padding_->bottom = padding.bottom; 219 } 220 } 221 GetParentGlobalOffset()222 const OffsetF& GetParentGlobalOffset() const 223 { 224 return parentGlobalOffset_; 225 } 226 SetParentGlobalOffset(const OffsetF & parentGlobalOffset)227 void SetParentGlobalOffset(const OffsetF& parentGlobalOffset) 228 { 229 parentGlobalOffset_ = parentGlobalOffset; 230 } 231 ResetParentLayoutConstraint()232 void ResetParentLayoutConstraint() 233 { 234 parentLayoutConstraint_ = std::nullopt; 235 } 236 SetParentLayoutConstraint(const LayoutConstraintF & layoutConstraint)237 void SetParentLayoutConstraint(const LayoutConstraintF& layoutConstraint) 238 { 239 parentLayoutConstraint_ = layoutConstraint; 240 } 241 GetParentLayoutConstraint()242 const std::optional<LayoutConstraintF>& GetParentLayoutConstraint() const 243 { 244 return parentLayoutConstraint_; 245 } 246 SetBaselineDistance(float baselineDistance)247 void SetBaselineDistance(float baselineDistance) 248 { 249 baselineDistance_ = baselineDistance; 250 } 251 GetBaselineDistance()252 float GetBaselineDistance() 253 { 254 return baselineDistance_.value_or(frame_.rect_.GetY()); 255 } 256 257 private: 258 // the layoutConstraint of prev measure task. 259 std::optional<LayoutConstraintF> parentLayoutConstraint_; 260 261 std::optional<float> baselineDistance_; 262 263 // the frame size in parent local coordinate. 264 GeometryProperty frame_; 265 // the size of margin property. 266 std::unique_ptr<MarginPropertyF> margin_; 267 // the size of padding property. 268 std::unique_ptr<MarginPropertyF> padding_; 269 // the size of content rect in current node local coordinate. 270 std::unique_ptr<GeometryProperty> content_; 271 272 OffsetF parentGlobalOffset_; 273 }; 274 } // namespace OHOS::Ace::NG 275 276 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_GEOMETRY_NODE_H 277