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/measure_property.h" 33 34 namespace OHOS::Ace::NG { 35 class InspectorFilter; 36 using ExpandEdges = PaddingPropertyF; 37 // GeometryNode acts as a physical property of the size and position of the component 38 class ACE_FORCE_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 55 SizeF GetMarginFrameSize(bool withSafeArea = false) const; 56 57 SizeF GetMarginPreFrameSize(bool withSafeArea = false) const; 58 59 OffsetF GetMarginFrameOffset(bool withSafeArea = false) const; 60 61 RectF GetMarginFrameRect(bool withSafeArea = false) const; 62 63 void SetMarginFrameOffset(const OffsetF& translate); 64 65 RectF GetFrameRect(bool withSafeArea = false) const; 66 67 SizeF GetFrameSize(bool withSafeArea = false) const; 68 69 OffsetF GetFrameOffset(bool withSafeArea = false) const; 70 SetFrameOffset(const OffsetF & offset)71 void SetFrameOffset(const OffsetF& offset) 72 { 73 frame_.rect_.SetOffset(offset); 74 } 75 SetFrameSize(const SizeF & size)76 void SetFrameSize(const SizeF& size) 77 { 78 frame_.rect_.SetSize(size); 79 } 80 SetFrameWidth(int32_t width)81 void SetFrameWidth(int32_t width) 82 { 83 frame_.rect_.SetWidth(width); 84 } 85 SetFrameHeight(int32_t height)86 void SetFrameHeight(int32_t height) 87 { 88 frame_.rect_.SetHeight(height); 89 } 90 91 void SetMarginFrameOffsetX(int32_t offsetX); 92 93 void SetMarginFrameOffsetY(int32_t offsetY); 94 95 SizeF GetPaddingSize(bool withSafeArea = false) const; 96 97 OffsetF GetPaddingOffset(bool withSafeArea = false) const; 98 99 RectF GetPaddingRect(bool withSafeArea = false) const; 100 101 void SetContentSize(const SizeF& size); 102 103 void SetContentOffset(const OffsetF& translate); 104 GetContentRect()105 RectF GetContentRect() const 106 { 107 return content_ ? content_->rect_ : RectF(); 108 } 109 GetContentSize()110 SizeF GetContentSize() const 111 { 112 return content_ ? content_->rect_.GetSize() : SizeF(); 113 } 114 GetContentOffset()115 OffsetF GetContentOffset() const 116 { 117 return content_ ? content_->rect_.GetOffset() : OffsetF(); 118 } 119 GetPixelRoundContentSize()120 SizeF GetPixelRoundContentSize() const 121 { 122 auto deltaSize = GetPixelGridRoundSize() - GetFrameSize(); 123 return content_ ? (content_->rect_.GetSize() + deltaSize) : SizeF(); 124 } 125 GetPixelRoundContentOffset()126 OffsetF GetPixelRoundContentOffset() const 127 { 128 return content_ ? content_->rect_.GetOffset() : OffsetF(); 129 } 130 GetContent()131 const std::unique_ptr<GeometryProperty>& GetContent() const 132 { 133 return content_; 134 } 135 GetMargin()136 const std::unique_ptr<MarginPropertyF>& GetMargin() const 137 { 138 return margin_; 139 } 140 GetPadding()141 const std::unique_ptr<PaddingPropertyF>& GetPadding() const 142 { 143 return padding_; 144 } 145 146 void UpdateMargin(const MarginPropertyF& margin); 147 148 void UpdatePaddingWithBorder(const PaddingPropertyF& padding); 149 GetParentGlobalOffset()150 const OffsetF& GetParentGlobalOffset() const 151 { 152 return parentGlobalOffset_; 153 } 154 SetParentGlobalOffset(const OffsetF & parentGlobalOffset)155 void SetParentGlobalOffset(const OffsetF& parentGlobalOffset) 156 { 157 parentGlobalOffset_ = parentGlobalOffset; 158 } 159 GetPixelGridRoundOffset()160 const OffsetF& GetPixelGridRoundOffset() const 161 { 162 return pixelGridRoundOffset_; 163 } 164 SetPixelGridRoundOffset(const OffsetF & pixelGridRoundOffset)165 void SetPixelGridRoundOffset(const OffsetF& pixelGridRoundOffset) 166 { 167 pixelGridRoundOffset_ = pixelGridRoundOffset; 168 } 169 GetPixelGridRoundSize()170 const SizeF& GetPixelGridRoundSize() const 171 { 172 return pixelGridRoundSize_; 173 } 174 GetPreFrameSize()175 const SizeF& GetPreFrameSize() const 176 { 177 return preFrameSize_; 178 } 179 GetPixelGridRoundRect()180 RectF GetPixelGridRoundRect() const 181 { 182 return RectF(pixelGridRoundOffset_, pixelGridRoundSize_); 183 } 184 SetPixelGridRoundSize(const SizeF & pixelGridRoundSize)185 void SetPixelGridRoundSize(const SizeF& pixelGridRoundSize) 186 { 187 pixelGridRoundSize_ = pixelGridRoundSize; 188 } 189 SetPreFrameSize(const SizeF & preFrameSize)190 void SetPreFrameSize(const SizeF& preFrameSize) 191 { 192 preFrameSize_ = preFrameSize; 193 } 194 GetParentAbsoluteOffset()195 const OffsetF& GetParentAbsoluteOffset() const 196 { 197 return parentAbsoluteOffset_; 198 } 199 SetParentAbsoluteOffset(const OffsetF & parentAbsoluteOffset)200 void SetParentAbsoluteOffset(const OffsetF& parentAbsoluteOffset) 201 { 202 parentAbsoluteOffset_ = parentAbsoluteOffset; 203 } 204 ResetParentLayoutConstraint()205 void ResetParentLayoutConstraint() 206 { 207 parentLayoutConstraint_ = std::nullopt; 208 } 209 SetParentLayoutConstraint(const LayoutConstraintF & layoutConstraint)210 void SetParentLayoutConstraint(const LayoutConstraintF& layoutConstraint) 211 { 212 parentLayoutConstraint_ = layoutConstraint; 213 } 214 GetParentLayoutConstraint()215 const std::optional<LayoutConstraintF>& GetParentLayoutConstraint() const 216 { 217 return parentLayoutConstraint_; 218 } 219 SetBaselineDistance(float baselineDistance)220 void SetBaselineDistance(float baselineDistance) 221 { 222 baselineDistance_ = baselineDistance; 223 } 224 GetBaselineDistance()225 float GetBaselineDistance() 226 { 227 return baselineDistance_.value_or(frame_.rect_.GetY()); 228 } 229 ResetContent()230 void ResetContent() 231 { 232 content_.reset(); 233 } 234 235 void SetAccumulatedSafeAreaEdges(const ExpandEdges& safeAreaPadding); 236 const std::unique_ptr<ExpandEdges>& GetAccumulatedSafeAreaExpand() const; 237 std::optional<RectF> ConvertExpandCacheToAdjustRect() const; 238 void ResetAccumulatedSafeAreaPadding(); 239 // once get resolved, value shou not be changed before reset 240 void SetResolvedSingleSafeAreaPadding(const PaddingPropertyF& safeAreaPadding); 241 const std::unique_ptr<PaddingPropertyF>& GetResolvedSingleSafeAreaPadding() const; 242 void ResetResolvedSelfSafeAreaPadding(); 243 244 RectF GetParentAdjust() const; 245 void SetParentAdjust(RectF parentAdjust); 246 RectF GetSelfAdjust() const; 247 void SetSelfAdjust(RectF selfAdjust); 248 OffsetF GetIgnoreAdjust() const; 249 void SetIgnoreAdjust(const OffsetF& ignoreAdjust); 250 RectF GetFrameRectWithoutSafeArea() const; 251 RectF GetFrameRectWithSafeArea() const; 252 253 void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const; 254 255 private: 256 // the layoutConstraint of prev measure task. 257 std::optional<LayoutConstraintF> parentLayoutConstraint_; 258 259 std::optional<float> baselineDistance_; 260 261 // the frame size in parent local coordinate. 262 GeometryProperty frame_; 263 // the size of margin property. 264 std::unique_ptr<MarginPropertyF> margin_; 265 // the size of padding property. 266 std::unique_ptr<MarginPropertyF> padding_; 267 // the size of content rect in current node local coordinate. 268 std::unique_ptr<GeometryProperty> content_; 269 // all parent safeArea paddings that can be concatenated to expand 270 std::unique_ptr<ExpandEdges> accumulatedSafeAreaExpand_; 271 // value converted from dimension to float to avoid duplicate calculation 272 std::unique_ptr<PaddingPropertyF> resolvedSingleSafeAreaPadding_; 273 274 RectF parentAdjust_; 275 RectF selfAdjust_; 276 OffsetF ignoreAdjust_; 277 278 OffsetF parentGlobalOffset_; 279 OffsetF parentAbsoluteOffset_; 280 OffsetF pixelGridRoundOffset_; 281 SizeF pixelGridRoundSize_; 282 SizeF preFrameSize_; 283 }; 284 } // namespace OHOS::Ace::NG 285 286 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_GEOMETRY_NODE_H 287