• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     virtual void SetActiveChildRange(int32_t start, int32_t end) = 0;
67 
68     RefPtr<FrameNode> GetHostNode() const;
69     virtual const std::string& GetHostTag() const = 0;
70     virtual bool IsActive() const = 0;
71     virtual void SetActive(bool active = true) = 0;
72 
IsRootMeasureNode()73     bool IsRootMeasureNode() const
74     {
75         return isRootNode_;
76     }
77 
78     void SetRootMeasureNode(bool isRoot = true)
79     {
80         isRootNode_ = isRoot;
81     }
82 
IsOutOfLayout()83     virtual bool IsOutOfLayout() const
84     {
85         return false;
86     }
87 
88     virtual bool SkipMeasureContent() const;
89 
90     virtual void SetCacheCount(
91         int32_t cacheCount = 0, const std::optional<LayoutConstraintF>& itemConstraint = std::nullopt) = 0;
92     virtual float GetBaselineDistance() const = 0;
CheckShouldRunOnMain()93     virtual bool CheckShouldRunOnMain()
94     {
95         return true;
96     }
97 
98     virtual bool CheckNeedForceMeasureAndLayout() = 0;
99 
SetIsOverlayNode(bool isOverlayNode)100     void SetIsOverlayNode(bool isOverlayNode)
101     {
102         isOverlayNode_ = isOverlayNode;
103     }
104 
105     // ------------------------------------------------------------------------
106     // performance check
107     void AddNodeFlexLayouts();
108     void AddNodeLayoutTime(int64_t time);
109     // ------------------------------------------------------------------------
110 
BuildLazyItem()111     virtual void BuildLazyItem() {}
112 
IsContraintNoChanged()113     bool IsContraintNoChanged() const
114     {
115         return isConstraintNotChanged_;
116     }
SetLongPredictTask()117     virtual void SetLongPredictTask() {}
118 
119     static void ApplySafeArea(const SafeAreaInsets& insets, LayoutConstraintF& constraint);
120 
121     // apply keyboard avoidance on content rootNodes
122     void AvoidKeyboard(bool isFocusOnPage = true);
123     // expand the SafeArea of expansive nodes, which are previously recorded during Layout traversal
124     void ExpandSafeArea(bool isFocusOnPage = true);
125 
126     // save geometry states before SafeArea expansion / keyboard avoidance
127     void SaveGeoState();
128     // restore to the geometry state after last Layout and before SafeArea expansion and keyboard avoidance
129     void RestoreGeoState();
130 
SkipSyncGeometryNode()131     bool SkipSyncGeometryNode() const
132     {
133         return needSkipSyncGeometryNode_;
134     }
135 
136     void SetSkipSyncGeometryNode(bool needSkip = true)
137     {
138         needSkipSyncGeometryNode_ = needSkip;
139     }
140 
141 protected:
142     void CreateRootConstraint();
143     void ApplyConstraint(LayoutConstraintF constraint);
144 
145     void OffsetNodeToSafeArea();
146     // keyboard avoidance is done by offsetting, to expand into keyboard area, reverse the offset.
147     void ExpandIntoKeyboard();
148     void RestoreExpansiveChildren();
149     void RestoreExpansiveChild(const RefPtr<UINode>& node);
150     bool CheckValidSafeArea();
151 
152     WeakPtr<FrameNode> hostNode_;
153 
154     bool isConstraintNotChanged_ = false;
155     bool isRootNode_ = false;
156     bool isOverlayNode_ = false;
157     bool needSkipSyncGeometryNode_ = false;
158     std::optional<bool> skipMeasureContent_;
159     std::optional<bool> needForceMeasureAndLayout_;
160 
161 private:
162     void AdjustChildren(const OffsetF& offset);
163     void AdjustChild(RefPtr<UINode> node, const OffsetF& offset);
164 
165     ACE_DISALLOW_COPY_AND_MOVE(LayoutWrapper);
166 };
167 } // namespace OHOS::Ace::NG
168 
169 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_LAYOUTS_LAYOUT_WRAPPER_H
170