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_BASE_ELEMENT_REGISTER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_BASE_ELEMENT_REGISTER_H 18 19 #include <mutex> 20 #include <unordered_map> 21 #include <unordered_set> 22 #include <list> 23 24 #include "base/memory/referenced.h" 25 #include "frameworks/base/memory/ace_type.h" 26 #include "frameworks/core/components_ng/animation/geometry_transition.h" 27 28 namespace OHOS::Ace::V2 { 29 class ElementProxy; 30 } // namespace OHOS::Ace::V2 31 32 namespace OHOS::Ace::NG { 33 class UINode; 34 class FrameNode; 35 } // namespace OHOS::Ace::NG 36 37 namespace OHOS::Ace { 38 using ElementIdType = int32_t; 39 class Element; 40 41 class ACE_EXPORT ElementRegister { 42 public: 43 static constexpr ElementIdType UndefinedElementId = static_cast<ElementIdType>(-1); 44 45 static ElementRegister* GetInstance(); 46 RefPtr<Element> GetElementById(ElementIdType elementId); 47 RefPtr<V2::ElementProxy> GetElementProxyById(ElementIdType elementId); 48 49 RefPtr<AceType> GetNodeById(ElementIdType elementId); 50 /** 51 * version of GetNodeById(elmtId) function to return an Element of 52 * given class. returns nullptr if Element with this elmtId baddest found 53 * or class mismatch 54 */ 55 template<class E> GetSpecificItemById(ElementIdType elmtId)56 RefPtr<E> GetSpecificItemById(ElementIdType elmtId) 57 { 58 return AceType::DynamicCast<E>(GetNodeById(elmtId)); 59 } 60 61 bool AddElementProxy(const WeakPtr<V2::ElementProxy>& element); 62 bool AddElement(const RefPtr<Element>& element); 63 64 RefPtr<NG::UINode> GetUINodeById(ElementIdType elementId); 65 bool AddUINode(const RefPtr<NG::UINode>& node); 66 67 bool Exists(ElementIdType elementId); 68 69 /** 70 * When a custom node is created from recycle, update its element id. 71 */ 72 void UpdateRecycleElmtId(int32_t oldElmtId, int32_t newElmtId); 73 74 /** 75 * remove Element with given elmtId from the Map 76 * means GetElementById on this elmtId no longer returns an Element 77 * method adds the elmtId to the removed Element Set 78 */ 79 bool RemoveItem(ElementIdType elementId); 80 81 /** 82 * remove Element with given elmtId from the Map 83 * means GetElementById on this elmtId no longer returns an Element 84 * method does NOT add the elmtId to the removed Element Set 85 * Use with caution: e.g. only use when knowing the Element will 86 * be added with new ElementId shortly 87 */ 88 bool RemoveItemSilently(ElementIdType elementId); 89 90 /** 91 * return removed elements set to the caller 92 * should be followed by ClearRemovedElements() call 93 */ 94 std::unordered_set<ElementIdType>& GetRemovedItems(); 95 void ClearRemovedItems(ElementIdType elmtId); 96 97 /** 98 * does a complete reset 99 * clears the Map of Elements and Set of removed Elements 100 */ 101 void Clear(); 102 MakeUniqueId()103 ElementIdType MakeUniqueId() 104 { 105 return nextUniqueElementId_++; 106 } 107 108 RefPtr<NG::GeometryTransition> GetOrCreateGeometryTransition(const std::string& id, 109 const WeakPtr<NG::FrameNode>& frameNode, 110 bool followWithoutTransition = false); 111 void DumpGeometryTransition(); 112 113 void ReSyncGeometryTransition(); 114 115 void AddPendingRemoveNode(const RefPtr<NG::UINode>& node); 116 void ClearPendingRemoveNodes(); 117 118 private: 119 // private constructor 120 ElementRegister() = default; 121 122 bool AddReferenced(ElementIdType elmtId, const WeakPtr<AceType>& referenced); 123 124 // Singleton instance 125 static thread_local ElementRegister* instance_; 126 static std::mutex mutex_; 127 128 // ElementID assigned during initial render 129 // first to Component, then synced to Element 130 ElementIdType nextUniqueElementId_ = 0; 131 132 // Map for created elements 133 std::unordered_map<ElementIdType, WeakPtr<AceType>> itemMap_; 134 135 // Set of removed Elements (not in itemMap_ anymore) 136 std::unordered_set<ElementIdType> removedItems_; 137 138 // Cache IDs that are referenced by other object 139 // which causes delayed destruction when custom node is destoryed. 140 std::unordered_set<ElementIdType> deletedCachedItems_; 141 142 std::unordered_map<std::string, RefPtr<NG::GeometryTransition>> geometryTransitionMap_; 143 144 std::list<RefPtr<NG::UINode>> pendingRemoveNodes_; 145 146 ACE_DISALLOW_COPY_AND_MOVE(ElementRegister); 147 }; 148 } // namespace OHOS::Ace 149 #endif 150