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_ANIMATION_PAGE_TRANSITION_LISTENER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_PAGE_TRANSITION_LISTENER_H
18 
19 #include <unordered_map>
20 
21 #include "base/log/log.h"
22 #include "base/memory/referenced.h"
23 #include "base/utils/event_callback.h"
24 
25 namespace OHOS::Ace {
26 
27 // Notify Event when stage handle page event in PostFlush at the same vsync when stage's performBuild happens.
28 // Because in PushPage scene, the page transition of the Stage needs to know whether this jump has shared transition.
29 // Further, shared transition needs to know dest page's size after performLayout, So Notify this Event in
30 // PostFlush.
31 enum class TransitionEvent {
32     POP_START,
33     POP_END,
34     PUSH_START,
35     PUSH_END,
36 };
37 
38 class PageElement;
39 
40 class PageTransitionListenable {
41 public:
42     using Callback = EventCallback<void(
43         const TransitionEvent& event, const WeakPtr<PageElement>& pageIn, const WeakPtr<PageElement>& pageOut)>;
44     using CallbackFuncType = Callback::FunctionType;
45 
46     PageTransitionListenable() = default;
47     virtual ~PageTransitionListenable() = default;
48 
AddPageTransitionListener(const Callback & callback)49     void AddPageTransitionListener(const Callback& callback)
50     {
51         callbacks_.emplace(callback.GetId(), callback);
52     }
53 
AddPageTransitionListener(const CallbackFuncType & funcObject)54     typename Callback::IdType AddPageTransitionListener(const CallbackFuncType& funcObject)
55     {
56         Callback callback(funcObject);
57         AddPageTransitionListener(callback);
58         return callback.GetId();
59     }
60 
RemovePageTransitionListener(const Callback & callback)61     void RemovePageTransitionListener(const Callback& callback)
62     {
63         callbacks_.erase(callback.GetId());
64     }
65 
RemovePageTransitionListener(typename Callback::IdType id)66     void RemovePageTransitionListener(typename Callback::IdType id)
67     {
68         callbacks_.erase(id);
69     }
70 
ClearPageTransitionListeners()71     void ClearPageTransitionListeners()
72     {
73         callbacks_.clear();
74     }
75 
NotifyPageTransitionListeners(const TransitionEvent event,const WeakPtr<PageElement> & pageIn,const WeakPtr<PageElement> & pageOut)76     void NotifyPageTransitionListeners(
77         const TransitionEvent event, const WeakPtr<PageElement>& pageIn, const WeakPtr<PageElement>& pageOut) const
78     {
79         for (auto&& [id, callback] : callbacks_) {
80             if (callback) {
81                 callback(event, pageIn, pageOut);
82             }
83         }
84     }
85 
86 private:
87     std::unordered_map<typename Callback::IdType, Callback> callbacks_;
88 };
89 } // namespace OHOS::Ace
90 
91 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_PAGE_TRANSITION_LISTENER_H
92