1 /*
2  * Copyright (c) 2021-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_ANIMATION_SHARED_TRANSITION_EFFECT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SHARED_TRANSITION_EFFECT_H
18 
19 #include "base/memory/ace_type.h"
20 #include "core/animation/page_transition_listener.h"
21 #include "core/components/common/properties/tween_option.h"
22 #include "core/pipeline/base/component.h"
23 #include "core/pipeline/base/element.h"
24 
25 namespace OHOS::Ace {
26 
27 class SharedTransitionElement;
28 class OverlayElement;
29 class Animator;
30 class TweenElement;
31 
32 enum class SharedTransitionEffectType {
33     SHARED_EFFECT_STATIC,
34     SHARED_EFFECT_EXCHANGE,
35 };
36 
37 class ACE_EXPORT SharedTransitionEffect : public AceType {
38     DECLARE_ACE_TYPE(SharedTransitionEffect, AceType);
39 
40 public:
41     explicit SharedTransitionEffect(const ShareId& shareId, SharedTransitionEffectType type);
42     ~SharedTransitionEffect() override = default;
GetType()43     SharedTransitionEffectType GetType() const
44     {
45         return type_;
46     }
SetSharedElement(const WeakPtr<SharedTransitionElement> & src,const WeakPtr<SharedTransitionElement> & dest)47     void SetSharedElement(const WeakPtr<SharedTransitionElement>& src, const WeakPtr<SharedTransitionElement>& dest)
48     {
49         dest_ = dest;
50         src_ = src;
51     }
GetDestSharedElement()52     const WeakPtr<SharedTransitionElement>& GetDestSharedElement() const
53     {
54         return dest_;
55     }
GetSrcSharedElement()56     const WeakPtr<SharedTransitionElement>& GetSrcSharedElement() const
57     {
58         return src_;
59     }
GetAnimator()60     const RefPtr<Animator>& GetAnimator() const
61     {
62         return controller_;
63     }
GetShareId()64     const std::string& GetShareId() const
65     {
66         return shareId_;
67     }
setCurrentSharedElement(const WeakPtr<SharedTransitionElement> & current)68     void setCurrentSharedElement(const WeakPtr<SharedTransitionElement>& current)
69     {
70         currentWorking_ = current;
71     }
GetCurrentSharedElement()72     const WeakPtr<SharedTransitionElement>& GetCurrentSharedElement() const
73     {
74         return currentWorking_;
75     }
76     virtual bool CreateAnimation(TweenOption& option, TransitionEvent event, bool isLazy) = 0;
77     virtual bool ApplyAnimation(RefPtr<OverlayElement>& overlay, RefPtr<Animator>& controller,
78         TweenOption& option, TransitionEvent event) = 0;
79     virtual bool Allow(TransitionEvent event) = 0;
80     static RefPtr<SharedTransitionEffect> GetSharedTransitionEffect(
81         SharedTransitionEffectType effect, const ShareId& shareId);
82 
83 protected:
84     bool CheckIn(TransitionEvent event, WeakPtr<SharedTransitionElement>& sharedWeak, Offset& ticket);
85     bool TakeOff(TransitionEvent event, RefPtr<OverlayElement>& overlay, WeakPtr<SharedTransitionElement>& sharedWeak,
86         const Offset& ticket, TweenOption& option);
87     bool TakeOffTween(const RefPtr<Element>& tweenElement, const RefPtr<Component>& passengerComponent,
88         const RefPtr<Element>& passengerElement, TweenOption& option);
89 
90     ShareId shareId_;
91     const SharedTransitionEffectType type_;
92     WeakPtr<SharedTransitionElement> dest_;
93     WeakPtr<SharedTransitionElement> src_;
94     WeakPtr<SharedTransitionElement> currentWorking_;
95     RefPtr<Animator> controller_;
96     WeakPtr<TweenElement> tweenSeatElement_;
97 };
98 
99 class SharedTransitionExchange : public SharedTransitionEffect {
100     DECLARE_ACE_TYPE(SharedTransitionExchange, SharedTransitionEffect);
101 
102 public:
SharedTransitionExchange(const ShareId & shareId)103     explicit SharedTransitionExchange(const ShareId& shareId)
104         : SharedTransitionEffect(shareId, SharedTransitionEffectType::SHARED_EFFECT_EXCHANGE) {}
105     ~SharedTransitionExchange() override = default;
106     bool CreateAnimation(TweenOption& option, TransitionEvent event, bool isLazy) override;
107     bool ApplyAnimation(RefPtr<OverlayElement>& overlay, RefPtr<Animator>& controller, TweenOption& option,
108         TransitionEvent event) override;
109     bool Allow(TransitionEvent event) override;
110 
111 private:
112     bool CreateTranslateAnimation(TweenOption& option, TransitionEvent event, bool calledByLazyLoad);
113     bool CreateSizeAnimation(TweenOption& option, TransitionEvent event, bool isLazy);
114     bool CreateOpacityAnimation(TweenOption& option, TransitionEvent event, bool isLazy);
115     void AddLazyLoadCallback(TransitionEvent event);
116     bool autoWidth_ = true;
117     bool autoHeight_ = true;
118     bool autoTranslate_ = true;
119 };
120 
121 class SharedTransitionStatic : public SharedTransitionEffect {
122     DECLARE_ACE_TYPE(SharedTransitionStatic, SharedTransitionEffect);
123 
124 public:
SharedTransitionStatic(const ShareId & shareId)125     explicit SharedTransitionStatic(const ShareId& shareId)
126         : SharedTransitionEffect(shareId, SharedTransitionEffectType::SHARED_EFFECT_STATIC) {}
127     ~SharedTransitionStatic() override = default;
128     bool CreateAnimation(TweenOption& option, TransitionEvent event, bool isLazy) override;
129     // the dest page and source page elements are in effect
130     bool ApplyAnimation(RefPtr<OverlayElement>& overlay, RefPtr<Animator>& controller, TweenOption& option,
131         TransitionEvent event) override;
132     bool Allow(TransitionEvent event) override;
133 
134 private:
135     void AddLazyLoadCallback();
136 };
137 
138 } // namespace OHOS::Ace
139 
140 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SHARED_TRANSITION_EFFECT_H
141