• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             if (SystemProperties::GetDebugEnabled()) {
67                 TAG_LOGI(
68                     AceLogTag::ACE_ANIMATION, "Stop playing transition effect, shareId:%{public}s", shareId_.c_str());
69             }
70             controller_->Finish();
71         }
72     }
GetOption()73     const std::shared_ptr<SharedTransitionOption>& GetOption() const
74     {
75         return option_;
76     }
GetPassengerHolder()77     const WeakPtr<FrameNode>& GetPassengerHolder() const
78     {
79         return passengerHolder_;
80     }
GetPassengerInitPos()81     const std::optional<OffsetT<Dimension>>& GetPassengerInitPos() const
82     {
83         return initialPosition_;
84     }
GetPassengerInitMargin()85     const std::optional<MarginProperty>& GetPassengerInitMargin() const
86     {
87         return initialMargin_;
88     }
GetPassengerInitFrameOffset()89     OffsetF GetPassengerInitFrameOffset() const
90     {
91         return initialFrameOffset_;
92     }
GetPassengerInitZIndex()93     const std::optional<int32_t>& GetPassengerInitZIndex() const
94     {
95         return initialZIndex_;
96     }
GetPassengerCurrentFocused()97     bool GetPassengerCurrentFocused() const
98     {
99         return isCurrentFocused_;
100     }
SetSharedNode(const WeakPtr<FrameNode> & src,const WeakPtr<FrameNode> & dest)101     void SetSharedNode(const WeakPtr<FrameNode>& src, const WeakPtr<FrameNode>& dest)
102     {
103         dest_ = dest;
104         src_ = src;
105     }
SetPassengerHolder(const WeakPtr<FrameNode> & holder)106     void SetPassengerHolder(const WeakPtr<FrameNode>& holder)
107     {
108         passengerHolder_ = holder;
109     }
SetPassengerInitPos(const std::optional<OffsetT<Dimension>> & position)110     void SetPassengerInitPos(const std::optional<OffsetT<Dimension>>& position)
111     {
112         initialPosition_ = position;
113     }
SetPassengerInitMargin(const std::optional<MarginProperty> & margin)114     void SetPassengerInitMargin(const std::optional<MarginProperty>& margin)
115     {
116         initialMargin_ = margin;
117     }
SetPassengerInitFrameOffset(const OffsetF & offset)118     void SetPassengerInitFrameOffset(const OffsetF& offset)
119     {
120         initialFrameOffset_ = offset;
121     }
SetPassengerInitZIndex(const std::optional<int32_t> & zIndex)122     void SetPassengerInitZIndex(const std::optional<int32_t>& zIndex)
123     {
124         initialZIndex_ = zIndex;
125     }
SetPassengerCurrentFocused(bool isFocused)126     void SetPassengerCurrentFocused(bool isFocused)
127     {
128         isCurrentFocused_ = isFocused;
129     }
130     void PerformFinishCallback();
131     bool ApplyAnimation();
132     static RefPtr<SharedTransitionEffect> GetSharedTransitionEffect(
133         const ShareId& shareId, const std::shared_ptr<SharedTransitionOption>& sharedOption);
134 
135 protected:
136     bool CreateOpacityAnimation(
137         float startOpacity, float endOpacity, float finalOpacity, const WeakPtr<FrameNode>& node);
138     WeakPtr<FrameNode> dest_;
139     WeakPtr<FrameNode> src_;
140     // placeholder used to hold the position after the passenger node is removed from the parent node
141     WeakPtr<FrameNode> passengerHolder_;
142     RefPtr<Animator> controller_;
143     ShareId shareId_;
144     std::shared_ptr<SharedTransitionOption> option_;
145     std::optional<OffsetT<Dimension>> initialPosition_;
146     std::optional<MarginProperty> initialMargin_;
147     OffsetF initialFrameOffset_;
148     std::optional<int32_t> initialZIndex_;
149     std::list<std::function<void()>> finishCallbacks_;
150     bool isCurrentFocused_ = false;
151 };
152 
153 class SharedTransitionExchange : public SharedTransitionEffect {
154     DECLARE_ACE_TYPE(SharedTransitionExchange, SharedTransitionEffect);
155 
156 public:
157     SharedTransitionExchange(const ShareId& shareId, const std::shared_ptr<SharedTransitionOption>& sharedOption);
158     ~SharedTransitionExchange() override = default;
159 
GetType()160     SharedTransitionEffectType GetType() const override
161     {
162         return SharedTransitionEffectType::SHARED_EFFECT_EXCHANGE;
163     }
GetPassengerNode()164     const WeakPtr<FrameNode>& GetPassengerNode() const override
165     {
166         return src_;
167     }
168     bool Allow() const override;
169     bool CreateAnimation() override;
GetInitialDestVisible()170     VisibleType GetInitialDestVisible() const
171     {
172         return destVisible_;
173     }
SetInitialDestVisible(VisibleType type)174     void SetInitialDestVisible(VisibleType type)
175     {
176         destVisible_ = type;
177     }
178     bool SetVisibleToDest(VisibleType type);
179     void DestRequestDefaultFocus();
180 
181 private:
182     bool CreateTranslateAnimation(const RefPtr<FrameNode>& src, const RefPtr<FrameNode>& dest);
183     bool CreateSizeAnimation(const RefPtr<FrameNode>& src, const RefPtr<FrameNode>& dest);
184     bool CreateOpacityAnimation(const RefPtr<FrameNode>& src, const RefPtr<FrameNode>& dest);
185     VisibleType destVisible_ = VisibleType::VISIBLE;
186 };
187 
188 class SharedTransitionStatic : public SharedTransitionEffect {
189     DECLARE_ACE_TYPE(SharedTransitionStatic, SharedTransitionEffect);
190 
191 public:
192     SharedTransitionStatic(const ShareId& shareId, const std::shared_ptr<SharedTransitionOption>& sharedOption);
193     ~SharedTransitionStatic() override = default;
194 
GetType()195     SharedTransitionEffectType GetType() const override
196     {
197         return SharedTransitionEffectType::SHARED_EFFECT_STATIC;
198     }
199     const WeakPtr<FrameNode>& GetPassengerNode() const override;
200     bool Allow() const override;
201     bool CreateAnimation() override;
202 };
203 
204 } // namespace OHOS::Ace::NG
205 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_MANAGER_SHARED_OVERLAY_SHARED_TRANSITION_EFFECT_H
206