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 "bridge/cj_frontend/cppview/gesture.h"
17
18 #include "bridge/declarative_frontend/view_stack_processor.h"
19 #include "core/gestures/long_press_gesture.h"
20 #include "core/gestures/pan_gesture.h"
21 #include "core/gestures/pinch_gesture.h"
22 #include "core/gestures/rotation_gesture.h"
23 #include "core/gestures/slide_gesture.h"
24
25 namespace OHOS::Ace::Framework {
26
27 namespace {
28 constexpr int32_t DEFAULT_TAP_FINGER = 1;
29 constexpr int32_t DEFAULT_TAP_COUNT = 1;
30 constexpr int32_t DEFAULT_LONG_PRESS_FINGER = 1;
31 constexpr int32_t DEFAULT_LONG_PRESS_DURATION = 500;
32 constexpr int32_t DEFAULT_PINCH_FINGER = 2;
33 constexpr double DEFAULT_PINCH_DISTANCE = 3.0;
34 constexpr int32_t DEFAULT_PAN_FINGER = 1;
35 constexpr double DEFAULT_PAN_DISTANCE = 5.0;
36 constexpr int32_t DEFAULT_SLIDE_FINGER = DEFAULT_PAN_FINGER;
37 constexpr double DEFAULT_SLIDE_SPEED = 100.0;
38 constexpr int32_t DEFAULT_ROTATION_FINGER = 2;
39 constexpr double DEFAULT_ROTATION_ANGLE = 1.0;
40
41 } // namespace
42
Create(GesturePriority priority, GestureMask gestureMask)43 void Gesture::Create(GesturePriority priority, GestureMask gestureMask)
44 {
45 LOGD("gesture create");
46 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
47 gestureComponent->SetPriority(priority);
48 gestureComponent->SetGestureMask(gestureMask);
49 }
50
Finish()51 void Gesture::Finish()
52 {
53 LOGD("gesture finish");
54 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
55 auto gesture = gestureComponent->FinishGesture();
56 if (!gesture) {
57 LOGE("Gesture: gesture is not exist when component finish");
58 return;
59 }
60
61 gesture->SetGestureMask(gestureComponent->GetGestureMask());
62
63 auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
64
65 boxComponent->AddGesture(gestureComponent->GetPriority(), gesture);
66 }
67
Pop()68 void Gesture::Pop()
69 {
70 LOGD("gesture pop");
71 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
72 gestureComponent->PopGesture();
73 }
74
HandlerOnAction(const std::function<void(const GestureEvent& event)>& callback)75 void Gesture::HandlerOnAction(const std::function<void(const GestureEvent& event)>& callback)
76 {
77 Gesture::HandlerOnGestureEvent(CJGestureEvent::ACTION, callback);
78 }
HandlerOnActionStart(const std::function<void(const GestureEvent& event)>& callback)79 void Gesture::HandlerOnActionStart(const std::function<void(const GestureEvent& event)>& callback)
80 {
81 Gesture::HandlerOnGestureEvent(CJGestureEvent::START, callback);
82 }
HandlerOnActionUpdate(const std::function<void(const GestureEvent& event)>& callback)83 void Gesture::HandlerOnActionUpdate(const std::function<void(const GestureEvent& event)>& callback)
84 {
85 Gesture::HandlerOnGestureEvent(CJGestureEvent::UPDATE, callback);
86 }
87
HandlerOnActionEnd(const std::function<void(const GestureEvent& event)>& callback)88 void Gesture::HandlerOnActionEnd(const std::function<void(const GestureEvent& event)>& callback)
89 {
90 Gesture::HandlerOnGestureEvent(CJGestureEvent::END, callback);
91 }
HandlerOnActionCancel(const std::function<void(const GestureEvent& event)>& callback)92 void Gesture::HandlerOnActionCancel(const std::function<void(const GestureEvent& event)>& callback)
93 {
94 Gesture::HandlerOnGestureEvent(CJGestureEvent::CANCEL, callback);
95 }
96
HandlerOnGestureEvent( const CJGestureEvent& action, const std::function<void(const GestureEvent& event)>& callback)97 void Gesture::HandlerOnGestureEvent(
98 const CJGestureEvent& action, const std::function<void(const GestureEvent& event)>& callback)
99 {
100 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
101 auto gesture = gestureComponent->TopGesture();
102 if (!gesture) {
103 LOGE("Gesture: top gesture is illegal");
104 return;
105 }
106 auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
107 if (!inspector) {
108 LOGE("Gesture: fail to get inspector for on handle event");
109 return;
110 }
111 auto impl = inspector->GetInspectorFunctionImpl();
112
113 if (action == CJGestureEvent::CANCEL) {
114 auto onActionCancelFunc = [callback = std::move(callback), impl]() {
115 auto info = GestureEvent();
116 if (impl) {
117 impl->UpdateEventInfo(info);
118 }
119 ACE_SCORING_EVENT("Gesture.onCancel");
120 callback(info);
121 };
122 gesture->SetOnActionCancelId(onActionCancelFunc);
123 return;
124 }
125 auto onActionFunc = [callback = std::move(callback), impl](GestureEvent& info) {
126 if (impl) {
127 impl->UpdateEventInfo(info);
128 }
129 ACE_SCORING_EVENT("Gesture.onActionCancel");
130 callback(info);
131 };
132
133 switch (action) {
134 case CJGestureEvent::ACTION:
135 gesture->SetOnActionId(onActionFunc);
136 break;
137 case CJGestureEvent::START:
138 gesture->SetOnActionStartId(onActionFunc);
139 break;
140 case CJGestureEvent::UPDATE:
141 gesture->SetOnActionUpdateId(onActionFunc);
142 break;
143 case CJGestureEvent::END:
144 gesture->SetOnActionEndId(onActionFunc);
145 break;
146 default:
147 LOGW("Gesture: Unknown gesture action %{public}d", action);
148 break;
149 }
150 }
151
Create(int32_t count, int32_t fingers)152 void TapGesture::Create(int32_t count, int32_t fingers)
153 {
154 int32_t countNum = DEFAULT_TAP_COUNT;
155 int32_t fingersNum = DEFAULT_TAP_FINGER;
156
157 countNum = count <= DEFAULT_TAP_COUNT ? DEFAULT_TAP_COUNT : count;
158 fingersNum = fingers <= DEFAULT_TAP_FINGER ? DEFAULT_TAP_FINGER : fingers;
159
160 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
161 auto gesture = AceType::MakeRefPtr<OHOS::Ace::TapGesture>(countNum, fingersNum);
162 gestureComponent->PushGesture(gesture);
163 }
164
Create(int32_t fingers, bool repeat, int32_t duration)165 void LongPressGesture::Create(int32_t fingers, bool repeat, int32_t duration)
166 {
167 int32_t fingersNum = DEFAULT_LONG_PRESS_FINGER;
168 int32_t durationNum = DEFAULT_LONG_PRESS_DURATION;
169
170 fingersNum = fingers <= DEFAULT_LONG_PRESS_FINGER ? DEFAULT_LONG_PRESS_FINGER : fingers;
171 durationNum = duration <= 0 ? DEFAULT_LONG_PRESS_DURATION : duration;
172
173 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
174 auto gesture = AceType::MakeRefPtr<OHOS::Ace::LongPressGesture>(fingersNum, repeat, durationNum);
175 gestureComponent->PushGesture(gesture);
176 }
177
Create(int32_t fingers, double distance)178 void PinchGesture::Create(int32_t fingers, double distance)
179 {
180 int32_t fingersNum = DEFAULT_PINCH_FINGER;
181 double distanceNum = DEFAULT_PINCH_DISTANCE;
182
183 fingersNum = fingers <= DEFAULT_PINCH_FINGER ? DEFAULT_PINCH_FINGER : fingers;
184 distanceNum = LessNotEqual(distance, 0.0) ? DEFAULT_PINCH_DISTANCE : distance;
185
186 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
187 auto gesture = AceType::MakeRefPtr<OHOS::Ace::PinchGesture>(fingersNum, distanceNum);
188 gestureComponent->PushGesture(gesture);
189 }
190
Create(int32_t fingers, const SwipeDirection& swipeDirection, double speed)191 void SwipeGesture::Create(int32_t fingers, const SwipeDirection& swipeDirection, double speed)
192 {
193 int32_t fingersNum = DEFAULT_SLIDE_FINGER;
194 double speedNum = DEFAULT_SLIDE_SPEED;
195
196 fingersNum = fingers <= DEFAULT_PAN_FINGER ? DEFAULT_PAN_FINGER : fingers;
197 speedNum = LessNotEqual(speed, 0.0) ? DEFAULT_SLIDE_SPEED : speed;
198
199 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
200 auto gesture = AceType::MakeRefPtr<OHOS::Ace::SwipeGesture>(fingersNum, swipeDirection, speedNum);
201 gestureComponent->PushGesture(gesture);
202 }
203
Create(int32_t fingers, double angle)204 void RotationGesture::Create(int32_t fingers, double angle)
205 {
206 double angleNum = DEFAULT_ROTATION_ANGLE;
207 int32_t fingersNum = DEFAULT_ROTATION_FINGER;
208
209 fingersNum = fingers <= DEFAULT_ROTATION_FINGER ? DEFAULT_ROTATION_FINGER : fingers;
210 angleNum = LessNotEqual(angle, 0.0) ? DEFAULT_ROTATION_ANGLE : angle;
211
212 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
213 auto gesture = AceType::MakeRefPtr<OHOS::Ace::RotationGesture>(fingersNum, angleNum);
214 gestureComponent->PushGesture(gesture);
215 }
216
Create(const GestureMode& mode)217 void GestureGroup::Create(const GestureMode& mode)
218 {
219 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
220 auto gesture = AceType::MakeRefPtr<OHOS::Ace::GestureGroup>(mode);
221 gestureComponent->PushGesture(gesture);
222 }
223
Create(int32_t fingers, const PanDirection& panDirection, double distance)224 void PanGesture::Create(int32_t fingers, const PanDirection& panDirection, double distance)
225 {
226 int32_t fingersNum = DEFAULT_PAN_FINGER;
227 double distanceNum = DEFAULT_PAN_DISTANCE;
228
229 fingersNum = fingers <= DEFAULT_PAN_FINGER ? DEFAULT_PAN_FINGER : fingers;
230 distanceNum = LessNotEqual(distance, 0.0) ? DEFAULT_PAN_DISTANCE : distance;
231
232 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
233 auto gesture = AceType::MakeRefPtr<OHOS::Ace::PanGesture>(fingersNum, panDirection, distanceNum);
234 gestureComponent->PushGesture(gesture);
235 }
236
Create(const sptr<NativePanGestureOption>& panGestureOption)237 void PanGesture::Create(const sptr<NativePanGestureOption>& panGestureOption)
238 {
239 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
240 auto gesture = AceType::MakeRefPtr<OHOS::Ace::PanGesture>(panGestureOption->GetPanGestureOption());
241 gestureComponent->PushGesture(gesture);
242 }
243
NativePanGestureOption(int32_t fingers, const PanDirection& panDirection, double distance)244 NativePanGestureOption::NativePanGestureOption(int32_t fingers, const PanDirection& panDirection, double distance)
245 : FFIData()
246 {
247 RefPtr<PanGestureOption> option = AceType::MakeRefPtr<PanGestureOption>();
248 SetPanGestureOption(option);
249 SetDirection(panDirection);
250 SetDistance(distance);
251 SetFingers(fingers);
252 LOGI("NativePanGestureOption constructed: %{public}" PRId64, GetID());
253 }
254
~NativePanGestureOption()255 NativePanGestureOption::~NativePanGestureOption()
256 {
257 LOGI("NativePanGestureOption Destroyed: %{public}" PRId64, GetID());
258 }
259
SetDirection(const PanDirection& panDirection)260 void NativePanGestureOption::SetDirection(const PanDirection& panDirection)
261 {
262 if (!panGestureOption_) {
263 LOGE("NativePanGestureOption: Native panGestureOption_ is nullptr");
264 return;
265 }
266 panGestureOption_->SetDirection(panDirection);
267 }
268
SetDistance(double distance)269 void NativePanGestureOption::SetDistance(double distance)
270 {
271 if (!panGestureOption_) {
272 LOGE("NativePanGestureOption: Native panGestureOption_ is nullptr");
273 return;
274 }
275 panGestureOption_->SetDistance(distance);
276 }
277
SetFingers(int32_t fingers)278 void NativePanGestureOption::SetFingers(int32_t fingers)
279 {
280 if (!panGestureOption_) {
281 LOGE("NativePanGestureOption: Native panGestureOption_ is nullptr");
282 return;
283 }
284 panGestureOption_->SetFingers(fingers);
285 }
286
287 } // namespace OHOS::Ace::Framework
288