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](GestureEvent& info) {
115 if (impl) {
116 impl->UpdateEventInfo(info);
117 }
118 ACE_SCORING_EVENT("Gesture.onCancel");
119 callback(info);
120 };
121 gesture->SetOnActionCancelId(onActionCancelFunc);
122 return;
123 }
124 auto onActionFunc = [callback = std::move(callback), impl](GestureEvent& info) {
125 if (impl) {
126 impl->UpdateEventInfo(info);
127 }
128 ACE_SCORING_EVENT("Gesture.onActionCancel");
129 callback(info);
130 };
131
132 switch (action) {
133 case CJGestureEvent::ACTION:
134 gesture->SetOnActionId(onActionFunc);
135 break;
136 case CJGestureEvent::START:
137 gesture->SetOnActionStartId(onActionFunc);
138 break;
139 case CJGestureEvent::UPDATE:
140 gesture->SetOnActionUpdateId(onActionFunc);
141 break;
142 case CJGestureEvent::END:
143 gesture->SetOnActionEndId(onActionFunc);
144 break;
145 default:
146 LOGW("Gesture: Unknown gesture action %{public}d", action);
147 break;
148 }
149 }
150
Create(int32_t count,int32_t fingers)151 void TapGesture::Create(int32_t count, int32_t fingers)
152 {
153 int32_t countNum = DEFAULT_TAP_COUNT;
154 int32_t fingersNum = DEFAULT_TAP_FINGER;
155
156 countNum = count <= DEFAULT_TAP_COUNT ? DEFAULT_TAP_COUNT : count;
157 fingersNum = fingers <= DEFAULT_TAP_FINGER ? DEFAULT_TAP_FINGER : fingers;
158
159 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
160 auto gesture = AceType::MakeRefPtr<OHOS::Ace::TapGesture>(countNum, fingersNum);
161 gestureComponent->PushGesture(gesture);
162 }
163
Create(int32_t fingers,bool repeat,int32_t duration)164 void LongPressGesture::Create(int32_t fingers, bool repeat, int32_t duration)
165 {
166 int32_t fingersNum = DEFAULT_LONG_PRESS_FINGER;
167 int32_t durationNum = DEFAULT_LONG_PRESS_DURATION;
168
169 fingersNum = fingers <= DEFAULT_LONG_PRESS_FINGER ? DEFAULT_LONG_PRESS_FINGER : fingers;
170 durationNum = duration <= 0 ? DEFAULT_LONG_PRESS_DURATION : duration;
171
172 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
173 auto gesture = AceType::MakeRefPtr<OHOS::Ace::LongPressGesture>(fingersNum, repeat, durationNum);
174 gestureComponent->PushGesture(gesture);
175 }
176
Create(int32_t fingers,double distance)177 void PinchGesture::Create(int32_t fingers, double distance)
178 {
179 int32_t fingersNum = DEFAULT_PINCH_FINGER;
180 double distanceNum = DEFAULT_PINCH_DISTANCE;
181
182 fingersNum = fingers <= DEFAULT_PINCH_FINGER ? DEFAULT_PINCH_FINGER : fingers;
183 distanceNum = LessNotEqual(distance, 0.0) ? DEFAULT_PINCH_DISTANCE : distance;
184
185 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
186 auto gesture = AceType::MakeRefPtr<OHOS::Ace::PinchGesture>(fingersNum, distanceNum);
187 gestureComponent->PushGesture(gesture);
188 }
189
Create(int32_t fingers,const SwipeDirection & swipeDirection,double speed)190 void SwipeGesture::Create(int32_t fingers, const SwipeDirection& swipeDirection, double speed)
191 {
192 int32_t fingersNum = DEFAULT_SLIDE_FINGER;
193 double speedNum = DEFAULT_SLIDE_SPEED;
194
195 fingersNum = fingers <= DEFAULT_PAN_FINGER ? DEFAULT_PAN_FINGER : fingers;
196 speedNum = LessNotEqual(speed, 0.0) ? DEFAULT_SLIDE_SPEED : speed;
197
198 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
199 auto gesture = AceType::MakeRefPtr<OHOS::Ace::SwipeGesture>(fingersNum, swipeDirection, speedNum);
200 gestureComponent->PushGesture(gesture);
201 }
202
Create(int32_t fingers,double angle)203 void RotationGesture::Create(int32_t fingers, double angle)
204 {
205 double angleNum = DEFAULT_ROTATION_ANGLE;
206 int32_t fingersNum = DEFAULT_ROTATION_FINGER;
207
208 fingersNum = fingers <= DEFAULT_ROTATION_FINGER ? DEFAULT_ROTATION_FINGER : fingers;
209 angleNum = LessNotEqual(angle, 0.0) ? DEFAULT_ROTATION_ANGLE : angle;
210
211 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
212 auto gesture = AceType::MakeRefPtr<OHOS::Ace::RotationGesture>(fingersNum, angleNum);
213 gestureComponent->PushGesture(gesture);
214 }
215
Create(const GestureMode & mode)216 void GestureGroup::Create(const GestureMode& mode)
217 {
218 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
219 auto gesture = AceType::MakeRefPtr<OHOS::Ace::GestureGroup>(mode);
220 gestureComponent->PushGesture(gesture);
221 }
222
Create(int32_t fingers,const PanDirection & panDirection,double distance)223 void PanGesture::Create(int32_t fingers, const PanDirection& panDirection, double distance)
224 {
225 int32_t fingersNum = DEFAULT_PAN_FINGER;
226 double distanceNum = DEFAULT_PAN_DISTANCE;
227
228 fingersNum = fingers <= DEFAULT_PAN_FINGER ? DEFAULT_PAN_FINGER : fingers;
229 distanceNum = LessNotEqual(distance, 0.0) ? DEFAULT_PAN_DISTANCE : distance;
230
231 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
232 auto gesture = AceType::MakeRefPtr<OHOS::Ace::PanGesture>(fingersNum, panDirection, distanceNum);
233 gestureComponent->PushGesture(gesture);
234 }
235
Create(const sptr<NativePanGestureOption> & panGestureOption)236 void PanGesture::Create(const sptr<NativePanGestureOption>& panGestureOption)
237 {
238 auto gestureComponent = ViewStackProcessor::GetInstance()->GetGestureComponent();
239 auto gesture = AceType::MakeRefPtr<OHOS::Ace::PanGesture>(panGestureOption->GetPanGestureOption());
240 gestureComponent->PushGesture(gesture);
241 }
242
NativePanGestureOption(int32_t fingers,const PanDirection & panDirection,double distance)243 NativePanGestureOption::NativePanGestureOption(int32_t fingers, const PanDirection& panDirection, double distance)
244 : FFIData()
245 {
246 RefPtr<PanGestureOption> option = AceType::MakeRefPtr<PanGestureOption>();
247 SetPanGestureOption(option);
248 SetDirection(panDirection);
249 SetDistance(distance);
250 SetFingers(fingers);
251 LOGI("NativePanGestureOption constructed: %{public}" PRId64, GetID());
252 }
253
~NativePanGestureOption()254 NativePanGestureOption::~NativePanGestureOption()
255 {
256 LOGI("NativePanGestureOption Destroyed: %{public}" PRId64, GetID());
257 }
258
SetDirection(const PanDirection & panDirection)259 void NativePanGestureOption::SetDirection(const PanDirection& panDirection)
260 {
261 if (!panGestureOption_) {
262 LOGE("NativePanGestureOption: Native panGestureOption_ is nullptr");
263 return;
264 }
265 panGestureOption_->SetDirection(panDirection);
266 }
267
SetDistance(double distance)268 void NativePanGestureOption::SetDistance(double distance)
269 {
270 if (!panGestureOption_) {
271 LOGE("NativePanGestureOption: Native panGestureOption_ is nullptr");
272 return;
273 }
274 panGestureOption_->SetDistance(distance);
275 }
276
SetFingers(int32_t fingers)277 void NativePanGestureOption::SetFingers(int32_t fingers)
278 {
279 if (!panGestureOption_) {
280 LOGE("NativePanGestureOption: Native panGestureOption_ is nullptr");
281 return;
282 }
283 panGestureOption_->SetFingers(fingers);
284 }
285
286 } // namespace OHOS::Ace::Framework
287