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_COMPONENTS_PAGE_PAGE_ELEMENT_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PAGE_PAGE_ELEMENT_H
18 
19 #include <functional>
20 #include <unordered_map>
21 
22 #include "core/components/shared_transition/shared_transition_element.h"
23 #include "core/pipeline/base/composed_element.h"
24 
25 namespace OHOS::Ace {
26 
27 class SharedTransitionElement;
28 class TransformElement;
29 
30 class PageElement : public ComposedElement, public FocusGroup {
31     DECLARE_ACE_TYPE(PageElement, ComposedElement, FocusGroup);
32 
33 public:
34     using SharedTransitionMap = std::unordered_map<ShareId, WeakPtr<SharedTransitionElement>>;
35     using HiddenCallbackMap = std::unordered_map<int32_t, std::function<void(bool)>>;
36     using CardTransitionMap = std::unordered_map<int32_t, WeakPtr<TransformElement>>;
37 
38     // Use this to store geometryTransition matched renderNodes and animation option
39     struct GeometryTransitionInfo {
40         AnimationOption sharedAnimationOption;
41         WeakPtr<BoxElement> appearElement;
42         WeakPtr<BoxElement> disappearElement;
43         bool isNeedCreate;
44     };
45     using GeometryTransitionMap = std::unordered_map<std::string, GeometryTransitionInfo>;
46     PageElement(int32_t pageId, const std::string& pageUrl, const ComposeId& id);
47     PageElement(int32_t pageId, const std::string& pageUrl, const ComposeId& cardComposeId, const ComposeId& id);
48     ~PageElement();
49 
GetPageId()50     int32_t GetPageId()
51     {
52         return pageId_;
53     }
54 
GetPageUrl()55     std::string GetPageUrl() const
56     {
57         return pageUrl_;
58     }
59 
60     void RemoveSharedTransition(const ShareId& shareId);
61     void AddSharedTransition(const RefPtr<SharedTransitionElement>& shared);
62     const SharedTransitionMap& GetSharedTransitionMap() const;
63 
64     void SetHidden(bool hidden);
65 
RegisterHiddenCallback(std::function<void (bool)> && callback)66     int32_t RegisterHiddenCallback(std::function<void(bool)>&& callback)
67     {
68         if (callback) {
69             hiddenCallbackMap_.emplace(callbackId_, std::move(callback));
70         }
71         return callbackId_++;
72     }
73 
CancelHiddenCallback(int32_t callbackId)74     void CancelHiddenCallback(int32_t callbackId)
75     {
76         hiddenCallbackMap_.erase(callbackId);
77     }
78 
GetCardComposeId()79     const ComposeId& GetCardComposeId() const
80     {
81         return cardComposeId_;
82     }
83 
84     void RemoveCardTransition(int32_t retakeId);
85     void AddCardTransition(const RefPtr<TransformElement>& transform);
86     const CardTransitionMap& GetCardTransitionMap() const;
87 
88     void AddGeometryTransition(const std::string& id, WeakPtr<BoxElement>& boxElement, AnimationOption& option);
89     const GeometryTransitionMap& GetGeometryTransition() const;
90     void RemoveGeometryTransition(const std::string& id);
91     void FinishCreateGeometryTransition(const std::string& id);
92     void Dump() override;
93     int32_t GetComponentsCount();
94 
95 protected:
96     bool RequestNextFocus(bool vertical, bool reverse, const Rect& rect) override;
97 
98 private:
99     int32_t callbackId_ = 0;
100     int32_t pageId_ = -1;
101     std::string pageUrl_;
102     ComposeId cardComposeId_;
103     SharedTransitionMap sharedTransitionElementMap_;
104     HiddenCallbackMap hiddenCallbackMap_;
105     CardTransitionMap cardTransitionMap_;
106     GeometryTransitionMap geometryTransitionMap_;
107 };
108 
109 } // namespace OHOS::Ace
110 
111 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PAGE_PAGE_ELEMENT_H
112