1 /* 2 * Copyright (c) 2021 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_FLEX_RENDER_FLEX_ITEM_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_FLEX_RENDER_FLEX_ITEM_H 18 19 #include "core/components/common/layout/constants.h" 20 #include "core/components/common/layout/grid_column_info.h" 21 #include "core/components/proxy/render_proxy.h" 22 23 namespace OHOS::Ace { 24 25 class ACE_EXPORT RenderFlexItem final : public RenderProxy { 26 DECLARE_ACE_TYPE(RenderFlexItem, RenderProxy); 27 28 public: 29 static RefPtr<RenderNode> Create(); 30 OnAttachContext()31 void OnAttachContext() override 32 { 33 positionParam_.left.first.SetContextAndCallback(context_, [weak = WeakClaim(this)] { 34 auto renderFlexItem = weak.Upgrade(); 35 if (renderFlexItem) { 36 renderFlexItem->MarkNeedLayout(); 37 } 38 }); 39 positionParam_.right.first.SetContextAndCallback(context_, [weak = WeakClaim(this)] { 40 auto renderFlexItem = weak.Upgrade(); 41 if (renderFlexItem) { 42 renderFlexItem->MarkNeedLayout(); 43 } 44 }); 45 positionParam_.top.first.SetContextAndCallback(context_, [weak = WeakClaim(this)] { 46 auto renderFlexItem = weak.Upgrade(); 47 if (renderFlexItem) { 48 renderFlexItem->MarkNeedLayout(); 49 } 50 }); 51 positionParam_.bottom.first.SetContextAndCallback(context_, [weak = WeakClaim(this)] { 52 auto renderFlexItem = weak.Upgrade(); 53 if (renderFlexItem) { 54 renderFlexItem->MarkNeedLayout(); 55 } 56 }); 57 } 58 59 void Update(const RefPtr<Component>& component) override; 60 61 void PerformLayout() override; 62 GetFlexGrow()63 double GetFlexGrow() const 64 { 65 return flexGrow_; 66 } 67 SetFlexGrow(double flexGrow)68 void SetFlexGrow(double flexGrow) 69 { 70 flexGrow_ = flexGrow; 71 } 72 GetFlexShrink()73 double GetFlexShrink() const 74 { 75 return flexShrink_; 76 } 77 SetFlexShrink(double flexShrink)78 void SetFlexShrink(double flexShrink) 79 { 80 flexShrink_ = flexShrink; 81 } 82 83 // should not used in Update. GetFlexBasisToPx()84 double GetFlexBasisToPx() const 85 { 86 auto basis = NormalizeToPx(flexBasis_); 87 return basis; 88 } 89 90 // should not used in Update. GetFlexBasis()91 const Dimension& GetFlexBasis() const 92 { 93 return flexBasis_; 94 } 95 SetFlexBasis(const Dimension & flexBasis)96 void SetFlexBasis(const Dimension& flexBasis) 97 { 98 flexBasis_ = flexBasis; 99 } 100 SetFlexBasis(double flexBasis)101 void SetFlexBasis(double flexBasis) 102 { 103 flexBasis_ = Dimension(flexBasis); 104 } 105 GetStretchFlag()106 bool GetStretchFlag() const 107 { 108 return canStretch_; 109 } 110 GetAlignSelf()111 FlexAlign GetAlignSelf() const 112 { 113 return alignSelf_; 114 } 115 SetAlignSelf(FlexAlign alignSelf)116 void SetAlignSelf(FlexAlign alignSelf) 117 { 118 alignSelf_ = alignSelf; 119 } 120 GetNormalizedConstraints()121 LayoutParam GetNormalizedConstraints() const 122 { 123 Size min = Size(NormalizePercentToPx(minWidth_, false), NormalizePercentToPx(minHeight_, true)); 124 Size max = Size(NormalizePercentToPx(maxWidth_, false), NormalizePercentToPx(maxHeight_, true)); 125 if (LessNotEqual(max.Width(), min.Width())) { 126 max.SetWidth(min.Width()); 127 } 128 if (LessNotEqual(max.Height(), min.Height())) { 129 max.SetHeight(min.Height()); 130 } 131 LayoutParam constraint = LayoutParam(max, min); 132 if (!constraint.IsValid()) { 133 return LayoutParam(Size(Size::INFINITE_SIZE, Size::INFINITE_SIZE), Size()); 134 } 135 return constraint; 136 } 137 IsHidden()138 bool IsHidden() const 139 { 140 return isHidden_; 141 } 142 MustStretch()143 bool MustStretch() const 144 { 145 return mustStretch_; 146 } 147 148 protected: 149 virtual void ClearRenderObject() override; 150 virtual bool MaybeRelease() override; 151 152 private: 153 double flexGrow_ = 0.0; 154 double flexShrink_ = 0.0; 155 Dimension flexBasis_ = 0.0_px; 156 bool canStretch_ = true; 157 bool mustStretch_ = false; 158 bool isHidden_ = false; 159 RefPtr<GridColumnInfo> gridColumnInfo_; 160 161 Dimension minWidth_ = Dimension(); 162 Dimension minHeight_ = Dimension(); 163 Dimension maxWidth_ = Dimension(Size::INFINITE_SIZE); 164 Dimension maxHeight_ = Dimension(Size::INFINITE_SIZE); 165 FlexAlign alignSelf_ = FlexAlign::AUTO; 166 }; 167 168 } // namespace OHOS::Ace 169 170 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_FLEX_RENDER_FLEX_ITEM_H 171