1 /*
2 * Copyright (c) 2024 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/interfaces/native/node/scrollable_modifier.h"
16
17 #include "core/components_ng/pattern/scrollable/scrollable_model_ng.h"
18 #include "core/components_ng/pattern/scrollable/scrollable_paint_property.h"
19
20 namespace OHOS::Ace::NG {
21 namespace {
22 constexpr bool DEFAULT_BACKTOTOP = false;
23
GetContentClip(ArkUINodeHandle node)24 ArkUI_Int32 GetContentClip(ArkUINodeHandle node)
25 {
26 auto* frameNode = reinterpret_cast<FrameNode*>(node);
27 return static_cast<int32_t>(ScrollableModelNG::GetContentClip(frameNode));
28 }
29
SetContentClip(ArkUINodeHandle node,ArkUI_Int32 mode)30 void SetContentClip(ArkUINodeHandle node, ArkUI_Int32 mode)
31 {
32 auto* frameNode = reinterpret_cast<FrameNode*>(node);
33 auto val = static_cast<ContentClipMode>(mode);
34 if (val < ContentClipMode::CONTENT_ONLY || val > ContentClipMode::SAFE_AREA) {
35 val = ContentClipMode::DEFAULT;
36 }
37 ScrollableModelNG::SetContentClip(frameNode, static_cast<ContentClipMode>(mode), nullptr);
38 }
39
ResetContentClip(ArkUINodeHandle node)40 void ResetContentClip(ArkUINodeHandle node)
41 {
42 auto* frameNode = reinterpret_cast<FrameNode*>(node);
43 ScrollableModelNG::SetContentClip(frameNode, ContentClipMode::DEFAULT, nullptr);
44 }
45
SetOnReachStartCallBack(ArkUINodeHandle node,void * extraParam)46 void SetOnReachStartCallBack(ArkUINodeHandle node, void* extraParam)
47 {
48 auto* frameNode = reinterpret_cast<FrameNode*>(node);
49 CHECK_NULL_VOID(frameNode);
50 if (extraParam) {
51 auto onReachStart = reinterpret_cast<OnReachEvent*>(extraParam);
52 ScrollableModelNG::SetOnReachStart(frameNode, std::move(*onReachStart));
53 } else {
54 ScrollableModelNG::SetOnReachStart(frameNode, nullptr);
55 }
56 }
57
ResetOnReachStartCallBack(ArkUINodeHandle node)58 void ResetOnReachStartCallBack(ArkUINodeHandle node)
59 {
60 auto* frameNode = reinterpret_cast<FrameNode*>(node);
61 CHECK_NULL_VOID(frameNode);
62 ScrollableModelNG::SetOnReachStart(frameNode, nullptr);
63 }
64
SetOnReachEndCallBack(ArkUINodeHandle node,void * extraParam)65 void SetOnReachEndCallBack(ArkUINodeHandle node, void* extraParam)
66 {
67 auto* frameNode = reinterpret_cast<FrameNode*>(node);
68 CHECK_NULL_VOID(frameNode);
69 if (extraParam) {
70 auto onReachEnd = reinterpret_cast<OnReachEvent*>(extraParam);
71 ScrollableModelNG::SetOnReachEnd(frameNode, std::move(*onReachEnd));
72 } else {
73 ScrollableModelNG::SetOnReachEnd(frameNode, nullptr);
74 }
75 }
76
ResetOnReachEndCallBack(ArkUINodeHandle node)77 void ResetOnReachEndCallBack(ArkUINodeHandle node)
78 {
79 auto* frameNode = reinterpret_cast<FrameNode*>(node);
80 CHECK_NULL_VOID(frameNode);
81 ScrollableModelNG::SetOnReachEnd(frameNode, nullptr);
82 }
83
SetOnWillScrollCallBack(ArkUINodeHandle node,void * extraParam)84 void SetOnWillScrollCallBack(ArkUINodeHandle node, void* extraParam)
85 {
86 auto* frameNode = reinterpret_cast<FrameNode*>(node);
87 CHECK_NULL_VOID(frameNode);
88 if (extraParam) {
89 auto onWillScroll = reinterpret_cast<OnWillScrollEvent*>(extraParam);
90 ScrollableModelNG::SetOnWillScroll(frameNode, std::move(*onWillScroll));
91 } else {
92 ScrollableModelNG::SetOnWillScroll(frameNode, nullptr);
93 }
94 }
95
ResetOnWillScrollCallBack(ArkUINodeHandle node)96 void ResetOnWillScrollCallBack(ArkUINodeHandle node)
97 {
98 auto* frameNode = reinterpret_cast<FrameNode*>(node);
99 CHECK_NULL_VOID(frameNode);
100 ScrollableModelNG::SetOnWillScroll(frameNode, nullptr);
101 }
102
SetOnDidScrollCallBack(ArkUINodeHandle node,void * extraParam)103 void SetOnDidScrollCallBack(ArkUINodeHandle node, void* extraParam)
104 {
105 auto* frameNode = reinterpret_cast<FrameNode*>(node);
106 CHECK_NULL_VOID(frameNode);
107 if (extraParam) {
108 auto onDidScroll = reinterpret_cast<OnScrollEvent*>(extraParam);
109 ScrollableModelNG::SetOnDidScroll(frameNode, std::move(*onDidScroll));
110 } else {
111 ScrollableModelNG::SetOnDidScroll(frameNode, nullptr);
112 }
113 }
114
ResetOnDidScrollCallBack(ArkUINodeHandle node)115 void ResetOnDidScrollCallBack(ArkUINodeHandle node)
116 {
117 auto* frameNode = reinterpret_cast<FrameNode*>(node);
118 CHECK_NULL_VOID(frameNode);
119 ScrollableModelNG::SetOnDidScroll(frameNode, nullptr);
120 }
121
SetOnScrollFrameBeginCallBack(ArkUINodeHandle node,void * extraParam)122 void SetOnScrollFrameBeginCallBack(ArkUINodeHandle node, void* extraParam)
123 {
124 auto* frameNode = reinterpret_cast<FrameNode*>(node);
125 CHECK_NULL_VOID(frameNode);
126 if (extraParam) {
127 auto onScrollFrameBegin = reinterpret_cast<OnScrollFrameBeginEvent*>(extraParam);
128 ScrollableModelNG::SetOnScrollFrameBegin(frameNode, std::move(*onScrollFrameBegin));
129 } else {
130 ScrollableModelNG::SetOnScrollFrameBegin(frameNode, nullptr);
131 }
132 }
133
ResetOnScrollFrameBeginCallBack(ArkUINodeHandle node)134 void ResetOnScrollFrameBeginCallBack(ArkUINodeHandle node)
135 {
136 auto* frameNode = reinterpret_cast<FrameNode*>(node);
137 CHECK_NULL_VOID(frameNode);
138 ScrollableModelNG::SetOnScrollFrameBegin(frameNode, nullptr);
139 }
140
SetOnScrollStartCallBack(ArkUINodeHandle node,void * extraParam)141 void SetOnScrollStartCallBack(ArkUINodeHandle node, void* extraParam)
142 {
143 auto* frameNode = reinterpret_cast<FrameNode*>(node);
144 CHECK_NULL_VOID(frameNode);
145 if (extraParam) {
146 auto onScrollStart = reinterpret_cast<OnScrollStartEvent*>(extraParam);
147 ScrollableModelNG::SetOnScrollStart(frameNode, std::move(*onScrollStart));
148 } else {
149 ScrollableModelNG::SetOnScrollStart(frameNode, nullptr);
150 }
151 }
152
ResetOnScrollStartCallBack(ArkUINodeHandle node)153 void ResetOnScrollStartCallBack(ArkUINodeHandle node)
154 {
155 auto* frameNode = reinterpret_cast<FrameNode*>(node);
156 CHECK_NULL_VOID(frameNode);
157 ScrollableModelNG::SetOnScrollStart(frameNode, nullptr);
158 }
159
SetOnScrollStopCallBack(ArkUINodeHandle node,void * extraParam)160 void SetOnScrollStopCallBack(ArkUINodeHandle node, void* extraParam)
161 {
162 auto* frameNode = reinterpret_cast<FrameNode*>(node);
163 CHECK_NULL_VOID(frameNode);
164 if (extraParam) {
165 auto onScrollStop = reinterpret_cast<OnScrollStopEvent*>(extraParam);
166 ScrollableModelNG::SetOnScrollStop(frameNode, std::move(*onScrollStop));
167 } else {
168 ScrollableModelNG::SetOnScrollStop(frameNode, nullptr);
169 }
170 }
171
ResetOnScrollStopCallBack(ArkUINodeHandle node)172 void ResetOnScrollStopCallBack(ArkUINodeHandle node)
173 {
174 auto* frameNode = reinterpret_cast<FrameNode*>(node);
175 CHECK_NULL_VOID(frameNode);
176 ScrollableModelNG::SetOnScrollStop(frameNode, nullptr);
177 }
178
GetEdgeEffect(ArkUINodeHandle node,ArkUI_Int32 (* values)[2])179 ArkUI_Int32 GetEdgeEffect(ArkUINodeHandle node, ArkUI_Int32 (*values)[2])
180 {
181 auto* frameNode = reinterpret_cast<FrameNode*>(node);
182 CHECK_NULL_RETURN(frameNode, -1);
183 (*values)[0] = ScrollableModelNG::GetEdgeEffect(frameNode);
184 (*values)[1] = ScrollableModelNG::GetAlwaysEnabled(frameNode);
185 return 2;
186 }
187
SetEdgeEffect(ArkUINodeHandle node,ArkUI_Int32 edgeEffect,ArkUI_Bool alwaysEnabled,ArkUI_Int32 edge)188 void SetEdgeEffect(ArkUINodeHandle node, ArkUI_Int32 edgeEffect, ArkUI_Bool alwaysEnabled, ArkUI_Int32 edge)
189 {
190 auto* frameNode = reinterpret_cast<FrameNode*>(node);
191 CHECK_NULL_VOID(frameNode);
192 ScrollableModelNG::SetEdgeEffect(
193 frameNode, static_cast<EdgeEffect>(edgeEffect), alwaysEnabled, static_cast<EffectEdge>(edge));
194 }
195
ResetEdgeEffect(ArkUINodeHandle node)196 void ResetEdgeEffect(ArkUINodeHandle node)
197 {
198 auto* frameNode = reinterpret_cast<FrameNode*>(node);
199 CHECK_NULL_VOID(frameNode);
200 ScrollableModelNG::SetEdgeEffect(frameNode, EdgeEffect::SPRING, false);
201 }
202
SetFadingEdge(ArkUINodeHandle node,ArkUI_Bool fadingEdge,ArkUI_Float32 fadingEdgeLengthValue,ArkUI_Int32 fadingEdgeLengthUnit)203 void SetFadingEdge(
204 ArkUINodeHandle node, ArkUI_Bool fadingEdge, ArkUI_Float32 fadingEdgeLengthValue, ArkUI_Int32 fadingEdgeLengthUnit)
205 {
206 auto* frameNode = reinterpret_cast<FrameNode*>(node);
207 CHECK_NULL_VOID(frameNode);
208 Dimension fadingEdgeLengthDimension =
209 Dimension(fadingEdgeLengthValue, static_cast<OHOS::Ace::DimensionUnit>(fadingEdgeLengthUnit));
210 ScrollableModelNG::SetFadingEdge(frameNode, fadingEdge, fadingEdgeLengthDimension);
211 }
212
ResetFadingEdge(ArkUINodeHandle node)213 void ResetFadingEdge(ArkUINodeHandle node)
214 {
215 auto* frameNode = reinterpret_cast<FrameNode*>(node);
216 CHECK_NULL_VOID(frameNode);
217 constexpr Dimension DEFAULT_FADING_EDGE_LENGTH(32.0f, DimensionUnit::VP);
218 ScrollableModelNG::SetFadingEdge(frameNode, false, DEFAULT_FADING_EDGE_LENGTH);
219 }
220
GetFadingEdge(ArkUINodeHandle node,ArkUIInt32orFloat32 (* values)[2])221 void GetFadingEdge(ArkUINodeHandle node, ArkUIInt32orFloat32 (*values)[2])
222 {
223 auto* frameNode = reinterpret_cast<FrameNode*>(node);
224 CHECK_NULL_VOID(frameNode);
225 (*values)[0].i32 = static_cast<int32_t>(ScrollableModelNG::GetFadingEdge(frameNode));
226 (*values)[1].f32 = ScrollableModelNG::GetFadingEdgeLength(frameNode);
227 }
228
SetBackToTop(ArkUINodeHandle node,ArkUI_Bool value)229 void SetBackToTop(ArkUINodeHandle node, ArkUI_Bool value)
230 {
231 auto* frameNode = reinterpret_cast<FrameNode*>(node);
232 CHECK_NULL_VOID(frameNode);
233 ScrollableModelNG::SetBackToTop(frameNode, value);
234 }
235
ResetBackToTop(ArkUINodeHandle node)236 void ResetBackToTop(ArkUINodeHandle node)
237 {
238 auto* frameNode = reinterpret_cast<FrameNode*>(node);
239 CHECK_NULL_VOID(frameNode);
240 ScrollableModelNG::ResetBackToTop(frameNode);
241 }
242
GetBackToTop(ArkUINodeHandle node)243 int32_t GetBackToTop(ArkUINodeHandle node)
244 {
245 auto* frameNode = reinterpret_cast<FrameNode*>(node);
246 CHECK_NULL_RETURN(frameNode, DEFAULT_BACKTOTOP);
247 return ScrollableModelNG::GetBackToTop(frameNode);
248 }
249
SetScrollBarMargin(ArkUINodeHandle node,ArkUI_Float32 marginStart,ArkUI_Int32 marginStartLengthUnit,ArkUI_Float32 marginEnd,ArkUI_Int32 marginEndLengthUnit)250 void SetScrollBarMargin(ArkUINodeHandle node, ArkUI_Float32 marginStart, ArkUI_Int32 marginStartLengthUnit,
251 ArkUI_Float32 marginEnd, ArkUI_Int32 marginEndLengthUnit)
252 {
253 auto* frameNode = reinterpret_cast<FrameNode*>(node);
254 CHECK_NULL_VOID(frameNode);
255 ScrollBarMargin scrollBarMargin;
256 scrollBarMargin.start_ = Dimension(marginStart, static_cast<OHOS::Ace::DimensionUnit>(marginStartLengthUnit));
257 scrollBarMargin.end_ = Dimension(marginEnd, static_cast<OHOS::Ace::DimensionUnit>(marginEndLengthUnit));
258 ScrollableModelNG::SetScrollBarMargin(frameNode, scrollBarMargin);
259 }
260
ResetScrollBarMargin(ArkUINodeHandle node)261 void ResetScrollBarMargin(ArkUINodeHandle node)
262 {
263 auto* frameNode = reinterpret_cast<FrameNode*>(node);
264 CHECK_NULL_VOID(frameNode);
265 ScrollableModelNG::ResetScrollBarMargin(frameNode);
266 }
267
GetScrollBarMargin(ArkUINodeHandle node,ArkUIInt32orFloat32 (* values)[2])268 void GetScrollBarMargin(ArkUINodeHandle node, ArkUIInt32orFloat32 (*values)[2])
269 {
270 auto* frameNode = reinterpret_cast<FrameNode*>(node);
271 CHECK_NULL_VOID(frameNode);
272 ScrollBarMargin scrollBarMargin;
273 ScrollableModelNG::GetScrollBarMargin(frameNode, scrollBarMargin);
274 (*values)[0].f32 = scrollBarMargin.start_.ConvertToVp();
275 (*values)[1].f32 = scrollBarMargin.end_.ConvertToVp();
276 }
277
SetFlingSpeedLimit(ArkUINodeHandle node,ArkUI_Float32 maxSpeed)278 void SetFlingSpeedLimit(ArkUINodeHandle node, ArkUI_Float32 maxSpeed)
279 {
280 auto* frameNode = reinterpret_cast<FrameNode*>(node);
281 CHECK_NULL_VOID(frameNode);
282 ScrollableModelNG::SetMaxFlingSpeed(frameNode, maxSpeed);
283 }
284
ResetFlingSpeedLimit(ArkUINodeHandle node)285 void ResetFlingSpeedLimit(ArkUINodeHandle node)
286 {
287 auto* frameNode = reinterpret_cast<FrameNode*>(node);
288 CHECK_NULL_VOID(frameNode);
289 ScrollableModelNG::SetMaxFlingSpeed(frameNode, -1.0);
290 }
291
GetFlingSpeedLimit(ArkUINodeHandle node)292 float GetFlingSpeedLimit(ArkUINodeHandle node)
293 {
294 auto* frameNode = reinterpret_cast<FrameNode*>(node);
295 CHECK_NULL_RETURN(frameNode, -1.0f);
296 return ScrollableModelNG::GetMaxFlingSpeed(frameNode);
297 }
298
SetOnWillStopDragging(ArkUINodeHandle node,void * extraParam)299 void SetOnWillStopDragging(ArkUINodeHandle node, void* extraParam)
300 {
301 auto* frameNode = reinterpret_cast<FrameNode*>(node);
302 CHECK_NULL_VOID(frameNode);
303 if (extraParam) {
304 auto onWillStopDragging = reinterpret_cast<OnWillStopDraggingEvent*>(extraParam);
305 ScrollableModelNG::SetOnWillStopDragging(frameNode, std::move(*onWillStopDragging));
306 } else {
307 ScrollableModelNG::SetOnWillStopDragging(frameNode, nullptr);
308 }
309 }
310
ResetOnWillStopDragging(ArkUINodeHandle node)311 void ResetOnWillStopDragging(ArkUINodeHandle node)
312 {
313 auto* frameNode = reinterpret_cast<FrameNode*>(node);
314 CHECK_NULL_VOID(frameNode);
315 ScrollableModelNG::SetOnWillStopDragging(frameNode, nullptr);
316 }
317 } // namespace
318
319 namespace NodeModifier {
GetScrollableModifier()320 const ArkUIScrollableModifier* GetScrollableModifier()
321 {
322 CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
323 static const ArkUIScrollableModifier modifier = {
324 .getContentClip = GetContentClip,
325 .setContentClip = SetContentClip,
326 .resetContentClip = ResetContentClip,
327 .getEdgeEffect = GetEdgeEffect,
328 .setEdgeEffect = SetEdgeEffect,
329 .resetEdgeEffect = ResetEdgeEffect,
330 .setFadingEdge = SetFadingEdge,
331 .resetFadingEdge = ResetFadingEdge,
332 .getFadingEdge = GetFadingEdge,
333 .setOnReachStartCallBack = SetOnReachStartCallBack,
334 .resetOnReachStartCallBack = ResetOnReachStartCallBack,
335 .setOnReachEndCallBack = SetOnReachEndCallBack,
336 .resetOnReachEndCallBack = ResetOnReachEndCallBack,
337 .getFlingSpeedLimit = GetFlingSpeedLimit,
338 .setFlingSpeedLimit = SetFlingSpeedLimit,
339 .resetFlingSpeedLimit = ResetFlingSpeedLimit,
340 .setBackToTop = SetBackToTop,
341 .resetBackToTop = ResetBackToTop,
342 .getBackToTop = GetBackToTop,
343 .setScrollBarMargin = SetScrollBarMargin,
344 .resetScrollBarMargin = ResetScrollBarMargin,
345 .getScrollBarMargin = GetScrollBarMargin,
346 .setOnWillScrollCallBack = SetOnWillScrollCallBack,
347 .resetOnWillScrollCallBack = ResetOnWillScrollCallBack,
348 .setOnDidScrollCallBack = SetOnDidScrollCallBack,
349 .resetOnDidScrollCallBack = ResetOnDidScrollCallBack,
350 .setOnScrollFrameBeginCallBack = SetOnScrollFrameBeginCallBack,
351 .resetOnScrollFrameBeginCallBack = ResetOnScrollFrameBeginCallBack,
352 .setOnScrollStartCallBack = SetOnScrollStartCallBack,
353 .resetOnScrollStartCallBack = ResetOnScrollStartCallBack,
354 .setOnScrollStopCallBack = SetOnScrollStopCallBack,
355 .resetOnScrollStopCallBack = ResetOnScrollStopCallBack,
356 .setOnWillStopDragging = SetOnWillStopDragging,
357 .resetOnWillStopDragging = ResetOnWillStopDragging,
358 };
359 CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
360 return &modifier;
361 }
362 } // namespace NodeModifier
363 } // namespace OHOS::Ace::NG
364