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
16 #include "core/components_ng/pattern/ui_extension/platform_event_proxy.h"
17
18 #include "core/components_ng/base/frame_node.h"
19
20 namespace OHOS::Ace::NG {
21 namespace {
22 const std::map<uint32_t, std::string> PAN_GESTURE_MAP = {
23 { PanDirection::LEFT, "Left" },
24 { PanDirection::RIGHT, "Right" },
25 { PanDirection::UP, "Up" },
26 { PanDirection::DOWN, "Down" },
27 };
28 }
29
SetHostNode(const WeakPtr<FrameNode> & host)30 void PlatformEventProxy::SetHostNode(const WeakPtr<FrameNode>& host)
31 {
32 host_ = host;
33 }
34
HasEventProxy(int32_t flag)35 bool PlatformEventProxy::HasEventProxy(int32_t flag)
36 {
37 uint64_t uFlag = static_cast<uint64_t>(flag);
38 if ((uFlag & EventProxyFlag::EVENT_CLICK) && clickEvent_ == nullptr) {
39 return false;
40 }
41
42 if ((uFlag & EventProxyFlag::EVENT_LONG_PRESS) && longPressEvent_ == nullptr) {
43 return false;
44 }
45
46 auto panFlag = GetPanDirection(static_cast<uint64_t>(uFlag));
47 if (panFlag != 0) {
48 if (panEvent_ == nullptr) {
49 return false;
50 }
51
52 if (!(panDirection_ & panFlag)) {
53 return false;
54 }
55 }
56
57 return true;
58 }
59
SetEventProxyFlag(int32_t flag)60 void PlatformEventProxy::SetEventProxyFlag(int32_t flag)
61 {
62 auto host = host_.Upgrade();
63 CHECK_NULL_VOID(host);
64 auto hub = host->GetOrCreateEventHub<EventHub>();
65 CHECK_NULL_VOID(hub);
66 auto gestureHub = hub->GetOrCreateGestureEventHub();
67 CHECK_NULL_VOID(gestureHub);
68 uint64_t uFlag = static_cast<uint64_t>(flag);
69 EventProxyResultCode clickResultCode = SetClickEventProxy(
70 gestureHub, uFlag & EventProxyFlag::EVENT_CLICK);
71 EventProxyResultCode longPressResultCode = SetLongPressEventProxy(
72 gestureHub, uFlag & EventProxyFlag::EVENT_LONG_PRESS);
73 EventProxyResultCode panGestureResultCode = SetPanGestureEventProxy(
74 gestureHub, GetPanDirection(uFlag));
75 LOGI("SetEventProxyFlag flag: %{public}d, clickResult: %{public}d, "
76 "longPressResultCode: %{public}d, panGestureResultCode: %{public}d",
77 flag, clickResultCode, longPressResultCode, panGestureResultCode);
78 }
79
GetPanDirection(uint64_t flag)80 uint32_t PlatformEventProxy::GetPanDirection(uint64_t flag)
81 {
82 return flag & 0xF;
83 }
84
SetClickEventProxy(const RefPtr<GestureEventHub> & gestureHub,bool addEvent)85 EventProxyResultCode PlatformEventProxy::SetClickEventProxy(
86 const RefPtr<GestureEventHub>& gestureHub, bool addEvent)
87 {
88 CHECK_NULL_RETURN(gestureHub, EventProxyResultCode::CHECK_POINTER_NULL);
89 if (!addEvent) {
90 if (clickEvent_ == nullptr) {
91 return EventProxyResultCode::NO_EXIST_WHEN_DELETE;
92 }
93
94 gestureHub->RemoveClickEvent(clickEvent_);
95 clickEvent_ = nullptr;
96 return EventProxyResultCode::REMOVE_WHEN_DELETE;
97 }
98
99 if (clickEvent_ != nullptr) {
100 return EventProxyResultCode::EXIST_WHEN_ADDING;
101 }
102
103 auto clickCallback = [wp = WeakClaim(this)](GestureEvent& info) {
104 LOGI("ClickEventProxy");
105 };
106 clickEvent_ = AceType::MakeRefPtr<ClickEvent>(std::move(clickCallback));
107 gestureHub->AddClickEvent(clickEvent_);
108 return EventProxyResultCode::ADD_WHEN_ADDING;
109 }
110
SetLongPressEventProxy(const RefPtr<GestureEventHub> & gestureHub,bool addEvent)111 EventProxyResultCode PlatformEventProxy::SetLongPressEventProxy(
112 const RefPtr<GestureEventHub>& gestureHub, bool addEvent)
113 {
114 CHECK_NULL_RETURN(gestureHub, EventProxyResultCode::CHECK_POINTER_NULL);
115 if (!addEvent) {
116 if (longPressEvent_ == nullptr) {
117 return EventProxyResultCode::NO_EXIST_WHEN_DELETE;
118 }
119
120 gestureHub->SetLongPressEvent(nullptr);
121 longPressEvent_ = nullptr;
122 return EventProxyResultCode::REMOVE_WHEN_DELETE;
123 }
124
125 if (longPressEvent_ != nullptr) {
126 return EventProxyResultCode::EXIST_WHEN_ADDING;
127 }
128
129 auto longPressCallback = [](const GestureEvent& info) {
130 LOGI("LongPressEventEventProxy");
131 };
132 longPressEvent_ = AceType::MakeRefPtr<LongPressEvent>(std::move(longPressCallback));
133 gestureHub->SetLongPressEvent(longPressEvent_);
134 return EventProxyResultCode::ADD_WHEN_ADDING;
135 }
136
SetPanGestureEventProxy(const RefPtr<GestureEventHub> & gestureHub,uint32_t panDirection)137 EventProxyResultCode PlatformEventProxy::SetPanGestureEventProxy(
138 const RefPtr<GestureEventHub>& gestureHub, uint32_t panDirection)
139 {
140 CHECK_NULL_RETURN(gestureHub, EventProxyResultCode::CHECK_POINTER_NULL);
141 if (panDirection_ == panDirection) {
142 return panDirection_ == PanDirection::NONE ?
143 EventProxyResultCode::NO_EXIST_WHEN_DELETE : EventProxyResultCode::EXIST_WHEN_ADDING;
144 }
145
146 if (panDirection_ != PanDirection::NONE) {
147 gestureHub->RemovePanEvent(panEvent_);
148 panEvent_ = nullptr;
149 }
150
151 if (panDirection == PanDirection::NONE) {
152 panDirection_ = PanDirection::NONE;
153 return EventProxyResultCode::REMOVE_WHEN_DELETE;
154 }
155
156 panDirection_ = panDirection;
157 PanDirection temPanDirection;
158 temPanDirection.type = panDirection_;
159 auto onActionStart = [weak = WeakClaim(this)](const GestureEvent& info) {
160 LOGI("PanGestureEventProxy");
161 };
162 auto onActionUpdate = [](const GestureEvent& info) {};
163 auto onActionEnd = [weak = WeakClaim(this)](const GestureEvent& info) {};
164 auto onActionCancel = [weak = WeakClaim(this)]() {};
165 panEvent_ = MakeRefPtr<PanEvent>(std::move(onActionStart), std::move(onActionUpdate),
166 std::move(onActionEnd), std::move(onActionCancel));
167 PanDistanceMap distanceMap = { { SourceTool::UNKNOWN, 1 } };
168 gestureHub->AddPanEvent(panEvent_, temPanDirection, 1, distanceMap);
169 return EventProxyResultCode::ADD_WHEN_ADDING;
170 }
171
GetCurEventProxyToString()172 std::string PlatformEventProxy::GetCurEventProxyToString()
173 {
174 std::string eventProxyStr = "[";
175 bool hasPre = false;
176 if (clickEvent_) {
177 eventProxyStr.append("Click");
178 hasPre = true;
179 }
180
181 if (longPressEvent_) {
182 if (hasPre) {
183 eventProxyStr.append(", ");
184 }
185 eventProxyStr.append("LongPress");
186 hasPre = true;
187 }
188
189 if (panEvent_) {
190 for (const auto& item : PAN_GESTURE_MAP) {
191 if (!(panDirection_ & item.first)) {
192 continue;
193 }
194
195 if (hasPre) {
196 eventProxyStr.append(", ");
197 }
198
199 eventProxyStr.append(item.second);
200 hasPre = true;
201 }
202 }
203
204 eventProxyStr.append("]");
205 return eventProxyStr;
206 }
207 } // namespace OHOS::Ace::NG
208