1 /*
2  * Copyright (c) 2022 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_PROCESSOR_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_PROCESSOR_H
18 
19 #include <stack>
20 
21 #include "base/memory/ace_type.h"
22 #include "core/components_ng/gestures/gesture_group.h"
23 #include "core/gestures/gesture_group.h"
24 #include "core/gestures/single_child_gesture.h"
25 
26 namespace OHOS::Ace {
27 
28 class GestureProcessor : public virtual AceType {
29     DECLARE_ACE_TYPE(GestureProcessor, AceType)
30 public:
31     GestureProcessor() = default;
32     ~GestureProcessor() override = default;
33 
SetPriority(GesturePriority priority)34     void SetPriority(GesturePriority priority)
35     {
36         priority_ = priority;
37     }
38 
GetPriority()39     GesturePriority GetPriority()
40     {
41         return priority_;
42     }
43 
SetGestureMask(GestureMask gestureMask)44     void SetGestureMask(GestureMask gestureMask)
45     {
46         gestureMask_ = gestureMask;
47     }
48 
GetGestureMask()49     GestureMask GetGestureMask()
50     {
51         return gestureMask_;
52     }
53 
PushGesture(const RefPtr<Gesture> & gesture)54     void PushGesture(const RefPtr<Gesture>& gesture)
55     {
56         gestureStack_.push(gesture);
57     }
58 
PopGesture()59     void PopGesture()
60     {
61         if (gestureStack_.size() <= 1) {
62             return;
63         }
64 
65         auto gesture = gestureStack_.top();
66         gestureStack_.pop();
67 
68         auto gestureGroup = AceType::DynamicCast<GestureGroup>(gestureStack_.top());
69         if (gestureGroup) {
70             gestureGroup->AddGesture(gesture);
71         }
72 
73         auto container = AceType::DynamicCast<SingleChildGesture>(gestureStack_.top());
74         if (container) {
75             container->SetChild(std::move(gesture));
76         }
77     }
78 
TopGesture()79     RefPtr<Gesture> TopGesture()
80     {
81         if (gestureStack_.empty()) {
82             return nullptr;
83         }
84         return gestureStack_.top();
85     }
86 
FinishGesture()87     RefPtr<Gesture> FinishGesture()
88     {
89         if (gestureStack_.empty()) {
90             return nullptr;
91         }
92 
93         auto gesture = gestureStack_.top();
94         gestureStack_.pop();
95 
96         return gesture;
97     }
98 
PushGestureNG(const RefPtr<NG::Gesture> & gesture)99     void PushGestureNG(const RefPtr<NG::Gesture>& gesture)
100     {
101         gestureStackNG_.push(gesture);
102     }
103 
PopGestureNG()104     void PopGestureNG()
105     {
106         if (gestureStackNG_.size() <= 1) {
107             return;
108         }
109 
110         auto gesture = gestureStackNG_.top();
111         gestureStackNG_.pop();
112 
113         auto gestureGroup = AceType::DynamicCast<NG::GestureGroup>(gestureStackNG_.top());
114         if (gestureGroup) {
115             gestureGroup->AddGesture(gesture);
116         }
117     }
118 
TopGestureNG()119     RefPtr<NG::Gesture> TopGestureNG()
120     {
121         if (gestureStackNG_.empty()) {
122             return nullptr;
123         }
124         return gestureStackNG_.top();
125     }
126 
FinishGestureNG()127     RefPtr<NG::Gesture> FinishGestureNG()
128     {
129         if (gestureStackNG_.empty()) {
130             return nullptr;
131         }
132 
133         auto gesture = gestureStackNG_.top();
134         gestureStackNG_.pop();
135 
136         return gesture;
137     }
138 
139 private:
140     GesturePriority priority_ = GesturePriority::Low;
141     GestureMask gestureMask_ = GestureMask::Normal;
142     std::stack<RefPtr<Gesture>> gestureStack_;
143     std::stack<RefPtr<NG::Gesture>> gestureStackNG_;
144 };
145 } // namespace OHOS::Ace
146 
147 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_PROCESSOR_H
148