1 /*
2  * Copyright (c) 2023 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_ANIMATION_GEOMETRY_TRANSITION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_ANIMATION_GEOMETRY_TRANSITION_H
18 
19 #include "base/memory/ace_type.h"
20 #include "base/memory/referenced.h"
21 #include "base/geometry/ng/rect_t.h"
22 #include "core/components/common/properties/animation_option.h"
23 
24 namespace OHOS::Ace::NG {
25 class FrameNode;
26 class LayoutWrapper;
27 class LayoutProperty;
28 
29 class GeometryTransition : public AceType {
30     DECLARE_ACE_TYPE(GeometryTransition, AceType);
31 
32 public:
33     explicit GeometryTransition(const std::string& id,
34         bool followWithoutTransition = false, bool doRegisterSharedTransition = true);
35     ~GeometryTransition() override = default;
36 
37     bool IsNodeInAndActive(const WeakPtr<FrameNode>& frameNode) const;
38     bool IsNodeOutAndActive(const WeakPtr<FrameNode>& frameNode) const;
39     bool IsRunning(const WeakPtr<FrameNode>& frameNode) const;
40     bool IsInAndOutEmpty() const;
41     bool IsInAndOutValid() const;
42     std::string ToString() const;
GetId()43     std::string GetId() const
44     {
45         return id_;
46     }
47 
GetFollowWithoutTransition()48     bool GetFollowWithoutTransition() const
49     {
50         return followWithoutTransition_;
51     }
52 
GetDoRegisterSharedTransition()53     bool GetDoRegisterSharedTransition() const
54     {
55         return doRegisterSharedTransition_;
56     }
57 
58     void Build(const WeakPtr<FrameNode>& frameNode, bool isNodeIn);
59     bool Update(const WeakPtr<FrameNode>& which, const WeakPtr<FrameNode>& value);
60     void OnReSync(const WeakPtr<FrameNode>& trigger = nullptr, const AnimationOption& option = AnimationOption());
61     bool OnAdditionalLayout(const WeakPtr<FrameNode>& frameNode);
62     bool OnFollowWithoutTransition(std::optional<bool> direction = std::nullopt);
63     void WillLayout(const RefPtr<LayoutWrapper>& layoutWrapper);
64     void DidLayout(const RefPtr<LayoutWrapper>& layoutWrapper);
65 
66 private:
67     enum class State {
68         IDLE,
69         ACTIVE,
70         IDENTITY,
71     };
72 
73     bool IsNodeInAndIdentity(const WeakPtr<FrameNode>& frameNode) const;
74     void SwapInAndOut(bool condition);
75     std::pair<RefPtr<FrameNode>, RefPtr<FrameNode>> GetMatchedPair(bool isNodeIn) const;
76     void RecordOutNodeFrame();
77     void MarkLayoutDirty(const RefPtr<FrameNode>& node, int32_t layoutPriority = 0);
78     void ModifyLayoutConstraint(const RefPtr<LayoutWrapper>& layoutWrapper, bool isNodeIn);
79     void SyncGeometry(bool isNodeIn);
80     bool IsParent(const WeakPtr<FrameNode>& parent, const WeakPtr<FrameNode>& child) const;
81     void RecordAnimationOption(const WeakPtr<FrameNode>& trigger, const AnimationOption& option);
82     RectF GetNodeAbsFrameRect(const RefPtr<FrameNode>& node, std::optional<OffsetF> parentPos = std::nullopt) const;
83     void AnimateWithSandBox(const OffsetF& inNodeParentPos, bool inNodeParentHasScales,
84         const std::function<void()>& propertyCallback, const AnimationOption& option);
85     void SyncGeometryPropertiesAfterLayout(const RefPtr<FrameNode>& syncNode);
86 
87     static constexpr int32_t RESYNC_DURATION = 1;
88     std::string id_;
89     WeakPtr<FrameNode> inNode_;
90     WeakPtr<FrameNode> outNode_;
91     State state_ = State::IDLE;
92     bool hasInAnim_ = false;
93     bool hasOutAnim_ = false;
94     bool isSynced_ = false;
95     SizeF inNodeActiveFrameSize_;
96     std::optional<RectF> inNodeAbsRect_;
97     std::optional<RectF> outNodeTargetAbsRect_;
98     OffsetF outNodePos_;
99     SizeF outNodeSize_;
100     OffsetF outNodeParentPos_;
101     bool outNodeParentHasScales_ = false;
102     RefPtr<FrameNode> holder_;
103     bool followWithoutTransition_ = false;
104     bool doRegisterSharedTransition_ = true;
105     // static node means the node on the tree without transition
106     std::optional<RectF> staticNodeAbsRect_;
107     RefPtr<LayoutProperty> layoutPropertyIn_;
108     RefPtr<LayoutProperty> layoutPropertyOut_;
109     AnimationOption animationOption_;
110 
111     ACE_DISALLOW_COPY_AND_MOVE(GeometryTransition);
112 };
113 } // namespace OHOS::Ace::NG
114 
115 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_ANIMATION_GEOMETRY_TRANSITION_H