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