1 /*
2 * Copyright (c) 2023-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_list_item_modifier.h"
16
17 #include "interfaces/native/node/list_option.h"
18
19 #include "base/geometry/calc_dimension.h"
20 #include "core/components_ng/base/frame_node.h"
21 #include "core/components_ng/pattern/list/list_item_model_ng.h"
22 #include "core/interfaces/arkoala/arkoala_api.h"
23
24 namespace OHOS::Ace::NG {
25
SetOptionBuilder(FrameNode * frameNode,ArkUIListItemSwipeActionItemHandle item,bool isStart)26 void SetOptionBuilder(FrameNode* frameNode, ArkUIListItemSwipeActionItemHandle item, bool isStart)
27 {
28 if (item) {
29 CalcDimension length { item->actionAreaDistance, DimensionUnit::VP };
30 auto onAction = [item]() {
31 if (item->onAction) {
32 using FuncType = void (*)(void*);
33 FuncType func = reinterpret_cast<FuncType>(item->onAction);
34 func(item->onActionUserData);
35 }
36 };
37 auto onEnterActionArea = [item]() {
38 if (item->onExitActionArea) {
39 using FuncType = void (*)(void*);
40 FuncType func = reinterpret_cast<FuncType>(item->onEnterActionArea);
41 func(item->onEnterActionAreaUserData);
42 }
43 };
44 auto onExitActionArea = [item]() {
45 if (item->onExitActionArea) {
46 using FuncType = void (*)(void*);
47 FuncType func = reinterpret_cast<FuncType>(item->onExitActionArea);
48 func(item->onExitActionAreaUserData);
49 }
50 };
51 auto onStateChange = [item](OHOS::Ace::SwipeActionState swipeActionState) {
52 if (item->onStateChange) {
53 using FuncType = float (*)(int32_t, void*);
54 FuncType func = reinterpret_cast<FuncType>(item->onStateChange);
55 func(static_cast<int32_t>(swipeActionState), item->onStateChangeUserData);
56 }
57 };
58 ListItemModelNG::SetDeleteArea(frameNode, reinterpret_cast<FrameNode*>(item->node), onAction,
59 onEnterActionArea, onExitActionArea, onStateChange, length, isStart);
60 } else {
61 CalcDimension length;
62 ListItemModelNG::SetDeleteArea(frameNode, nullptr, nullptr, nullptr, nullptr, nullptr, length, isStart);
63 }
64 }
65
SetListItemSwiperAction(ArkUINodeHandle node,ArkUIListItemSwipeActionOptionHandle option)66 void SetListItemSwiperAction(ArkUINodeHandle node, ArkUIListItemSwipeActionOptionHandle option)
67 {
68 CHECK_NULL_VOID(option);
69 auto* frameNode = reinterpret_cast<FrameNode*>(node);
70 CHECK_NULL_VOID(frameNode);
71 SetOptionBuilder(frameNode, option->start, true);
72 SetOptionBuilder(frameNode, option->end, false);
73
74 auto onOffsetChange = [option](int32_t offset) {
75 if (option->onOffsetChange) {
76 using FuncType = float (*)(float, void*);
77 FuncType func = reinterpret_cast<FuncType>(option->onOffsetChange);
78 func(static_cast<float>(offset), option->userData);
79 }
80 };
81 auto edgeEffect = static_cast<V2::SwipeEdgeEffect>(option->edgeEffect);
82 ListItemModelNG::SetSwiperAction(frameNode, nullptr, nullptr, onOffsetChange, edgeEffect);
83 }
84
ResetListItemSwiperAction(ArkUINodeHandle node)85 void ResetListItemSwiperAction(ArkUINodeHandle node)
86 {
87 auto* frameNode = reinterpret_cast<FrameNode*>(node);
88 CHECK_NULL_VOID(frameNode);
89 CalcDimension length;
90 ListItemModelNG::SetDeleteArea(frameNode, nullptr, nullptr, nullptr, nullptr, nullptr, length, true);
91 ListItemModelNG::SetDeleteArea(frameNode, nullptr, nullptr, nullptr, nullptr, nullptr, length, false);
92 ListItemModelNG::SetSwiperAction(frameNode, nullptr, nullptr, nullptr, V2::SwipeEdgeEffect::None);
93 }
94
SetListItemSelected(ArkUINodeHandle node,ArkUI_Bool selected)95 void SetListItemSelected(ArkUINodeHandle node, ArkUI_Bool selected)
96 {
97 auto* frameNode = reinterpret_cast<FrameNode*>(node);
98 CHECK_NULL_VOID(frameNode);
99 ListItemModelNG::SetSelected(frameNode, selected);
100 }
101
ResetListItemSelected(ArkUINodeHandle node)102 void ResetListItemSelected(ArkUINodeHandle node)
103 {
104 auto* frameNode = reinterpret_cast<FrameNode*>(node);
105 CHECK_NULL_VOID(frameNode);
106 ListItemModelNG::SetSelected(frameNode, false);
107 }
108
SetSelectable(ArkUINodeHandle node,ArkUI_Bool selectable)109 void SetSelectable(ArkUINodeHandle node, ArkUI_Bool selectable)
110 {
111 auto* frameNode = reinterpret_cast<FrameNode*>(node);
112 CHECK_NULL_VOID(frameNode);
113 ListItemModelNG::SetSelectable(frameNode, selectable);
114 }
115
ResetSelectable(ArkUINodeHandle node)116 void ResetSelectable(ArkUINodeHandle node)
117 {
118 auto* frameNode = reinterpret_cast<FrameNode*>(node);
119 CHECK_NULL_VOID(frameNode);
120 ListItemModelNG::SetSelectable(frameNode, true);
121 }
122
123 namespace NodeModifier {
GetListItemModifier()124 const ArkUIListItemModifier* GetListItemModifier()
125 {
126 static const ArkUIListItemModifier modifier = { SetListItemSelected, ResetListItemSelected, SetSelectable,
127 ResetSelectable, SetListItemSwiperAction, ResetListItemSwiperAction };
128 return &modifier;
129 }
130
GetCJUIListItemModifier()131 const CJUIListItemModifier* GetCJUIListItemModifier()
132 {
133 static const CJUIListItemModifier modifier = { SetListItemSelected, ResetListItemSelected, SetSelectable,
134 ResetSelectable, SetListItemSwiperAction, ResetListItemSwiperAction };
135 return &modifier;
136 }
137
SetListItemOnSelect(ArkUINodeHandle node,void * extraParam)138 void SetListItemOnSelect(ArkUINodeHandle node, void* extraParam)
139 {
140 auto* frameNode = reinterpret_cast<FrameNode*>(node);
141 CHECK_NULL_VOID(frameNode);
142 auto onEvent = [node, extraParam](bool isSelected) {
143 ArkUINodeEvent event;
144 event.kind = COMPONENT_ASYNC_EVENT;
145 event.extraParam = reinterpret_cast<intptr_t>(extraParam);
146 event.componentAsyncEvent.subKind = ON_LIST_ITEM_SELECTED;
147 event.componentAsyncEvent.data[0].i32 = static_cast<int32_t>(isSelected);
148 SendArkUIAsyncEvent(&event);
149 };
150 ListItemModelNG::SetSelectCallback(frameNode, std::move(onEvent));
151 }
152
ResetListItemOnSelect(ArkUINodeHandle node)153 void ResetListItemOnSelect(ArkUINodeHandle node)
154 {
155 auto* frameNode = reinterpret_cast<FrameNode*>(node);
156 CHECK_NULL_VOID(frameNode);
157 ListItemModelNG::SetSelectCallback(frameNode, nullptr);
158 }
159 } // namespace NodeModifier
160 } // namespace OHOS::Ace::NG