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 
17 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_TIMEOUT_RECOGNIZER_H
18 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_TIMEOUT_RECOGNIZER_H
19 
20 #include "core/gestures/multi_fingers_recognizer.h"
21 
22 #include <set>
23 #include <chrono>
24 
25 namespace OHOS::Ace {
26 class SingleChildGestureRecognizer : public MultiFingersRecognizer {
27     DECLARE_ACE_TYPE(SingleChildGestureRecognizer, GestureRecognizer);
28 
29 public:
30     explicit SingleChildGestureRecognizer(WeakPtr<PipelineBase> context);
31     SingleChildGestureRecognizer(WeakPtr<PipelineBase> context, RefPtr<GestureRecognizer> child);
32     ~SingleChildGestureRecognizer();
33 
34     void SetChild(RefPtr<GestureRecognizer> recognizer);
35     bool ReconcileFrom(const RefPtr<GestureRecognizer>& recognizer) override;
36 
37     void AddToReferee(size_t touchId, const RefPtr<GestureRecognizer>& recognizer) override;
38     void DelFromReferee(size_t touchId, const RefPtr<GestureRecognizer>& recognizer) override;
39 
40 protected:
41     RefPtr<GestureRecognizer> GetChild() const;
42 
43 private:
44     RefPtr<GestureRecognizer> child_;
45     WeakPtr<PipelineBase> context_;
46 };
47 
48 class TimeoutRecognizer : public SingleChildGestureRecognizer {
49     DECLARE_ACE_TYPE(TimeoutRecognizer, SingleChildGestureRecognizer);
50 public:
51     explicit TimeoutRecognizer(WeakPtr<PipelineBase> context, std::chrono::duration<float> timeout);
52     TimeoutRecognizer(WeakPtr<PipelineBase> context, RefPtr<GestureRecognizer> child,
53         std::chrono::duration<float> timeout);
54 
55     void OnAccepted(size_t touchId) override;
56 
57     void OnRejected(size_t touchId) override;
58 
59     bool ReconcileFrom(const RefPtr<GestureRecognizer>& recognizer) override;
60 
61     void BatchAdjudicate(
62         const std::set<size_t>& touchIds,
63         const RefPtr<GestureRecognizer>& recognizer,
64         GestureDisposal disposal) override;
65 
66     void HandleTouchDownEvent(const TouchEvent& event) override;
67     void HandleTouchUpEvent(const TouchEvent& event) override;
68     void HandleTouchMoveEvent(const TouchEvent& event) override;
69     void HandleTouchCancelEvent(const TouchEvent& event) override;
70 
71     void HandleTouchDownEvent(const AxisEvent& event) override;
72     void HandleTouchUpEvent(const AxisEvent& event) override;
73     void HandleTouchMoveEvent(const AxisEvent& event) override;
74     void HandleTouchCancelEvent(const AxisEvent& event) override;
75 
76 private:
77     bool CheckTimeout(TimeStamp time);
78 
79 private:
80     WeakPtr<PipelineBase> context_;
81     std::chrono::duration<float> timeout_;
82     std::set<size_t> touchIds_;
83     std::optional<TimeStamp> start_;
84 };
85 } // namespace OHOS::Ace
86 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_TIMEOUT_RECOGNIZER_H
87 
88