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