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_LAYOUTS_LAYOUT_WRAPPER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_WRAPPER_H 18 19 #include <map> 20 #include <optional> 21 #include <string> 22 #include <unordered_map> 23 24 #include "base/memory/referenced.h" 25 #include "base/thread/cancelable_callback.h" 26 #include "base/utils/macros.h" 27 #include "base/utils/noncopyable.h" 28 #include "core/components_ng/base/geometry_node.h" 29 #include "core/components_ng/layout/box_layout_algorithm.h" 30 #include "core/components_ng/layout/layout_algorithm.h" 31 #include "core/components_ng/layout/layout_property.h" 32 #include "core/components_ng/layout/layout_wrapper_builder.h" 33 #include "core/components_ng/property/geometry_property.h" 34 #include "core/components_ng/property/layout_constraint.h" 35 #include "core/components_ng/property/magic_layout_property.h" 36 #include "core/components_ng/property/measure_property.h" 37 #include "core/components_ng/property/position_property.h" 38 #include "core/components_ng/property/property.h" 39 #include "core/components_v2/inspector/inspector_constants.h" 40 41 namespace OHOS::Ace::NG { 42 class FrameNode; 43 44 class ACE_FORCE_EXPORT LayoutWrapper : public virtual AceType { DECLARE_ACE_TYPE(LayoutWrapper,AceType)45 DECLARE_ACE_TYPE(LayoutWrapper, AceType) 46 public: 47 LayoutWrapper(WeakPtr<FrameNode> hostNode) : hostNode_(std::move(hostNode)) {} 48 ~LayoutWrapper() override = default; 49 50 virtual const RefPtr<LayoutAlgorithmWrapper>& GetLayoutAlgorithm(bool needReset = false) = 0; 51 // This will call child and self measure process. 52 virtual void Measure(const std::optional<LayoutConstraintF>& parentConstraint) = 0; 53 54 // Called to perform layout children. 55 virtual void Layout() = 0; 56 57 virtual int32_t GetTotalChildCount() const = 0; 58 virtual const RefPtr<GeometryNode>& GetGeometryNode() const = 0; 59 virtual const RefPtr<LayoutProperty>& GetLayoutProperty() const = 0; 60 61 virtual RefPtr<LayoutWrapper> GetOrCreateChildByIndex(uint32_t index, bool addToRenderTree = true) = 0; 62 virtual RefPtr<LayoutWrapper> GetChildByIndex(uint32_t index) = 0; 63 virtual const std::list<RefPtr<LayoutWrapper>>& GetAllChildrenWithBuild(bool addToRenderTree = true) = 0; 64 virtual void RemoveChildInRenderTree(uint32_t index) = 0; 65 virtual void RemoveAllChildInRenderTree() = 0; 66 67 RefPtr<FrameNode> GetHostNode() const; 68 virtual const std::string& GetHostTag() const = 0; 69 virtual bool IsActive() const = 0; 70 virtual void SetActive(bool active = true) = 0; 71 IsRootMeasureNode()72 bool IsRootMeasureNode() const 73 { 74 return isRootNode_; 75 } 76 77 void SetRootMeasureNode(bool isRoot = true) 78 { 79 isRootNode_ = isRoot; 80 } 81 IsOutOfLayout()82 virtual bool IsOutOfLayout() const 83 { 84 return false; 85 } 86 87 virtual bool SkipMeasureContent() const; 88 89 virtual void SetCacheCount( 90 int32_t cacheCount = 0, const std::optional<LayoutConstraintF>& itemConstraint = std::nullopt) = 0; 91 virtual float GetBaselineDistance() const = 0; CheckShouldRunOnMain()92 virtual bool CheckShouldRunOnMain() 93 { 94 return true; 95 } 96 97 virtual bool CheckNeedForceMeasureAndLayout() = 0; 98 SetIsOverlayNode(bool isOverlayNode)99 void SetIsOverlayNode(bool isOverlayNode) 100 { 101 isOverlayNode_ = isOverlayNode; 102 } 103 104 // ------------------------------------------------------------------------ 105 // performance check 106 void AddNodeFlexLayouts(); 107 void AddNodeLayoutTime(int64_t time); 108 // ------------------------------------------------------------------------ 109 BuildLazyItem()110 virtual void BuildLazyItem() {} 111 IsContraintNoChanged()112 bool IsContraintNoChanged() const 113 { 114 return isConstraintNotChanged_; 115 } SetLongPredictTask()116 virtual void SetLongPredictTask() {} 117 118 static void ApplySafeArea(const SafeAreaInsets& insets, LayoutConstraintF& constraint); 119 120 // apply keyboard avoidance on content rootNodes 121 void AvoidKeyboard(); 122 // expand the SafeArea of expansive nodes, which are previously recorded during Layout traversal 123 void ExpandSafeArea(); 124 125 // save geometry states before SafeArea expansion / keyboard avoidance 126 void SaveGeoState(); 127 // restore to the geometry state after last Layout and before SafeArea expansion and keyboard avoidance 128 void RestoreGeoState(); 129 130 protected: 131 void CreateRootConstraint(); 132 void ApplyConstraint(LayoutConstraintF constraint); 133 134 void OffsetNodeToSafeArea(); 135 // keyboard avoidance is done by offsetting, to expand into keyboard area, reverse the offset. 136 void ExpandIntoKeyboard(); 137 138 WeakPtr<FrameNode> hostNode_; 139 140 bool isConstraintNotChanged_ = false; 141 bool isRootNode_ = false; 142 bool isOverlayNode_ = false; 143 std::optional<bool> skipMeasureContent_; 144 std::optional<bool> needForceMeasureAndLayout_; 145 146 ACE_DISALLOW_COPY_AND_MOVE(LayoutWrapper); 147 }; 148 } // namespace OHOS::Ace::NG 149 150 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_WRAPPER_H 151