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/node_refresh_modifier.h"
16
17 #include "core/components_ng/pattern/refresh/refresh_model_ng.h"
18 #include "core/interfaces/native/node/node_refresh_modifier.h"
19
20 namespace OHOS::Ace::NG {
21 namespace {
22 const float ERROR_FLOAT_CODE = -1.0f;
23
SetRefreshing(ArkUINodeHandle node,ArkUI_Bool value)24 void SetRefreshing(ArkUINodeHandle node, ArkUI_Bool value)
25 {
26 auto* frameNode = reinterpret_cast<FrameNode*>(node);
27 CHECK_NULL_VOID(frameNode);
28 RefreshModelNG::SetRefreshing(frameNode, static_cast<bool>(value));
29 }
30
GetRefreshing(ArkUINodeHandle node)31 ArkUI_Bool GetRefreshing(ArkUINodeHandle node)
32 {
33 auto* frameNode = reinterpret_cast<FrameNode*>(node);
34 CHECK_NULL_RETURN(frameNode, false);
35 return static_cast<ArkUI_Bool>(RefreshModelNG::GetRefreshing(frameNode));
36 }
37
SetRefreshOffset(ArkUINodeHandle node,ArkUI_Float32 value,ArkUI_Int32 unit)38 void SetRefreshOffset(ArkUINodeHandle node, ArkUI_Float32 value, ArkUI_Int32 unit)
39 {
40 auto* frameNode = reinterpret_cast<FrameNode*>(node);
41 CHECK_NULL_VOID(frameNode);
42 RefreshModelNG::SetRefreshOffset(frameNode, Dimension(value, static_cast<OHOS::Ace::DimensionUnit>(unit)));
43 }
44
ResetRefreshOffset(ArkUINodeHandle node)45 void ResetRefreshOffset(ArkUINodeHandle node)
46 {
47 auto* frameNode = reinterpret_cast<FrameNode*>(node);
48 CHECK_NULL_VOID(frameNode);
49 RefreshModelNG::SetRefreshOffset(frameNode, Dimension(0.0f, DimensionUnit::VP));
50 }
51
SetPullToRefresh(ArkUINodeHandle node,ArkUI_Bool value)52 void SetPullToRefresh(ArkUINodeHandle node, ArkUI_Bool value)
53 {
54 auto* frameNode = reinterpret_cast<FrameNode*>(node);
55 CHECK_NULL_VOID(frameNode);
56 RefreshModelNG::SetPullToRefresh(frameNode, value);
57 }
58
ResetPullToRefresh(ArkUINodeHandle node)59 void ResetPullToRefresh(ArkUINodeHandle node)
60 {
61 auto* frameNode = reinterpret_cast<FrameNode*>(node);
62 CHECK_NULL_VOID(frameNode);
63 RefreshModelNG::SetPullToRefresh(frameNode, true);
64 }
65
SetRefreshContent(ArkUINodeHandle node,ArkUINodeHandle content)66 void SetRefreshContent(ArkUINodeHandle node, ArkUINodeHandle content)
67 {
68 auto* frameNode = reinterpret_cast<FrameNode*>(node);
69 CHECK_NULL_VOID(frameNode);
70 auto contentNode = reinterpret_cast<FrameNode*>(content);
71 CHECK_NULL_VOID(contentNode);
72 RefreshModelNG::SetCustomBuilder(frameNode, contentNode);
73 }
74
SetPullDownRatio(ArkUINodeHandle node,ArkUI_Float32 ratio)75 void SetPullDownRatio(ArkUINodeHandle node, ArkUI_Float32 ratio)
76 {
77 auto* frameNode = reinterpret_cast<FrameNode*>(node);
78 CHECK_NULL_VOID(frameNode);
79 float value = 0.0;
80 if (LessNotEqual(ratio, 0.0)) {
81 value = 0.0;
82 } else if (GreatNotEqual(ratio, 1.0)) {
83 value = 1.0;
84 }
85 value = ratio ;
86 std::optional<float> ratioValue = value;
87 RefreshModelNG::SetPullDownRatio(frameNode, ratioValue);
88 }
89
ResetPullDownRatio(ArkUINodeHandle node)90 void ResetPullDownRatio(ArkUINodeHandle node)
91 {
92 auto* frameNode = reinterpret_cast<FrameNode*>(node);
93 CHECK_NULL_VOID(frameNode);
94 std::optional<float> ratioValue = std::nullopt;
95 RefreshModelNG::SetPullDownRatio(frameNode, ratioValue);
96 }
97
GetPullDownRatio(ArkUINodeHandle node)98 ArkUI_Float32 GetPullDownRatio(ArkUINodeHandle node)
99 {
100 auto* frameNode = reinterpret_cast<FrameNode*>(node);
101 CHECK_NULL_RETURN(frameNode, ERROR_FLOAT_CODE);
102 return RefreshModelNG::GetPullDownRatio(frameNode);
103 }
104
GetRefreshOffset(ArkUINodeHandle node,ArkUI_Int32 unit)105 ArkUI_Float32 GetRefreshOffset(ArkUINodeHandle node, ArkUI_Int32 unit)
106 {
107 auto* frameNode = reinterpret_cast<FrameNode*>(node);
108 CHECK_NULL_RETURN(frameNode, ERROR_FLOAT_CODE);
109 return RefreshModelNG::GetRefreshOffset(frameNode).GetNativeValue(static_cast<DimensionUnit>(unit));
110 }
111
GetPullToRefresh(ArkUINodeHandle node)112 ArkUI_Bool GetPullToRefresh(ArkUINodeHandle node)
113 {
114 auto* frameNode = reinterpret_cast<FrameNode*>(node);
115 CHECK_NULL_RETURN(frameNode, false);
116 return static_cast<ArkUI_Bool>(RefreshModelNG::GetPullToRefresh(frameNode));
117 }
118
SetRefreshOnStateChangeCallback(ArkUINodeHandle node,void * callback)119 void SetRefreshOnStateChangeCallback(ArkUINodeHandle node, void* callback)
120 {
121 auto* frameNode = reinterpret_cast<FrameNode*>(node);
122 CHECK_NULL_VOID(frameNode);
123 if (callback) {
124 auto onStateChangeEvent = reinterpret_cast<StateChangeEvent*>(callback);
125 RefreshModelNG::SetOnStateChange(frameNode, std::move(*onStateChangeEvent));
126 } else {
127 RefreshModelNG::SetOnStateChange(frameNode, nullptr);
128 }
129 }
130
ResetRefreshOnStateChangeCallback(ArkUINodeHandle node)131 void ResetRefreshOnStateChangeCallback(ArkUINodeHandle node)
132 {
133 auto* frameNode = reinterpret_cast<FrameNode*>(node);
134 CHECK_NULL_VOID(frameNode);
135 RefreshModelNG::SetOnStateChange(frameNode, nullptr);
136 }
137
SetOnRefreshingCallback(ArkUINodeHandle node,void * callback)138 void SetOnRefreshingCallback(ArkUINodeHandle node, void* callback)
139 {
140 auto* frameNode = reinterpret_cast<FrameNode*>(node);
141 CHECK_NULL_VOID(frameNode);
142 if (callback) {
143 auto onRefreshingEvent = reinterpret_cast<RefreshingEvent*>(callback);
144 RefreshModelNG::SetOnRefreshing(frameNode, std::move(*onRefreshingEvent));
145 } else {
146 RefreshModelNG::SetOnRefreshing(frameNode, nullptr);
147 }
148 }
149
ResetOnRefreshingCallback(ArkUINodeHandle node)150 void ResetOnRefreshingCallback(ArkUINodeHandle node)
151 {
152 auto* frameNode = reinterpret_cast<FrameNode*>(node);
153 CHECK_NULL_VOID(frameNode);
154 RefreshModelNG::SetOnRefreshing(frameNode, nullptr);
155 }
156
SetRefreshOnOffsetChangeCallback(ArkUINodeHandle node,void * callback)157 void SetRefreshOnOffsetChangeCallback(ArkUINodeHandle node, void* callback)
158 {
159 auto* frameNode = reinterpret_cast<FrameNode*>(node);
160 CHECK_NULL_VOID(frameNode);
161 if (callback) {
162 auto onOffsetChangeEvent = reinterpret_cast<OffsetChangeEvent*>(callback);
163 RefreshModelNG::SetOnOffsetChange(frameNode, std::move(*onOffsetChangeEvent));
164 } else {
165 RefreshModelNG::SetOnOffsetChange(frameNode, nullptr);
166 }
167 }
168
ResetRefreshOnOffsetChangeCallback(ArkUINodeHandle node)169 void ResetRefreshOnOffsetChangeCallback(ArkUINodeHandle node)
170 {
171 auto* frameNode = reinterpret_cast<FrameNode*>(node);
172 CHECK_NULL_VOID(frameNode);
173 RefreshModelNG::SetOnOffsetChange(frameNode, nullptr);
174 }
175
SetMaxPullDownDistance(ArkUINodeHandle node,ArkUI_Float32 distance)176 void SetMaxPullDownDistance(ArkUINodeHandle node, ArkUI_Float32 distance)
177 {
178 auto* frameNode = reinterpret_cast<FrameNode*>(node);
179 CHECK_NULL_VOID(frameNode);
180 std::optional<float> distanceValue = std::max(distance, 0.0f);
181 RefreshModelNG::SetMaxPullDownDistance(frameNode, distanceValue);
182 }
183
ResetMaxPullDownDistance(ArkUINodeHandle node)184 void ResetMaxPullDownDistance(ArkUINodeHandle node)
185 {
186 auto* frameNode = reinterpret_cast<FrameNode*>(node);
187 CHECK_NULL_VOID(frameNode);
188 std::optional<float> distanceValue = std::nullopt;
189 RefreshModelNG::SetMaxPullDownDistance(frameNode, distanceValue);
190 }
191
GetMaxPullDownDistance(ArkUINodeHandle node)192 ArkUI_Float32 GetMaxPullDownDistance(ArkUINodeHandle node)
193 {
194 auto* frameNode = reinterpret_cast<FrameNode*>(node);
195 CHECK_NULL_RETURN(frameNode, ERROR_FLOAT_CODE);
196 return RefreshModelNG::GetMaxPullDownDistance(frameNode);
197 }
198
SetOnStepOffsetChangeCallback(ArkUINodeHandle node,void (* callback)(const ArkUI_Float32 offset,void * extraData),void * extraData)199 void SetOnStepOffsetChangeCallback(ArkUINodeHandle node,
200 void (*callback)(const ArkUI_Float32 offset, void* extraData), void* extraData)
201 {
202 auto* frameNode = reinterpret_cast<FrameNode*>(node);
203 CHECK_NULL_VOID(frameNode && callback);
204 auto onChange = [callback, extraData](const float offset) {
205 callback(offset, extraData);
206 };
207 RefreshModelNG::SetStepOffsetChange(frameNode, std::move(onChange));
208 }
209
ResetOnStepOffsetChangeCallback(ArkUINodeHandle node)210 void ResetOnStepOffsetChangeCallback(ArkUINodeHandle node)
211 {
212 auto* frameNode = reinterpret_cast<FrameNode*>(node);
213 CHECK_NULL_VOID(frameNode);
214 RefreshModelNG::SetStepOffsetChange(frameNode, nullptr);
215 }
216 } // namespace
217 namespace NodeModifier {
218
GetRefreshModifier()219 const ArkUIRefreshModifier* GetRefreshModifier()
220 {
221 CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
222 static const ArkUIRefreshModifier modifier = {
223 .setRefreshing = SetRefreshing,
224 .getRefreshing = GetRefreshing,
225 .setRefreshOffset = SetRefreshOffset,
226 .resetRefreshOffset = ResetRefreshOffset,
227 .setPullToRefresh = SetPullToRefresh,
228 .resetPullToRefresh = ResetPullToRefresh,
229 .setRefreshContent = SetRefreshContent,
230 .setPullDownRatio = SetPullDownRatio,
231 .resetPullDownRatio = ResetPullDownRatio,
232 .getPullDownRatio = GetPullDownRatio,
233 .getRefreshOffset = GetRefreshOffset,
234 .getPullToRefresh = GetPullToRefresh,
235 .setRefreshOnStateChangeCallback = SetRefreshOnStateChangeCallback,
236 .resetRefreshOnStateChangeCallback = ResetRefreshOnStateChangeCallback,
237 .setOnRefreshingCallback = SetOnRefreshingCallback,
238 .resetOnRefreshingCallback = ResetOnRefreshingCallback,
239 .setRefreshOnOffsetChangeCallback = SetRefreshOnOffsetChangeCallback,
240 .resetRefreshOnOffsetChangeCallback = ResetRefreshOnOffsetChangeCallback,
241 .setMaxPullDownDistance = SetMaxPullDownDistance,
242 .resetMaxPullDownDistance = ResetMaxPullDownDistance,
243 .getMaxPullDownDistance = GetMaxPullDownDistance,
244 .setOnStepOffsetChangeCallback = SetOnStepOffsetChangeCallback,
245 .resetOnStepOffsetChangeCallback = ResetOnStepOffsetChangeCallback,
246 };
247 CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
248 return &modifier;
249 }
250
GetCJUIRefreshModifier()251 const CJUIRefreshModifier* GetCJUIRefreshModifier()
252 {
253 CHECK_INITIALIZED_FIELDS_BEGIN(); // don't move this line
254 static const CJUIRefreshModifier modifier = {
255 .setRefreshing = SetRefreshing,
256 .getRefreshing = GetRefreshing,
257 .setRefreshContent = SetRefreshContent,
258 .setRefreshOffset = SetRefreshOffset,
259 .resetRefreshOffset = ResetRefreshOffset,
260 .setPullToRefresh = SetPullToRefresh,
261 .resetPullToRefresh = ResetPullToRefresh,
262 .setPullDownRatio = SetPullDownRatio,
263 .resetPullDownRatio = ResetPullDownRatio,
264 .getPullDownRatio = GetPullDownRatio,
265 .getRefreshOffset = GetRefreshOffset,
266 .getPullToRefresh = GetPullToRefresh,
267 };
268 CHECK_INITIALIZED_FIELDS_END(modifier, 0, 0, 0); // don't move this line
269 return &modifier;
270 }
271
SetRefreshOnStateChange(ArkUINodeHandle node,void * extraParam)272 void SetRefreshOnStateChange(ArkUINodeHandle node, void* extraParam)
273 {
274 auto* frameNode = reinterpret_cast<FrameNode*>(node);
275 CHECK_NULL_VOID(frameNode);
276 auto onEvent = [node, extraParam](const int32_t value) {
277 ArkUINodeEvent event;
278 event.kind = COMPONENT_ASYNC_EVENT;
279 event.extraParam = reinterpret_cast<intptr_t>(extraParam);
280 event.componentAsyncEvent.subKind = ON_REFRESH_STATE_CHANGE;
281 event.componentAsyncEvent.data[0].i32 = value;
282 SendArkUISyncEvent(&event);
283 };
284 RefreshModelNG::SetOnStateChange(frameNode, std::move(onEvent));
285 }
286
SetOnRefreshing(ArkUINodeHandle node,void * extraParam)287 void SetOnRefreshing(ArkUINodeHandle node, void* extraParam)
288 {
289 auto* frameNode = reinterpret_cast<FrameNode*>(node);
290 CHECK_NULL_VOID(frameNode);
291 auto onEvent = [node, extraParam]() {
292 ArkUINodeEvent event;
293 event.kind = COMPONENT_ASYNC_EVENT;
294 event.extraParam = reinterpret_cast<intptr_t>(extraParam);
295 event.componentAsyncEvent.subKind = ON_REFRESH_REFRESHING;
296 SendArkUISyncEvent(&event);
297 };
298 RefreshModelNG::SetOnRefreshing(frameNode, std::move(onEvent));
299 }
300
SetRefreshOnOffsetChange(ArkUINodeHandle node,void * extraParam)301 void SetRefreshOnOffsetChange(ArkUINodeHandle node, void* extraParam)
302 {
303 auto* frameNode = reinterpret_cast<FrameNode*>(node);
304 CHECK_NULL_VOID(frameNode);
305 auto onEvent = [node, extraParam](const float value) {
306 ArkUINodeEvent event;
307 event.kind = COMPONENT_ASYNC_EVENT;
308 event.extraParam = reinterpret_cast<intptr_t>(extraParam);
309 event.componentAsyncEvent.subKind = ON_REFRESH_ON_OFFSET_CHANGE;
310 event.componentAsyncEvent.data[0].f32 = value;
311 SendArkUISyncEvent(&event);
312 };
313 RefreshModelNG::SetOnOffsetChange(frameNode, std::move(onEvent));
314 }
315
SetRefreshChangeEvent(ArkUINodeHandle node,void * extraParam)316 void SetRefreshChangeEvent(ArkUINodeHandle node, void* extraParam)
317 {
318 auto* frameNode = reinterpret_cast<FrameNode*>(node);
319 CHECK_NULL_VOID(frameNode);
320 auto onEvent = [node, extraParam](const std::string& value) {
321 ArkUINodeEvent event;
322 event.kind = COMPONENT_ASYNC_EVENT;
323 event.extraParam = reinterpret_cast<intptr_t>(extraParam);
324 event.componentAsyncEvent.subKind = ON_REFRESH_CHANGE_EVENT;
325 bool newValue = value == "true";
326 event.componentAsyncEvent.data[0].u32 = newValue;
327 SendArkUISyncEvent(&event);
328 };
329 RefreshModelNG::SetChangeEvent(frameNode, std::move(onEvent));
330 }
331
ResetRefreshOnStateChange(ArkUINodeHandle node)332 void ResetRefreshOnStateChange(ArkUINodeHandle node)
333 {
334 auto* frameNode = reinterpret_cast<FrameNode*>(node);
335 CHECK_NULL_VOID(frameNode);
336 RefreshModelNG::SetOnStateChange(frameNode, nullptr);
337 }
ResetOnRefreshing(ArkUINodeHandle node)338 void ResetOnRefreshing(ArkUINodeHandle node)
339 {
340 auto* frameNode = reinterpret_cast<FrameNode*>(node);
341 CHECK_NULL_VOID(frameNode);
342 RefreshModelNG::SetOnRefreshing(frameNode, nullptr);
343 }
ResetRefreshOnOffsetChange(ArkUINodeHandle node)344 void ResetRefreshOnOffsetChange(ArkUINodeHandle node)
345 {
346 auto* frameNode = reinterpret_cast<FrameNode*>(node);
347 CHECK_NULL_VOID(frameNode);
348 RefreshModelNG::SetOnOffsetChange(frameNode, nullptr);
349 }
ResetRefreshChangeEvent(ArkUINodeHandle node)350 void ResetRefreshChangeEvent(ArkUINodeHandle node)
351 {
352 auto* frameNode = reinterpret_cast<FrameNode*>(node);
353 CHECK_NULL_VOID(frameNode);
354 RefreshModelNG::SetChangeEvent(frameNode, nullptr);
355 }
356 } // namespace NodeModifier
357 } // namespace OHOS::Ace::NG
358