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 ACE_SCOPED_TRACE("XComponent[%s] FireLoadEvent[%d]", xcomponentId.c_str(), !!loadEvent_); 47 CHECK_NULL_VOID(loadEvent_); 48 loadEvent_(xcomponentId); 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 ACE_SCOPED_TRACE("XComponent[%s] FireDestroyEvent[%d]", xcomponentId.c_str(), !!destroyEvent_); 59 CHECK_NULL_VOID(destroyEvent_); 60 destroyEvent_(xcomponentId); 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 CHECK_NULL_VOID(surfaceInitEvent_); 71 surfaceInitEvent_(componentId, nodeId, false); 72 } 73 SetDetachEvent(DetachEvent && detachEvent)74 void SetDetachEvent(DetachEvent&& detachEvent) 75 { 76 detachEvent_ = std::move(detachEvent); 77 } 78 FireDetachEvent(const std::string & componentId)79 void FireDetachEvent(const std::string& componentId) 80 { 81 CHECK_NULL_VOID(detachEvent_); 82 detachEvent_(componentId); 83 } 84 SetControllerCreatedEvent(SurfaceCreatedEvent && controllerCreatedEvent)85 void SetControllerCreatedEvent(SurfaceCreatedEvent&& controllerCreatedEvent) 86 { 87 controllerCreatedEvent_ = std::move(controllerCreatedEvent); 88 } 89 FireControllerCreatedEvent(const std::string & surfaceId,const std::string & xcomponentId)90 void FireControllerCreatedEvent(const std::string& surfaceId, const std::string& xcomponentId) const 91 { 92 ACE_SCOPED_TRACE( 93 "XComponent[%s] FireControllerCreatedEvent[%d]", xcomponentId.c_str(), !!controllerCreatedEvent_); 94 CHECK_NULL_VOID(controllerCreatedEvent_); 95 controllerCreatedEvent_(surfaceId, xcomponentId); 96 } 97 SetControllerChangedEvent(SurfaceChangedEvent && controllerChangedEvent)98 void SetControllerChangedEvent(SurfaceChangedEvent&& controllerChangedEvent) 99 { 100 controllerChangedEvent_ = std::move(controllerChangedEvent); 101 } 102 FireControllerChangedEvent(const std::string & surfaceId,const RectF & rect,const std::string & xcomponentId)103 void FireControllerChangedEvent( 104 const std::string& surfaceId, const RectF& rect, const std::string& xcomponentId) const 105 { 106 ACE_SCOPED_TRACE("XComponent[%s] FireControllerChangedEvent[w:%f,h:%f][%d]", xcomponentId.c_str(), rect.Width(), 107 rect.Height(), !!controllerChangedEvent_); 108 CHECK_NULL_VOID(controllerChangedEvent_); 109 controllerChangedEvent_(surfaceId, rect); 110 } 111 SetControllerDestroyedEvent(SurfaceDestroyedEvent && controllerDestroyedEvent)112 void SetControllerDestroyedEvent(SurfaceDestroyedEvent&& controllerDestroyedEvent) 113 { 114 controllerDestroyedEvent_ = std::move(controllerDestroyedEvent); 115 } 116 FireControllerDestroyedEvent(const std::string & surfaceId,const std::string & xcomponentId)117 void FireControllerDestroyedEvent(const std::string& surfaceId, const std::string& xcomponentId) const 118 { 119 ACE_SCOPED_TRACE( 120 "XComponent[%s] FireControllerDestroyedEvent[%d]", xcomponentId.c_str(), !!controllerDestroyedEvent_); 121 CHECK_NULL_VOID(controllerDestroyedEvent_); 122 controllerDestroyedEvent_(surfaceId, xcomponentId); 123 } 124 125 private: 126 LoadEvent loadEvent_; 127 DestroyEvent destroyEvent_; 128 ExternalEvent surfaceInitEvent_; 129 DetachEvent detachEvent_; 130 SurfaceCreatedEvent controllerCreatedEvent_; 131 SurfaceChangedEvent controllerChangedEvent_; 132 SurfaceDestroyedEvent controllerDestroyedEvent_; 133 }; 134 } // namespace OHOS::Ace::NG 135 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_XCOMPONENT_XCOMPONENT_EVENT_HUB_H 136