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_COMPONENTS_NG_MANAGER_SHARED_OVERLAY_SHARED_TRANSITION_EFFECT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_SHARED_OVERLAY_SHARED_TRANSITION_EFFECT_H
18 
19 #include "base/memory/ace_type.h"
20 #include "base/memory/referenced.h"
21 #include "base/utils/noncopyable.h"
22 #include "core/animation/animator.h"
23 #include "core/components/common/layout/constants.h"
24 #include "core/components/common/properties/shared_transition_option.h"
25 #include "core/components_ng/property/measure_property.h"
26 
27 namespace OHOS::Ace::NG {
28 
29 class FrameNode;
30 
31 class ACE_EXPORT SharedTransitionEffect : public AceType {
32     DECLARE_ACE_TYPE(SharedTransitionEffect, AceType);
33 
34 public:
35     SharedTransitionEffect(const ShareId& shareId, const std::shared_ptr<SharedTransitionOption>& sharedOption);
36     ~SharedTransitionEffect() override = default;
37 
38     virtual SharedTransitionEffectType GetType() const = 0;
39     virtual bool Allow() const = 0;
40     virtual bool CreateAnimation() = 0;
41     virtual const WeakPtr<FrameNode>& GetPassengerNode() const = 0;
42 
GetDestSharedNode()43     const WeakPtr<FrameNode>& GetDestSharedNode() const
44     {
45         return dest_;
46     }
GetSrcSharedNode()47     const WeakPtr<FrameNode>& GetSrcSharedNode() const
48     {
49         return src_;
50     }
GetController()51     const RefPtr<Animator>& GetController() const
52     {
53         return controller_;
54     }
GetShareId()55     ShareId GetShareId() const
56     {
57         return shareId_;
58     }
GetZIndex()59     int32_t GetZIndex() const
60     {
61         return option_ ? option_->zIndex : 0;
62     }
StopPlayingEffect()63     void StopPlayingEffect() const
64     {
65         if (controller_->IsRunning()) {
66             TAG_LOGD(AceLogTag::ACE_ANIMATION, "Stop playing transition effect, shareId:%{public}s", shareId_.c_str());
67             controller_->Finish();
68         }
69     }
GetOption()70     const std::shared_ptr<SharedTransitionOption>& GetOption() const
71     {
72         return option_;
73     }
GetPassengerHolder()74     const WeakPtr<FrameNode>& GetPassengerHolder() const
75     {
76         return passengerHolder_;
77     }
GetPassengerInitPos()78     const std::optional<OffsetT<Dimension>>& GetPassengerInitPos() const
79     {
80         return initialPosition_;
81     }
GetPassengerInitMargin()82     const std::optional<MarginProperty>& GetPassengerInitMargin() const
83     {
84         return initialMargin_;
85     }
GetPassengerInitFrameOffset()86     OffsetF GetPassengerInitFrameOffset() const
87     {
88         return initialFrameOffset_;
89     }
GetPassengerInitZIndex()90     const std::optional<int32_t>& GetPassengerInitZIndex() const
91     {
92         return initialZIndex_;
93     }
GetPassengerCurrentFocused()94     bool GetPassengerCurrentFocused() const
95     {
96         return isCurrentFocused_;
97     }
SetSharedNode(const WeakPtr<FrameNode> & src,const WeakPtr<FrameNode> & dest)98     void SetSharedNode(const WeakPtr<FrameNode>& src, const WeakPtr<FrameNode>& dest)
99     {
100         dest_ = dest;
101         src_ = src;
102     }
SetPassengerHolder(const WeakPtr<FrameNode> & holder)103     void SetPassengerHolder(const WeakPtr<FrameNode>& holder)
104     {
105         passengerHolder_ = holder;
106     }
SetPassengerInitPos(const std::optional<OffsetT<Dimension>> & position)107     void SetPassengerInitPos(const std::optional<OffsetT<Dimension>>& position)
108     {
109         initialPosition_ = position;
110     }
SetPassengerInitMargin(const std::optional<MarginProperty> & margin)111     void SetPassengerInitMargin(const std::optional<MarginProperty>& margin)
112     {
113         initialMargin_ = margin;
114     }
SetPassengerInitFrameOffset(const OffsetF & offset)115     void SetPassengerInitFrameOffset(const OffsetF& offset)
116     {
117         initialFrameOffset_ = offset;
118     }
SetPassengerInitZIndex(const std::optional<int32_t> & zIndex)119     void SetPassengerInitZIndex(const std::optional<int32_t>& zIndex)
120     {
121         initialZIndex_ = zIndex;
122     }
SetPassengerCurrentFocused(bool isFocused)123     void SetPassengerCurrentFocused(bool isFocused)
124     {
125         isCurrentFocused_ = isFocused;
126     }
127     void PerformFinishCallback();
128     bool ApplyAnimation();
129     static RefPtr<SharedTransitionEffect> GetSharedTransitionEffect(
130         const ShareId& shareId, const std::shared_ptr<SharedTransitionOption>& sharedOption);
131 
132 protected:
133     bool CreateOpacityAnimation(
134         float startOpacity, float endOpacity, float finalOpacity, const WeakPtr<FrameNode>& node);
135     WeakPtr<FrameNode> dest_;
136     WeakPtr<FrameNode> src_;
137     // placeholder used to hold the position after the passenger node is removed from the parent node
138     WeakPtr<FrameNode> passengerHolder_;
139     RefPtr<Animator> controller_;
140     ShareId shareId_;
141     std::shared_ptr<SharedTransitionOption> option_;
142     std::optional<OffsetT<Dimension>> initialPosition_;
143     std::optional<MarginProperty> initialMargin_;
144     OffsetF initialFrameOffset_;
145     std::optional<int32_t> initialZIndex_;
146     std::list<std::function<void()>> finishCallbacks_;
147     bool isCurrentFocused_ = false;
148 };
149 
150 class SharedTransitionExchange : public SharedTransitionEffect {
151     DECLARE_ACE_TYPE(SharedTransitionExchange, SharedTransitionEffect);
152 
153 public:
154     SharedTransitionExchange(const ShareId& shareId, const std::shared_ptr<SharedTransitionOption>& sharedOption);
155     ~SharedTransitionExchange() override = default;
156 
GetType()157     SharedTransitionEffectType GetType() const override
158     {
159         return SharedTransitionEffectType::SHARED_EFFECT_EXCHANGE;
160     }
GetPassengerNode()161     const WeakPtr<FrameNode>& GetPassengerNode() const override
162     {
163         return src_;
164     }
165     bool Allow() const override;
166     bool CreateAnimation() override;
GetInitialDestVisible()167     VisibleType GetInitialDestVisible() const
168     {
169         return destVisible_;
170     }
SetInitialDestVisible(VisibleType type)171     void SetInitialDestVisible(VisibleType type)
172     {
173         destVisible_ = type;
174     }
175     bool SetVisibleToDest(VisibleType type);
176     void DestRequestDefaultFocus();
177 
178 private:
179     bool CreateTranslateAnimation(const RefPtr<FrameNode>& src, const RefPtr<FrameNode>& dest);
180     bool CreateSizeAnimation(const RefPtr<FrameNode>& src, const RefPtr<FrameNode>& dest);
181     bool CreateOpacityAnimation(const RefPtr<FrameNode>& src, const RefPtr<FrameNode>& dest);
182     VisibleType destVisible_ = VisibleType::VISIBLE;
183 };
184 
185 class SharedTransitionStatic : public SharedTransitionEffect {
186     DECLARE_ACE_TYPE(SharedTransitionStatic, SharedTransitionEffect);
187 
188 public:
189     SharedTransitionStatic(const ShareId& shareId, const std::shared_ptr<SharedTransitionOption>& sharedOption);
190     ~SharedTransitionStatic() override = default;
191 
GetType()192     SharedTransitionEffectType GetType() const override
193     {
194         return SharedTransitionEffectType::SHARED_EFFECT_STATIC;
195     }
196     const WeakPtr<FrameNode>& GetPassengerNode() const override;
197     bool Allow() const override;
198     bool CreateAnimation() override;
199 };
200 
201 } // namespace OHOS::Ace::NG
202 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_SHARED_OVERLAY_SHARED_TRANSITION_EFFECT_H
203