1 /* 2 * Copyright (c) 2021-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_BOX_RENDER_BOX_BASE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_RENDER_BOX_BASE_H 18 19 #include "base/geometry/offset.h" 20 #include "base/geometry/size.h" 21 #include "core/animation/property_animatable.h" 22 #include "core/animation/property_animatable_helper.h" 23 #include "core/components/box/box_base_component.h" 24 #include "core/components/box/box_component.h" 25 #include "core/components/box/mask.h" 26 #include "core/components/common/layout/grid_column_info.h" 27 #include "core/components/common/properties/edge.h" 28 #include "core/pipeline/base/render_node.h" 29 30 namespace OHOS::Ace { 31 32 class RenderBoxBase : public RenderNode { 33 DECLARE_ACE_TYPE(RenderBoxBase, RenderNode); 34 35 public: 36 using LayoutCallback = std::function<void()>; 37 38 void Update(const RefPtr<Component>& component) override; 39 void PerformLayout() override; 40 void Dump() override; 41 42 Offset GetPaintPosition() const; 43 const Size& GetPaintSize() const; 44 void SetPaintSize(const Size& paintSize); 45 virtual Size GetBorderSize() const; 46 double CalculateHeightPercent(double percent) const; // add for text filed DrawOnPixelMap()47 virtual void DrawOnPixelMap() {} 48 49 FloatPropertyAnimatable::SetterMap GetFloatPropertySetterMap() override; 50 FloatPropertyAnimatable::GetterMap GetFloatPropertyGetterMap() override; 51 OnAttachContext()52 void OnAttachContext() override 53 { 54 width_.SetContextAndCallback(context_, [weak = WeakClaim(this)] { 55 auto renderBox = weak.Upgrade(); 56 if (renderBox) { 57 renderBox->OnAnimationCallback(); 58 } 59 }); 60 height_.SetContextAndCallback(context_, [weak = WeakClaim(this)] { 61 auto renderBox = weak.Upgrade(); 62 if (renderBox) { 63 renderBox->OnAnimationCallback(); 64 } 65 }); 66 marginOrigin_.SetContextAndCallback(context_, [weak = WeakClaim(this)] { 67 auto renderBox = weak.Upgrade(); 68 if (renderBox) { 69 renderBox->OnAnimationCallback(); 70 } 71 }); 72 paddingOrigin_.SetContextAndCallback(context_, [weak = WeakClaim(this)] { 73 auto renderBox = weak.Upgrade(); 74 if (renderBox) { 75 renderBox->OnAnimationCallback(); 76 } 77 }); 78 aspectRatio_.SetContextAndCallback(context_, [weak = WeakClaim(this)] { 79 auto renderBox = weak.Upgrade(); 80 if (renderBox) { 81 renderBox->OnAnimationCallback(); 82 } 83 }); 84 } 85 GetColor()86 virtual const Color& GetColor() const 87 { 88 return Color::TRANSPARENT; 89 } 90 UpdateStyleFromRenderNode(PropertyAnimatableType type)91 virtual void UpdateStyleFromRenderNode(PropertyAnimatableType type) {} 92 GetMargin(const DimensionHelper & helper)93 AnimatableDimension GetMargin(const DimensionHelper& helper) const 94 { 95 return helper.Get(marginOrigin_); 96 } 97 SetMargin(const AnimatableDimension & value,const DimensionHelper & helper)98 void SetMargin(const AnimatableDimension& value, const DimensionHelper& helper) // add for animation 99 { 100 if (helper.Set(value, &marginOrigin_)) { 101 MarkNeedLayout(); 102 } 103 } 104 GetPadding(const DimensionHelper & helper)105 AnimatableDimension GetPadding(const DimensionHelper& helper) const 106 { 107 return helper.Get(paddingOrigin_); 108 } 109 SetPadding(const AnimatableDimension & value,const DimensionHelper & helper)110 void SetPadding(const AnimatableDimension& value, const DimensionHelper& helper) // add for animation 111 { 112 if (helper.Set(value, &paddingOrigin_)) { 113 MarkNeedLayout(); 114 } 115 } 116 SetPadding(const Edge & padding)117 void SetPadding(const Edge& padding) 118 { 119 paddingOrigin_ = padding; 120 MarkNeedLayout(); 121 } 122 GetWidth()123 double GetWidth() const // add for animation 124 { 125 return width_.Value(); 126 } 127 GetHeight()128 double GetHeight() const // add for animation 129 { 130 return height_.Value(); 131 } 132 SetWidth(double width)133 virtual void SetWidth(double width) // add for animation 134 { 135 if (GreatOrEqual(width, 0.0) && !NearEqual(width_.Value(), width)) { 136 width_.SetValue(width); 137 MarkNeedLayout(); 138 } 139 } 140 SetHeight(double height)141 virtual void SetHeight(double height) // add for animation 142 { 143 if (GreatOrEqual(height, 0.0) && !NearEqual(height_.Value(), height)) { 144 height_.SetValue(height); 145 MarkNeedLayout(); 146 } 147 } 148 SetWidth(const Dimension & width)149 virtual void SetWidth(const Dimension& width) // add for animation 150 { 151 if (width_ != width) { 152 width_ = width; 153 MarkNeedLayout(); 154 } 155 } 156 GetWidthDimension()157 Dimension GetWidthDimension() const 158 { 159 return width_; 160 } 161 SetHeight(const Dimension & height)162 virtual void SetHeight(const Dimension& height) // add for animation 163 { 164 if (height_ != height) { 165 height_ = height; 166 MarkNeedLayout(); 167 } 168 } 169 GetHeightDimension()170 Dimension GetHeightDimension() const 171 { 172 return height_; 173 } 174 GetMargin()175 EdgePx GetMargin() const 176 { 177 return margin_; 178 } 179 GetPadding()180 EdgePx GetPadding() const 181 { 182 return padding_; 183 } 184 GetMarginSize()185 Size GetMarginSize() const 186 { 187 return margin_.GetLayoutSize(); 188 } 189 GetPaddingSize()190 Size GetPaddingSize() const 191 { 192 return padding_.GetLayoutSize(); 193 } 194 SetLayoutCallback(LayoutCallback && layoutCallback)195 void SetLayoutCallback(LayoutCallback&& layoutCallback) 196 { 197 layoutCallback_ = layoutCallback; 198 } 199 GetTouchArea()200 const Rect& GetTouchArea() const 201 { 202 return touchArea_; 203 } 204 SetConstraints(const LayoutParam & constraints)205 void SetConstraints(const LayoutParam& constraints) 206 { 207 constraints_ = constraints; 208 MarkNeedLayout(); 209 } GetConstraints()210 const LayoutParam& GetConstraints() 211 { 212 return constraints_; 213 } 214 SetBoxFlex(BoxFlex flex)215 void SetBoxFlex(BoxFlex flex) 216 { 217 flex_ = flex; 218 } 219 GetAlignDeclarationPtr()220 AlignDeclarationPtr GetAlignDeclarationPtr() const 221 { 222 return alignPtr_; 223 } 224 GetUseAlignSide()225 AlignDeclaration::Edge GetUseAlignSide() const 226 { 227 return alignSide_; 228 } 229 GetUseAlignOffset()230 const Dimension& GetUseAlignOffset() const 231 { 232 return alignItemOffset_; 233 } 234 235 virtual void CalculateAlignDeclaration(); 236 GetAlign()237 const Alignment& GetAlign() const 238 { 239 return align_; 240 } 241 GetAspectRatio()242 double GetAspectRatio() const 243 { 244 return aspectRatio_.Value(); 245 } 246 SetAspectRatio(const Dimension & aspectRatio)247 void SetAspectRatio(const Dimension& aspectRatio) 248 { 249 aspectRatio_ = aspectRatio; 250 } 251 GetClipPath()252 const RefPtr<ClipPath>& GetClipPath() const 253 { 254 return clipPath_; 255 } 256 GetBoxClipFlag()257 bool GetBoxClipFlag() const 258 { 259 return boxClipFlag_; 260 } 261 GetMask()262 const RefPtr<Mask>& GetMask() const 263 { 264 return mask_; 265 } 266 GetGridColumnInfo()267 const RefPtr<GridColumnInfo>& GetGridColumnInfo() const 268 { 269 return gridColumnInfo_; 270 } 271 GetGridContainerInfo()272 const RefPtr<GridContainerInfo>& GetGridContainerInfo() const 273 { 274 return gridContainerInfo_; 275 } 276 GetPixelMap()277 const RefPtr<PixelMap>& GetPixelMap() const 278 { 279 return pixelMap_; 280 } 281 GetPaintRectExcludeMargin()282 Rect GetPaintRectExcludeMargin() const 283 { 284 Rect rect; 285 rect.SetSize(paintSize_); 286 rect.SetOffset(GetPaintRect().GetOffset() + Offset(margin_.LeftPx(), margin_.TopPx())); 287 return rect; 288 } 289 290 protected: 291 virtual void ClearRenderObject() override; 292 virtual Offset GetBorderOffset() const; 293 virtual Radius GetBorderRadius() const; 294 295 EdgePx ConvertEdgeToPx(const Edge& edge, bool additional); 296 double ConvertMarginToPx(CalcDimension dimension, bool vertical, bool additional) const; 297 double ConvertDimensionToPx(CalcDimension dimension, bool vertical, bool defaultZero = false) const; 298 double ConvertHorizontalDimensionToPx(CalcDimension dimension, bool defaultZero = false) const; 299 double ConvertVerticalDimensionToPx(CalcDimension dimension, bool defaultZero = false) const; 300 void CalculateWidth(); 301 void CalculateHeight(); 302 void CalculateAutoMargin(); 303 void ConvertMarginPaddingToPx(); 304 void ConvertConstraintsToPx(); 305 void CalculateGridLayoutSize(); 306 void CalculateSelfLayoutParam(); 307 void SetChildLayoutParam(); 308 void ConvertPaddingForLayoutInBox(); 309 void CalculateSelfLayoutSize(); 310 void CalculateChildPosition(); 311 void AdjustSizeByAspectRatio(); 312 void PerformLayoutInLiteMode(); 313 virtual void OnAnimationCallback(); 314 315 AnimatableDimension width_ { AnimatableDimension(-1.0, DimensionUnit::PX) }; // exclude margin 316 AnimatableDimension height_ { AnimatableDimension(-1.0, DimensionUnit::PX) }; // exclude margin 317 AnimatableDimension aspectRatio_ = AnimatableDimension(); 318 319 BoxFlex flex_ = BoxFlex::FLEX_NO; 320 LayoutParam constraints_ = LayoutParam(Size(), Size()); // exclude margin 321 EdgePx padding_; 322 EdgePx margin_; 323 Alignment align_; 324 Size paintSize_; // exclude margin 325 Rect touchArea_; // exclude margin 326 bool deliverMinToChild_ = true; 327 bool scrollPage_ = false; 328 uint32_t percentFlag_ = 0; 329 bool layoutInBox_ = false; 330 Edge paddingOrigin_; 331 Edge marginOrigin_; 332 LayoutCallback layoutCallback_; 333 bool useLiteStyle_ = false; 334 Overflow overflow_ = Overflow::OBSERVABLE; 335 bool isChildOverflow_ = false; 336 RefPtr<ClipPath> clipPath_; 337 RefPtr<Mask> mask_; 338 BoxSizing boxSizing_ = BoxSizing::BORDER_BOX; 339 WeakPtr<BoxComponent> boxComponent_; 340 341 bool isUseAlign_ = false; 342 AlignDeclarationPtr alignPtr_ = nullptr; 343 AlignDeclaration::Edge alignSide_ { AlignDeclaration::Edge::AUTO }; 344 Dimension alignItemOffset_; 345 Offset alignOffset_; 346 bool boxClipFlag_ = false; 347 RefPtr<PixelMap> pixelMap_ = nullptr; 348 349 private: 350 bool IsSizeValid(const Dimension& value, double maxLimit); 351 352 Edge additionalPadding_; 353 bool useFlexWidth_ = false; 354 bool useFlexHeight_ = false; 355 bool selfDefineWidth_ = false; 356 bool selfDefineHeight_ = false; 357 double selfMaxWidth_ = Size::INFINITE_SIZE; // exclude margin 358 double selfMinWidth_ = 0.0; // exclude margin 359 double selfMaxHeight_ = Size::INFINITE_SIZE; // exclude margin 360 double selfMinHeight_ = 0.0; // exclude margin 361 CalcDimension minWidth_ = Dimension(); 362 CalcDimension minHeight_ = Dimension(); 363 CalcDimension maxWidth_ = Dimension(); 364 CalcDimension maxHeight_ = Dimension(); 365 366 // result for layout 367 LayoutParam selfLayoutParam_; // include margin 368 Size selfLayoutSize_; // include margin 369 LayoutParam childLayoutParam_; 370 Size childSize_; 371 Offset childPosition_; 372 373 // grid layout 374 RefPtr<GridColumnInfo> gridColumnInfo_; 375 RefPtr<GridContainerInfo> gridContainerInfo_; 376 }; 377 378 } // namespace OHOS::Ace 379 380 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BOX_RENDER_BOX_BASE_H 381