1 /*
2  * Copyright (c) 2023 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/declarative_frontend/jsview/models/gesture_model_impl.h"
17 
18 #include "core/gestures/gesture_group.h"
19 #include "core/gestures/gesture_processor.h"
20 #include "core/gestures/long_press_gesture.h"
21 #include "core/gestures/rotation_gesture.h"
22 #include "core/gestures/pan_gesture.h"
23 #include "core/gestures/pinch_gesture.h"
24 #include "core/gestures/slide_gesture.h"
25 #include "core/gestures/tap_gesture.h"
26 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
27 
28 namespace OHOS::Ace::Framework {
Create(int32_t priorityNum,int32_t gestureMaskNum)29 void GestureModelImpl::Create(int32_t priorityNum, int32_t gestureMaskNum)
30 {
31     RefPtr<GestureProcessor> gestureProcessor;
32     gestureProcessor = ViewStackProcessor::GetInstance()->GetGestureComponent();
33 
34     GesturePriority priority = GesturePriority::Low;
35     if (priorityNum > static_cast<int32_t>(GesturePriority::Begin) &&
36         priorityNum < static_cast<int32_t>(GesturePriority::End)) {
37         priority = static_cast<GesturePriority>(priorityNum);
38     }
39     gestureProcessor->SetPriority(priority);
40 
41     GestureMask gestureMask = GestureMask::Normal;
42     if (gestureMaskNum > static_cast<int32_t>(GestureMask::Begin) &&
43         gestureMaskNum < static_cast<int32_t>(GestureMask::End)) {
44         gestureMask = static_cast<GestureMask>(gestureMaskNum);
45     }
46     gestureProcessor->SetGestureMask(gestureMask);
47 }
48 
Finish()49 void GestureModelImpl::Finish()
50 {
51     RefPtr<GestureProcessor> gestureProcessor;
52     gestureProcessor = ViewStackProcessor::GetInstance()->GetGestureComponent();
53     auto gesture = gestureProcessor->FinishGesture();
54     CHECK_NULL_VOID(gesture);
55     gesture->SetGestureMask(gestureProcessor->GetGestureMask());
56     gesture->SetPriority(gestureProcessor->GetPriority());
57     auto boxComponent = ViewStackProcessor::GetInstance()->GetBoxComponent();
58     boxComponent->AddGesture(gestureProcessor->GetPriority(), gesture);
59 }
60 
Pop()61 void GestureModelImpl::Pop()
62 {
63     RefPtr<GestureProcessor> gestureProcessor;
64     gestureProcessor = ViewStackProcessor::GetInstance()->GetGestureComponent();
65     gestureProcessor->PopGesture();
66 }
67 
Create(int32_t countNum,int32_t fingersNum,double distanceThreshold)68 void TapGestureModelImpl::Create(int32_t countNum, int32_t fingersNum, double distanceThreshold)
69 {
70     RefPtr<GestureProcessor> gestureProcessor;
71     gestureProcessor = ViewStackProcessor::GetInstance()->GetGestureComponent();
72     auto gesture = AceType::MakeRefPtr<TapGesture>(countNum, fingersNum, distanceThreshold);
73     gestureProcessor->PushGesture(gesture);
74 }
75 
Create(int32_t fingersNum,bool repeatResult,int32_t durationNum)76 void LongPressGestureModelImpl::Create(int32_t fingersNum, bool repeatResult, int32_t durationNum)
77 {
78     RefPtr<GestureProcessor> gestureProcessor;
79     gestureProcessor = ViewStackProcessor::GetInstance()->GetGestureComponent();
80     auto gesture = AceType::MakeRefPtr<LongPressGesture>(fingersNum, repeatResult, durationNum);
81     gestureProcessor->PushGesture(gesture);
82 }
83 
Create(int32_t fingersNum,const PanDirection & panDirection,double distanceNum)84 void PanGestureModelImpl::Create(int32_t fingersNum, const PanDirection& panDirection, double distanceNum)
85 {
86     RefPtr<GestureProcessor> gestureProcessor;
87     gestureProcessor = ViewStackProcessor::GetInstance()->GetGestureComponent();
88     auto gesture = AceType::MakeRefPtr<PanGesture>(fingersNum, panDirection, distanceNum);
89     gestureProcessor->PushGesture(gesture);
90 }
91 
SetPanGestureOption(const RefPtr<PanGestureOption> & panGestureOption)92 void PanGestureModelImpl::SetPanGestureOption(const RefPtr<PanGestureOption>& panGestureOption)
93 {
94     RefPtr<GestureProcessor> gestureProcessor;
95     gestureProcessor = ViewStackProcessor::GetInstance()->GetGestureComponent();
96     auto gesture = AceType::MakeRefPtr<PanGesture>(panGestureOption);
97     gestureProcessor->PushGesture(gesture);
98 }
99 
Create(int32_t fingersNum,const SwipeDirection & slideDirection,double speedNum)100 void SwipeGestureModelImpl::Create(int32_t fingersNum, const SwipeDirection& slideDirection, double speedNum)
101 {
102     RefPtr<GestureProcessor> gestureProcessor;
103     gestureProcessor = ViewStackProcessor::GetInstance()->GetGestureComponent();
104     auto gesture = AceType::MakeRefPtr<SwipeGesture>(fingersNum, slideDirection, speedNum);
105     gestureProcessor->PushGesture(gesture);
106 }
107 
Create(int32_t fingersNum,double distanceNum)108 void PinchGestureModelImpl::Create(int32_t fingersNum, double distanceNum)
109 {
110     RefPtr<GestureProcessor> gestureProcessor;
111     gestureProcessor = ViewStackProcessor::GetInstance()->GetGestureComponent();
112     auto gesture = AceType::MakeRefPtr<PinchGesture>(fingersNum, distanceNum);
113     gestureProcessor->PushGesture(gesture);
114 }
115 
Create(int32_t fingersNum,double angleNum)116 void RotationGestureModelImpl::Create(int32_t fingersNum, double angleNum)
117 {
118     RefPtr<GestureProcessor> gestureProcessor;
119     gestureProcessor = ViewStackProcessor::GetInstance()->GetGestureComponent();
120     auto gesture = AceType::MakeRefPtr<RotationGesture>(fingersNum, angleNum);
121     gestureProcessor->PushGesture(gesture);
122 }
123 
Create(int32_t gestureMode)124 void GestureGroupModelImpl::Create(int32_t gestureMode)
125 {
126     RefPtr<GestureProcessor> gestureProcessor;
127     gestureProcessor = ViewStackProcessor::GetInstance()->GetGestureComponent();
128     auto gesture = AceType::MakeRefPtr<GestureGroup>(static_cast<GestureMode>(gestureMode));
129     gestureProcessor->PushGesture(gesture);
130 }
131 
GetGestureProcessor()132 RefPtr<GestureProcessor> TimeoutGestureModelImpl::GetGestureProcessor()
133 {
134     RefPtr<GestureProcessor> gestureProcessor;
135     gestureProcessor = ViewStackProcessor::GetInstance()->GetGestureComponent();
136     return gestureProcessor;
137 }
138 
SetOnGestureEvent(const GestureEventNoParameter & gestureEventNoParameter)139 void GestureModelImpl::SetOnGestureEvent(const GestureEventNoParameter& gestureEventNoParameter)
140 {
141     RefPtr<GestureProcessor> gestureProcessor;
142     gestureProcessor = ViewStackProcessor::GetInstance()->GetGestureComponent();
143     auto gesture = gestureProcessor->TopGesture();
144     CHECK_NULL_VOID(gesture);
145 
146     RefPtr<V2::InspectorFunctionImpl> impl;
147     auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
148     CHECK_NULL_VOID(inspector);
149     impl = inspector->GetInspectorFunctionImpl();
150 
151     gesture->SetOnActionCancelId([func = std::move(gestureEventNoParameter), impl]() {
152         auto info = GestureEvent();
153         if (impl) {
154             impl->UpdateEventInfo(info);
155         }
156         func();
157     });
158 }
159 
SetOnActionFunc(const GestureEventFunc & gestureEventFunc,const Ace::GestureEventAction & action)160 void GestureModelImpl::SetOnActionFunc(const GestureEventFunc& gestureEventFunc,
161     const Ace::GestureEventAction& action)
162 {
163     RefPtr<GestureProcessor> gestureProcessor;
164     gestureProcessor = ViewStackProcessor::GetInstance()->GetGestureComponent();
165     auto gesture = gestureProcessor->TopGesture();
166     CHECK_NULL_VOID(gesture);
167 
168     RefPtr<V2::InspectorFunctionImpl> impl;
169     auto inspector = ViewStackProcessor::GetInstance()->GetInspectorComposedComponent();
170     CHECK_NULL_VOID(inspector);
171     impl = inspector->GetInspectorFunctionImpl();
172 
173     auto onActionFunc = [func = std::move(gestureEventFunc), impl](GestureEvent& info) {
174         if (impl) {
175             impl->UpdateEventInfo(info);
176         }
177         func(info);
178     };
179 
180     switch (action) {
181         case Ace::GestureEventAction::ACTION:
182             gesture->SetOnActionId(onActionFunc);
183             break;
184         case Ace::GestureEventAction::START:
185             gesture->SetOnActionStartId(onActionFunc);
186             break;
187         case Ace::GestureEventAction::UPDATE:
188             gesture->SetOnActionUpdateId(onActionFunc);
189             break;
190         case Ace::GestureEventAction::END:
191             gesture->SetOnActionEndId(onActionFunc);
192             break;
193         default:
194             LOGW("Unknown gesture action %{public}d", action);
195             break;
196     }
197 }
198 } // namespace OHOS::Ace::Framework
199