1 /* 2 * Copyright (c) 2022-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_PATTERNS_XCOMPONENT_XCOMPONENT_EVENT_HUB_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_EVENT_HUB_H 18 19 #include "base/memory/ace_type.h" 20 #include "core/components_ng/event/event_hub.h" 21 #include "core/components_ng/event/gesture_event_hub.h" 22 23 namespace OHOS::Ace::NG { 24 using LoadEvent = std::function<void(const std::string&)>; 25 using DestroyEvent = std::function<void()>; 26 using ExternalEvent = std::function<void(const std::string&, const uint32_t, const bool)>; 27 using DetachEvent = std::function<void(const std::string&)>; 28 29 class XComponentEventHub : public EventHub { 30 DECLARE_ACE_TYPE(XComponentEventHub, EventHub) 31 32 public: 33 XComponentEventHub() = default; 34 ~XComponentEventHub() override = default; 35 SetOnLoad(LoadEvent && loadEvent)36 void SetOnLoad(LoadEvent&& loadEvent) 37 { 38 loadEvent_ = std::move(loadEvent); 39 } 40 FireLoadEvent(const std::string & xcomponentId)41 void FireLoadEvent(const std::string& xcomponentId) const 42 { 43 if (loadEvent_) { 44 loadEvent_(xcomponentId); 45 } 46 } 47 SetOnDestroy(DestroyEvent && destroyEvent)48 void SetOnDestroy(DestroyEvent&& destroyEvent) 49 { 50 destroyEvent_ = std::move(destroyEvent); 51 } 52 FireDestroyEvent()53 void FireDestroyEvent() const 54 { 55 if (destroyEvent_) { 56 destroyEvent_(); 57 } 58 } 59 SetOnSurfaceInitEvent(ExternalEvent && surfaceInitEvent)60 void SetOnSurfaceInitEvent(ExternalEvent&& surfaceInitEvent) 61 { 62 surfaceInitEvent_ = std::move(surfaceInitEvent); 63 } 64 FireSurfaceInitEvent(const std::string & componentId,const uint32_t nodeId)65 void FireSurfaceInitEvent(const std::string& componentId, const uint32_t nodeId) const 66 { 67 if (surfaceInitEvent_) { 68 surfaceInitEvent_(componentId, nodeId, false); 69 } 70 } 71 SetDetachEvent(DetachEvent && detachEvent)72 void SetDetachEvent(DetachEvent&& detachEvent) 73 { 74 detachEvent_ = std::move(detachEvent); 75 } 76 FireDetachEvent(const std::string & componentId)77 void FireDetachEvent(const std::string& componentId) 78 { 79 if (detachEvent_) { 80 detachEvent_(componentId); 81 } 82 } 83 84 private: 85 LoadEvent loadEvent_; 86 DestroyEvent destroyEvent_; 87 ExternalEvent surfaceInitEvent_; 88 DetachEvent detachEvent_; 89 }; 90 } // namespace OHOS::Ace::NG 91 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_EVENT_HUB_H 92