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(const std::string&)>; 26 using ExternalEvent = std::function<void(const std::string&, const uint32_t, const bool)>; 27 using DetachEvent = std::function<void(const std::string&)>; 28 using SurfaceCreatedEvent = std::function<void(const std::string&, const std::string&)>; 29 using SurfaceChangedEvent = std::function<void(const std::string&, const RectF&)>; 30 using SurfaceDestroyedEvent = std::function<void(const std::string&, const std::string&)>; 31 32 class XComponentEventHub : public EventHub { 33 DECLARE_ACE_TYPE(XComponentEventHub, EventHub) 34 35 public: 36 XComponentEventHub() = default; 37 ~XComponentEventHub() override = default; 38 SetOnLoad(LoadEvent && loadEvent)39 void SetOnLoad(LoadEvent&& loadEvent) 40 { 41 loadEvent_ = std::move(loadEvent); 42 } 43 FireLoadEvent(const std::string & xcomponentId)44 void FireLoadEvent(const std::string& xcomponentId) const 45 { 46 if (loadEvent_) { 47 loadEvent_(xcomponentId); 48 } 49 } 50 SetOnDestroy(DestroyEvent && destroyEvent)51 void SetOnDestroy(DestroyEvent&& destroyEvent) 52 { 53 destroyEvent_ = std::move(destroyEvent); 54 } 55 FireDestroyEvent(const std::string & xcomponentId)56 void FireDestroyEvent(const std::string& xcomponentId) const 57 { 58 if (destroyEvent_) { 59 destroyEvent_(xcomponentId); 60 } 61 } 62 SetOnSurfaceInitEvent(ExternalEvent && surfaceInitEvent)63 void SetOnSurfaceInitEvent(ExternalEvent&& surfaceInitEvent) 64 { 65 surfaceInitEvent_ = std::move(surfaceInitEvent); 66 } 67 FireSurfaceInitEvent(const std::string & componentId,const uint32_t nodeId)68 void FireSurfaceInitEvent(const std::string& componentId, const uint32_t nodeId) const 69 { 70 if (surfaceInitEvent_) { 71 surfaceInitEvent_(componentId, nodeId, false); 72 } 73 } 74 SetDetachEvent(DetachEvent && detachEvent)75 void SetDetachEvent(DetachEvent&& detachEvent) 76 { 77 detachEvent_ = std::move(detachEvent); 78 } 79 FireDetachEvent(const std::string & componentId)80 void FireDetachEvent(const std::string& componentId) 81 { 82 if (detachEvent_) { 83 detachEvent_(componentId); 84 } 85 } 86 SetControllerCreatedEvent(SurfaceCreatedEvent && controllerCreatedEvent)87 void SetControllerCreatedEvent(SurfaceCreatedEvent&& controllerCreatedEvent) 88 { 89 controllerCreatedEvent_ = std::move(controllerCreatedEvent); 90 } 91 FireControllerCreatedEvent(const std::string & surfaceId,const std::string & xcomponentId)92 void FireControllerCreatedEvent(const std::string& surfaceId, const std::string& xcomponentId) const 93 { 94 if (controllerCreatedEvent_) { 95 controllerCreatedEvent_(surfaceId, xcomponentId); 96 } 97 } 98 SetControllerChangedEvent(SurfaceChangedEvent && controllerChangedEvent)99 void SetControllerChangedEvent(SurfaceChangedEvent&& controllerChangedEvent) 100 { 101 controllerChangedEvent_ = std::move(controllerChangedEvent); 102 } 103 FireControllerChangedEvent(const std::string & surfaceId,const RectF & rect)104 void FireControllerChangedEvent(const std::string& surfaceId, const RectF& rect) const 105 { 106 if (controllerChangedEvent_) { 107 controllerChangedEvent_(surfaceId, rect); 108 } 109 } 110 SetControllerDestroyedEvent(SurfaceDestroyedEvent && controllerDestroyedEvent)111 void SetControllerDestroyedEvent(SurfaceDestroyedEvent&& controllerDestroyedEvent) 112 { 113 controllerDestroyedEvent_ = std::move(controllerDestroyedEvent); 114 } 115 FireControllerDestroyedEvent(const std::string & surfaceId,const std::string & xcomponentId)116 void FireControllerDestroyedEvent(const std::string& surfaceId, const std::string& xcomponentId) const 117 { 118 if (controllerDestroyedEvent_) { 119 controllerDestroyedEvent_(surfaceId, xcomponentId); 120 } 121 } 122 123 private: 124 LoadEvent loadEvent_; 125 DestroyEvent destroyEvent_; 126 ExternalEvent surfaceInitEvent_; 127 DetachEvent detachEvent_; 128 SurfaceCreatedEvent controllerCreatedEvent_; 129 SurfaceChangedEvent controllerChangedEvent_; 130 SurfaceDestroyedEvent controllerDestroyedEvent_; 131 }; 132 } // namespace OHOS::Ace::NG 133 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_EVENT_HUB_H 134