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/for_each_base_node.h"
18 #include "core/components_ng/syntax/lazy_for_each_node.h"
19 #include "core/pipeline_ng/pipeline_context.h"
20 namespace OHOS::Ace::NG {
21 namespace {
22 Dimension FOCUS_SCROLL_MARGIN = 5.0_vp;
GetForEachNodes(RefPtr<FrameNode> & host)23 std::vector<RefPtr<ForEachBaseNode>> GetForEachNodes(RefPtr<FrameNode>& host)
24 {
25 std::vector<RefPtr<ForEachBaseNode>> 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
CheckHeightExpansion(const RefPtr<LayoutProperty> & layoutProps,Axis axis)109 float ScrollableUtils::CheckHeightExpansion(const RefPtr<LayoutProperty>& layoutProps, Axis axis)
110 {
111 float expandHeight = 0.0f;
112 auto&& safeAreaOpts = layoutProps->GetSafeAreaExpandOpts();
113 bool canExpand = axis == Axis::VERTICAL && safeAreaOpts && (safeAreaOpts->edges & SAFE_AREA_EDGE_BOTTOM) &&
114 (safeAreaOpts->type & SAFE_AREA_TYPE_SYSTEM);
115 if (canExpand) {
116 auto pipeline = PipelineContext::GetCurrentContext();
117 CHECK_NULL_RETURN(pipeline, {});
118 auto safeArea = pipeline->GetSafeArea();
119 expandHeight = safeArea.bottom_.Length();
120 }
121 return expandHeight;
122 }
123
RecycleItemsOutOfBoundary(Axis axis,float offset,int32_t start,int32_t end,LayoutWrapper * wrapper)124 void ScrollableUtils::RecycleItemsOutOfBoundary(
125 Axis axis, float offset, int32_t start, int32_t end, LayoutWrapper* wrapper)
126 {
127 if (start >= end || start < 0 || end < 0 || offset == 0) {
128 return;
129 }
130 if (axis != Axis::HORIZONTAL && axis != Axis::VERTICAL) {
131 return;
132 }
133
134 auto host = wrapper->GetHostNode();
135 std::vector<RefPtr<ForEachBaseNode>> foreachNodes = GetForEachNodes(host);
136 if (foreachNodes.empty()) {
137 return;
138 }
139 if (offset >= 0) {
140 int32_t inIndex = GetScrollDownOrRightItemIndex(axis, offset, start, end, host);
141 if (inIndex >= end) {
142 return;
143 }
144 RecycleItemsByIndex(inIndex + 1, end + 1, foreachNodes, wrapper);
145 } else {
146 int32_t outIndex = GetScrollUpOrLeftItemIndex(axis, offset, start, end, host);
147 if (outIndex <= start) {
148 return;
149 }
150 RecycleItemsByIndex(start, outIndex, foreachNodes, wrapper);
151 }
152 }
153
GetMoveOffset(const RefPtr<FrameNode> & parentFrameNode,const RefPtr<FrameNode> & curFrameNode,bool isVertical,float contentStartOffset,float contentEndOffset)154 float ScrollableUtils::GetMoveOffset(
155 const RefPtr<FrameNode>& parentFrameNode,
156 const RefPtr<FrameNode>& curFrameNode,
157 bool isVertical,
158 float contentStartOffset,
159 float contentEndOffset)
160 {
161 constexpr float notMove = 0.0f;
162 CHECK_NULL_RETURN(parentFrameNode, notMove);
163 CHECK_NULL_RETURN(curFrameNode, notMove);
164 auto parentGeometryNode = parentFrameNode->GetGeometryNode();
165 CHECK_NULL_RETURN(parentGeometryNode, notMove);
166 auto parentFrameSize = parentGeometryNode->GetFrameSize();
167 auto curFrameOffsetToWindow = curFrameNode->GetTransformRelativeOffset();
168 auto parentFrameOffsetToWindow = parentFrameNode->GetTransformRelativeOffset();
169 auto offsetToTarFrame = curFrameOffsetToWindow - parentFrameOffsetToWindow;
170 auto curGeometry = curFrameNode->GetGeometryNode();
171 CHECK_NULL_RETURN(curGeometry, notMove);
172 auto curFrameSize = curGeometry->GetFrameSize();
173 TAG_LOGD(AceLogTag::ACE_FOCUS,
174 "Node: %{public}s/%{public}d - %{public}s-%{public}s on focus. Offset to target node: "
175 "%{public}s/%{public}d - %{public}s-%{public}s is (%{public}f,%{public}f).",
176 curFrameNode->GetTag().c_str(), curFrameNode->GetId(), curFrameOffsetToWindow.ToString().c_str(),
177 curFrameSize.ToString().c_str(), parentFrameNode->GetTag().c_str(), parentFrameNode->GetId(),
178 parentFrameOffsetToWindow.ToString().c_str(), parentFrameSize.ToString().c_str(), offsetToTarFrame.GetX(),
179 offsetToTarFrame.GetY());
180
181 float diffToTarFrame = isVertical ? offsetToTarFrame.GetY() : offsetToTarFrame.GetX();
182 if (NearZero(diffToTarFrame)) {
183 return notMove;
184 }
185 float curFrameLength = isVertical ? curFrameSize.Height() : curFrameSize.Width();
186 float parentFrameLength = isVertical ? parentFrameSize.Height() : parentFrameSize.Width();
187 float focusMarginStart = std::max(static_cast<float>(FOCUS_SCROLL_MARGIN.ConvertToPx()), contentStartOffset);
188 float focusMarginEnd = std::max(static_cast<float>(FOCUS_SCROLL_MARGIN.ConvertToPx()), contentEndOffset);
189
190 bool totallyShow = LessOrEqual(curFrameLength + focusMarginStart + focusMarginEnd, (parentFrameLength));
191 float startAlignOffset = -diffToTarFrame + focusMarginStart;
192 float endAlignOffset = parentFrameLength - diffToTarFrame - curFrameLength - focusMarginEnd;
193 bool start2End = LessOrEqual(diffToTarFrame, focusMarginStart);
194 bool needScroll = !NearZero(startAlignOffset, 1.0f) && !NearZero(endAlignOffset, 1.0f) &&
195 (std::signbit(startAlignOffset) == std::signbit(endAlignOffset));
196 if (needScroll) {
197 return (totallyShow ^ start2End) ? endAlignOffset : startAlignOffset;
198 }
199 return notMove;
200 }
201 } // namespace OHOS::Ace::NG
202