1 /*
2 * Copyright (c) 2023 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 #include "core/components_ng/pattern/scrollable/scrollable_utils.h"
16
17 #include "core/components_ng/syntax/if_else_node.h"
18 #include "core/components_ng/syntax/lazy_for_each_node.h"
19 namespace OHOS::Ace::NG {
20 namespace {
21 Dimension FOCUS_SCROLL_MARGIN = 5.0_vp;
GetForEachNodes(RefPtr<FrameNode> & host)22 std::vector<RefPtr<ForEachBaseNode>> GetForEachNodes(RefPtr<FrameNode>& host)
23 {
24 std::vector<RefPtr<ForEachBaseNode>> foreachNodes;
25 CHECK_NULL_RETURN(host, foreachNodes);
26 for (const auto& child : host->GetChildren()) {
27 if (!AceType::InstanceOf<ForEachBaseNode>(child)) {
28 continue;
29 }
30 auto node = AceType::DynamicCast<ForEachBaseNode>(child);
31 if (!node) {
32 continue;
33 }
34 foreachNodes.push_back(node);
35 }
36 return foreachNodes;
37 }
38
OutOfBottomOrRightBoundary(Axis axis,RefPtr<GeometryNode> & childGeoNode,float offset,RefPtr<GeometryNode> & hostGeoNode)39 bool OutOfBottomOrRightBoundary(
40 Axis axis, RefPtr<GeometryNode>& childGeoNode, float offset, RefPtr<GeometryNode>& hostGeoNode)
41 {
42 auto nodeOffset = childGeoNode->GetFrameOffset();
43 auto hostSize = hostGeoNode->GetFrameSize();
44 if (axis == Axis::VERTICAL) {
45 return (nodeOffset.GetY() + offset) > hostSize.Height();
46 } else if (axis == Axis::HORIZONTAL) {
47 return (nodeOffset.GetX() + offset) > hostSize.Width();
48 } else {
49 return false;
50 }
51 }
52
OutOfTopOrLeftBoundary(Axis axis,RefPtr<GeometryNode> & geoNode,float offset)53 bool OutOfTopOrLeftBoundary(Axis axis, RefPtr<GeometryNode>& geoNode, float offset)
54 {
55 auto nodeSize = geoNode->GetFrameSize();
56 auto nodeOffset = geoNode->GetFrameOffset();
57 if (axis == Axis::VERTICAL) {
58 return nodeSize.Height() + nodeOffset.GetY() + offset < 0;
59 } else if (axis == Axis::HORIZONTAL) {
60 return nodeSize.Width() + nodeOffset.GetX() + offset < 0;
61 } else {
62 return false;
63 }
64 }
65
GetScrollDownOrRightItemIndex(Axis axis,float offset,int32_t start,int32_t end,RefPtr<FrameNode> & host)66 int32_t GetScrollDownOrRightItemIndex(Axis axis, float offset, int32_t start, int32_t end, RefPtr<FrameNode>& host)
67 {
68 auto inIndex = end;
69 auto hostGeoNode = host->GetGeometryNode();
70 for (; inIndex >= start; inIndex--) {
71 auto child = host->GetChildByIndex(inIndex);
72 if (!child) {
73 continue;
74 }
75 auto childGeoNode = child->GetGeometryNode();
76 if (!OutOfBottomOrRightBoundary(axis, childGeoNode, offset, hostGeoNode)) {
77 break;
78 }
79 }
80 return inIndex;
81 }
82
GetScrollUpOrLeftItemIndex(Axis axis,float offset,int32_t start,int32_t end,RefPtr<FrameNode> & host)83 int32_t GetScrollUpOrLeftItemIndex(Axis axis, float offset, int32_t start, int32_t end, RefPtr<FrameNode>& host)
84 {
85 auto outIndex = start;
86 for (; outIndex <= end; outIndex++) {
87 auto child = host->GetChildByIndex(outIndex);
88 if (!child) {
89 continue;
90 }
91 auto geoNode = child->GetGeometryNode();
92 if (!OutOfTopOrLeftBoundary(axis, geoNode, offset)) {
93 break;
94 }
95 }
96 return outIndex;
97 }
98
RecycleItemsByIndex(int32_t start,int32_t end,std::vector<RefPtr<ForEachBaseNode>> & lazyNodes,LayoutWrapper * wrapper)99 void RecycleItemsByIndex(
100 int32_t start, int32_t end, std::vector<RefPtr<ForEachBaseNode>>& lazyNodes, LayoutWrapper* wrapper)
101 {
102 wrapper->RecycleItemsByIndex(start, end);
103 for (const auto& node : lazyNodes) {
104 node->RecycleItems(start, end);
105 }
106 }
107 } // namespace
108
DisableLazyForEachBuildCache(const RefPtr<UINode> & node)109 void ScrollableUtils::DisableLazyForEachBuildCache(const RefPtr<UINode>& node)
110 {
111 CHECK_NULL_VOID(node);
112 for (const auto& child : node->GetChildren()) {
113 auto lazyNode = AceType::DynamicCast<LazyForEachNode>(child);
114 if (lazyNode) {
115 lazyNode->EnablePreBuild(false);
116 } else if (AceType::InstanceOf<IfElseNode>(child)) {
117 DisableLazyForEachBuildCache(child);
118 }
119 }
120 }
121
CheckHeightExpansion(const RefPtr<LayoutProperty> & layoutProps,Axis axis)122 float ScrollableUtils::CheckHeightExpansion(const RefPtr<LayoutProperty>& layoutProps, Axis axis)
123 {
124 float expandHeight = 0.0f;
125 auto&& safeAreaOpts = layoutProps->GetSafeAreaExpandOpts();
126 bool canExpand = axis == Axis::VERTICAL && safeAreaOpts && (safeAreaOpts->edges & SAFE_AREA_EDGE_BOTTOM) &&
127 (safeAreaOpts->type & SAFE_AREA_TYPE_SYSTEM);
128 if (canExpand) {
129 auto pipeline = PipelineContext::GetCurrentContextSafelyWithCheck();
130 CHECK_NULL_RETURN(pipeline, {});
131 auto safeArea = pipeline->GetSafeArea();
132 expandHeight = safeArea.bottom_.Length();
133 }
134 return expandHeight;
135 }
136
RecycleItemsOutOfBoundary(Axis axis,float offset,int32_t start,int32_t end,LayoutWrapper * wrapper)137 void ScrollableUtils::RecycleItemsOutOfBoundary(
138 Axis axis, float offset, int32_t start, int32_t end, LayoutWrapper* wrapper)
139 {
140 if (start >= end || start < 0 || end < 0 || offset == 0) {
141 return;
142 }
143 if (axis != Axis::HORIZONTAL && axis != Axis::VERTICAL) {
144 return;
145 }
146
147 auto host = wrapper->GetHostNode();
148 std::vector<RefPtr<ForEachBaseNode>> foreachNodes = GetForEachNodes(host);
149 if (foreachNodes.empty()) {
150 return;
151 }
152 if (offset >= 0) {
153 int32_t inIndex = GetScrollDownOrRightItemIndex(axis, offset, start, end, host);
154 if (inIndex >= end) {
155 return;
156 }
157 RecycleItemsByIndex(inIndex + 1, end + 1, foreachNodes, wrapper);
158 } else {
159 int32_t outIndex = GetScrollUpOrLeftItemIndex(axis, offset, start, end, host);
160 if (outIndex <= start) {
161 return;
162 }
163 RecycleItemsByIndex(start, outIndex, foreachNodes, wrapper);
164 }
165 }
166
GetMoveOffset(const RefPtr<FrameNode> & parentFrameNode,const RefPtr<FrameNode> & curFrameNode,const MoveOffsetParam & param)167 float ScrollableUtils::GetMoveOffset(
168 const RefPtr<FrameNode>& parentFrameNode,
169 const RefPtr<FrameNode>& curFrameNode,
170 const MoveOffsetParam& param)
171 {
172 constexpr float notMove = 0.0f;
173 CHECK_NULL_RETURN(parentFrameNode, notMove);
174 CHECK_NULL_RETURN(curFrameNode, notMove);
175 auto parentGeometryNode = parentFrameNode->GetGeometryNode();
176 CHECK_NULL_RETURN(parentGeometryNode, notMove);
177 auto parentFrameSize = parentGeometryNode->GetPaddingSize();
178 auto parentPaddingOffset = parentGeometryNode->GetPaddingOffset(true) - parentGeometryNode->GetFrameOffset();
179 auto curFrameOffsetToWindow = curFrameNode->GetTransformRelativeOffset();
180 auto parentFrameOffsetToWindow = parentFrameNode->GetTransformRelativeOffset() + parentPaddingOffset;
181 auto offsetToTarFrame = curFrameOffsetToWindow - parentFrameOffsetToWindow;
182 auto curGeometry = curFrameNode->GetGeometryNode();
183 CHECK_NULL_RETURN(curGeometry, notMove);
184 auto curFrameSize = curGeometry->GetFrameSize();
185
186 float diffToTarFrame = param.isVertical ? offsetToTarFrame.GetY() : offsetToTarFrame.GetX();
187 if (NearZero(diffToTarFrame)) {
188 return notMove;
189 }
190 float focusMargin = param.noNeedMargin ? 0 : static_cast<float>(FOCUS_SCROLL_MARGIN.ConvertToPx());
191 float curFrameLength = param.isVertical ? curFrameSize.Height() : curFrameSize.Width();
192 float parentFrameLength = param.isVertical ? parentFrameSize.Height() : parentFrameSize.Width();
193 float focusMarginStart = std::max(focusMargin, param.contentStartOffset);
194 float focusMarginEnd = std::max(focusMargin, param.contentEndOffset);
195
196 bool totallyShow = LessOrEqual(curFrameLength + focusMarginStart + focusMarginEnd, (parentFrameLength));
197 float startAlignOffset = -diffToTarFrame + focusMarginStart;
198 float endAlignOffset = parentFrameLength - diffToTarFrame - curFrameLength - focusMarginEnd;
199 bool start2End = LessOrEqual(diffToTarFrame, focusMarginStart);
200 bool needScroll = !NearZero(startAlignOffset, 1.0f) && !NearZero(endAlignOffset, 1.0f) &&
201 (std::signbit(startAlignOffset) == std::signbit(endAlignOffset));
202 if (needScroll) {
203 return (totallyShow ^ start2End) ? endAlignOffset : startAlignOffset;
204 }
205 return notMove;
206 }
207 } // namespace OHOS::Ace::NG
208