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_BASE_CUSTOM_NODE_BASE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_CUSTOM_NODE_BASE_H 18 19 #include <functional> 20 #include <string> 21 22 #include "base/memory/ace_type.h" 23 #include "base/utils/macros.h" 24 #include "core/components_ng/base/frame_node.h" 25 #include "core/components_ng/pattern/custom/custom_node_pattern.h" 26 #include "core/pipeline/base/element_register.h" 27 28 namespace OHOS::Ace::NG { 29 30 class ACE_EXPORT CustomNodeBase : public virtual AceType { 31 DECLARE_ACE_TYPE(CustomNodeBase, AceType); 32 33 public: 34 CustomNodeBase() = default; 35 ~CustomNodeBase() override; 36 FireOnAppear()37 void FireOnAppear() const 38 { 39 if (appearFunc_) { 40 appearFunc_(); 41 } 42 } 43 SetRenderFunction(const RenderFunction & renderFunction)44 virtual void SetRenderFunction(const RenderFunction& renderFunction) {} 45 SetAppearFunction(std::function<void ()> && appearFunc)46 void SetAppearFunction(std::function<void()>&& appearFunc) 47 { 48 appearFunc_ = std::move(appearFunc); 49 } 50 SetUpdateFunction(std::function<void ()> && updateFunc)51 void SetUpdateFunction(std::function<void()>&& updateFunc) 52 { 53 updateFunc_ = std::move(updateFunc); 54 } 55 SetDestroyFunction(std::function<void ()> && destroyFunc)56 void SetDestroyFunction(std::function<void()>&& destroyFunc) 57 { 58 destroyFunc_ = std::move(destroyFunc); 59 } 60 SetReloadFunction(std::function<void (bool)> && reloadFunc)61 void SetReloadFunction(std::function<void(bool)>&& reloadFunc) 62 { 63 reloadFunc_ = std::move(reloadFunc); 64 } 65 FireReloadFunction(bool deep)66 void FireReloadFunction(bool deep) 67 { 68 if (reloadFunc_) { 69 reloadFunc_(deep); 70 } 71 } 72 SetPageTransitionFunction(std::function<void ()> && pageTransitionFunc)73 void SetPageTransitionFunction(std::function<void()>&& pageTransitionFunc) 74 { 75 pageTransitionFunc_ = std::move(pageTransitionFunc); 76 } 77 CallPageTransitionFunction()78 void CallPageTransitionFunction() const 79 { 80 if (pageTransitionFunc_) { 81 pageTransitionFunc_(); 82 } 83 } 84 SetForceUpdateNodeFunc(std::function<void (int32_t)> && forceNodeUpdateFunc)85 void SetForceUpdateNodeFunc(std::function<void(int32_t)>&& forceNodeUpdateFunc) 86 { 87 forceNodeUpdateFunc_ = std::move(forceNodeUpdateFunc); 88 } 89 FireNodeUpdateFunc(ElementIdType id)90 void FireNodeUpdateFunc(ElementIdType id) 91 { 92 if (forceNodeUpdateFunc_) { 93 forceNodeUpdateFunc_(id); 94 } else { 95 LOGE("fail to find node update func to execute %{public}d node update", id); 96 } 97 } 98 Reset()99 void Reset() 100 { 101 updateFunc_ = nullptr; 102 appearFunc_ = nullptr; 103 destroyFunc_ = nullptr; 104 } 105 106 // called by view in js thread 107 void MarkNeedUpdate(); 108 109 // called by pipeline in js thread of update. 110 void Update(); 111 112 private: 113 std::function<void()> updateFunc_; 114 std::function<void()> appearFunc_; 115 std::function<void()> destroyFunc_; 116 std::function<void()> pageTransitionFunc_; 117 std::function<void(bool)> reloadFunc_; 118 std::function<void(int32_t)> forceNodeUpdateFunc_; 119 bool needRebuild_ = false; 120 }; 121 } // namespace OHOS::Ace::NG 122 123 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_CUSTOM_NODE_BASE_H 124