1 /* 2 * Copyright (c) 2021 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_REFEREE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_REFEREE_H 18 19 #include <list> 20 #include <unordered_map> 21 22 #include "base/memory/ace_type.h" 23 #include "base/utils/singleton.h" 24 25 namespace OHOS::Ace { 26 27 class GestureRecognizer; 28 29 enum class GestureDisposal { 30 ACCEPT = 0, 31 REJECT, 32 PENDING, 33 }; 34 35 class GestureScope { 36 public: GestureScope(size_t touchId)37 explicit GestureScope(size_t touchId) : touchId_(touchId) {} 38 ~GestureScope() = default; 39 GestureScope(GestureScope&&) = default; 40 GestureScope& operator=(GestureScope&&) = default; 41 42 void AddMember(const RefPtr<GestureRecognizer>& recognizer); 43 void DelMember(const RefPtr<GestureRecognizer>& recognizer); 44 45 void ForceClose(); 46 47 void HandleGestureDisposal(const RefPtr<GestureRecognizer>& recognizer, GestureDisposal disposal); 48 IsEmpty()49 bool IsEmpty() const 50 { 51 return highRecognizers_.empty() && lowRecognizers_.empty() && parallelRecognizers_.empty(); 52 } 53 54 bool IsPending() const; 55 56 private: 57 bool Existed(const RefPtr<GestureRecognizer>& recognizer); 58 const std::list<WeakPtr<GestureRecognizer>>& GetMembersByRecognizer(const RefPtr<GestureRecognizer>& recognizer); 59 bool CheckNeedBlocked(const RefPtr<GestureRecognizer>& recognizer); 60 void AcceptGesture(const RefPtr<GestureRecognizer>& recognizer); 61 void UnBlockGesture(std::list<WeakPtr<GestureRecognizer>>& members); 62 void HandleParallelDisposal(const RefPtr<GestureRecognizer>& recognizer, GestureDisposal disposal); 63 void HandleAcceptDisposal(const RefPtr<GestureRecognizer>& recognizer); 64 void HandlePendingDisposal(const RefPtr<GestureRecognizer>& recognizer); 65 void HandleRejectDisposal(const RefPtr<GestureRecognizer>& recognizer); 66 void RemoveAndUnBlockGesture(bool isPrevPending, const WeakPtr<GestureRecognizer>& recognizer); 67 68 size_t touchId_ = 0; 69 70 std::list<WeakPtr<GestureRecognizer>> highRecognizers_; 71 std::list<WeakPtr<GestureRecognizer>> lowRecognizers_; 72 std::list<WeakPtr<GestureRecognizer>> parallelRecognizers_; 73 }; 74 75 class GestureReferee : public Singleton<GestureReferee> { 76 public: 77 // Each gesture recognizer should add itself to the gesture scope at the beginning of the gesture sequence 78 // (touch down event) for gesture adjudicating. 79 void AddGestureRecognizer(size_t touchId, const RefPtr<GestureRecognizer>& recognizer); 80 81 // In multi finger recognizers, touch up one finger does not result to the whole gesture failed. Recognizer should 82 // remove the recognizer out of the referee. 83 void DelGestureRecognizer(size_t touchId, const RefPtr<GestureRecognizer>& recognizer); 84 85 // Try to clean gesture scope when receive cancel event. 86 void CleanGestureScope(size_t touchId); 87 88 // Called by the gesture recognizer when the gesture recognizer has completed the recognition of the gesture (accept 89 // or reject) 90 void Adjudicate(size_t touchId, const RefPtr<GestureRecognizer>& recognizer, GestureDisposal disposal); 91 92 private: 93 // Stores gesture recognizer collection according to Id. 94 std::unordered_map<size_t, GestureScope> gestureScopes_; 95 }; 96 97 } // namespace OHOS::Ace 98 99 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_GESTURES_GESTURE_REFEREE_H 100