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