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 73 virtual void SetCompleteReloadFunc(RenderFunction&& func) = 0; 74 SetPageTransitionFunction(std::function<void ()> && pageTransitionFunc)75 void SetPageTransitionFunction(std::function<void()>&& pageTransitionFunc) 76 { 77 pageTransitionFunc_ = std::move(pageTransitionFunc); 78 } 79 CallPageTransitionFunction()80 void CallPageTransitionFunction() const 81 { 82 if (pageTransitionFunc_) { 83 pageTransitionFunc_(); 84 } 85 } 86 SetForceUpdateNodeFunc(std::function<void (int32_t)> && forceNodeUpdateFunc)87 void SetForceUpdateNodeFunc(std::function<void(int32_t)>&& forceNodeUpdateFunc) 88 { 89 forceNodeUpdateFunc_ = std::move(forceNodeUpdateFunc); 90 } 91 FireNodeUpdateFunc(ElementIdType id)92 void FireNodeUpdateFunc(ElementIdType id) 93 { 94 if (forceNodeUpdateFunc_) { 95 forceNodeUpdateFunc_(id); 96 } else { 97 LOGE("fail to find node update func to execute %{public}d node update", id); 98 } 99 } 100 Reset()101 void Reset() 102 { 103 updateFunc_ = nullptr; 104 appearFunc_ = nullptr; 105 destroyFunc_ = nullptr; 106 } 107 108 // called by view in js thread 109 void MarkNeedUpdate(); 110 111 // called by pipeline in js thread of update. 112 void Update(); 113 114 private: 115 std::function<void()> updateFunc_; 116 std::function<void()> appearFunc_; 117 std::function<void()> destroyFunc_; 118 std::function<void()> pageTransitionFunc_; 119 std::function<void(bool)> reloadFunc_; 120 std::function<void()> completeReloadFunc_; 121 std::function<void(int32_t)> forceNodeUpdateFunc_; 122 bool needRebuild_ = false; 123 }; 124 } // namespace OHOS::Ace::NG 125 126 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_BASE_CUSTOM_NODE_BASE_H 127