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